diff options
| author | tv <tv@krebsco.de> | 2026-11-21 17:40:55 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-21 17:40:55 +0000 |
| commit | 5089201bd78c82687377a50ef33878d35407d23b (patch) | |
| tree | 6088b06ab0587131d7f0394b0e945cc077d77b97 | |
| parent | 5b918e0b892a3c3eeddd866170eefe2cbd7e6ba3 (diff) | |
run-firecracker-vm: drives by label, not by vda, vdb, ...
The guest mounted its drives by their device names, which come in the
order the kernel probes the drives, not necessarily in their order in
the Firecracker configuration: booted with linuxPackages_latest, where
virtio_blk and virtio_mmio are modules, the base image and nix's came
swapped (vdb and vdc), which went unnoticed only because the store's
lower layers can be in any order. With /home or the rw-store swapped,
it wouldn't have been harmless.
Now the guest goes by filesystem labels: image k is fc-layer-K (mkfs.erofs
-L), the rw-store fc-store, and home fc-home. The run script labels
the ext4 images when it creates them, and existing ones, from before
labels, before the guest starts. This also drops the drive letters'
limit of 26 drives.
| -rw-r--r-- | layers/nix.nix | 2 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 46 |
2 files changed, 26 insertions, 22 deletions
diff --git a/layers/nix.nix b/layers/nix.nix index 8e00d13..67639be 100644 --- a/layers/nix.nix +++ b/layers/nix.nix @@ -27,7 +27,7 @@ in neededForBoot = true; }; fileSystems."/nix/.rw-store" = { - device = "/dev/vda"; # the first drive, see the run script + device = "/dev/disk/by-label/fc-store"; # see the run script fsType = "ext4"; options = [ "noatime" ]; noCheck = true; diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index 041bf60..19a6c9e 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -112,8 +112,8 @@ let imports = [ (modulesPath + "/profiles/minimal.nix") (modulesPath + "/profiles/perlless.nix") - ({ config, ... }: { fileSystems = imageFileSystems (firstImage config) (n + 1); }) - ({ config, ... }: { systemd.generators.fc-home = homeGenerator (drive (firstImage config + n + 1)); }) + { fileSystems = imageFileSystems (n + 1); } + { systemd.generators.fc-home = homeGenerator (label "fc-home"); } { # For a layer whose state is in /home: the run script then # refuses to start the guest without --home. @@ -201,7 +201,7 @@ let if lib.length lowerdirs > 1 then { overlay.lowerdir = lowerdirs; } else - (imageFileSystems (firstImage config) 1)."/nix/.image/0"; + (imageFileSystems 1)."/nix/.image/0"; fileSystems."/xchg" = { device = "564"; @@ -380,7 +380,7 @@ let }; # /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). + # line (--home, see the run script), the drive labelled fc-home (device). # A generator rather than fileSystems, so a guest without it doesn't wait # for the drive. homeGenerator = device: pkgs.writeShellScript "fc-home-generator" '' @@ -395,18 +395,18 @@ let ${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 guest's drives go by their filesystems' labels, not by vda, vdb, + # ...: those come in the order the kernel probes the drives, which + # needn't be theirs in the Firecracker configuration (with virtio_blk + # and virtio_mmio as modules, as in linuxPackages_latest, two images + # came swapped). The run script labels the rw-store and home. + label = l: "/dev/disk/by-label/${l}"; - # The drive of image 0: after the rw-store (vda), if there is one, i.e. - # with layers/nix.nix, else the first. - firstImage = config: if config.fc.nix then 1 else 0; - - # The images, read-only drives from drive first on: image k (of layer - # k) is drive first + k. - imageFileSystems = first: count: lib.listToAttrs (lib.genList (k: + # The images, read-only drives, image k (of layer k) labelled + # fc-layer-K. + imageFileSystems = count: lib.listToAttrs (lib.genList (k: lib.nameValuePair "/nix/.image/${toString k}" { - device = drive (first + k); + device = label "fc-layer-${toString k}"; fsType = "erofs"; options = [ "ro" "nodev" "nosuid" ]; neededForBoot = true; @@ -447,7 +447,7 @@ let 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 + mkfs.erofs --quiet -L fc-layer-${toString k} --tar=f --force-uid=0 --force-gid=0 --hard-dereference -T 0 $out '') toplevels; config = pkgs.writeText "firecracker.json" (builtins.toJSON { @@ -467,7 +467,7 @@ let host_dev_name = "@TAP@"; # see modules/host guest_mac = "@MAC@"; }]; - # In this order: vda, vdb, ... (see drive). The run script drops home + # The guest finds them by label (see label). The run script drops home # without --home. drives = lib.optional nix { drive_id = "rw-store"; @@ -642,14 +642,15 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' esac done - # disk VAR FILE SIZE: sets VAR to the absolute path of FILE, an ext4 + # disk VAR FILE SIZE LABEL: sets VAR to the absolute path of FILE, an ext4 # image, created (sparse, of SIZE) if it doesn't exist; locked while the - # guest runs, as two guests with the same file would wreck it. + # guest runs, as two guests with the same file would wreck it. The guest + # finds it by LABEL, which gets set on images from before labels, too. disk() { local f=$2 lock if ! test -e "$f"; then truncate -s "$3" "$f" - mkfs.ext4 -q -F "$f" + mkfs.ext4 -q -F -L "$4" "$f" fi f=$(realpath "$f") exec {lock}<"$f" @@ -657,13 +658,16 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' echo "$0: $f is in use by another VM" >&2 exit 1 fi + if test "$(e2label "$f")" != "$4"; then + e2label "$f" "$4" + fi printf -v "$1" %s "$f" } if test -n "$rw_store"; then - disk rw_store "$rw_store" "$rw_store_size" + disk rw_store "$rw_store" "$rw_store_size" fc-store fi if test -n "$home"; then - disk home "$home" "$home_size" + disk home "$home" "$home_size" fc-home args="$args fc.home" fi |
