diff options
| author | tv <tv@krebsco.de> | 2026-11-25 13:00:36 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 13:00:36 +0000 |
| commit | 2ca2286661d64b4f93bba4c6a998e271be65d5f4 (patch) | |
| tree | 6b9b02cc844c9f009fb1a66c5472ad371a0b3751 /pkgs | |
| parent | b54b98a0b11bac582286899115d40ddff168a948 (diff) | |
run-firecracker-vm: reboot and poweroff both end the VM, in either VMM
Firecracker ends on a reset (reboot, with reboot=k), but stays on
poweroff, with the guest halted; Cloud Hypervisor ends on poweroff, but
restarts the guest on a reset. So which one left the VM depended on the
VMM. Now the run script tells the guest (fc.vmm on the kernel command
line), and a generator, fc-vmm-exit, changes the shutdown's last step,
the SuccessAction of systemd-poweroff and -halt to reboot-force in
Firecracker, and of systemd-reboot and -halt to poweroff-force in Cloud
Hypervisor, after the same clean shutdown. Not a hook of
systemd-shutdown, which runs those after unmounting, i.e. without /etc,
an overlay here (found out the hard way: the first version, with such a
hook, did nothing).
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index eb97011..e225451 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -114,6 +114,34 @@ let (modulesPath + "/profiles/perlless.nix") { fileSystems = imageFileSystems (n + 1); } { systemd.generators.fc-home = homeGenerator (label "fc-home"); } + # reboot and poweroff both end the VM, whichever the VMM (fc.vmm, + # from the run script): Firecracker ends on a reset (reboot=k), but + # stays on poweroff, with the guest halted; Cloud Hypervisor ends on + # poweroff, but restarts the guest on a reset. So the shutdown's + # last step (SuccessAction of systemd-poweroff, -halt, -reboot) + # becomes a reset in Firecracker, and a poweroff in Cloud + # Hypervisor, after the same clean shutdown. (Not a hook of + # systemd-shutdown: it runs those after unmounting, i.e. without + # /etc.) + { systemd.generators.fc-vmm-exit = pkgs.writeShellScript "fc-vmm-exit" '' + read -r cmdline < /proc/cmdline + exit_by() { # ACTION UNIT... + action=$1 + shift + for unit; do + ${pkgs.coreutils}/bin/mkdir -p "$dir/$unit.d" + printf '[Unit]\nSuccessAction=%s\n' "$action" > "$dir/$unit.d/fc-vmm-exit.conf" + done + } + dir=$1 + case " $cmdline " in + *" fc.vmm=firecracker "*) + exit_by reboot-force systemd-poweroff.service systemd-halt.service ;; + *" fc.vmm=cloud-hypervisor "*) + exit_by poweroff-force systemd-reboot.service systemd-halt.service ;; + esac + ''; + } { # For a layer whose state is in /home: the run script then # refuses to start the guest without --home. @@ -547,8 +575,8 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # to the guest only, never in the store (see fc-secrets); a guest # doesn't start without the secrets it declares (fc.secrets). # --vmm: the hypervisor, Firecracker (default) or Cloud Hypervisor, which - # restarts the guest on reboot (leave it with poweroff instead), and - # takes no commas in --store and --home. + # takes no commas in --store and --home. Either ends when the guest + # reboots or powers off (see fc-vmm-exit). set -efu export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.cloud-hypervisor pkgs.git pkgs.socat pkgs.unpfs ]} @@ -627,6 +655,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: bad --vmm: $vmm (firecracker or cloud-hypervisor)" >&2 exit 1 esac + args="$args fc.vmm=$vmm" # see fc-vmm-exit tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT |
