Skip to content

Commit

Permalink
Merge pull request #326 from nanonyme/sudo-helper
Browse files Browse the repository at this point in the history
Add a wrapper for commmands needing sudo that checks if you are euid 0
  • Loading branch information
stikonas authored Nov 14, 2023
2 parents 7747e86 + f893df5 commit 9b81f13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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"""
Expand Down Expand Up @@ -45,7 +45,7 @@ def __del__(self):
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}")
Expand Down Expand Up @@ -75,7 +75,7 @@ def add_disk(self, name, size="16G", filesystem="ext4"):
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"""
Expand All @@ -85,7 +85,7 @@ def mount_disk(self, name, mountpoint=None):
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

Expand Down
21 changes: 14 additions & 7 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
8 changes: 4 additions & 4 deletions rootfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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

Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 9b81f13

Please sign in to comment.