From 25100d8884da14c48bfac12753e820be04028a05 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 22:09:34 +0000 Subject: run-firecracker-vm: /xchg only with --xchg DIR Every guest got the directory xchg, where the run script was started, at /xchg, writable, and shared with every other VM started in the same directory: a way across the boundary between the VMs, and to the host, that most guests don't need (tv works with them via ssh), which is what the VMs are for. Now there's no /xchg unless asked for: --xchg DIR serves DIR (created if missing) via 9P, and passes fc.xchg, for which a generator in the guest writes the automount; without it, there's no 9P server for it, and no /xchg in the guest at all. --- pkgs/run-firecracker-vm/default.nix | 42 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'pkgs/run-firecracker-vm/default.nix') diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 8a58c60..8cf8da4 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -114,6 +114,20 @@ let (modulesPath + "/profiles/perlless.nix") { fileSystems = imageFileSystems (n + 1); } { systemd.generators.fc-home = homeGenerator (label "fc-home"); } + # /xchg: a directory of the host's, via 9P on vsock port 564, only + # with fc.xchg on the kernel command line (run-firecracker-vm + # --xchg DIR), mounted when first used. A generator, so a guest + # without it has no /xchg at all. + { systemd.generators.fc-xchg = pkgs.writeShellScript "fc-xchg-generator" '' + read -r cmdline < /proc/cmdline + case " $cmdline " in *" fc.xchg "*) ;; *) exit 0 + esac + printf '%s\n' '[Mount]' 'What=564' 'Where=/xchg' 'Type=vsock9p' > "$1"/xchg.mount + printf '%s\n' '[Automount]' 'Where=/xchg' > "$1"/xchg.automount + ${pkgs.coreutils}/bin/mkdir -p "$1"/local-fs.target.wants + ${pkgs.coreutils}/bin/ln -s ../xchg.automount "$1"/local-fs.target.wants/xchg.automount + ''; + } # reboot and poweroff both end the VM, whichever the VMM (fc.vmm, # from the run script): Firecracker ends on a reset (reboot=k), but # stays on poweroff, with the guest halted; Cloud Hypervisor ends on @@ -246,12 +260,6 @@ let } else (imageFileSystems 1)."/nix/.image/0"; - fileSystems."/xchg" = { - device = "564"; - fsType = "vsock9p"; - noCheck = true; - options = [ "x-systemd.automount" "nofail" ]; - }; system.fsPackages = [ (pkgs.runCommand "mount.vsock9p" { } "install -D ${mountVsock9p} $out/bin/mount.vsock9p") ]; @@ -555,9 +563,10 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # [--egress LIST] [--name NAME] [--vcpus N] # [--secret NAME=FILE]... # [--vmm firecracker|cloud-hypervisor] [--nested] - # The guest gets the directory xchg (in the current working directory) - # at /xchg, via 9P over vsock. VMs started in the same directory share - # it. Its store is read-only, unless it has nix (layers/nix.nix). + # [--xchg DIR] + # With --xchg DIR (created if missing), the guest gets DIR at /xchg, via + # 9P over vsock; without, there's no /xchg. The guest's store is + # read-only, unless it has nix (layers/nix.nix). # A guest with nix gets the host's store, read-only, the same way, and # also nixpkgs at /nixpkgs, read-only: DIR, by default the nixpkgs the # guest was built from. It isn't cached in the guest, so changes show @@ -593,7 +602,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.cloud-hypervisor pkgs.git pkgs.socat pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]... [--vmm firecracker|cloud-hypervisor] [--nested]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]... [--vmm firecracker|cloud-hypervisor] [--nested] [--xchg DIR]" } args= rw_store= @@ -604,6 +613,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' egress= name= vcpus=2 + xchg= vmm=firecracker nested= nixpkgs=${lib.escapeShellArg (toString pkgs.path)} @@ -624,6 +634,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --egress) egress=$2; shift 2 ;; --name) name=$2; shift 2 ;; --vcpus) vcpus=$2; shift 2 ;; + --xchg) xchg=$2; shift 2 ;; --secret) secrets+=("$2"); shift 2 ;; --vmm) vmm=$2; shift 2 ;; --nested) nested=on; shift ;; @@ -670,6 +681,9 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' exit 1 esac args="$args fc.vmm=$vmm" # see fc-vmm-exit + if test -n "$xchg"; then + args="$args fc.xchg" # see fc-xchg + fi tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT @@ -855,7 +869,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' vm= fi - # The 9P servers, for /xchg, the secrets, and with nix, the store + # The 9P servers, for the secrets, /xchg (--xchg), and with nix, the store # and nixpkgs, one each per VM. They run as the user running this # script: xchg's files end up owned by the user, and the store is # read-only to them. When the guest connects to vsock port PORT on the @@ -883,8 +897,10 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' servers="$servers $!" ports="$ports $1" } - mkdir -p xchg - serve 564 xchg + if test -n "$xchg"; then + mkdir -p "$xchg" + serve 564 "$xchg" + fi serve 567 "$tmp"/secrets ${lib.optionalString nix '' serve 565 /nix/store -- cgit v1.3.1