chroot: combine user.py and root.py into run.py (MR 2252)

These are small related utility functions, combine them together.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-05-24 04:16:00 +02:00 committed by Oliver Smith
parent 1d9bbd613e
commit cf651e56d5
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
9 changed files with 52 additions and 64 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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?")

View file

@ -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

View file

@ -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"

View file

@ -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