aboutsummaryrefslogtreecommitdiffstats
path: root/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix33
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=$?