aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--pkgs/run-firecracker-vm/default.nix29
2 files changed, 22 insertions, 10 deletions
diff --git a/README.md b/README.md
index 0c47dff..8aa03aa 100644
--- a/README.md
+++ b/README.md
@@ -84,6 +84,9 @@ guest, so changes to it show up right away, but evaluating is slow;
not show up while the guest runs). Other modes: `mmap`, `readahead`,
`none` (the default).
+The guest gets 2 CPUs and 1024 MiB unless told otherwise (`--vcpus N`,
+1 to 32; `--mem-size MIB`).
+
The guest's system can be built in layers, each a NixOS module on top of
the ones before (the first on top of the base system):
diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix
index 6df6222..2f20528 100644
--- a/pkgs/run-firecracker-vm/default.nix
+++ b/pkgs/run-firecracker-vm/default.nix
@@ -397,8 +397,8 @@ let
uds_path = "@VSOCK@";
};
machine-config = {
- vcpu_count = 2;
- mem_size_mib = "@MEM_SIZE@"; # a number, once substituted
+ vcpu_count = 2; # --vcpus
+ mem_size_mib = 1024; # --mem-size
};
});
in
@@ -407,7 +407,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# [--home FILE] [--home-size SIZE]
# [--mem-size MIB]
# [--nixpkgs DIR] [--nixpkgs-cache MODE]
- # [--egress LIST] [--name NAME]
+ # [--egress LIST] [--name NAME] [--vcpus N]
# The guest gets the host's store, read-only, via 9P over vsock, and the
# directory xchg (in the current working directory) at /xchg, the same
# way. VMs started in the same directory share it. Also the same way,
@@ -425,14 +425,15 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
# likewise (of --home-size, default 16G).
# --mem-size sets the guest's memory, in MiB (default 1024).
# The guest's egress goes by the user's whitelist LIST (default: the
- # VM's name, or without one, default), see modules/host and fc-egress. --name names the VM, for
- # fc-egress and as the guest's host name; only one running VM of the
- # user's can have a given name.
+ # VM's name, or without one, default), see modules/host and fc-egress.
+ # --name names the VM, for fc-egress and as the guest's host name; only
+ # one running VM of the user's can have a given name. --vcpus: the
+ # guest's number of CPUs (default 2; Firecracker takes 1 to 32).
set -efu
export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.util-linux pkgs.firecracker 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]"
+ 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]"
}
args=
rw_store=
@@ -442,6 +443,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
mem_size=1024
egress=
name=
+ vcpus=2
nixpkgs=${lib.escapeShellArg (toString pkgs.path)}
while test $# -gt 0; do
case $1 in
@@ -455,6 +457,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
--nixpkgs-cache) args="$args fc.9p566=cache=$2"; shift 2 ;;
--egress) egress=$2; shift 2 ;;
--name) name=$2; shift 2 ;;
+ --vcpus) vcpus=$2; shift 2 ;;
-h|--help) usage; exit ;;
*) usage >&2; exit 1
esac
@@ -464,6 +467,10 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
echo "$0: bad memory size: $mem_size (MiB, a number)" >&2
exit 1
fi
+ if ! printf %s "$vcpus" | grep -qxE '[1-9][0-9]?' || test "$vcpus" -gt 32; then
+ echo "$0: bad number of CPUs: $vcpus (1 to 32)" >&2
+ exit 1
+ fi
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
@@ -592,10 +599,12 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ ''
-e "s|@MAC@|$mac|" \
-e "s|@RW_STORE@|$rw_store|" \
-e "s|@HOME@|$home|" \
- -e "s|\"@MEM_SIZE@\"|$mem_size|" \
${config} |
- if test -n "$home"; then cat; else jq 'del(.drives[] | select(.drive_id == "home"))'; fi \
- > "$tmp"/config.json
+ jq --argjson vcpus "$vcpus" --argjson mem_size "$mem_size" --arg home "$home" '
+ ."machine-config".vcpu_count = $vcpus |
+ ."machine-config".mem_size_mib = $mem_size |
+ if $home == "" then del(.drives[] | select(.drive_id == "home")) end
+ ' > "$tmp"/config.json
status=0
firecracker --no-api --config-file "$tmp"/config.json || status=$?