]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Check if already root in become_root
authorGeorges Discry <georges@discry.be>
Thu, 20 Apr 2023 20:09:34 +0000 (22:09 +0200)
committerGeorges Discry <georges@discry.be>
Fri, 21 Apr 2023 08:51:58 +0000 (10:51 +0200)
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.

mkosi/__init__.py
mkosi/run.py

index 12db7bfbe87c6d38f9d425d79da73a4fc9c914b4..d3d066638d503685f16f1d6394e62ee6139453ed 100644 (file)
@@ -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)
 
index 0a527ad346fd354895d912b502acfbffe52cac8d..90e935d54c5e0a0a54c119bc4e24a4c10173f57c 100644 (file)
@@ -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"))