1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-23 04:25:10 +03:00
pmbootstrap/pmb/init/run.py
Casey Connolly 2f3f36397f
init: add run/sudo helpers
These helpers could be used to run commands as root prior to unsharing
namespaces, for example to probe the binfmt module. They aren't used
currently but are left in just in case...

Signed-off-by: Casey Connolly <kcxt@postmarketos.org>
2025-05-30 21:29:28 +02:00

27 lines
661 B
Python

# Copyright 2025 Casey Connolly
# SPDX-License-Identifier: GPL-3.0-or-later
# Wrappers for running commands prior to pmbootstrap init/unshare
from pmb.init.sudo import which_sudo
import subprocess
import shlex
def sudo(cmd: list[str]) -> list[str]:
"""
Prefix with "sudo --" unless already running as root
"""
sudo = which_sudo()
if not sudo:
return cmd
return [sudo, "--", *[shlex.quote(x) for x in cmd]]
def run_root(cmd: list[str]) -> tuple[str, str]:
"""
Run a command as root and get stdout/stderr result
"""
proc = subprocess.run(sudo(cmd), capture_output=True)
return (proc.stdout, proc.stderr)