]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make mkosi available inside mkosi sandbox via zipapp
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 22 Jan 2025 11:32:35 +0000 (12:32 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 22 Jan 2025 13:11:15 +0000 (14:11 +0100)
Currently, mkosi has to be installed outside of /usr when using a
tools tree with mkosi sandbox to make it available inside mkosi
sandbox. Let's remove this restriction by packaging up the host's
mkosi as a zipapp and making the zipapp available in the sandbox.

action.yaml
mkosi/__init__.py

index 95c5a1ee26513915bce6d6bdd408b2293c0d16d0..5104379cda92beda39aa19cf3543c46bef61942f 100644 (file)
@@ -75,12 +75,7 @@ runs:
 
     - name: Install
       shell: bash
-      run: |
-        sudo ln -svf ${{ github.action_path }}/bin/mkosi /usr/bin/mkosi
-        # Make sure that mkosi is still found inside "mkosi sandbox" where /usr is potentially replaced with
-        # a tools tree's /usr.
-        mkdir -p $HOME/.local/bin
-        ln -svf ${{ github.action_path }}/bin/mkosi $HOME/.local/bin/mkosi
+      run: sudo ln -svf ${{ github.action_path }}/bin/mkosi /usr/bin/mkosi
 
     - name: Dependencies
       shell: bash
index e4ff8b0d8820e24b90c3bb6e41a9be22e1dbbc66..10ace4d6cdcb24effdf4bf6f2cd0ed033f40763b 100644 (file)
@@ -20,6 +20,7 @@ import sys
 import tempfile
 import textwrap
 import uuid
+import zipapp
 from collections.abc import Iterator, Mapping, Sequence
 from contextlib import AbstractContextManager
 from pathlib import Path
@@ -3881,19 +3882,31 @@ def run_sandbox(args: Args, config: Config) -> None:
             *cmdline,
         ]
 
-    run(
-        cmdline,
-        stdin=sys.stdin,
-        stdout=sys.stdout,
-        env=os.environ | env,
-        log=False,
-        sandbox=config.sandbox(
-            devices=True,
-            network=True,
-            relaxed=True,
-            options=["--same-dir", *mounts],
-        ),
-    )
+    with contextlib.ExitStack() as stack:
+        if config.tools() != Path("/"):
+            d = stack.enter_context(tempfile.TemporaryDirectory(prefix="mkosi-path-"))
+            zipapp.create_archive(
+                source=Path(__file__).parent,
+                target=Path(d) / "mkosi",
+                interpreter="/usr/bin/env python3",
+            )
+            make_executable(Path(d) / "mkosi")
+            mounts += ["--ro-bind", d, "/mkosi"]
+            stack.enter_context(scopedenv({"PATH": f"/mkosi:{os.environ['PATH']}"}))
+
+        run(
+            cmdline,
+            stdin=sys.stdin,
+            stdout=sys.stdout,
+            env=os.environ | env,
+            log=False,
+            sandbox=config.sandbox(
+                devices=True,
+                network=True,
+                relaxed=True,
+                options=["--same-dir", *mounts],
+            ),
+        )
 
 
 def run_shell(args: Args, config: Config) -> None: