diff options
Diffstat (limited to 'pkgs/run-firecracker-vm')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 47 |
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 '' // { |
