diff options
| author | tv <tv@krebsco.de> | 2026-11-20 04:12:43 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 04:12:43 +0000 |
| commit | 2d3fabb736e681d1b3f5f91e2d7897ea35aa4c58 (patch) | |
| tree | 6654e360d82b496fc3468823bb37c33200d9999f /pkgs | |
| parent | 1bb6c184f4aff2451af0aba600a8ba08d7c947e5 (diff) | |
run-firecracker-vm: no /home disk without --home
Without --home, /home is now simply part of the guest's root file
system (a tmpfs, gone when it exits, as before), instead of a scratch
disk. With --home, the disk is attached after the images, and the run
script puts fc.home on the kernel command line, which makes a small
generator in the guest mount it, so a guest without it doesn't wait for
a drive that isn't there. The images are back at vdb, vdc, ...
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 67 |
1 files changed, 43 insertions, 24 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 942fea9..2705d1f 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -51,6 +51,7 @@ let (modulesPath + "/profiles/minimal.nix") (modulesPath + "/profiles/perlless.nix") { fileSystems = imageFileSystems (n + 1); } + { systemd.generators.fc-home = homeGenerator (drive (n + 2)); } ] ++ lib.take n layers; # Activate and switch root from the initrd with a single program, @@ -91,20 +92,12 @@ let neededForBoot = true; }; fileSystems."/nix/.rw-store" = { - device = "/dev/vda"; + device = drive 0; fsType = "ext4"; options = [ "noatime" ]; 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"; @@ -267,11 +260,30 @@ let }; }; - # The images, read-only drives after the rw-store (vda) and /home (vdb): - # image k (of layer k) is vd(c+k). + # /home: part of / (the tmpfs), or with fc.home on the kernel command + # line (--home, see the run script), the drive after the images (device). + # A generator rather than fileSystems, so a guest without it doesn't wait + # for the drive. + homeGenerator = device: pkgs.writeShellScript "fc-home-generator" '' + read -r cmdline < /proc/cmdline + case " $cmdline " in *" fc.home "*) ;; *) exit 0 + esac + printf '%s\n' \ + '[Unit]' 'Before=local-fs.target' \ + '[Mount]' 'What=${device}' 'Where=/home' 'Type=ext4' 'Options=noatime' \ + > "$1"/home.mount + ${pkgs.coreutils}/bin/mkdir -p "$1"/local-fs.target.requires + ${pkgs.coreutils}/bin/ln -s ../home.mount "$1"/local-fs.target.requires/home.mount + ''; + + # The guest's device of drive k (0: vda), see drives below. + drive = k: "/dev/vd${lib.elemAt (lib.stringToCharacters "abcdefghijklmnopqrstuvwxyz") k}"; + + # The images, read-only drives after the rw-store (vda): image k (of + # layer k) is drive k + 1. imageFileSystems = count: lib.listToAttrs (lib.genList (k: lib.nameValuePair "/nix/.image/${toString k}" { - device = "/dev/vd${lib.elemAt (lib.stringToCharacters "cdefghijklmnopqrstuvwxyz") k}"; + device = drive (k + 1); fsType = "erofs"; options = [ "ro" "nodev" "nosuid" ]; neededForBoot = true; @@ -312,23 +324,24 @@ let host_dev_name = "@TAP@"; # see modules/host guest_mac = "@MAC@"; }]; - # In this order: vda, vdb, ... (see imageFileSystems). + # In this order: vda, vdb, ... (see drive). The run script drops home + # without --home. drives = [{ drive_id = "rw-store"; 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}"; is_root_device = false; is_read_only = true; - }) images; + }) images ++ [{ + drive_id = "home"; + path_on_host = "@HOME@"; + is_root_device = false; + is_read_only = false; + }]; vsock = { guest_cid = 3; uds_path = "@VSOCK@"; @@ -356,14 +369,15 @@ 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). Likewise /home, and - # --home and --home-size. + # file is sparse, so that's only an upper limit). /home is part of the + # guest's root file system, gone when it exits, or with --home, on FILE, + # likewise (of --home-size, default 16G). # 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 # user's can have a given name. set -efu - export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} + export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME]" @@ -415,7 +429,10 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' printf -v "$1" %s "$f" } disk rw_store "$rw_store" "$rw_store_size" rw-store - disk home "$home" "$home_size" home + if test -n "$home"; then + disk home "$home" "$home_size" home + args="$args fc.home" + 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 @@ -517,7 +534,9 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' -e "s|@MAC@|$mac|" \ -e "s|@RW_STORE@|$rw_store|" \ -e "s|@HOME@|$home|" \ - ${config} > "$tmp"/config.json + ${config} | + if test -n "$home"; then cat; else jq 'del(.drives[] | select(.drive_id == "home"))'; fi \ + > "$tmp"/config.json status=0 firecracker --no-api --config-file "$tmp"/config.json || status=$? |
