aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md11
-rw-r--r--pkgs/run-firecracker-vm/default.nix47
2 files changed, 52 insertions, 6 deletions
diff --git a/README.md b/README.md
index 10fceb1..8556192 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,7 @@
# firecracker
-NixOS guests in [Firecracker](https://firecracker-microvm.github.io/),
+NixOS guests in [Firecracker](https://firecracker-microvm.github.io/)
+(or Cloud Hypervisor, see below),
started from a NixOS host, reached by ssh, or with `layers/console.nix`,
dropping into a root shell on the serial console. Boots in about a
second and a quarter. Stable Nix, no flakes.
@@ -97,7 +98,8 @@ the shell there asks the terminal before each prompt (`resize`, which
works in tmux, too). A tmux attached on the console keeps the size it
started with: detach and attach again after resizing.
-Leave the guest with `reboot` (there, or via ssh, see below).
+Leave the guest with `reboot` (there, or via ssh, see below; in Cloud
+Hypervisor, `poweroff`).
The guest's store is read-only, and there's no nix, unless it has the
nix layer, preferably first, so its image is the same for all guests
@@ -168,6 +170,11 @@ faster, but changes may not show up while the guest runs). Other modes:
The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`,
1 to 32; `--mem-size MIB`).
+The same build also runs in [Cloud
+Hypervisor](https://www.cloudhypervisor.org/) instead of Firecracker,
+with `--vmm cloud-hypervisor`. It restarts the guest on `reboot`, so
+leave it with `poweroff` there.
+
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 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
'' // {