diff --git a/pmb/build/newapkbuild.py b/pmb/build/newapkbuild.py index 04037bcd..f0385ba3 100644 --- a/pmb/build/newapkbuild.py +++ b/pmb/build/newapkbuild.py @@ -3,7 +3,7 @@ import os from pmb.helpers import logging from pathlib import Path -import pmb.chroot.user +import pmb.chroot.run from pmb.core.types import PmbArgs import pmb.helpers.cli import pmb.parse diff --git a/pmb/chroot/__init__.py b/pmb/chroot/__init__.py index 8d675d54..eaac5ead 100644 --- a/pmb/chroot/__init__.py +++ b/pmb/chroot/__init__.py @@ -2,8 +2,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later from pmb.chroot.init import init, init_keys, UsrMerge from pmb.chroot.mount import mount, mount_native_into_foreign, remove_mnt_pmbootstrap -from pmb.chroot.root import root -from pmb.chroot.user import user -from pmb.chroot.user import exists as user_exists +from pmb.chroot.run import root, user, exists as user_exists from pmb.chroot.shutdown import shutdown from pmb.chroot.zap import zap diff --git a/pmb/chroot/root.py b/pmb/chroot/run.py similarity index 71% rename from pmb/chroot/root.py rename to pmb/chroot/run.py index 65d91ecf..4a61dda4 100644 --- a/pmb/chroot/root.py +++ b/pmb/chroot/run.py @@ -86,3 +86,42 @@ def root(args: PmbArgs, cmd: Sequence[PathString], chroot: Chroot=Chroot.native( return pmb.helpers.run_core.core(args, msg, cmd_sudo, None, output, output_return, check, True, disable_timeout) + + +def user(args: PmbArgs, cmd, chroot: Chroot=Chroot.native(), working_dir: Path = Path("/"), output="log", + output_return=False, check=None, env={}): + """ + Run a command inside a chroot as "user". We always use the BusyBox + implementation of 'su', because other implementations may override the PATH + environment variable (#1071). + + :param env: dict of environment variables to be passed to the command, e.g. + {"JOBS": "5"} + + See pmb.helpers.run_core.core() for a detailed description of all other + arguments and the return value. + """ + env = env.copy() + pmb.helpers.run_core.add_proxy_env_vars(env) + + if "HOME" not in env: + env["HOME"] = "/home/pmos" + + flat_cmd = pmb.helpers.run_core.flat_cmd(cmd, env=env) + cmd = ["busybox", "su", "pmos", "-c", flat_cmd] + return pmb.chroot.root(args, cmd, chroot, working_dir, output, + output_return, check, {}, + add_proxy_env_vars=False) + + +def exists(args: PmbArgs, username, chroot: Chroot=Chroot.native()): + """ + Checks if username exists in the system + + :param username: User name + :returns: bool + """ + output = pmb.chroot.root(args, ["getent", "passwd", username], + chroot, output_return=True, check=False) + return len(output) > 0 + diff --git a/pmb/chroot/user.py b/pmb/chroot/user.py deleted file mode 100644 index a3c780cc..00000000 --- a/pmb/chroot/user.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2023 Oliver Smith -# SPDX-License-Identifier: GPL-3.0-or-later -from pathlib import Path -import pmb.chroot -import pmb.helpers.run -import pmb.helpers.run_core -from pmb.core import Chroot -from pmb.core.types import PathString, PmbArgs - - -def user(args: PmbArgs, cmd, chroot: Chroot=Chroot.native(), working_dir: Path = Path("/"), output="log", - output_return=False, check=None, env={}): - """ - Run a command inside a chroot as "user". We always use the BusyBox - implementation of 'su', because other implementations may override the PATH - environment variable (#1071). - - :param env: dict of environment variables to be passed to the command, e.g. - {"JOBS": "5"} - - See pmb.helpers.run_core.core() for a detailed description of all other - arguments and the return value. - """ - env = env.copy() - pmb.helpers.run_core.add_proxy_env_vars(env) - - if "HOME" not in env: - env["HOME"] = "/home/pmos" - - flat_cmd = pmb.helpers.run_core.flat_cmd(cmd, env=env) - cmd = ["busybox", "su", "pmos", "-c", flat_cmd] - return pmb.chroot.root(args, cmd, chroot, working_dir, output, - output_return, check, {}, - add_proxy_env_vars=False) - - -def exists(args: PmbArgs, username, chroot: Chroot=Chroot.native()): - """ - Checks if username exists in the system - - :param username: User name - :returns: bool - """ - output = pmb.chroot.root(args, ["getent", "passwd", username], - chroot, output_return=True, check=False) - return len(output) > 0 diff --git a/pmb/helpers/apk.py b/pmb/helpers/apk.py index 0e496d83..cc4a63c8 100644 --- a/pmb/helpers/apk.py +++ b/pmb/helpers/apk.py @@ -4,7 +4,7 @@ import os from pathlib import Path from typing import List, Sequence -import pmb.chroot.root +import pmb.chroot.run import pmb.config.pmaports from pmb.core.types import PathString, PmbArgs import pmb.helpers.cli diff --git a/pmb/netboot/__init__.py b/pmb/netboot/__init__.py index 0f6d0652..3be87b3a 100644 --- a/pmb/netboot/__init__.py +++ b/pmb/netboot/__init__.py @@ -5,7 +5,7 @@ from pathlib import Path import socket import time -import pmb.chroot.root +import pmb.chroot.run from pmb.core.types import PmbArgs import pmb.helpers.run from pmb.core import Chroot @@ -33,7 +33,7 @@ def start_nbd_server(args: PmbArgs, ip="172.16.42.2", port=9999): f"replace the rootfs for " f"{args.device}?"): return - pmb.chroot.root(args, ["cp", rootfs_path2, rootfs_path]) + pmb.chroot.run(args, ["cp", rootfs_path2, rootfs_path]) logging.info(f"NOTE: Copied device image to {pmb.config.work}" f"/images_netboot/. The image will persist \"pmbootstrap " f"zap\" for your convenience. Use \"pmbootstrap netboot " @@ -61,7 +61,7 @@ def start_nbd_server(args: PmbArgs, ip="172.16.42.2", port=9999): break logging.info("Found postmarketOS device, serving image...") - pmb.chroot.root( + pmb.chroot.run( args, ["nbd-server", f"{ip}@{port}", rootfs_path, "-d"], check=False, disable_timeout=True) logging.info("nbd-server quit. Connection lost?") diff --git a/pmb/parse/bootimg.py b/pmb/parse/bootimg.py index 75e2fb0f..fa8c3f3c 100644 --- a/pmb/parse/bootimg.py +++ b/pmb/parse/bootimg.py @@ -5,8 +5,7 @@ from pmb.helpers import logging from pathlib import Path from pmb.core.types import PmbArgs import pmb.helpers.run -import pmb.chroot.root -import pmb.chroot.user +import pmb.chroot.run import pmb.chroot.other import pmb.chroot.apk from pmb.core import Chroot @@ -167,6 +166,6 @@ def bootimg(args: PmbArgs, path: Path): output["cmdline"] = f.read().replace('\n', '') # Cleanup - pmb.chroot.root(args, ["rm", "-r", temp_path]) + pmb.chroot.run(args, ["rm", "-r", temp_path]) return output diff --git a/test/test_proxy.py b/test/test_proxy.py index 1a22e586..3e130c6e 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -8,8 +8,7 @@ import pytest import sys import pmb_test # noqa -import pmb.chroot.root -import pmb.chroot.user +import pmb.chroot.run import pmb.helpers.run import pmb.helpers.run_core @@ -47,7 +46,7 @@ def test_proxy_chroot_user(args: PmbArgs, monkeypatch): def test_proxy_chroot_root(args: PmbArgs, monkeypatch): - func = pmb.chroot.root + func = pmb.chroot.run monkeypatch.setattr(os, "environ", {"HTTP_PROXY": "testproxy"}) ret = func(args, ["sh", "-c", 'echo "$HTTP_PROXY"'], output_return=True) assert ret == "testproxy\n" diff --git a/test/test_shell_escape.py b/test/test_shell_escape.py index d27286ab..3aa73a4f 100644 --- a/test/test_shell_escape.py +++ b/test/test_shell_escape.py @@ -6,8 +6,7 @@ from pmb.core.types import Env, PmbArgs import pytest import pmb_test # noqa -import pmb.chroot.root -import pmb.chroot.user +import pmb.chroot.run import pmb.helpers.run import pmb.helpers.run_core import pmb.helpers.logging @@ -47,7 +46,7 @@ def test_shell_escape(args: PmbArgs): assert expected == root assert cmd == copy - chroot_root = pmb.chroot.root(args, cmd, output_return=True) + chroot_root = pmb.chroot.run(args, cmd, output_return=True) assert expected == chroot_root assert cmd == copy @@ -72,7 +71,7 @@ def test_shell_escape_env(args: PmbArgs): assert func(args, cmd, output_return=True, env=env) == ret assert cmd == copy - func = pmb.chroot.root + func = pmb.chroot.run assert func(args, cmd, output_return=True, env=env) == ret assert cmd == copy