Chroot.run_cmd(): add user parameter

This commit is contained in:
InsanePrawn 2022-07-06 23:00:55 +02:00
parent 064f265247
commit d277d926fd

View file

@ -214,6 +214,7 @@ class Chroot(AbstractChroot):
def run_cmd( def run_cmd(
self, self,
script: Union[str, list[str]], script: Union[str, list[str]],
user: str = 'root',
inner_env: dict[str, str] = {}, inner_env: dict[str, str] = {},
outer_env: dict[str, str] = os.environ.copy() | {'QEMU_LD_PREFIX': '/usr/aarch64-linux-gnu'}, outer_env: dict[str, str] = os.environ.copy() | {'QEMU_LD_PREFIX': '/usr/aarch64-linux-gnu'},
attach_tty: bool = False, attach_tty: bool = False,
@ -227,6 +228,7 @@ class Chroot(AbstractChroot):
if outer_env is None: if outer_env is None:
outer_env = os.environ.copy() outer_env = os.environ.copy()
env_cmd = ['/usr/bin/env'] + [f'{shell_quote(key)}={shell_quote(value)}' for key, value in inner_env.items()] env_cmd = ['/usr/bin/env'] + [f'{shell_quote(key)}={shell_quote(value)}' for key, value in inner_env.items()]
su_cmd = []
kwargs: dict = { kwargs: dict = {
'env': outer_env, 'env': outer_env,
} }
@ -237,7 +239,9 @@ class Chroot(AbstractChroot):
script = ' '.join(script) script = ' '.join(script)
if cwd: if cwd:
script = f"cd {shell_quote(cwd)} && ( {script} )" 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', '/bin/bash',
'-c', '-c',
script, script,