]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Fix nspawn settings
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 5 Dec 2023 09:28:31 +0000 (10:28 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 5 Dec 2023 12:46:24 +0000 (13:46 +0100)
When --machine= is used, nspawn looks for a settings file named after
the machine so we have to make sure to copy to the right location.

While we're at it, let's also stop considering the nspawn settings
an output artifact, since this means we have to build the image to
apply new settings. Instead, let's copy the settings when running
the image and remove the copied file again afterwards. This means
that new settings are applied immediately instead of only after a
rebuild.

mkosi/__init__.py
mkosi/config.py

index bc33a6059812c188f31de83e13d0a4a7166e3569..a9e6ad5e6a70d5bb4704affeb15c08f308fc32f1 100644 (file)
@@ -1488,14 +1488,6 @@ def maybe_compress(config: MkosiConfig, compression: Compression, src: Path, dst
                 run(compressor_command(compression), stdin=i, stdout=o)
 
 
-def copy_nspawn_settings(state: MkosiState) -> None:
-    if state.config.nspawn_settings is None:
-        return None
-
-    with complete_step("Copying nspawn settings file…"):
-        shutil.copy2(state.config.nspawn_settings, state.staging / state.config.output_nspawn_settings)
-
-
 def copy_vmlinuz(state: MkosiState) -> None:
     if (state.staging / state.config.output_split_kernel).exists():
         return
@@ -1694,7 +1686,6 @@ def check_outputs(config: MkosiConfig) -> None:
         config.output_with_compression,
         config.output_checksum if config.checksum else None,
         config.output_signature if config.sign else None,
-        config.output_nspawn_settings if config.nspawn_settings is not None else None,
     ):
         if f and (config.output_dir_or_cwd() / f).exists():
             die(f"Output path {f} exists already. (Consider invocation with --force.)")
@@ -2318,7 +2309,6 @@ def build_image(args: MkosiArgs, config: MkosiConfig) -> None:
                            state.staging / state.config.output_with_format,
                            state.staging / state.config.output_with_compression)
 
-        copy_nspawn_settings(state)
         calculate_sha256sum(state)
         calculate_signature(state)
         save_manifest(state, manifest)
@@ -2411,7 +2401,7 @@ def run_shell(args: MkosiArgs, config: MkosiConfig) -> None:
     cmdline: list[PathString] = ["systemd-nspawn", "--quiet"]
 
     # If we copied in a .nspawn file, make sure it's actually honoured
-    if config.nspawn_settings is not None:
+    if config.nspawn_settings:
         cmdline += ["--settings=trusted"]
 
     if args.verb == Verb.boot:
@@ -2423,12 +2413,17 @@ def run_shell(args: MkosiArgs, config: MkosiConfig) -> None:
         ]
 
     # Underscores are not allowed in machine names so replace them with hyphens.
-    cmdline += ["--machine", (config.image_id or config.image or config.output).replace("_", "-")]
+    name = config.name().replace("_", "-")
+    cmdline += ["--machine", name]
 
     for k, v in config.credentials.items():
         cmdline += [f"--set-credential={k}:{v}"]
 
     with contextlib.ExitStack() as stack:
+        if config.nspawn_settings:
+            copy_tree(config.nspawn_settings, config.output_dir_or_cwd() / f"{name}.nspawn")
+            stack.callback(lambda: rmtree(config.output_dir_or_cwd() / f"{name}.nspawn"))
+
         if config.ephemeral:
             fname = stack.enter_context(copy_ephemeral(config, config.output_dir_or_cwd() / config.output))
         else:
index e11ab9a3c8727069ba3975599a2a170b8ee208ab..3ac3b9ad6780958998b9dc79e8ffa6feee026070 100644 (file)
@@ -1024,10 +1024,6 @@ class MkosiConfig:
     def output_split_initrd(self) -> str:
         return f"{self.output_with_version}.initrd"
 
-    @property
-    def output_nspawn_settings(self) -> str:
-        return f"{self.output_with_version}.nspawn"
-
     @property
     def output_checksum(self) -> str:
         return f"{self.output_with_version}.SHA256SUMS"