From 1bb6c184f4aff2451af0aba600a8ba08d7c947e5 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 20 Nov 2026 03:49:29 +0000 Subject: run-firecracker-vm: /home on a disk of its own, --home /home is now on its own disk: a scratch file, removed when the guest exits, or with --home FILE, one that persists (created if it doesn't exist, sparse, --home-size, default 16G), handled like the store's (--store), and also locked against use by two VMs. Separate from the store, so either can be kept or thrown away. The images move one drive up (vdc, vdd, ...). --- README.md | 7 +++- pkgs/run-firecracker-vm/default.nix | 64 ++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2b6446a..a54ae48 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,12 @@ removed when the guest exits. To keep it across boots: ``` The file gets created (sparse, default 16G) if it doesn't exist. Only one -VM at a time can use it. +VM at a time can use it. `/home` works the same way, on a disk of its +own (scratch unless given, so it can be kept apart from the store): + +```sh +./result/bin/run-firecracker-vm --home ~/fc-home.img [--home-size 50G] +``` `/nixpkgs` is the nixpkgs the guest was built from (``, checkout or not); `--nixpkgs DIR` serves another one. It's not cached in the diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index e2c29a8..942fea9 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -97,6 +97,14 @@ let noCheck = true; neededForBoot = true; }; + # /home: the second drive, a scratch file, or one that persists (see + # the run script). + fileSystems."/home" = { + device = "/dev/vdb"; + fsType = "ext4"; + options = [ "noatime" ]; + noCheck = true; + }; # nix's database goes there, too, so it matches what's in the store. fileSystems."/nix/var" = { device = "/nix/.rw-store/var"; @@ -259,11 +267,11 @@ let }; }; - # The images, read-only drives after the rw-store (vda): image k (of - # layer k) is vd(b+k). + # The images, read-only drives after the rw-store (vda) and /home (vdb): + # image k (of layer k) is vd(c+k). imageFileSystems = count: lib.listToAttrs (lib.genList (k: lib.nameValuePair "/nix/.image/${toString k}" { - device = "/dev/vd${lib.elemAt (lib.stringToCharacters "bcdefghijklmnopqrstuvwxyz") k}"; + device = "/dev/vd${lib.elemAt (lib.stringToCharacters "cdefghijklmnopqrstuvwxyz") k}"; fsType = "erofs"; options = [ "ro" "nodev" "nosuid" ]; neededForBoot = true; @@ -310,6 +318,11 @@ let path_on_host = "@RW_STORE@"; is_root_device = false; is_read_only = false; + } { + drive_id = "home"; + path_on_host = "@HOME@"; + is_root_device = false; + is_read_only = false; }] ++ lib.imap0 (k: image: { drive_id = "layer-${toString k}"; path_on_host = "${image}"; @@ -328,6 +341,7 @@ let in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # usage: run-firecracker-vm [--store FILE] [--store-size SIZE] + # [--home FILE] [--home-size SIZE] # [--nixpkgs DIR] [--nixpkgs-cache MODE] # [--egress LIST] [--name NAME] # The guest gets the host's store, read-only, via 9P over vsock, and the @@ -342,7 +356,8 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # 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). + # file is sparse, so that's only an upper limit). Likewise /home, and + # --home and --home-size. # The guest's egress goes by the user's whitelist LIST (default: the # VM's name, or without one, default), see modules/host and fc-egress. --name names the VM, for # fc-egress and as the guest's host name; only one running VM of the @@ -351,11 +366,13 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME]" } args= rw_store= rw_store_size=16G + home= + home_size=16G egress= name= nixpkgs=${lib.escapeShellArg (toString pkgs.path)} @@ -364,6 +381,8 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --*=*) set -- "''${1%%=*}" "''${1#*=}" "''${@:2}"; continue ;; --store) rw_store=$2; shift 2 ;; --store-size) rw_store_size=$2; shift 2 ;; + --home) home=$2; shift 2 ;; + --home-size) home_size=$2; shift 2 ;; --nixpkgs) nixpkgs=$2; shift 2 ;; --nixpkgs-cache) args="$args fc.9p566=cache=$2"; shift 2 ;; --egress) egress=$2; shift 2 ;; @@ -377,20 +396,26 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' 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 + # disk VAR FILE SIZE NAME: sets VAR to the absolute path of FILE, an ext4 + # image, created (sparse, of SIZE) if it doesn't exist, or without FILE, + # of a scratch one, removed on exit; locked while the guest runs, as two + # guests with the same file would wreck it. + disk() { + local f=''${2:-$tmp/$4.img} lock + if ! test -e "$f"; then + truncate -s "$3" "$f" + mkfs.ext4 -q -F "$f" + fi + f=$(realpath "$f") + exec {lock}<"$f" + if ! flock -n "$lock"; then + echo "$0: $f is in use by another VM" >&2 + exit 1 + fi + printf -v "$1" %s "$f" + } + disk rw_store "$rw_store" "$rw_store_size" rw-store + disk home "$home" "$home_size" home # 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 @@ -491,6 +516,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' -e "s|@TAP@|$tap|" \ -e "s|@MAC@|$mac|" \ -e "s|@RW_STORE@|$rw_store|" \ + -e "s|@HOME@|$home|" \ ${config} > "$tmp"/config.json status=0 firecracker --no-api --config-file "$tmp"/config.json || status=$? -- cgit v1.3.1