From: Daan De Meyer Date: Wed, 15 Jan 2025 13:49:05 +0000 (+0100) Subject: Add support for machined registration using varlink X-Git-Tag: v25~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb4b27df0d4d6006570af79dfb23d367cb992bce;p=thirdparty%2Fmkosi.git Add support for machined registration using varlink --- diff --git a/mkosi/qemu.py b/mkosi/qemu.py index 928b892d3..e43b4186e 100644 --- a/mkosi/qemu.py +++ b/mkosi/qemu.py @@ -921,38 +921,63 @@ def scope_cmd( ] # fmt: skip -def register_machine(config: Config, pid: int, fname: Path) -> None: - 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()) - ): +def register_machine(config: Config, pid: int, fname: Path, cid: Optional[int]) -> None: + if not config.register or os.getuid() != 0: return - run( - [ - "busctl", - "call", - "--quiet", - "org.freedesktop.machine1", - "/org/freedesktop/machine1", - "org.freedesktop.machine1.Manager", - "RegisterMachine", - "sayssus", - config.machine_or_name().replace("_", "-"), - "0", - "mkosi", - "vm", - str(pid), - fname if fname.is_dir() else "", - ], # fmt: skip - foreground=False, - env=os.environ | config.environment, - sandbox=config.sandbox(relaxed=True), - # systemd-machined might not be installed so let's ignore any failures unless running in debug mode. - check=ARG_DEBUG.get(), - stderr=None if ARG_DEBUG.get() else subprocess.DEVNULL, - ) + if (p := Path("/run/systemd/machine/io.systemd.Machine")).is_socket(): + run( + [ + "varlinkctl", + "call", + p, + "io.systemd.Machine.Register", + json.dumps( + { + "name": config.machine_or_name().replace("_", "-"), + "service": "mkosi", + "class": "vm", + "leader": pid, + **({"rootDirectory": os.fspath(fname)} if fname.is_dir() else {}), + **({"vSockCid": cid} if cid is not None else {}), + **({"sshAddress": f"vsock/{cid}"} if cid is not None else {}), + **({"sshPrivateKeyPath": f"{config.ssh_key}"} if config.ssh_key else {}), + } + ), + ], + foreground=False, + env=os.environ | config.environment, + sandbox=config.sandbox(relaxed=True), + # Make sure varlinkctl doesn't write to stdout which messes up the terminal. + stdout=subprocess.DEVNULL, + stderr=sys.stderr, + ) + elif "DBUS_SYSTEM_ADDRESS" in os.environ or Path("/run/dbus/system_bus_socket").is_socket(): + run( + [ + "busctl", + "call", + "--quiet", + "org.freedesktop.machine1", + "/org/freedesktop/machine1", + "org.freedesktop.machine1.Manager", + "RegisterMachine", + "sayssus", + config.machine_or_name().replace("_", "-"), + "0", + "mkosi", + "vm", + str(pid), + fname if fname.is_dir() else "", + ], # fmt: skip + foreground=False, + env=os.environ | config.environment, + sandbox=config.sandbox(relaxed=True), + # systemd-machined might not be installed so let's ignore any failures unless running in debug + # mode. + check=ARG_DEBUG.get(), + stderr=None if ARG_DEBUG.get() else subprocess.DEVNULL, + ) def run_qemu(args: Args, config: Config) -> None: @@ -1437,7 +1462,7 @@ def run_qemu(args: Args, config: Config) -> None: for fd in qemu_device_fds.values(): os.close(fd) - register_machine(config, proc.pid, fname) + register_machine(config, proc.pid, fname, cid) if status := int(notifications.get("EXIT_STATUS", 0)): raise subprocess.CalledProcessError(status, cmdline)