From 9d7f66b9cbdc203226ef919a4108706248d9779d Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 00:44:08 +0000 Subject: add --nixpkgs-cache MODE /nixpkgs isn't cached in the guest, so changes to a checkout show up right away, but evaluating is slow. --nixpkgs-cache sets 9P's cache mode for it, e.g. loose, which made evaluating tmux several times faster. It gets passed on the kernel command line as fc.9p566=cache=MODE, and mount.vsock9p appends such options for the port it mounts. Options also take the --option=value form now. --- README.md | 6 +++++- pkgs/run-firecracker-vm/default.nix | 38 +++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 4174b03..69ee20c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,11 @@ The file gets created (sparse, default 16G) if it doesn't exist. Only one VM at a time can use it. `/nixpkgs` is the nixpkgs the guest was built from (``, checkout -or not); `--nixpkgs DIR` serves another one. +or not); `--nixpkgs DIR` serves another one. It's not cached in the +guest, so changes to it show up right away, but evaluating is slow; +`--nixpkgs-cache=loose` caches it (several times faster, but changes may +not show up while the guest runs). Other modes: `mmap`, `readahead`, +`none` (the default). To use a cut-down kernel, based on Firecracker's recommended guest configuration, instead of `linuxPackages_latest`: diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 98b676b..37423a8 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -12,17 +12,27 @@ let # fds of a connected socket (trans=fd), which socat passes to mount as # stdin and stdout (nofork). Separate -o options, as socat would take # commas as its own option separators. In the initrd, too, for the - # store. + # store. More options for PORT can come from the kernel command line, + # as fc.9pPORT=OPTIONS (see --nixpkgs-cache in the run script); they come + # last, so they win. mountVsock9p = pkgs.writeShellScript "mount.vsock9p" '' # usage: mount.vsock9p PORT DIR [-o OPTIONS] set -efu opts= - if test "''${3-}" = -o; then - for o in ''${4//,/ }; do + add() { + for o in ''${1//,/ }; do case $o in x-*|nofail|auto|noauto|defaults) ;; *) opts="$opts -o $o" esac done + } + if test "''${3-}" = -o; then + add "$4" fi + read -r cmdline < /proc/cmdline + for arg in $cmdline; do + case $arg in fc.9p$1=*) add "''${arg#*=}" + esac + done exec ${pkgs.socat}/bin/socat VSOCK-CONNECT:2:"$1" EXEC:"${pkgs.util-linux}/bin/mount -t 9p -o trans=fd -o rfdno=0 -o wfdno=1 -o version=9p2000.L -o msize=1048576$opts vsock9p $2",nofork ''; @@ -264,7 +274,7 @@ let boot_args = toString (nixos.config.boot.kernelParams ++ [ "init=${toplevel}/init" "fc.registration=${pkgs.closureInfo { rootPaths = [ toplevel ]; }}/registration" - "@TTY_ARGS@" + "@ARGS@" ]); }; network-interfaces = [{ @@ -294,12 +304,17 @@ let }); in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' - # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] [--nixpkgs DIR] + # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] + # [--nixpkgs DIR] [--nixpkgs-cache MODE] # 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. Also the same way, # nixpkgs at /nixpkgs, read-only: DIR, by default the nixpkgs the guest - # was built from. + # was built from. It isn't cached in the guest, so changes show up + # right away, but evaluating is slow. --nixpkgs-cache sets 9P's cache + # mode for it: loose caches everything (fast, but changes may not show + # up while the guest runs), mmap and readahead some (see the kernel's + # Documentation/filesystems/9p.rst), none is the default. # 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 @@ -308,16 +323,19 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' 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] [--nixpkgs DIR]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE]" } + args= rw_store= rw_store_size=16G nixpkgs=${lib.escapeShellArg (toString pkgs.path)} while test $# -gt 0; do case $1 in + --*=*) set -- "''${1%%=*}" "''${1#*=}" "''${@:2}"; continue ;; --store) rw_store=$2; shift 2 ;; --store-size) rw_store_size=$2; shift 2 ;; --nixpkgs) nixpkgs=$2; shift 2 ;; + --nixpkgs-cache) args="$args fc.9p566=cache=$2"; shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -346,10 +364,10 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # the type of /dev/console when starting (in the initrd and again after # switching root), and both type and size of ttyS0 when starting the getty. term=''${TERM:-dumb} - tty_args="systemd.tty.term.console=$term systemd.tty.term.ttyS0=$term" + args="$args systemd.tty.term.console=$term systemd.tty.term.ttyS0=$term" if size=$(stty size 2>/dev/null) && test "$size" != "0 0"; then read -r rows cols <<< "$size" - tty_args="$tty_args systemd.tty.rows.ttyS0=$rows systemd.tty.columns.ttyS0=$cols" + args="$args systemd.tty.rows.ttyS0=$rows systemd.tty.columns.ttyS0=$cols" fi # Take the first tap device (see modules/host) that no other VM holds. The @@ -392,7 +410,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' while ! test -S "$vsock"_565; do sleep 0.01; done sed \ - -e "s|@TTY_ARGS@|$tty_args|" \ + -e "s|@ARGS@|$args|" \ -e "s|@VSOCK@|$vsock|" \ -e "s|@TAP@|$tap|" \ -e "s|@MAC@|$mac|" \ -- cgit v1.3.1