diff --git a/chroot/abstract.py b/chroot/abstract.py index 1e8b5db..972b4a7 100644 --- a/chroot/abstract.py +++ b/chroot/abstract.py @@ -214,6 +214,7 @@ class Chroot(AbstractChroot): def run_cmd( self, script: Union[str, list[str]], + user: str = 'root', inner_env: dict[str, str] = {}, outer_env: dict[str, str] = os.environ.copy() | {'QEMU_LD_PREFIX': '/usr/aarch64-linux-gnu'}, attach_tty: bool = False, @@ -227,6 +228,7 @@ class Chroot(AbstractChroot): if outer_env is None: outer_env = os.environ.copy() env_cmd = ['/usr/bin/env'] + [f'{shell_quote(key)}={shell_quote(value)}' for key, value in inner_env.items()] + su_cmd = [] kwargs: dict = { 'env': outer_env, } @@ -237,7 +239,9 @@ class Chroot(AbstractChroot): script = ' '.join(script) if cwd: script = f"cd {shell_quote(cwd)} && ( {script} )" - cmd = ['chroot', self.path] + env_cmd + [ + if user != 'root': + su_cmd = ['su', user, '--'] + cmd = ['chroot', self.path] + su_cmd + env_cmd + [ '/bin/bash', '-c', script, diff --git a/image.py b/image.py index 7003f24..e69ec46 100644 --- a/image.py +++ b/image.py @@ -11,7 +11,7 @@ from typing import Optional from chroot.device import DeviceChroot, get_device_chroot from constants import Arch, BASE_PACKAGES, DEVICES, FLAVOURS -from config import config, Profile +from config import config, Profile, PROFILE_DEFAULTS from distro.distro import get_base_distro, get_kupfer_https from packages import build_enable_qemu_binfmt, discover_packages, build_packages from ssh import copy_ssh_keys @@ -307,9 +307,11 @@ def install_rootfs( packages: list[str], use_local_repos: bool, profile: Profile, + kupfer_config_apply: bool = True, ): - user = profile['username'] or 'kupfer' - post_cmds = FLAVOURS[flavour].get('post_cmds', []) + packages += ['kupfer-config'] if kupfer_config_apply else [] + profile = PROFILE_DEFAULTS | profile + user = profile['username'] chroot = get_device_chroot(device=device, flavour=flavour, arch=arch, packages=packages, use_local_repos=use_local_repos) mount_chroot(rootfs_device, bootfs_device, chroot) @@ -332,11 +334,12 @@ def install_rootfs( for target, content in files.items(): with open(os.path.join(chroot.path, target.lstrip('/')), 'w') as file: file.write(content) - if post_cmds: - result = chroot.run_cmd(' && '.join(post_cmds)) + + if kupfer_config_apply: + result = chroot.run_cmd(['kupfer-config', 'apply']) assert isinstance(result, subprocess.CompletedProcess) if result.returncode != 0: - raise Exception('Error running post_cmds') + raise Exception('Error running kupfer-config apply') logging.info('Preparing to unmount chroot') res = chroot.run_cmd('sync && umount /boot', attach_tty=True) @@ -355,11 +358,12 @@ def cmd_image(): @cmd_image.command(name='build') @click.argument('profile_name', required=False) -@click.option('--local-repos/--no-local-repos', '-l/-L', default=True, help='Whether to use local packages. Defaults to true.') -@click.option('--build-pkgs/--no-build-pkgs', '-p/-P', default=True, help='Whether to build missing/outdated local packages. Defaults to true.') +@click.option('--local-repos/--no-local-repos', '-l/-L', is_flag=True, default=True, help='Whether to use local packages. Defaults to true.') +@click.option('--build-pkgs/--no-build-pkgs', '-p/-P', is_flag=True, default=True, help='Whether to build missing/outdated local packages. Defaults to true.') @click.option('--block-target', default=None, help='Override the block device file to target') @click.option('--skip-part-images', default=False, help='Skip creating image files for the partitions and directly work on the target block device.') -def cmd_build(profile_name: str = None, local_repos: bool = True, build_pkgs: bool = True, block_target: str = None, skip_part_images: bool = False): +@click.option('--no-kupfer-config-apply', is_flag=True, help='skip applying kupfer-config, which mainly enables services according to the image flavor') +def cmd_build(profile_name: str = None, local_repos: bool = True, build_pkgs: bool = True, block_target: str = None, skip_part_images: bool = False, no_kupfer_config_apply: bool = False): """Build a device image""" enforce_wrap() profile: Profile = config.get_profile(profile_name) @@ -371,7 +375,7 @@ def cmd_build(profile_name: str = None, local_repos: bool = True, build_pkgs: bo sector_size = 4096 rootfs_size_mb = FLAVOURS[flavour].get('size', 2) * 1000 - packages = BASE_PACKAGES + DEVICES[device] + FLAVOURS[flavour]['packages'] + profile['pkgs_include'] + packages = BASE_PACKAGES + DEVICES[device] + FLAVOURS[flavour]['packages'] + profile['pkgs_include'] + ([] if no_kupfer_config_apply else ['kupfer-config']) if arch != config.runtime['arch']: build_enable_qemu_binfmt(arch)