aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-23 19:44:15 +0000
committertv <tv@krebsco.de>2026-11-25 12:22:23 +0000
commit312605171f6e3ef59a0f7c0cffc69e98c346a60c (patch)
tree0128bddfcd0500f9a553fa68452415df75203ba9 /pkgs
parent18b1f50456a1105c6eecb8bf3c7a42ebbe9dd4cd (diff)
run-firecracker-vm: --vmm cloud-hypervisor
Firecracker was the only hypervisor, with its limit of about 19 devices (virtio-mmio, an interrupt each), no hot-plugging, and no virtio-fs. Now the run script can also start the same build in Cloud Hypervisor (--vmm cloud-hypervisor; Firecracker stays the default), translating the Firecracker configuration it makes anyway into Cloud Hypervisor's options: kernel (the same vmlinux, which it boots via PVH), initrd and command line, CPUs and memory, the drives (read-only for the images), the tap and MAC, vsock. The drives are image_type=raw: without it, Cloud Hypervisor guesses, and then refuses writes to sector 0, which wrecks ext4 (the rw-store, home). Its vsock works like Firecracker's (a guest's connection to port PORT goes to the socket SOCKET_PORT), so the 9P servers stay as they are, and the guest finds its drives by label, so their order on PCI doesn't matter. Unlike Firecracker, it restarts the guest on reboot (a reset), so the way out there is poweroff. Its options are KEY=VALUE,... lists, so it takes no commas in --store and --home.
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/run-firecracker-vm/default.nix47
1 files changed, 43 insertions, 4 deletions
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix
index 02ffc8d..11dfa46 100644
--- a/pkgs/run-firecracker-vm/default.nix
+++ b/pkgs/run-firecracker-vm/default.nix
@@ -517,6 +517,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# [--nixpkgs DIR] [--nixpkgs-cache MODE]
# [--egress LIST] [--name NAME] [--vcpus N]
# [--secret NAME=FILE]...
+ # [--vmm firecracker|cloud-hypervisor]
# The guest gets the directory xchg (in the current working directory)
# at /xchg, via 9P over vsock. VMs started in the same directory share
# it. Its store is read-only, unless it has nix (layers/nix.nix).
@@ -545,11 +546,14 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# '_' and '-', FILE read once (it may be a pipe, like <(...)), and handed
# to the guest only, never in the store (see fc-secrets); a guest
# doesn't start without the secrets it declares (fc.secrets).
+ # --vmm: the hypervisor, Firecracker (default) or Cloud Hypervisor, which
+ # restarts the guest on reboot (leave it with poweroff instead), and
+ # takes no commas in --store and --home.
set -efu
- export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.unpfs ]}
+ export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.cloud-hypervisor pkgs.unpfs ]}
usage() {
- echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]..."
+ echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--secret NAME=FILE]... [--vmm firecracker|cloud-hypervisor]"
}
args=
rw_store=
@@ -560,6 +564,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
egress=
name=
vcpus=2
+ vmm=firecracker
nixpkgs=${lib.escapeShellArg (toString pkgs.path)}
nix_opt= # the last option that's only for a guest with nix
secrets=() # NAME=FILE, as given
@@ -577,6 +582,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
--name) name=$2; shift 2 ;;
--vcpus) vcpus=$2; shift 2 ;;
--secret) secrets+=("$2"); shift 2 ;;
+ --vmm) vmm=$2; shift 2 ;;
-h|--help) usage; exit ;;
*) usage >&2; exit 1
esac
@@ -607,6 +613,18 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
echo "$0: bad number of CPUs: $vcpus (1 to 32)" >&2
exit 1
fi
+ case $vmm in
+ firecracker) ;;
+ cloud-hypervisor)
+ # Its options are KEY=VALUE,... lists.
+ case $rw_store$home in *,*)
+ echo "$0: --vmm cloud-hypervisor: no commas in --store and --home" >&2
+ exit 1
+ esac ;;
+ *)
+ echo "$0: bad --vmm: $vmm (firecracker or cloud-hypervisor)" >&2
+ exit 1
+ esac
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
@@ -793,7 +811,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# and nixpkgs, one each per VM. They run as the user running this
# script: xchg's files end up owned by the user, and the store is
# read-only to them. When the guest connects to vsock port PORT on the
- # host (CID 2), Firecracker connects to the socket uds_path_PORT. unpfs
+ # host (CID 2), the VMM connects to the socket uds_path_PORT. unpfs
# listens on ADDR:PORT (from unix!ADDR!PORT), hence the colon at the end
# of uds_path. RUST_LOG=off silences its logging of failed requests
# (like walking to a file that doesn't exist), fatal errors still go to
@@ -833,7 +851,28 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
if $home == "" then del(.drives[] | select(.drive_id == "home")) end
' > "$tmp"/config.json
status=0
- firecracker --no-api --config-file "$tmp"/config.json || status=$?
+ case $vmm in
+ firecracker)
+ firecracker --no-api --config-file "$tmp"/config.json || status=$? ;;
+ cloud-hypervisor)
+ # The same configuration, as Cloud Hypervisor's options. Its vsock
+ # works like Firecracker's: the guest's connections to port PORT go
+ # to the socket SOCKET_PORT.
+ get() { jq -r "$1" "$tmp"/config.json; }
+ # image_type=raw: without it, Cloud Hypervisor guesses the type, and
+ # then refuses writes to sector 0, i.e. ext4's superblock (the
+ # rw-store, home). The images, read-only, can't be sparse.
+ mapfile -t disks < <(get '.drives[] | "path=\(.path_on_host),image_type=raw" + if .is_read_only then ",readonly=on,sparse=off" else "" end')
+ cloud-hypervisor \
+ --kernel "$(get '."boot-source".kernel_image_path')" \
+ --initramfs "$(get '."boot-source".initrd_path')" \
+ --cmdline "$(get '."boot-source".boot_args')" \
+ --cpus boot="$vcpus" --memory size="$mem_size"M \
+ --disk "''${disks[@]}" \
+ --net tap="$tap",mac="$mac" \
+ --vsock cid=3,socket="$vsock" \
+ --serial tty --console off || status=$? ;;
+ esac
exit $status
'' // {