aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-21 04:10:08 +0000
committertv <tv@krebsco.de>2026-11-21 04:10:08 +0000
commit86d6f7341fedd5e0e78969d2c37829a0012dc3ae (patch)
treea496937b50b8de3b612d997f1615e8977d9d8319 /pkgs
parent4e35ef641c9bca1f63c3248877295fe61d4c1d20 (diff)
host: static addresses for VMs, by name, on whichever tap they run
A VM got the first free tap of its user's, and a tap's address came from its number, so a VM's address depended on what else ran, and on the users before it, and a reverse proxy on the host could only guess it (172.16.0.2, i.e. fctap0, for hermes). Now addresses are the VMs': the host module takes vms, VM name -> user and address (vms = { hermes = { user = "tv"; ip = "172.16.0.2"; }; }), any number of them, and a VM takes any free tap of its user's, which gets its address while it runs, so at most as many run at the same time as the user has taps. Taps have addresses of their own, for VMs without one: from 172.16.0.200 on, so they stand out (but for VMs' there). The MAC address, which dnsmasq hands out every address by, is 06:00 and the address (06:00:ac:10:00:02 for 172.16.0.2), and with it the IPv6 addresses; the run script takes the VM's from /etc/fc-vms, or the tap's own from /etc/fc-taps. The VMs' addresses are also in config.fc.vms.NAME (user, ip, ip6), for the host's configuration, e.g. proxyPass = "http://${config.fc.vms.hermes.ip}:9120"; Which address a tap has is up to fcbr0-firewall, which now also runs when a tap's VM's name (/run/fc-egress/TAP.name, which the run script writes) changes: the VM's, if it's one of the tap's user's in vms and on none of their other taps, too, else the tap's own. It puts it into the tap's new nftables sets addr_TAP and addr6_TAP, in the bridge's table, which lets the tap send from nothing else (instead of the static set guests), and in the other, whose per-tap rules go by them (instead of the tap's fixed addresses); and into /run/fcbr0-addr, ADDRESS: TAP and TAP: ADDRESS, where egress.lua finds a guest's tap (as long as both agree, and the address and the tap are the same user's), and the run script waits for its address before starting the guest. fc-egress vms shows the taps' addresses, too.
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/run-firecracker-vm/default.nix56
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