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
38
pmb/init/sudo.py
Normal file
38
pmb/init/sudo.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
# Copyright 2023 Anjandev Momi
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import os
|
||||
import shutil
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
@lru_cache
|
||||
def which_sudo() -> str | None:
|
||||
"""Return a command required to run commands as root, if any.
|
||||
|
||||
Find whether sudo or doas is installed for commands that require root.
|
||||
Allows user to override preferred sudo with PMB_SUDO env variable.
|
||||
"""
|
||||
if os.getuid() == 0:
|
||||
return None
|
||||
|
||||
supported_sudos = ["doas", "sudo"]
|
||||
|
||||
user_set_sudo = os.getenv("PMB_SUDO")
|
||||
if user_set_sudo is not None:
|
||||
if shutil.which(user_set_sudo) is None:
|
||||
raise RuntimeError(
|
||||
"PMB_SUDO environmental variable is set to"
|
||||
f" {user_set_sudo} but pmbootstrap cannot find"
|
||||
" this command on your system."
|
||||
)
|
||||
return user_set_sudo
|
||||
|
||||
for sudo in supported_sudos:
|
||||
if shutil.which(sudo) is not None:
|
||||
return sudo
|
||||
|
||||
raise RuntimeError(
|
||||
"Can't find sudo or doas required to run pmbootstrap."
|
||||
" Please install sudo, doas, or specify your own sudo"
|
||||
" with the PMB_SUDO environmental variable."
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue