From 6c262600015f6e7ce832722d7bd326d4a0e91d22 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Mon, 29 Aug 2022 20:12:09 +0200 Subject: [PATCH] chroot: add chroot.get_uid(user: str), use in chroot.mount_{ccache,rust} to apply correct ownership --- chroot/abstract.py | 12 ++++++++++++ chroot/build.py | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/chroot/abstract.py b/chroot/abstract.py index 00b89be..eb33346 100644 --- a/chroot/abstract.py +++ b/chroot/abstract.py @@ -346,6 +346,18 @@ class Chroot(AbstractChroot): if result.returncode != 0: raise Exception(f'Failed to setup user {user} in self.name') + def get_uid(self, user: Union[str, int]) -> int: + if isinstance(user, int): + return user + if user == 'root': + return 0 + res = self.run_cmd(['id', '-u', user], capture_output=True) + assert isinstance(res, subprocess.CompletedProcess) + if res.returncode or not res.stdout: + raise Exception(f"chroot {self.name}: Couldnt detect uid for user {user}: {repr(res.stdout)}") + uid = res.stdout.decode() + return int(uid) + def add_sudo_config(self, config_name: str = 'wheel', privilegee: str = '%wheel', password_required: bool = True): if '.' in config_name: raise Exception(f"won't create sudoers.d file {config_name} since it will be ignored by sudo because it contains a dot!") diff --git a/chroot/build.py b/chroot/build.py index 21d17a0..1ecdc2d 100644 --- a/chroot/build.py +++ b/chroot/build.py @@ -135,7 +135,8 @@ class BuildChroot(Chroot): def mount_ccache(self, user: str = 'kupfer', fail_if_mounted: bool = False): mount_source = os.path.join(config.file.paths.ccache, self.arch) mount_dest = os.path.join(f'/home/{user}' if user != 'root' else '/root', '.ccache') - makedir(mount_source) + uid = self.get_uid(user) + makedir(mount_source, user=uid) return self.mount( absolute_source=mount_source, relative_destination=mount_dest, @@ -144,11 +145,12 @@ class BuildChroot(Chroot): def mount_rust(self, user: str = 'kupfer', fail_if_mounted: bool = False) -> list[str]: results = [] + uid = self.get_uid(user) mount_source_base = config.file.paths.rust # apparently arch-agnostic for rust_dir in ['cargo', 'rustup']: mount_source = os.path.join(mount_source_base, rust_dir) mount_dest = os.path.join(f'/home/{user}' if user != 'root' else '/root', f'.{rust_dir}') - makedir(mount_source) + makedir(mount_source, user=uid) results.append(self.mount( absolute_source=mount_source, relative_destination=mount_dest,