aboutsummaryrefslogtreecommitdiffstats
path: root/tests/qemu
diff options
context:
space:
mode:
Diffstat (limited to 'tests/qemu')
-rw-r--r--tests/qemu/debug.nix18
-rw-r--r--tests/qemu/default.nix25
-rw-r--r--tests/qemu/firecracker.sh27
3 files changed, 70 insertions, 0 deletions
diff --git a/tests/qemu/debug.nix b/tests/qemu/debug.nix
new file mode 100644
index 0000000..a8790de
--- /dev/null
+++ b/tests/qemu/debug.nix
@@ -0,0 +1,18 @@
+# A layer for testing in qemu (see default.nix): a root shell script run
+# at the end of boot, whose output goes to the console (the test's
+# stdout). (virtio_pci, which qemu needs, is in the base system.)
+script: { pkgs, config, lib, ... }: {
+ systemd.services.fc-test = {
+ wantedBy = [ "multi-user.target" ];
+ after = [ "multi-user.target" ];
+ path = [ pkgs.util-linux pkgs.coreutils "/run/current-system/sw" ];
+ environment = lib.mkIf config.fc.nix {
+ NIX_REMOTE = "daemon";
+ NIX_PATH = "nixpkgs=/nixpkgs";
+ };
+ serviceConfig.StandardOutput = "tty";
+ serviceConfig.StandardError = "tty";
+ serviceConfig.TTYPath = "/dev/console";
+ inherit script;
+ };
+}
diff --git a/tests/qemu/default.nix b/tests/qemu/default.nix
new file mode 100644
index 0000000..82c7de6
--- /dev/null
+++ b/tests/qemu/default.nix
@@ -0,0 +1,25 @@
+# The run script with a qemu stand-in for Firecracker (firecracker.sh),
+# for testing without Firecracker (fast with /dev/kvm, else under TCG).
+#
+# nix-build tests/qemu --argstr script 'echo TEST $(nproc); systemctl poweroff'
+# ./result/bin/run-firecracker-vm --secret ssh_host_ed25519_key=KEY [OPTION...] | tee log
+#
+# script: a shell script run as root at the end of boot (see debug.nix);
+# end it with `systemctl poweroff` for the guest to exit, or leave it out
+# to log in via ssh (127.0.0.1:2222; authorize a key with another layer).
+# layers: further layers, below the test's (e.g. [ ../../layers/nix.nix ],
+# which needs --store).
+# The run script still takes a tap, so this needs modules/host on the
+# machine it runs on, and a free tap of the user's; qemu doesn't use it.
+{ pkgs ? import <nixpkgs> {}, layers ? [ ], script ? "", ... }@args:
+let
+ firecracker = pkgs.writeShellApplication {
+ name = "firecracker";
+ runtimeInputs = [ pkgs.jq pkgs.gnused pkgs.qemu_kvm pkgs.vhost-device-vsock ];
+ text = builtins.readFile ./firecracker.sh;
+ };
+in
+import ../../pkgs/run-firecracker-vm (builtins.removeAttrs args [ "script" "layers" ] // {
+ pkgs = pkgs.extend (_: _: { inherit firecracker; });
+ layers = layers ++ [ (import ./debug.nix script) ];
+})
diff --git a/tests/qemu/firecracker.sh b/tests/qemu/firecracker.sh
new file mode 100644
index 0000000..08f766d
--- /dev/null
+++ b/tests/qemu/firecracker.sh
@@ -0,0 +1,27 @@
+# A stand-in for firecracker --no-api --config-file FILE, for testing the
+# run script where there's no Firecracker: boots the same kernel, initrd,
+# drives, vsock (vhost-device-vsock does the same uds_path_PORT
+# convention) and network interface (qemu's user networking; guest port 22
+# forwarded to 127.0.0.1:${FC_TEST_SSH_PORT:-2222}) in qemu, with
+# virtio-pci instead of virtio-mmio (see debug.nix).
+set -efu
+config=$3
+get() { jq -r "$1" "$config"; }
+uds=$(get .vsock.uds_path)
+vhost=$uds.vhost
+vhost-device-vsock --guest-cid 3 --socket "$vhost" --uds-path "$uds" > "$uds.vhost.log" 2>&1 &
+vhost_pid=$!
+trap 'kill $vhost_pid 2> /dev/null || :' EXIT
+while ! test -S "$vhost"; do sleep 0.1; done
+mem=$(get '."machine-config".mem_size_mib')
+args=$(get '."boot-source".boot_args')
+# shellcheck disable=SC2046
+qemu-system-x86_64 -machine accel=kvm:tcg -nographic -no-reboot \
+ -m "$mem" -smp "$(get '."machine-config".vcpu_count')" \
+ -object memory-backend-memfd,id=mem,size="$mem"M,share=on -numa node,memdev=mem \
+ -kernel "$(printf %s "$args" | sed -n 's|.*init=\([^ ]*\)/init.*|\1|p')/kernel" \
+ -initrd "$(get '."boot-source".initrd_path')" -append "$args" \
+ -chardev socket,id=vs,path="$vhost" -device vhost-user-vsock-pci,chardev=vs \
+ $(jq -r '.drives[] | "-drive file=\(.path_on_host),if=virtio,format=raw" + (if .is_read_only then ",readonly=on" else "" end)' "$config") \
+ -netdev user,id=n,hostfwd=tcp:127.0.0.1:"${FC_TEST_SSH_PORT:-2222}"-:22 \
+ -device virtio-net-pci,netdev=n,mac="$(get '."network-interfaces"[0].guest_mac')"