From: Daan De Meyer Date: Tue, 14 Jan 2025 14:32:20 +0000 (+0100) Subject: Make sure we use the same host distribution/release in sandbox X-Git-Tag: v25~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1ae257e270e768088eadf7b44fbbbb48c575709;p=thirdparty%2Fmkosi.git Make sure we use the same host distribution/release in sandbox 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. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 9f2ca8eb6..7ac87b324 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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, diff --git a/mkosi/config.py b/mkosi/config.py index 8ef2c15fb..525a73a35 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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