]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add option to control whether to register with machined
authorLuca Boccassi <luca.boccassi@gmail.com>
Thu, 26 Dec 2024 15:05:45 +0000 (15:05 +0000)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 27 Dec 2024 11:57:20 +0000 (11:57 +0000)
On an ephemeral test runner it is useless churn to register the
guest, and can trigger nasty bugs like the one where machined
kills the user session. Add a config knob, enabled by default,
so that it can be disabled where it's not useful.

This also allows running nspawn where machined is not available,
as currently it just fails since the default is that it tries to
register and fails if it can't.

mkosi/__init__.py
mkosi/config.py
mkosi/qemu.py
mkosi/resources/man/mkosi.1.md
mkosi/vmspawn.py
tests/test_json.py

index da8a4fa29b6385a6bf25a29321b086a6d479c099..5207a033d4ed7686446a9776e8dc636df5b98476 100644 (file)
@@ -3803,6 +3803,8 @@ def run_shell(args: Args, config: Config) -> None:
     for k, v in finalize_credentials(config).items():
         cmdline += [f"--set-credential={k}:{v}"]
 
+    cmdline += ["--register", yes_no(config.register)]
+
     with contextlib.ExitStack() as stack:
         # Make sure the latest nspawn settings are always used.
         if config.nspawn_settings:
index 782f79205a2299c0ccb5f43f40984f9e284bddee..9e976ec292ed79f000de2a8ad296f193f1cdcd3b 100644 (file)
@@ -1862,6 +1862,7 @@ class Config:
     ephemeral: bool
     credentials: dict[str, str]
     kernel_command_line_extra: list[str]
+    register: bool
     runtime_trees: list[ConfigTree]
     runtime_size: Optional[int]
     runtime_scratch: ConfigFeature
@@ -3648,6 +3649,14 @@ SETTINGS: list[ConfigSetting[Any]] = [
         # arguments.
         help=argparse.SUPPRESS,
     ),
+    ConfigSetting(
+        dest="register",
+        metavar="BOOL",
+        section="Runtime",
+        parse=config_parse_boolean,
+        default=True,
+        help="Register booted vm/container with systemd-machined",
+    ),
 ]
 SETTINGS_LOOKUP_BY_NAME = {name: s for s in SETTINGS for name in [s.name, *s.compat_names]}
 SETTINGS_LOOKUP_BY_DEST = {s.dest: s for s in SETTINGS}
@@ -4887,6 +4896,7 @@ def summary(config: Config) -> str:
                     SSH Certificate: {none_to_none(config.ssh_certificate)}
                             Machine: {config.machine_or_name()}
                     Forward Journal: {none_to_none(config.forward_journal)}
+       Register guest with machined: {yes_no(config.register)}
 
             Virtual Machine Monitor: {config.vmm}
                            QEMU GUI: {yes_no(config.qemu_gui)}
index 0793a60f1017a77b953fdcf19d140b65c76c4091..c00afe388ad0177148c599883d8247e778458456 100644 (file)
@@ -918,8 +918,10 @@ def scope_cmd(
 
 
 def register_machine(config: Config, pid: int, fname: Path) -> None:
-    if os.getuid() != 0 or (
-        "DBUS_SYSTEM_ADDRESS" not in os.environ and not Path("/run/dbus/system_bus_socket").exists()
+    if (
+        not config.register
+        or os.getuid() != 0
+        or ("DBUS_SYSTEM_ADDRESS" not in os.environ and not Path("/run/dbus/system_bus_socket").exists())
     ):
         return
 
index c896fa5a2a2574e7e8f0cd776584fee462b1070c..9bc22f83e764751c62d87ab8301b3d1a37463299 100644 (file)
@@ -1801,6 +1801,10 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
     Note that `Ephemeral=` has to be enabled to start multiple instances
     of the same image.
 
+`Register=`, `--register=`
+:   Takes a boolean value. Enabled by default. Specifies whether to register
+    the vm/container with systemd-machined.
+
 `ForwardJournal=`, `--forward-journal=`
 :   Specify the path to which journal logs from containers and virtual
     machines should be forwarded. If the path has the `.journal`
index 95726168ef1732a1b481a4445840a00d70a5e185..1c3b73f52273be206100c06ec72548e6caec532e 100644 (file)
@@ -59,6 +59,7 @@ def run_vmspawn(args: Args, config: Config) -> None:
         "--vsock", config.qemu_vsock.to_tristate(),
         "--tpm", config.qemu_swtpm.to_tristate(),
         "--secure-boot", yes_no(config.secure_boot),
+        "--register", yes_no(config.register),
     ]  # fmt: skip
 
     if config.runtime_network == Network.user:
index 108b01b27b8d9ab467c7bdcd14a7aac00b4ba492..3cd3a5de25e485f70b862f987205f5d3e2dd5fb0 100644 (file)
@@ -262,6 +262,7 @@ def test_config() -> None:
             "QemuSwtpm": "auto",
             "QemuVsock": "enabled",
             "QemuVsockConnectionId": -2,
+            "Register": true,
             "Release": "53",
             "RemoveFiles": [],
             "RemovePackages": [
@@ -512,6 +513,7 @@ def test_config() -> None:
         qemu_swtpm=ConfigFeature.auto,
         qemu_vsock=ConfigFeature.enabled,
         qemu_vsock_cid=QemuVsockCID.hash,
+        register=True,
         release="53",
         remove_files=[],
         remove_packages=["all"],