From b54b98a0b11bac582286899115d40ddff168a948 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 12:58:32 +0000 Subject: run-firecracker-vm: helpers without the lock fds bash passes the fds of the locks (on the store and home images, the tap, the name) on to every child, so the 9P servers and the watcher of nixpkgs held them, too, and when the script got killed (without its trap running), they stayed, and kept the VM's files locked: "is in use by another VM" on the next start. Now the helpers close them first, and only the VMM keeps them, as long as it runs (tested: a script killed with a helper left over leaves the file locked, and doesn't with close_locks). --- pkgs/run-firecracker-vm/default.nix | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'pkgs/run-firecracker-vm') diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 23507b2..eb97011 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -569,6 +569,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' nixpkgs_cache= nix_opt= # the last option that's only for a guest with nix secrets=() # NAME=FILE, as given + locks=() # the lock fds, see close_locks while test $# -gt 0; do case $1 in --*=*) set -- "''${1%%=*}" "''${1#*=}" "''${@:2}"; continue ;; @@ -692,6 +693,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: $f is in use by another VM" >&2 exit 1 fi + locks+=("$lock") if test "$(e2label "$f")" != "$4"; then e2label "$f" "$4" fi @@ -741,6 +743,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' test "$(cat /sys/class/net/$t/owner 2>/dev/null)" = "$(id -u)" || continue exec {lock}>"$lockdir/run-firecracker-vm.$t.lock" if flock -n "$lock"; then + locks+=("$lock") tap=$t addr=$a mac=$m @@ -774,6 +777,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: a VM named $name is running already" >&2 exit 1 fi + locks+=("$name_lock") args="$args systemd.hostname=$name" fi # The guest's whitelist: modules/host goes by the name in the tap's file @@ -821,8 +825,18 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' vsock=$tmp/vsock: servers= ports= + # The helpers (the 9P servers, the watcher below) run without the lock + # fds, which bash passes on to every child: one left over, after this + # script got killed, would keep the VM's files, tap and name locked. + # Only the VMM keeps them, as long as it runs. + close_locks() { + local fd + for fd in ''${locks[@]+"''${locks[@]}"}; do + eval "exec $fd<&-" + done + } serve() { - RUST_LOG=off unpfs "unix!$tmp/vsock!_$1" "$2" > /dev/null & + (close_locks; RUST_LOG=off exec unpfs "unix!$tmp/vsock!_$1" "$2" > /dev/null) & servers="$servers $!" ports="$ports $1" } @@ -840,6 +854,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # need fc-drop-caches by hand. if test -n "$nixpkgs_cache" && git -C "$nixpkgs" rev-parse HEAD > /dev/null 2>&1; then ( + close_locks head=$(git -C "$nixpkgs" rev-parse HEAD) while sleep 5; do new=$(git -C "$nixpkgs" rev-parse HEAD 2>/dev/null) || continue -- cgit v1.3.1