commit: f893df52fec88f2377d56432df08b5b6c8594e7f
parent 7747e86834030cb073eb134a8147842f41cd0cd6
Author: Seppo Yli-Olli <seppo.yliolli@gmail.com>
Date: Wed, 8 Nov 2023 17:24:19 +0200
Add a wrapper for commmands needing sudo that checks if you are euid 0
Diffstat:
3 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/lib/tmpdir.py b/lib/tmpdir.py
@@ -12,7 +12,7 @@ import getpass
import os
import shutil
-from lib.utils import mount, umount, create_disk, run
+from lib.utils import mount, umount, create_disk, run_as_root
class TmpType(enum.Enum):
"""Different types of tmpdirs we can have"""
@@ -45,7 +45,7 @@ class Tmpdir:
if not self.preserve:
for disk in self._disks.values():
print(f"Detaching {disk}")
- run("sudo", "losetup", "-d", disk)
+ run_as_root("losetup", "-d", disk)
if self._type == TmpType.TMPFS:
print(f"Unmounting tmpdir from {self.path}")
@@ -75,7 +75,7 @@ class Tmpdir:
self._disks[name] = create_disk(disk_path, "msdos", filesystem, size)
self._disk_filesystems[name] = filesystem
# Allow executing user to access it
- run("sudo", "chown", getpass.getuser(), self._disks[name])
+ run_as_root("chown", getpass.getuser(), self._disks[name])
def mount_disk(self, name, mountpoint=None):
"""Mount the disk"""
@@ -85,7 +85,7 @@ class Tmpdir:
os.mkdir(mountpoint)
mount(self._disks[name] + "p1", mountpoint, self._disk_filesystems[name])
# Allow executing user to access it
- run("sudo", "chown", getpass.getuser(), mountpoint)
+ run_as_root("chown", getpass.getuser(), mountpoint)
self._mountpoints[name] = mountpoint
return mountpoint
diff --git a/lib/utils.py b/lib/utils.py
@@ -25,27 +25,34 @@ def run(*args, **kwargs):
print("Bootstrapping failed")
sys.exit(1)
+def run_as_root(*args, **kwargs):
+ """A helper for run that invokes sudo when unprivileged"""
+ if os.geteuid() != 0:
+ run("sudo", *args, **kwargs)
+ else:
+ run(*args, **kwargs)
+
def create_disk(image, disk_type, fs_type, size):
"""Create a disk image, with a filesystem on it"""
run('truncate', '-s', size, image)
# First find the device we will use, then actually use it
- loop_dev = run('sudo', 'losetup', '-f', capture_output=True).stdout.decode().strip()
- run('sudo', 'losetup', '-P', loop_dev, image)
+ loop_dev = run_as_root('losetup', '-f', capture_output=True).stdout.decode().strip()
+ run_as_root('losetup', '-P', loop_dev, image)
# Create the partition
if disk_type != "none":
- run('sudo', 'parted', '--script', image, 'mklabel', disk_type, 'mkpart',
+ run_as_root('parted', '--script', image, 'mklabel', disk_type, 'mkpart',
'primary', 'ext4', '0%', '100%')
- run('sudo', 'partprobe', loop_dev)
- run('sudo', 'mkfs.' + fs_type, loop_dev + "p1")
+ run_as_root('partprobe', loop_dev)
+ run_as_root('mkfs.' + fs_type, loop_dev + "p1")
return loop_dev
def mount(source, target, fs_type, options='', **kwargs):
"""Mount filesystem"""
- run('sudo', 'mount', source, target, '-t', fs_type, '-o', options, **kwargs)
+ run_as_root('mount', source, target, '-t', fs_type, '-o', options, **kwargs)
def umount(target, **kwargs):
"""Unmount filesystem"""
- run('sudo', 'umount', '--recursive', target, **kwargs)
+ run_as_root('umount', '--recursive', target, **kwargs)
def copytree(src, dst, ignore=shutil.ignore_patterns('*.git*')):
"""Copy directory tree into another directory"""
diff --git a/rootfs.py b/rootfs.py
@@ -19,7 +19,7 @@ import shutil
from sysa import SysA
from sysc import SysC
-from lib.utils import run
+from lib.utils import run, run_as_root
from lib.sysgeneral import stage0_arch_map
from lib.tmpdir import Tmpdir
@@ -168,15 +168,15 @@ def bootstrap(args, system_a, system_c, tmpdir):
import shutil
print(shutil.which('chroot'))
"""
- chroot_binary = run('sudo', 'python3', '-c', find_chroot,
- capture_output=True).stdout.decode().strip()
+ chroot_binary = run_as_root('python3', '-c', find_chroot,
+ capture_output=True).stdout.decode().strip()
system_c.prepare(create_disk_image=False)
system_a.prepare(create_initramfs=False)
arch = stage0_arch_map.get(args.arch, args.arch)
init = os.path.join(os.sep, 'bootstrap-seeds', 'POSIX', arch, 'kaem-optional-seed')
- run('sudo', 'env', '-i', 'PATH=/bin', chroot_binary, system_a.tmp_dir, init)
+ run_as_root('env', '-i', 'PATH=/bin', chroot_binary, system_a.tmp_dir, init)
elif args.bwrap:
if not args.internal_ci or args.internal_ci == "pass1":