]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make sure we use the same host distribution/release in sandbox
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 14 Jan 2025 14:32:20 +0000 (15:32 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 14 Jan 2025 15:52:47 +0000 (16:52 +0100)
If the distribution is not explicitly specified in the config file
and the host distribution does not match the tools tree distribution,
then currently mkosi -f sandbox mkosi -f and mkosi -f will build images
for different distributions.

Let's make sure these commands use the same default distribution by
communicating the host distribution and release to the sandbox via
environment variables and using them to determine the default values to
use if available.

mkosi/__init__.py
mkosi/config.py

index 9f2ca8eb6f219c1f92cafddbd957afe6c8a697e9..7ac87b324e975f27bd5792eca43830cf11eeafc7 100644 (file)
@@ -79,7 +79,7 @@ from mkosi.config import (
     yes_no,
 )
 from mkosi.context import Context
-from mkosi.distributions import Distribution
+from mkosi.distributions import Distribution, detect_distribution
 from mkosi.documentation import show_docs
 from mkosi.installer import clean_package_manager_metadata
 from mkosi.kmod import gen_required_kernel_modules, loaded_modules, process_kernel_modules
@@ -3778,11 +3778,19 @@ def run_sandbox(args: Args, config: Config) -> None:
                 hint=f"Create an empty directory at {dst} using 'mkdir -p {dst}' as root and try again",
             )
 
+    hd, hr = detect_distribution()
+
+    env = {"MKOSI_IN_SANDBOX": "1"}
+    if hd:
+        env |= {"MKOSI_HOST_DISTRIBUTION": str(hd)}
+    if hr:
+        env |= {"MKOSI_HOST_RELEASE": hr}
+
     run(
         args.cmdline,
         stdin=sys.stdin,
         stdout=sys.stdout,
-        env=os.environ | {"MKOSI_IN_SANDBOX": "1"},
+        env=os.environ | env,
         log=False,
         sandbox=config.sandbox(
             devices=True,
index 8ef2c15fbe070fb4a6a9b3a1fc9de5b653a1c0b5..525a73a353780b3f0f6f6a3fc1e60e086c087613 100644 (file)
@@ -861,6 +861,9 @@ def config_default_output(namespace: argparse.Namespace) -> str:
 
 
 def config_default_distribution(namespace: argparse.Namespace) -> Distribution:
+    if d := os.getenv("MKOSI_HOST_DISTRIBUTION"):
+        return Distribution(d)
+
     detected = detect_distribution()[0]
 
     if not detected:
@@ -874,8 +877,15 @@ def config_default_distribution(namespace: argparse.Namespace) -> Distribution:
 
 
 def config_default_release(namespace: argparse.Namespace) -> str:
+    hd: Optional[Distribution]
+    hr: Optional[str]
+
+    if (d := os.getenv("MKOSI_HOST_DISTRIBUTION")) and (r := os.getenv("MKOSI_HOST_RELEASE")):
+        hd, hr = Distribution(d), r
+    else:
+        hd, hr = detect_distribution()
+
     # If the configured distribution matches the host distribution, use the same release as the host.
-    hd, hr = detect_distribution()
     if namespace.distribution == hd and hr is not None:
         return hr