aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-25 22:09:34 +0000
committertv <tv@krebsco.de>2026-11-25 22:09:34 +0000
commit25100d8884da14c48bfac12753e820be04028a05 (patch)
tree57e6a0d29d38fd739738160e95df39e3818dc9e6
parent00b15b27c2f4510c262474c8ae54be588c575476 (diff)
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.
-rw-r--r--README.md6
-rw-r--r--pkgs/run-firecracker-vm/default.nix42
2 files changed, 32 insertions, 16 deletions
diff --git a/README.md b/README.md
index fe54ab0..f9e32de 100644
--- a/README.md
+++ b/README.md
@@ -14,8 +14,8 @@ The guest gets:
host's Nix store, read-only, and a writable layer on top of both, so
nix and `nix-shell` work, and `/nixpkgs`: the host's nixpkgs,
read-only, on the guest's `NIX_PATH`;
-- `/xchg`: the directory `xchg` in the current working directory, shared
- with the host (and with other VMs started in the same directory);
+- optionally (`--xchg DIR`), `/xchg`: a directory of the host's, shared
+ with it (and with other VMs given the same directory);
- network access only to HTTPS, through a proxy on the host that allows
only listed domains, optionally only listed paths.
@@ -204,7 +204,7 @@ A guest's address is its VM's, by name (`vms`, see host setup), or else
its tap's own (see `fc-egress vms`).
Secrets, like API keys (and that host key), come from the host at boot,
-never through the store or `/xchg` (which other VMs started in the same
+never through the store or `/xchg` (which other VMs given the same
directory see): a layer declares what it needs, where it goes (default:
`/run/secrets/NAME`), and whose it is (default: root's, 0400):
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