From 8330ab45047db0360b44d9b5f17de92bafebaef6 Mon Sep 17 00:00:00 2001 From: Dor Askayo Date: Mon, 23 May 2022 17:02:33 +0300 Subject: [PATCH] Add an option to avoid creating a tmpfs in prepare() Root access is required for creating tmpfs mounts in the context of the current mount namespace, and creating a tmpfs in the context of a new mount namespace is less useful because a process in the parent namespace can't easily access it. So add an option to avoid creating tmpfs mounts, which will be used by the rootless bootstrap mode for now. In addition, when tmp directories aren't mounted as tmpfs, their contents can't be removed using os.umount(). So instead remove them recursively using shutil.rmtree(). --- lib/sysgeneral.py | 16 ++++++++++++++-- rootfs.py | 26 +++++++++++++++++--------- sysa.py | 11 +++++++---- sysc.py | 9 ++++++--- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/lib/sysgeneral.py b/lib/sysgeneral.py index ebc51e0d..467b3e5b 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 fa695659..33273d52 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 673b5103..db8954db 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 c4eb427b..a915c536 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