From f9f9b3a430b1588e170437c21633d22d5a5d4935 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:00:02 +0000 Subject: put what nix adds to the guest's store on a disk, optionally persistent The store's writable layer was a tmpfs, i.e. guest RAM (half of it at most), and gone with every reboot. Now it's an ext4 file system on a second drive: by default a sparse scratch file that gets removed when the guest exits, or with --store FILE one that persists across boots (created if missing, of --store-size, default 16G). nix's database lives there, too, so it matches the store, and stale entries get dropped on first use. The file is locked, so two VMs can't share it. --- default.nix | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/default.nix b/default.nix index cdaa458..3511dbb 100644 --- a/default.nix +++ b/default.nix @@ -59,9 +59,9 @@ let # storeImage below), as reading it is much faster than reading the # host's store, which comes next, read-only via 9P over vsock (see # mount.vsock9p and the run script), for anything else, like - # nixpkgs; then a tmpfs on top, so nix can add to the store. What it - # adds lives in RAM, and is gone after a reboot. The host's store - # doesn't change under a path, so 9P may cache freely. + # nixpkgs; then the second drive on top, so nix can add to the store + # (a scratch file, or one that persists, see the run script). The + # host's store doesn't change under a path, so 9P may cache freely. fileSystems."/nix/.image" = { device = "/dev/vda"; fsType = "erofs"; @@ -76,10 +76,27 @@ let neededForBoot = true; }; fileSystems."/nix/.rw-store" = { - fsType = "tmpfs"; - options = [ "mode=0755" ]; + device = "/dev/vdb"; + fsType = "ext4"; + options = [ "noatime" ]; + noCheck = true; neededForBoot = true; }; + # nix's database goes there, too, so it matches what's in the store. + fileSystems."/nix/var" = { + device = "/nix/.rw-store/var"; + fsType = "none"; + options = [ "bind" ]; + depends = [ "/nix/.rw-store" ]; + }; + systemd.services.nix-var-dir = { + requiredBy = [ "nix-var.mount" ]; + before = [ "nix-var.mount" ]; + unitConfig.DefaultDependencies = false; + unitConfig.RequiresMountsFor = "/nix/.rw-store"; + serviceConfig.Type = "oneshot"; + serviceConfig.ExecStart = "${pkgs.coreutils}/bin/mkdir -p -m 0755 /nix/.rw-store/var"; + }; fileSystems."/nix/store".overlay = { lowerdir = [ "/nix/.image" "/nix/.host-store" ]; upperdir = "/nix/.rw-store/store"; @@ -101,12 +118,15 @@ let before = [ "nix-daemon.service" ]; serviceConfig.Type = "oneshot"; serviceConfig.RemainAfterExit = true; + # With a persistent store, paths from an earlier image or host + # store may be gone; --verify removes them from the database. script = '' for arg in $(cat /proc/cmdline); do case $arg in fc.registration=*) ${config.nix.package.out}/bin/nix-store --load-db < "''${arg#*=}" esac done + ${config.nix.package.out}/bin/nix-store --verify ''; }; fileSystems."/xchg" = { @@ -256,6 +276,11 @@ let path_on_host = "${storeImage}"; is_root_device = false; is_read_only = true; + } { + drive_id = "rw-store"; + path_on_host = "@RW_STORE@"; + is_root_device = false; + is_read_only = false; }]; vsock = { guest_cid = 3; @@ -268,16 +293,49 @@ let }); in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' - # usage: run-firecracker-vm + # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] # The guest gets the host's store, read-only, via 9P over vsock, and the # directory xchg (in the current working directory) at /xchg, the same # way. VMs started in the same directory share it. + # What nix in the guest adds to the store goes to a scratch file, removed + # when the guest exits, or with --store, to FILE, which keeps it across + # boots (created if it doesn't exist, of --store-size, default 16G; the + # file is sparse, so that's only an upper limit). set -efu - export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.findutils pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} + + usage() { + echo "usage: $0 [--store FILE] [--store-size SIZE]" + } + rw_store= + rw_store_size=16G + while test $# -gt 0; do + case $1 in + --store) rw_store=$2; shift 2 ;; + --store-size) rw_store_size=$2; shift 2 ;; + -h|--help) usage; exit ;; + *) usage >&2; exit 1 + esac + done tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT + if test -z "$rw_store"; then + rw_store=$tmp/rw-store.img + fi + if ! test -e "$rw_store"; then + truncate -s "$rw_store_size" "$rw_store" + mkfs.ext4 -q -F "$rw_store" + fi + rw_store=$(realpath "$rw_store") + # Two guests with the same store file would wreck it. + exec {rw_store_lock}<"$rw_store" + if ! flock -n "$rw_store_lock"; then + echo "$0: $rw_store is in use by another VM" >&2 + exit 1 + fi + # Tell systemd the terminal's type and size, so it doesn't query them via # ANSI sequences (and wait up to 333ms for each answer): PID 1 asks for # the type of /dev/console when starting (in the initrd and again after @@ -331,6 +389,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' -e "s|@VSOCK@|$vsock|" \ -e "s|@TAP@|$tap|" \ -e "s|@MAC@|$mac|" \ + -e "s|@RW_STORE@|$rw_store|" \ ${config} > "$tmp"/config.json status=0 firecracker --no-api --config-file "$tmp"/config.json || status=$? -- cgit v1.3.1