diff options
| -rw-r--r-- | default.nix | 33 | ||||
| -rw-r--r-- | host.nix | 48 |
2 files changed, 69 insertions, 12 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=$? @@ -1,15 +1,49 @@ -# Host side of the guest's network: a tap device owned by the user running -# the VM, NATed to the outside world. -# usage (in the host's configuration.nix): imports = [ (import ./host.nix "tv") ]; -user: { - networking.interfaces.fctap0 = { +# Host side of the guests' network: a bridge with tap devices fctap0, +# fctap1, ... owned by the user running the VMs, a DHCP server, and NAT to +# the outside world. Each running VM takes one tap device, so taps is the +# number of VMs that can run at the same time. +# usage (in the host's configuration.nix): +# imports = [ (import ./host.nix { user = "tv"; }) ]; +# The host's firewall has to let the guests in on fcbr0: DHCP (udp 67) +# as input, and anything from fcbr0 (plus the return traffic) as forward. +{ user, taps ? 8 }: { lib, pkgs, ... }: let + tapNames = map (i: "fctap${toString i}") (lib.range 0 (taps - 1)); +in { + networking.interfaces = lib.genAttrs tapNames (_: { virtual = true; virtualType = "tap"; virtualOwner = user; - ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }]; + }) // { + fcbr0.ipv4.addresses = [{ address = "172.16.0.1"; prefixLength = 24; }]; }; + networking.bridges.fcbr0.interfaces = tapNames; + networking.nat = { enable = true; - internalInterfaces = [ "fctap0" ]; + internalInterfaces = [ "fcbr0" ]; + }; + + # A dnsmasq of its own, so it doesn't get mixed up with one the host might + # run otherwise. DHCP only (port=0 disables DNS); bind-dynamic copes with + # fcbr0 not being there yet. + systemd.services.fcbr0-dhcp = { + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + serviceConfig = { + RuntimeDirectory = "fcbr0-dhcp"; + Restart = "on-failure"; + ExecStart = lib.escapeShellArgs [ + "${pkgs.dnsmasq}/bin/dnsmasq" + "--keep-in-foreground" + "--conf-file=/dev/null" + "--port=0" + "--bind-dynamic" + "--interface=fcbr0" + "--dhcp-authoritative" + "--dhcp-range=172.16.0.2,172.16.0.254,1h" + "--dhcp-option=option:dns-server,9.9.9.9" + "--dhcp-leasefile=/run/fcbr0-dhcp/leases" + ]; + }; }; } |
