aboutsummaryrefslogtreecommitdiffstats
path: root/default.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 03:20:57 +0000
committertv <tv@krebsco.de>2026-11-16 03:20:57 +0000
commit0f6e55150719956307f36f8b11ffd39978288376 (patch)
treee540de18fc97310a23be00688691654ddab404e1 /default.nix
parent2be134b0b5e7173573c64bac49c880276bcb3d59 (diff)
add exchange directory
Firecracker can't share directories, so run-firecracker-vm copies the directory xchg (in the current working directory) into an ext4 image before booting, and replaces it with the image's contents when the guest exits. The guest automounts the image at /xchg. Changes made in the guest only arrive if it exits cleanly (e.g. via reboot), as debugfs doesn't replay the ext4 journal.
Diffstat (limited to 'default.nix')
-rw-r--r--default.nix37
1 files changed, 34 insertions, 3 deletions
diff --git a/default.nix b/default.nix
index aa8a7e4..7ef0426 100644
--- a/default.nix
+++ b/default.nix
@@ -34,6 +34,12 @@ let
fsType = "erofs";
options = [ "ro" ];
};
+ fileSystems."/xchg" = {
+ device = "/dev/vdb";
+ fsType = "ext4";
+ noCheck = true;
+ options = [ "x-systemd.automount" "nofail" ];
+ };
services.getty.autologinUser = "root";
@@ -90,6 +96,11 @@ 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;
@@ -102,8 +113,11 @@ pkgs.writeShellScriptBin "run-firecracker-vm" ''
# 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.
set -efu
- export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gnused pkgs.gnutar pkgs.erofs-utils pkgs.nix pkgs.firecracker ]}
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.gnutar pkgs.erofs-utils pkgs.nix pkgs.firecracker ]}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
@@ -131,6 +145,23 @@ pkgs.writeShellScriptBin "run-firecracker-vm" ''
tty_size="systemd.tty.rows.ttyS0=$rows systemd.tty.columns.ttyS0=$cols"
fi
- sed -e "s|@STORE_IMAGE@|$img|" -e "s|@TTY_SIZE@|$tty_size|" ${config} > "$tmp"/config.json
- firecracker --no-api --config-file "$tmp"/config.json
+ mkdir -p xchg
+ truncate -s 1G "$tmp"/xchg.img
+ mkfs.ext4 -q -F -d xchg "$tmp"/xchg.img
+
+ sed \
+ -e "s|@STORE_IMAGE@|$img|" \
+ -e "s|@XCHG_IMAGE@|$tmp/xchg.img|" \
+ -e "s|@TTY_SIZE@|$tty_size|" \
+ ${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 '^debugfs [0-9]' >&2 || :; }
+ rm -rf xchg xchg.new/lost+found
+ mv xchg.new xchg
+
+ exit $status
''