From e0b8ef7a5f907baf7362ee34f24c107238590a8f Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 02:52:46 +0000 Subject: run-firecracker-vm --name A VM can now have a name: run-firecracker-vm --name NAME. It's unique among the user's running VMs (a lock in the user's runtime directory, like the taps'), becomes the guest's host name (systemd.hostname= on the kernel command line), and goes to /run/fc-egress/TAP.name while the VM runs, for fc-egress. Names are letters, digits and -, and can't look like a tap's. fc-egress takes a running VM's name or tap where it takes a VM: -v VM works on the list the VM uses, use VM LIST switches it to another list, and vms shows tap, name and list. The logs keep saying taps (fc-egress vms maps them). --- pkgs/run-firecracker-vm/default.nix | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'pkgs/run-firecracker-vm') diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 5b148da..223654a 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -306,7 +306,7 @@ in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] # [--nixpkgs DIR] [--nixpkgs-cache MODE] - # [--egress LIST] + # [--egress LIST] [--name NAME] # The guest gets the host's store, read-only, via 9P over vsock, and the # directory xchg (in the current working directory) at /xchg, the same # way. VMs started in the same directory share it. Also the same way, @@ -321,17 +321,20 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # boots (created if it doesn't exist, of --store-size, default 16G; the # file is sparse, so that's only an upper limit). # The guest's egress goes by the user's whitelist LIST (default: - # default), see modules/host and fc-egress. + # default), see modules/host and fc-egress. --name names the VM, for + # fc-egress and as the guest's host name; only one running VM of the + # user's can have a given name. set -efu export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME]" } args= rw_store= rw_store_size=16G egress=default + name= nixpkgs=${lib.escapeShellArg (toString pkgs.path)} while test $# -gt 0; do case $1 in @@ -341,6 +344,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --nixpkgs) nixpkgs=$2; shift 2 ;; --nixpkgs-cache) args="$args fc.9p566=cache=$2"; shift 2 ;; --egress) egress=$2; shift 2 ;; + --name) name=$2; shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -401,6 +405,22 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' exit 1 fi n=''${tap#fctap} + # 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. + if test -n "$name"; then + if ! printf %s "$name" | grep -qxE '[A-Za-z0-9][A-Za-z0-9-]{0,62}' || + printf %s "$name" | grep -qxE 'fctap[0-9]+'; then + echo "$0: bad name: $name (letters, digits and -, not like a tap)" >&2 + exit 1 + fi + exec {name_lock}>"$lockdir/run-firecracker-vm.name.$name.lock" + if ! flock -n "$name_lock"; then + echo "$0: a VM named $name is running already" >&2 + exit 1 + fi + args="$args systemd.hostname=$name" + fi # The guest's whitelist: modules/host goes by the name in the tap's file # in /run/fc-egress (emptied again when the guest exits, below). vm=/run/fc-egress/$tap @@ -413,6 +433,9 @@ 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 else vm= fi @@ -435,7 +458,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' xchg=$! RUST_LOG=off unpfs "unix!$tmp/vsock!_566" "$nixpkgs" > /dev/null & nixpkgs_server=$! - trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; fi' EXIT + trap 'kill $store $xchg $nixpkgs_server; rm -rf "$tmp"; if test -n "$vm"; then : > "$vm"; if test -w "$vm.name"; then : > "$vm.name"; fi; fi' EXIT while ! test -S "$vsock"_565; do sleep 0.01; done sed \ -- cgit v1.3.1