diff options
| author | tv <tv@krebsco.de> | 2026-11-16 16:57:46 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 18:08:39 +0100 |
| commit | f17f88f1f53247ec86a472105cf94cfc664cd1b8 (patch) | |
| tree | eb7b564820fc3de989906865a4f2078bb287bc92 /default.nix | |
| parent | 2c37475d89b75d382728e7b33c01892770b15a26 (diff) | |
get guest addresses via DHCP, one tap device per VM
host.nix now makes a bridge fcbr0 with a number of tap devices (default 8)
and runs a dnsmasq of its own for DHCP. The run script takes the first
tap device it can lock and derives the guest MAC from its number. The
guest uses DHCP, with the MAC as client identifier, as the machine-id
(and thus the default DUID) is the same in every VM.
host.nix now takes { user, taps ? 8 } instead of just the user.
Diffstat (limited to 'default.nix')
| -rw-r--r-- | default.nix | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/default.nix b/default.nix index 3f7c522..b501b1a 100644 --- a/default.nix +++ b/default.nix @@ -58,8 +58,10 @@ let systemd.network.wait-online.enable = false; systemd.network.networks.eth = { matchConfig.Type = "ether"; - address = [ "172.16.0.2/24" ]; - gateway = [ "172.16.0.1" ]; + networkConfig.DHCP = "ipv4"; + # The default client identifier is derived from the machine-id, + # which is the same in every VM, so they'd all get the same lease. + dhcpV4Config.ClientIdentifier = "mac"; }; boot.bcache.enable = false; @@ -130,8 +132,8 @@ let }; network-interfaces = [{ iface_id = "eth0"; - host_dev_name = "fctap0"; # see host.nix - guest_mac = "06:00:ac:10:00:02"; + host_dev_name = "@TAP@"; # see host.nix + guest_mac = "@MAC@"; }]; drives = [{ drive_id = "store"; @@ -159,7 +161,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # ext4 image, which the guest mounts at /xchg, and copied back when the # guest exits. set -efu - export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.erofs-utils pkgs.nix pkgs.firecracker ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.util-linux pkgs.erofs-utils pkgs.nix pkgs.firecracker ]} tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT @@ -190,6 +192,25 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' tty_args="$tty_args systemd.tty.rows.ttyS0=$rows systemd.tty.columns.ttyS0=$cols" fi + # Take the first tap device (see host.nix) that no other VM holds. The + # lock is held until the script exits. The MAC address is derived from + # the tap's number, so the VM gets the same DHCP lease every time. + tap= + for t in $(find /sys/class/net -maxdepth 1 -name 'fctap*' -printf '%f\n' | sort -V); do + exec {lock}>"''${XDG_RUNTIME_DIR:-/tmp}/run-firecracker-vm.$t.lock" + if flock -n "$lock"; then + tap=$t + break + fi + exec {lock}>&- + done + if test -z "$tap"; then + echo "$0: no free tap device fctap*, see host.nix" >&2 + exit 1 + fi + n=''${tap#fctap} + mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255))) + mkdir -p xchg truncate -s 1G "$tmp"/xchg.img mkfs.ext4 -q -F -d xchg "$tmp"/xchg.img @@ -198,6 +219,8 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' -e "s|@STORE_IMAGE@|$img|" \ -e "s|@XCHG_IMAGE@|$tmp/xchg.img|" \ -e "s|@TTY_ARGS@|$tty_args|" \ + -e "s|@TAP@|$tap|" \ + -e "s|@MAC@|$mac|" \ ${config} > "$tmp"/config.json status=0 firecracker --no-api --config-file "$tmp"/config.json || status=$? |
