diff options
| author | tv <tv@krebsco.de> | 2026-11-25 14:28:29 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 15:36:16 +0100 |
| commit | db8771df6809be57362477395bc4450b32dd3e74 (patch) | |
| tree | fc5e655df1c6ba3c50a9a6ddb2414733504b0530 | |
| parent | e199202421b24be09aa2b3b654c8f3f82afed52d (diff) | |
run-firecracker-vm: --nested, hardware virtualization for the guest
Cloud Hypervisor offers its guests hardware virtualization by default
(nested=on, its CPUs show svm), and Firecracker passes on whatever KVM
offers (it's "not tested with nested virtualization", but has no
switch), so whether a guest got it depended on the VMM, and the host's
kvm settings, not on anyone's choice. Now it's off unless asked for
with --nested: Cloud Hypervisor gets nested=off or on, Firecracker a
custom CPU template (cpu-config) clearing SVM (CPUID 0x80000001 ECX[2])
and VMX (CPUID 1 ECX[5]), dropped with --nested. With it, and KVM in
the guest's kernel (base/kernel.nix), the guest gets /dev/kvm, to run
VMs itself, like NixOS VM tests. Off by default, as it opens more of
the host's KVM to the guest.
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 25 |
2 files changed, 25 insertions, 5 deletions
@@ -178,6 +178,11 @@ The same build also runs in [Cloud Hypervisor](https://www.cloudhypervisor.org/) instead of Firecracker, with `--vmm cloud-hypervisor`. +With `--nested`, the guest gets hardware virtualization (SVM, VMX), and +with it `/dev/kvm` (with the cut-down kernel, `base/kernel.nix`, or any +with KVM), to run VMs itself, like NixOS VM tests. It's off by default, +as it opens more of the host's KVM to the guest. + The guests run sshd. Its host key is a secret (see below), so it's not in the store, and the run script requires it: `--secret ssh_host_ed25519_key=FILE` (an ed25519 private key without passphrase). diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index e225451..8a58c60 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -536,6 +536,15 @@ let vcpu_count = 2; # --vcpus mem_size_mib = 1024; # --mem-size }; + # No hardware virtualization for the guest (SVM, VMX), unless + # --nested (which drops this): Firecracker passes on what KVM offers. + # The bitmaps go from bit 31 to 0, x keeps a bit. + cpu-config.cpuid_modifiers = [ + { leaf = "0x80000001"; subleaf = "0x0"; flags = 0; # AMD: ECX[2] SVM + modifiers = [ { register = "ecx"; bitmap = "0bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0xx"; } ]; } + { leaf = "0x1"; subleaf = "0x0"; flags = 0; # Intel: ECX[5] VMX + modifiers = [ { register = "ecx"; bitmap = "0bxxxxxxxxxxxxxxxxxxxxxxxxxx0xxxxx"; } ]; } + ]; }); in pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' @@ -545,7 +554,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] + # [--vmm firecracker|cloud-hypervisor] [--nested] # 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). @@ -577,11 +586,14 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # --vmm: the hypervisor, Firecracker (default) or Cloud Hypervisor, which # takes no commas in --store and --home. Either ends when the guest # reboots or powers off (see fc-vmm-exit). + # --nested: hardware virtualization (SVM, VMX) for the guest, so it can + # run VMs itself (base/kernel.nix has KVM); off by default, as it's more + # of the host's KVM for the guest to reach. 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.cloud-hypervisor pkgs.git pkgs.socat 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]... [--vmm firecracker|cloud-hypervisor]" + 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] [--nested]" } args= rw_store= @@ -593,6 +605,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' name= vcpus=2 vmm=firecracker + nested= nixpkgs=${lib.escapeShellArg (toString pkgs.path)} nixpkgs_cache= nix_opt= # the last option that's only for a guest with nix @@ -613,6 +626,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --vcpus) vcpus=$2; shift 2 ;; --secret) secrets+=("$2"); shift 2 ;; --vmm) vmm=$2; shift 2 ;; + --nested) nested=on; shift ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -909,10 +923,11 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' -e "s|@RW_STORE@|$rw_store|" \ -e "s|@HOME@|$home|" \ ${config} | - jq --argjson vcpus "$vcpus" --argjson mem_size "$mem_size" --arg home "$home" ' + jq --argjson vcpus "$vcpus" --argjson mem_size "$mem_size" --arg home "$home" --arg nested "$nested" ' ."machine-config".vcpu_count = $vcpus | ."machine-config".mem_size_mib = $mem_size | - if $home == "" then del(.drives[] | select(.drive_id == "home")) end + if $home == "" then del(.drives[] | select(.drive_id == "home")) end | + if $nested != "" then del(."cpu-config") end ' > "$tmp"/config.json status=0 case $vmm in @@ -931,7 +946,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --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 \ + --cpus boot="$vcpus",nested="''${nested:-off}" --memory size="$mem_size"M \ --disk "''${disks[@]}" \ --net tap="$tap",mac="$mac" \ --vsock cid=3,socket="$vsock" \ |
