From: Georges Discry Date: Thu, 20 Apr 2023 20:09:34 +0000 (+0200) Subject: Check if already root in become_root X-Git-Tag: v15~221^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49b72a8ab72f7ad256b45e965cb0f4efa3833bfe;p=thirdparty%2Fmkosi.git Check if already root in become_root Instead of requiring the check for `os.getuid() != 0` before calling `mkosi.run.become_root()`, that check is now performed at the beginning with an early return if the user is already root. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 12db7bfbe..d3d066638 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -2412,8 +2412,7 @@ def run_verb(config: MkosiConfig) -> None: if needs_build(config) or config.verb == Verb.clean: def target() -> None: - if os.getuid() != 0: - become_root() + become_root() unlink_output(config) fork_and_wait(target) @@ -2421,7 +2420,7 @@ def run_verb(config: MkosiConfig) -> None: if needs_build(config): def target() -> None: # Get the user UID/GID either on the host or in the user namespace running the build - uid, gid = become_root() if os.getuid() != 0 else current_user_uid_gid() + uid, gid = become_root() init_mount_namespace() build_stuff(uid, gid, config) diff --git a/mkosi/run.py b/mkosi/run.py index 0a527ad34..90e935d54 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -13,7 +13,7 @@ from pathlib import Path from types import TracebackType from typing import Any, Callable, Mapping, Optional, Sequence, Type, TypeVar -from mkosi.backend import MkosiState +from mkosi.backend import MkosiState, current_user_uid_gid from mkosi.log import ARG_DEBUG, MkosiPrinter, die from mkosi.types import _FILE, CompletedProcess, PathString, Popen @@ -66,6 +66,8 @@ def become_root() -> tuple[int, int]: The function returns the UID-GID pair of the invoking user in the namespace (65436, 65436). """ + if os.getuid() == 0: + return current_user_uid_gid() subuid = read_subrange(Path("/etc/subuid")) subgid = read_subrange(Path("/etc/subgid"))