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().
This commit is contained in:
Dor Askayo 2022-05-23 17:02:33 +03:00
parent 6d357226a9
commit 8330ab4504
4 changed files with 44 additions and 18 deletions

View File

@ -3,11 +3,13 @@
This file contains a few functions to be shared by all Sys* classes
"""
# SPDX-FileCopyrightText: 2022 Dor Askayo <dor.askayo@gmail.com>
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# 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"""

View File

@ -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,

11
sysa.py
View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""System A"""
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022 Dor Askayo <dor.askayo@gmail.com>
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2021 Melg Eight <public.melg8@gmail.com>
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
@ -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()

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""System C"""
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022 Dor Askayo <dor.askayo@gmail.com>
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
@ -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