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). --- README.md | 13 +++---- modules/host/default.nix | 67 ++++++++++++++++++++++++++++++------- pkgs/run-firecracker-vm/default.nix | 31 ++++++++++++++--- 3 files changed, 89 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 497c07c..667fc4a 100644 --- a/README.md +++ b/README.md @@ -101,16 +101,17 @@ fc-egress lists fc-egress log -f ``` -Each VM uses one list, `default` unless given another: +Each VM uses one list, `default` unless given another, and can have a +name (also its host name; unique among your running VMs): ```sh -./result/bin/run-firecracker-vm --egress work +./result/bin/run-firecracker-vm --egress work --name box +fc-egress vms # running VMs: tap (as in the log), name, list +fc-egress -v box allow example.org # the list box uses +fc-egress use box default # switch box to another list ``` -A list that doesn't exist allows nothing. `fc-egress vms` shows the -running VMs' taps (as in the log) and their lists. Which list a running -VM uses is in `/run/fc-egress/TAP`; to switch, write another name there -(`echo default > /run/fc-egress/fctap3`). Changes to lists and switches +A list that doesn't exist allows nothing. Changes to lists and switches apply to the next connection. Whole domains get passed through encrypted. Domains with path rules get decrypted to check each request, with certificates from a CA the host creates on first start diff --git a/modules/host/default.nix b/modules/host/default.nix index 4f31c19..c70e505 100644 --- a/modules/host/default.nix +++ b/modules/host/default.nix @@ -26,7 +26,8 @@ # line, the list's name, writable by the tap's user), which the run script # writes when it takes the tap (--egress LIST, default "default") and # empties when the VM exits. No list, or one that doesn't exist, allows -# nothing. +# nothing. Likewise /run/fc-egress/TAP.name, the VM's name (--name), so +# fc-egress can take names instead of taps; only fc-egress reads it. # # Users can also allow plain TCP or UDP to given addresses and ports, like # ssh to some host: rules like tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] in the @@ -71,6 +72,9 @@ vmDir = "/run/fc-egress"; # TAP: the name of the VM's list # List names: no slashes, no dot files. listName = "[A-Za-z0-9_-]+"; + # VM names (run-firecracker-vm --name, also the guest's host name): + # /run/fc-egress/TAP.name, for fc-egress. + vmName = "[A-Za-z0-9][A-Za-z0-9-]{0,62}"; userNames = lib.attrNames users; deny4 = lib.filter (a: !lib.hasInfix ":" a) denyDestinations; deny6 = lib.filter (a: lib.hasInfix ":" a) denyDestinations; @@ -155,24 +159,45 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps} exit 1 fi usage() { - echo "usage: fc-egress [-l LIST] list" - echo " fc-egress [-l LIST] allow RULE..." - echo " fc-egress [-l LIST] deny RULE..." + echo "usage: fc-egress [-l LIST | -v VM] list" + echo " fc-egress [-l LIST | -v VM] allow RULE..." + echo " fc-egress [-l LIST | -v VM] deny RULE..." echo " fc-egress lists" echo " fc-egress vms" + echo " fc-egress use VM LIST" echo " fc-egress log [JOURNALCTL-OPTION...]" echo "LIST is a whitelist's name (default: default); a VM uses the one" echo " given to run-firecracker-vm --egress (default: default)" + echo "VM is a running VM's name (run-firecracker-vm --name) or tap;" + echo " -v VM means the list it uses" echo "RULE is DOMAIN (HTTPS, with subdomains, not decrypted)" echo " or DOMAIN/PATH-PREFIX (HTTPS, decrypted to check the path)" echo " or tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] (likewise udp://...)" - echo "vms shows the running VMs' taps (as in the log) and lists" + echo "vms shows the running VMs' taps (as in the log), names and lists" + echo "use switches a running VM to another list" + } + # the tap of a running VM of the user's, by name or tap + tap_of() { + for f in $(find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'fctap*' ! -name '*.*' -printf '%f\n'); do + if test "$f" = "$1" || test "$(head -n 1 ${vmDir}/$f.name 2>/dev/null)" = "$1"; then + echo "$f" + return + fi + done + echo "fc-egress: no running VM $1 of $user's" >&2 + exit 1 } name=default - if test "''${1-}" = -l; then - name=''${2-} - shift 2 || : - fi + case ''${1-} in + -l) + name=''${2-} + shift 2 || : + ;; + -v) + tap=$(tap_of "''${2-}") + name=$(head -n 1 ${vmDir}/$tap) + shift 2 + esac if ! printf %s "$name" | grep -qxE '${listName}'; then echo "fc-egress: bad list name: $name" >&2 exit 1 @@ -230,8 +255,23 @@ ${lib.concatMapStrings (t: " ${t.name}) user=${t.user} ;;\n") taps} find ${dir}/$user -mindepth 1 -maxdepth 1 -type f -printf '%f\n' | sort ;; vms) - find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -printf '%f\n' | sort -V | - while read -r tap; do printf '%s %s\n' "$tap" "$(head -n 1 ${vmDir}/$tap)"; done + find ${vmDir} -mindepth 1 -maxdepth 1 -user "$user" -size +0 -name 'fctap*' ! -name '*.*' -printf '%f\n' | sort -V | + while read -r tap; do + vm=$(head -n 1 ${vmDir}/$tap.name 2>/dev/null) || : + printf '%s %s %s\n' "$tap" "''${vm:--}" "$(head -n 1 ${vmDir}/$tap)" + done + ;; + use) + if test $# != 2; then usage >&2; exit 1; fi + tap=$(tap_of "$1") + if ! printf %s "$2" | grep -qxE '${listName}'; then + echo "fc-egress: bad list name: $2" >&2 + exit 1 + fi + if ! test -f ${dir}/$user/"$2"; then + echo "fc-egress: no list $2, $1 gets no egress" >&2 + fi + printf '%s\n' "$2" > ${vmDir}/$tap ;; log) # HTTPS connections: ALLOW|DENY, user, tap, list, server name, @@ -585,7 +625,10 @@ ${lib.concatMapStrings (t: " ip saddr ${t.ip} limit rate 10/second burst "d ${dir}/${u} 0755 ${u} - -" "f ${dir}/${u}/default 0644 ${u} - -" ]) userNames - ++ map (t: "f ${vmDir}/${t.name} 0644 ${t.user} - -") taps; + ++ lib.concatMap (t: [ + "f ${vmDir}/${t.name} 0644 ${t.user} - -" + "f ${vmDir}/${t.name}.name 0644 ${t.user} - -" + ]) taps; # Forwarding, for the tcp:// and udp:// rules. boot.kernel.sysctl."net.ipv4.ip_forward" = 1; 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