mirror of
https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git
synced 2025-07-24 04:55:07 +03:00
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>
This commit is contained in:
parent
be5e18cf99
commit
2f3f36397f
2 changed files with 65 additions and 0 deletions
27
pmb/init/run.py
Normal file
27
pmb/init/run.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# 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)
|
Loading…
Add table
Add a link
Reference in a new issue