diff options
Diffstat (limited to 'pkgs/run-firecracker-vm')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 2035613..b9a2984 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -535,10 +535,13 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' args="$args systemd.tty.rows.ttyS0=$rows systemd.tty.columns.ttyS0=$cols" fi - # Take the first tap device of the user's (see modules/host) that no other - # VM holds. The lock is held until the script exits. The MAC address is - # derived from the tap's number, as modules/host does, for the tap's - # fixed address. + # Take the first tap device of the user's that no other VM holds, from + # the ones modules/host lists in /etc/fc-taps (TAP USER IP MAC, with the + # tap's own address). The lock is held until the script exits. The + # guest's address, and the MAC address that DHCP hands it out by, is + # its VM's, if it has a name in /etc/fc-vms (VM USER IP MAC), else the + # tap's own; modules/host gives the tap that address once the VM's name + # is in /run/fc-egress/TAP.name (see below). # Lock files in a directory of the user's own: in /tmp, another user # could take them first. lockdir=''${XDG_RUNTIME_DIR:-/run/user/$(id -u)} @@ -546,21 +549,36 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: $lockdir isn't a directory of $(id -un)'s; set XDG_RUNTIME_DIR" >&2 exit 1 fi + if ! test -r /etc/fc-taps; then + echo "$0: no /etc/fc-taps, see modules/host" >&2 + exit 1 + fi + me=$(id -un) tap= - for t in $(find /sys/class/net -maxdepth 1 -name 'fctap*' -printf '%f\n' | sort -V); do - test "$(cat /sys/class/net/$t/owner)" = "$(id -u)" || continue + while read -r t u a m; do + test "$u" = "$me" || continue + 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 tap=$t + addr=$a + mac=$m break fi exec {lock}>&- - done + done < /etc/fc-taps if test -z "$tap"; then - echo "$0: no free tap device fctap* of $(id -un)'s, see modules/host" >&2 + echo "$0: no free tap device fctap* of $me's, see modules/host" >&2 exit 1 fi - n=''${tap#fctap} + if test -n "$name" && test -r /etc/fc-vms; then + while read -r v u a m; do + if test "$v $u" = "$name $me"; then + addr=$a + mac=$m + fi + done < /etc/fc-vms + fi # The VM's name: held by a lock, like the tap, so it's unique among the # user's running VMs. Not like a tap's, so fc-egress can tell them # apart. @@ -589,13 +607,25 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: no list $egress (see fc-egress), no egress" >&2 fi printf '%s\n' "$egress" > "$vm" - if test -w "$vm.name"; then - printf '%s\n' "$name" > "$vm.name" - fi + printf '%s\n' "$name" > "$vm.name" + # The tap's address, by the VM's name (see above): wait for it, up to + # 10s, writing the name again every second, in case modules/host + # missed it (fcbr0-firewall@TAP was busy). + n=0 + while test "$(head -n 1 /run/fcbr0-addr/$tap 2>/dev/null)" != "$addr"; do + n=$((n + 1)) + if test $n -gt 100; then + echo "$0: $tap didn't get $addr, see fcbr0-firewall@$tap on the host" >&2 + exit 1 + fi + if test $((n % 10)) = 0; then + printf '%s\n' "$name" > "$vm.name" + fi + sleep 0.1 + done else vm= fi - mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255))) # The 9P servers, for /xchg, the SSH host key, and with nix, the store # and nixpkgs, one each per VM. They run as the user running this |
