diff --git a/lib/sysgeneral.py b/lib/sysgeneral.py index ebc51e0..467b3e5 100644 --- a/lib/sysgeneral.py +++ b/lib/sysgeneral.py @@ -3,11 +3,13 @@ This file contains a few functions to be shared by all Sys* classes """ +# SPDX-FileCopyrightText: 2022 Dor Askayo # SPDX-FileCopyrightText: 2021-22 fosslinux # SPDX-FileCopyrightText: 2021 Andrius Štikonas # SPDX-License-Identifier: GPL-3.0-or-later import os +import shutil import hashlib import glob import subprocess @@ -33,10 +35,20 @@ class SysGeneral: mounted_tmpfs = False def __del__(self): - if self.mounted_tmpfs and not self.preserve_tmp: + if not self.preserve_tmp: + self.remove_tmp() + + def remove_tmp(self): + """Remove the tmp directory""" + if self.tmp_dir is None: + return + + if self.mounted_tmpfs: print(f"Unmounting tmpfs from {self.tmp_dir}") umount(self.tmp_dir) - os.rmdir(self.tmp_dir) + + print(f"Removing {self.tmp_dir}") + shutil.rmtree(self.tmp_dir, ignore_errors=True) def mount_tmpfs(self): """Mount the tmpfs for this sysx""" diff --git a/rootfs.py b/rootfs.py index fa69565..33273d5 100755 --- a/rootfs.py +++ b/rootfs.py @@ -45,7 +45,7 @@ def main(): default="x86") parser.add_argument("-c", "--chroot", help="Run inside chroot", action="store_true") - parser.add_argument("-p", "--preserve", help="Do not unmount temporary dir", + parser.add_argument("-p", "--preserve", help="Do not remove temporary dir", action="store_true") parser.add_argument("-t", "--tmpdir", help="Temporary directory") parser.add_argument("--force-timestamps", @@ -128,8 +128,10 @@ print(shutil.which('chroot')) chroot_binary = run('sudo', 'python3', '-c', find_chroot, capture_output=True).stdout.decode().strip() - system_c.prepare(create_disk_image=False) - system_a.prepare(copy_sysc=True, + system_c.prepare(mount_tmpfs=True, + create_disk_image=False) + system_a.prepare(mount_tmpfs=True, + copy_sysc=True, create_initramfs=False) # sysa @@ -141,8 +143,10 @@ print(shutil.which('chroot')) if os.path.isdir('kritis-linux'): shutil.rmtree('kritis-linux') - system_c.prepare(create_disk_image=True) - system_a.prepare(copy_sysc=False, + system_c.prepare(mount_tmpfs=True, + create_disk_image=True) + system_a.prepare(mount_tmpfs=True, + copy_sysc=False, create_initramfs=True) run('git', 'clone', @@ -162,8 +166,10 @@ print(shutil.which('chroot')) '--log', '/tmp/bootstrap.log') elif args.bare_metal: - system_c.prepare(create_disk_image=True) - system_a.prepare(copy_sysc=False, + system_c.prepare(mount_tmpfs=True, + create_disk_image=True) + system_a.prepare(mount_tmpfs=True, + copy_sysc=False, create_initramfs=True) print("Please:") @@ -171,8 +177,10 @@ print(shutil.which('chroot')) print(" 2. Take sysc/tmp/disk.img and put this on a writable storage medium.") else: - system_c.prepare(create_disk_image=True) - system_a.prepare(copy_sysc=False, + system_c.prepare(mount_tmpfs=True, + create_disk_image=True) + system_a.prepare(mount_tmpfs=True, + copy_sysc=False, create_initramfs=True) run(args.qemu_cmd, diff --git a/sysa.py b/sysa.py index 673b510..db8954d 100755 --- a/sysa.py +++ b/sysa.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """System A""" # SPDX-License-Identifier: GPL-3.0-or-later +# SPDX-FileCopyrightText: 2022 Dor Askayo # SPDX-FileCopyrightText: 2021 Andrius Štikonas # SPDX-FileCopyrightText: 2021 Melg Eight # SPDX-FileCopyrightText: 2021-22 fosslinux @@ -27,20 +28,22 @@ class SysA(SysGeneral): self.tmp_dir = os.path.join(self.git_dir, 'tmp') else: self.tmp_dir = os.path.join(tmpdir, 'sysa') - os.mkdir(self.tmp_dir) self.sysa_dir = os.path.join(self.tmp_dir, 'sysa') self.base_dir = self.sysa_dir self.cache_dir = os.path.join(self.sys_dir, 'distfiles') self.sysb_dir = sysb_dir self.sysc_tmp = sysc_tmp - def prepare(self, copy_sysc, create_initramfs): + def prepare(self, mount_tmpfs, copy_sysc, create_initramfs): """ Prepare directory structure for System A. - We create an empty tmpfs, unpack stage0-posix. + We create an empty tmp directory, unpack stage0-posix. Rest of the files are unpacked into more structured directory /sysa """ - self.mount_tmpfs() + if mount_tmpfs: + self.mount_tmpfs() + else: + os.mkdir(self.tmp_dir) self.stage0_posix() self.sysa() diff --git a/sysc.py b/sysc.py index c4eb427..a915c53 100755 --- a/sysc.py +++ b/sysc.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """System C""" # SPDX-License-Identifier: GPL-3.0-or-later +# SPDX-FileCopyrightText: 2022 Dor Askayo # SPDX-FileCopyrightText: 2021-22 fosslinux # SPDX-FileCopyrightText: 2021 Andrius Štikonas @@ -31,7 +32,6 @@ class SysC(SysGeneral): self.tmp_dir = os.path.join(self.sys_dir, 'tmp') else: self.tmp_dir = os.path.join(tmpdir, 'sysc') - os.mkdir(self.tmp_dir) def __del__(self): if not self.preserve_tmp: @@ -41,11 +41,14 @@ class SysC(SysGeneral): super().__del__() - def prepare(self, create_disk_image): + def prepare(self, mount_tmpfs, create_disk_image): """ Prepare directory structure for System C. """ - self.mount_tmpfs() + if mount_tmpfs: + self.mount_tmpfs() + else: + os.mkdir(self.tmp_dir) rootfs_dir = None