Merge pull request #326 from nanonyme/sudo-helper

Add a wrapper for commmands needing sudo that checks if you are euid 0
This commit is contained in:
Andrius Štikonas 2023-11-14 23:29:52 +00:00 committed by GitHub
commit 9b81f13714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 15 deletions

View File

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

View File

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

View File

@ -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":