diff options
Diffstat (limited to 'pkgs/run-firecracker-vm/default.nix')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 17 |
1 files changed, 16 insertions, 1 deletions
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 |
