aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 17:25:06 +0000
committertv <tv@krebsco.de>2026-11-16 18:38:44 +0100
commita042f54b019dc4b1a9796dc0f5a49767c3ce5254 (patch)
treeb4d378d50f2390c2e07007c7c33887fa1dd16cd9
parentdf7d88915d7e4022cb5f85fc95bc174fa5fc9d50 (diff)
xchg: serve it via 9P over TCP instead of copying an ext4 image
The run script starts unpfs (as the user) on 172.16.0.1, port 5640 plus the tap's number, and tells the guest to mount it at /xchg via systemd.mount-extra on the kernel command line. VMs started in the same directory share xchg, writes land on the host right away, and nothing is lost when a guest doesn't shut down cleanly. No more debugfs rdump. The cut-down kernel gets 9P as modules.
-rw-r--r--default.nix41
-rw-r--r--kernel.nix2
2 files changed, 18 insertions, 25 deletions
diff --git a/default.nix b/default.nix
index 538c928..0c7e52c 100644
--- a/default.nix
+++ b/default.nix
@@ -38,12 +38,7 @@ let
fsType = "erofs";
options = [ "ro" "nodev" "nosuid" ];
};
- fileSystems."/xchg" = {
- device = "/dev/vdb";
- fsType = "ext4";
- noCheck = true;
- options = [ "x-systemd.automount" "nofail" ];
- };
+ # /xchg gets mounted via 9P, see systemd.mount-extra in the run script.
services.getty.autologinUser = "root";
systemd.targets.getty.wants = lib.mkForce [ ]; # no getty on tty1
@@ -129,7 +124,7 @@ let
boot-source = {
kernel_image_path = "${kernel.dev}/vmlinux";
initrd_path = "${initialRamdisk}/${nixos.config.system.boot.loader.initrdFile}";
- boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" "@TTY_ARGS@" ]);
+ boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" "@TTY_ARGS@" "@XCHG_ARGS@" ]);
};
network-interfaces = [{
iface_id = "eth0";
@@ -141,11 +136,6 @@ let
path_on_host = "@STORE_IMAGE@";
is_root_device = false;
is_read_only = true;
- } {
- drive_id = "xchg";
- path_on_host = "@XCHG_IMAGE@";
- is_root_device = false;
- is_read_only = false;
}];
machine-config = {
vcpu_count = 2;
@@ -158,11 +148,11 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# Packs the closure of the guest system (plus any given store paths) into
# an erofs image, which the guest mounts read-only at /nix/store.
# The image of the last closure is kept in the cache directory.
- # The directory xchg (in the current working directory) is copied into an
- # ext4 image, which the guest mounts at /xchg, and copied back when the
- # guest exits.
+ # The directory xchg (in the current working directory) is served via 9P
+ # over TCP and mounted by the guest at /xchg. VMs started in the same
+ # directory share it.
set -efu
- export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.util-linux pkgs.erofs-utils pkgs.nix pkgs.firecracker ]}
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.util-linux pkgs.erofs-utils pkgs.nix pkgs.firecracker pkgs.unpfs ]}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
@@ -212,25 +202,26 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
n=''${tap#fctap}
mac=$(printf '06:00:ac:10:%02x:%02x' $((n >> 8 & 255)) $((n & 255)))
+ # One 9P server per VM, on a port derived from the tap's number. Its
+ # files end up owned by the user running this script. RUST_LOG=off
+ # silences its logging of failed requests (like walking to a file that
+ # doesn't exist), fatal errors still go to stderr.
+ port=$((5640 + n))
mkdir -p xchg
- truncate -s 1G "$tmp"/xchg.img
- mkfs.ext4 -q -F -d xchg "$tmp"/xchg.img
+ RUST_LOG=off unpfs "tcp!172.16.0.1!$port" xchg > /dev/null &
+ unpfs=$!
+ trap 'kill $unpfs; rm -rf "$tmp"' EXIT
+ xchg_args="systemd.mount-extra=172.16.0.1:/xchg:9p:trans=tcp,port=$port,version=9p2000.L,x-systemd.automount,nofail"
sed \
-e "s|@STORE_IMAGE@|$img|" \
- -e "s|@XCHG_IMAGE@|$tmp/xchg.img|" \
-e "s|@TTY_ARGS@|$tty_args|" \
+ -e "s|@XCHG_ARGS@|$xchg_args|" \
-e "s|@TAP@|$tap|" \
-e "s|@MAC@|$mac|" \
${config} > "$tmp"/config.json
status=0
firecracker --no-api --config-file "$tmp"/config.json || status=$?
- rm -rf xchg.new
- mkdir xchg.new
- debugfs -R 'rdump / xchg.new' "$tmp"/xchg.img 2>&1 | { grep -v -e '^debugfs [0-9]' -e 'Operation not permitted while changing ownership' >&2 || :; }
- rm -rf xchg xchg.new/lost+found
- mv xchg.new xchg
-
exit $status
''
diff --git a/kernel.nix b/kernel.nix
index 184a014..5b43d27 100644
--- a/kernel.nix
+++ b/kernel.nix
@@ -17,6 +17,8 @@ let
buildPhase = ''
cat ${firecrackerConfig} - > .config <<EOF
CONFIG_EROFS_FS=m
+ CONFIG_NET_9P=m
+ CONFIG_9P_FS=m
EOF
make olddefconfig
'';