diff options
| author | tv <tv@krebsco.de> | 2026-11-17 02:52:46 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 02:52:46 +0000 |
| commit | e0b8ef7a5f907baf7362ee34f24c107238590a8f (patch) | |
| tree | bf254e0a6901c79a8398e9d8833e5459c7315e17 /modules/host/default.nix | |
| parent | d6c5c010f3764e2a7bc7c6b654ef7986d704199b (diff) | |
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).
Diffstat (limited to 'modules/host/default.nix')
| -rw-r--r-- | modules/host/default.nix | 67 |
1 files changed, 55 insertions, 12 deletions
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; |
