diff options
| author | tv <tv@krebsco.de> | 2026-11-20 02:49:39 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-20 02:49:39 +0000 |
| commit | 1d8327ebfa7bc859ecafb0629c93e756b0b10357 (patch) | |
| tree | be787131011dca4e338ef612809f127444fba612 | |
| parent | e6b6fbb9c48093fc460dd90e88a797f79c6c6ee6 (diff) | |
run-firecracker-vm: store layers
The new argument layers is a list of NixOS modules, each a layer on top
of the ones before, the first on top of the base system. The guest runs
the top layer's system. Each layer gets its own erofs image with only
the store paths of its system's closure that the layers below don't
have, so the base image (and those of layers below) stays the same
whatever goes on top:
nix-build pkgs/run-firecracker-vm --arg layers '[ ./my-service.nix ]'
The images become read-only drives, mounted as overlay lower layers
(with the host store below them, as before). The writable store disk
is now the first drive (/dev/vda), the images follow (vdb, vdc, ...).
| -rw-r--r-- | README.md | 18 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 83 |
2 files changed, 69 insertions, 32 deletions
@@ -6,8 +6,9 @@ console. Boots in about a second and a quarter. Stable Nix, no flakes. The guest gets: -- the host's Nix store, read-only, with its own closure from a local image - for speed, and a writable layer on top, so nix and `nix-shell` work; +- the host's Nix store, read-only, with its own closure from local images + for speed (one per layer, see below), and a writable layer on top, so + nix and `nix-shell` work; - `/xchg`: the directory `xchg` in the current working directory, shared with the host (and with other VMs started in the same directory); - `/nixpkgs`: the host's nixpkgs, read-only, on the guest's `NIX_PATH`; @@ -73,6 +74,19 @@ guest, so changes to it show up right away, but evaluating is slow; not show up while the guest runs). Other modes: `mmap`, `readahead`, `none` (the default). +The guest's system can be built in layers, each a NixOS module on top of +the ones before (the first on top of the base system): + +```sh +nix-build pkgs/run-firecracker-vm --arg layers '[ ./base-tools.nix ./my-service.nix ]' +``` + +The guest runs the top layer's system. Each layer gets its own image, +with only the store paths the layers below don't have, so the base +image, and those of layers below, stay the same whatever goes on top: +a service layer costs a small image of its own. Changing a layer +changes the images of the layers above it. + 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 f77520e..e2c29a8 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -2,6 +2,12 @@ , kernelPackages ? pkgs.linuxPackages_latest # The CA of the host's egress proxy (see modules/host), if it's there. , caCert ? "/var/lib/fcbr0-egress/ca.pem" + # NixOS modules, each a layer on top of the ones before, the first on + # top of the base system (below). The guest runs the top layer's + # system; its store has an image per layer, each with only the store + # paths the layers below don't have, so the images below stay the same + # for any layers on top of them. +, layers ? [ ] }: let inherit (pkgs) lib; @@ -36,13 +42,16 @@ let 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 ''; - nixos = import (pkgs.path + "/nixos") { + # The system of the first n layers (the base system being layer 0), + # with n + 1 images. + evalSystem = n: import (pkgs.path + "/nixos") { inherit (pkgs.stdenv.hostPlatform) system; configuration = { config, modulesPath, ... }: { imports = [ (modulesPath + "/profiles/minimal.nix") (modulesPath + "/profiles/perlless.nix") - ]; + { fileSystems = imageFileSystems (n + 1); } + ] ++ lib.take n layers; # Activate and switch root from the initrd with a single program, # instead of bash running prepare-root and the activation script. @@ -65,19 +74,15 @@ let fsType = "tmpfs"; options = [ "mode=755" ]; }; - # The store, in layers: the guest's own closure from an image (see - # 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 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"; - options = [ "ro" "nodev" "nosuid" ]; - neededForBoot = true; - }; + # The store, in layers: the guest's own closure from images (see + # imageFileSystems and images below), as reading them 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 the first 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. Store paths don't collide, so the order of the + # lower layers doesn't matter. fileSystems."/nix/.host-store" = { device = "565"; fsType = "vsock9p"; @@ -86,7 +91,7 @@ let neededForBoot = true; }; fileSystems."/nix/.rw-store" = { - device = "/dev/vdb"; + device = "/dev/vda"; fsType = "ext4"; options = [ "noatime" ]; noCheck = true; @@ -108,7 +113,7 @@ let serviceConfig.ExecStart = "${pkgs.coreutils}/bin/mkdir -p -m 0755 /nix/.rw-store/var"; }; fileSystems."/nix/store".overlay = { - lowerdir = [ "/nix/.image" "/nix/.host-store" ]; + lowerdir = lib.genList (k: "/nix/.image/${toString k}") (n + 1) ++ [ "/nix/.host-store" ]; upperdir = "/nix/.rw-store/store"; workdir = "/nix/.rw-store/work"; }; @@ -254,18 +259,35 @@ let }; }; + # The images, read-only drives after the rw-store (vda): image k (of + # layer k) is vd(b+k). + imageFileSystems = count: lib.listToAttrs (lib.genList (k: + lib.nameValuePair "/nix/.image/${toString k}" { + device = "/dev/vd${lib.elemAt (lib.stringToCharacters "bcdefghijklmnopqrstuvwxyz") k}"; + fsType = "erofs"; + options = [ "ro" "nodev" "nosuid" ]; + neededForBoot = true; + }) count); + + systems = lib.genList evalSystem (lib.length layers + 1); + nixos = lib.last systems; + toplevels = map (s: s.config.system.build.toplevel) systems; inherit (nixos.config.system.build) toplevel kernel initialRamdisk; - # The guest's closure, as erofs image. - storeImage = pkgs.runCommand "firecracker-store.img" { + # Layer k's image: the store paths of its system's closure that aren't + # in the closures of the layers below, as erofs image. + images = lib.imap0 (k: top: pkgs.runCommand "firecracker-layer-${toString k}.img" { nativeBuildInputs = [ pkgs.erofs-utils pkgs.gnutar ]; - closure = pkgs.closureInfo { rootPaths = [ toplevel ]; }; + closure = pkgs.closureInfo { rootPaths = [ top ]; }; + below = map (t: pkgs.closureInfo { rootPaths = [ t ]; }) (lib.take k toplevels); } '' - tar -c -P --verbatim-files-from \ - --transform 'flags=rSh;s|^/nix/store/||' \ - --files-from $closure/store-paths | + for b in $below; do cat $b/store-paths; done | sort -u > below + sort $closure/store-paths | comm -23 - below | + tar -c -P --verbatim-files-from \ + --transform 'flags=rSh;s|^/nix/store/||' \ + --files-from - | mkfs.erofs --quiet --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 $out - ''; + '') toplevels; config = pkgs.writeText "firecracker.json" (builtins.toJSON { boot-source = { @@ -282,17 +304,18 @@ let host_dev_name = "@TAP@"; # see modules/host guest_mac = "@MAC@"; }]; + # In this order: vda, vdb, ... (see imageFileSystems). drives = [{ - drive_id = "store"; - 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; - }]; + }] ++ lib.imap0 (k: image: { + drive_id = "layer-${toString k}"; + path_on_host = "${image}"; + is_root_device = false; + is_read_only = true; + }) images; vsock = { guest_cid = 3; uds_path = "@VSOCK@"; |
