]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Start systemd-storagetm in mkosi serve as well if available
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 8 Feb 2025 11:25:57 +0000 (12:25 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 8 Feb 2025 20:02:32 +0000 (21:02 +0100)
Allows accessing a disk image over NVME-TCP instead of having to
download it completely over HTTP.

mkosi/__init__.py

index de8cca331bfc3a88bc4657d3b53bc8adc9cf60fa..4b8d5f940bccf373a6e8dc7abce39d7d7a6f6d9e 100644 (file)
@@ -13,6 +13,7 @@ import re
 import resource
 import shlex
 import shutil
+import signal
 import socket
 import stat
 import subprocess
@@ -107,6 +108,7 @@ from mkosi.run import (
     finalize_passwd_symlinks,
     fork_and_wait,
     run,
+    spawn,
     workdir,
 )
 from mkosi.sandbox import (
@@ -4250,16 +4252,45 @@ def run_coredumpctl(args: Args, config: Config) -> None:
 def run_serve(args: Args, config: Config) -> None:
     """Serve the output directory via a tiny HTTP server"""
 
-    run(
-        [python_binary(config), "-m", "http.server", "8081"],
-        stdin=sys.stdin,
-        stdout=sys.stdout,
-        sandbox=config.sandbox(
-            network=True,
-            relaxed=True,
-            options=["--chdir", config.output_dir_or_cwd()],
-        ),
-    )
+    with contextlib.ExitStack() as stack:
+        want_storagetm = config.output_format == OutputFormat.disk and config.find_binary(
+            "/usr/lib/systemd/systemd-storagetm"
+        )
+
+        http = stack.enter_context(
+            spawn(
+                [python_binary(config), "-m", "http.server", "8081"],
+                stdin=sys.stdin,
+                stdout=sys.stdout,
+                sandbox=config.sandbox(
+                    network=True,
+                    relaxed=True,
+                    options=["--chdir", config.output_dir_or_cwd()],
+                ),
+                foreground=not want_storagetm,
+            )
+        )
+
+        if want_storagetm:
+            storagetm = stack.enter_context(
+                spawn(
+                    ["/usr/lib/systemd/systemd-storagetm", config.output_with_format],
+                    stdin=sys.stdin,
+                    stdout=sys.stdout,
+                    sandbox=config.sandbox(
+                        network=True,
+                        relaxed=True,
+                        options=["--chdir", config.output_dir_or_cwd()],
+                        setup=become_root_cmd(),
+                    ),
+                    foreground=True,
+                )
+            )
+
+            storagetm.wait()
+            http.send_signal(signal.SIGINT)
+
+        http.wait()
 
 
 def generate_key_cert_pair(args: Args) -> None: