From: Rémi Palancher Date: Mon, 24 Jan 2022 08:59:29 +0000 (+0100) Subject: Add option to run nspawn in current unit X-Git-Tag: v13~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ab16cda00f5b24848b427ddd28ad62c822079ff;p=thirdparty%2Fmkosi.git Add option to run nspawn in current unit This commit adds --nspawn-keep-unit option to add --keep-unit option to underlying systemd-nspawn commands. This makes systemd-nspawn uses the current unit scope and allocated ressources. This can be notably useful when mkosi is run by a system service. --- diff --git a/mkosi.md b/mkosi.md index cd974fad8..0ad3f43be 100644 --- a/mkosi.md +++ b/mkosi.md @@ -1095,6 +1095,13 @@ a machine ID. : When used with the `qemu` verb, this options sets `qemu`'s `-m` argument which controls the amount of guest's RAM. Defaults to `1G`. +`NspawnKeepUnit=`, `--nspawn-keep-unit` + +: When used, this option instructs underlying calls of systemd-nspawn to + use the current unit scope, instead of creating a dedicated transcient + scope unit for the containers. This option should be used when mkosi is + run by a service unit. + `Netdev=`, `--netdev` : When used with the boot or qemu verbs, this option creates a virtual diff --git a/mkosi/__init__.py b/mkosi/__init__.py index d529c081b..9714a3556 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -5630,6 +5630,11 @@ def create_parser() -> ArgumentParserMkosi: group.add_argument("--qemu-headless", action=BooleanAction, help="Configure image for qemu's -nographic mode") group.add_argument("--qemu-smp", help="Configure guest's SMP settings", metavar="SMP", default="2") group.add_argument("--qemu-mem", help="Configure guest's RAM size", metavar="MEM", default="1G") + group.add_argument( + "--nspawn-keep-unit", + action=BooleanAction, + help="If specified, underlying systemd-nspawn containers use the ressources of the current unit." + ) group.add_argument( "--network-veth", dest="netdev", @@ -7258,6 +7263,9 @@ def run_build_script(args: MkosiArgs, root: Path, raw: Optional[BinaryIO]) -> No if args.usr_only: cmdline += [f"--bind={root_home(args, root)}:/root"] + if args.nspawn_keep_unit: + cmdline += ["--keep-unit"] + cmdline += [f"/root/{args.build_script.name}"] cmdline += args.cmdline @@ -7519,6 +7527,9 @@ def run_shell_cmdline(args: MkosiArgs, pipe: bool = False, commands: Optional[Se cmdline += ["--machine", virt_name(args)] + if args.nspawn_keep_unit: + cmdline += ["--keep-unit"] + if commands or args.cmdline: # If the verb is 'shell', args.cmdline contains the command to run. # Otherwise, the verb is 'boot', and we assume args.cmdline contains nspawn arguments. diff --git a/mkosi/backend.py b/mkosi/backend.py index f69277399..36c856f99 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -517,6 +517,9 @@ class MkosiArgs: qemu_smp: str qemu_mem: str + # systemd-nspawn specific options + nspawn_keep_unit: bool + # Some extra stuff that's stored in MkosiArgs for convenience but isn't populated by arguments machine_id_is_fixed: bool original_umask: int @@ -661,6 +664,9 @@ def run_workspace_command( stdout = subprocess.PIPE nspawn += ["--console=pipe"] + if args.nspawn_keep_unit: + nspawn += ["--keep-unit"] + result = run([*nspawn, "--", *cmd], check=False, stdout=stdout, text=capture_stdout) if result.returncode != 0: if "workspace-command" in ARG_DEBUG: diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index f51658dfd..7f8e1bd56 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -132,6 +132,7 @@ class MkosiConfig: "qemu_headless": False, "qemu_smp": "2", "qemu_mem": "1G", + "nspawn_keep_unit": False, "netdev": False, "ephemeral": False, "with_unified_kernel_images": True,