Skip to content

eBPF-based security platform

lima: diagnose nested KVM stalls and make GICv3 traps opt-in

Andrea Lattuada52f00944dc75
diff --git a/Documentation/start/quickstart-macos.rst b/Documentation/start/quickstart-macos.rst
index 47b78a8f..6e4306ba 100644
--- a/Documentation/start/quickstart-macos.rst
+++ b/Documentation/start/quickstart-macos.rst
@@ -50,7 +50,7 @@ Create the VM
.. code-block:: console
- $ limactl start --name=shirudo-dev --param checkout="$PWD" infra/lima/shirudo.yaml
+ $ limactl start --progress --name=shirudo-dev --param checkout="$PWD" infra/lima/shirudo.yaml
.. raw:: html
@@ -62,7 +62,7 @@ Create the VM
.. code-block:: console
- $ limactl start --name=shirudo-dev --param checkout="$PWD" \
+ $ limactl start --progress --name=shirudo-dev --param checkout="$PWD" \
--mount="$(git rev-parse --path-format=absolute --git-common-dir)" \
infra/lima/shirudo.yaml
@@ -122,6 +122,61 @@ Troubleshooting
The Mac does not offer nested virtualization: it is older than an M3
or runs a macOS before 15.
+A nested guest stalls during boot
+ On the first start, provisioning tries one nested KVM appliance boot
+ with a 150-second deadline, including kernel selection. It reports any
+ failure and lets setup continue. The console is saved inside the VM at
+ ``~/test-vm/kvm-appliance-check.log``. A new VM created with the GICv3
+ traps already requested skips this check because the traps take effect
+ after a restart.
+
+ ``make -C testing vm`` can stall while libguestfs boots its appliance to
+ prepare the runtime image, or later while the test target boots. To
+ check the appliance on its own, stop the stalled command and run this
+ in the Lima shell from the checkout:
+
+ .. code-block:: console
+
+ operator$ testing/vm/build-bundle.sh --check-kvm-appliance
+
+ The check uses the bundle's appliance kernel selection and boots the
+ appliance with KVM required. It prints the console and allows 150
+ seconds for kernel selection, appliance preparation, and readiness.
+ Exit status 124 means the deadline expired. If the command
+ reports an error, use its diagnostics. After the appliance passes,
+ ``make -C testing vm`` still has to reach ``VM ready`` separately.
+ ``TEST_VM_TIMEOUT=180 make -C testing vm`` sets the target readiness
+ deadline in seconds. On a target failure, inspect ``console.log`` and
+ ``qemu.log`` in the preserved VM state directory.
+
+ If the console shows a nested guest starts booting and then stops
+ making progress, try the GICv3 traps below. A timeout can also come
+ from slow preparation or other boot problems.
+
+ On some Macs, a nested guest can stall when it accesses GICv3
+ interrupt-controller registers directly. The traps make KVM in the
+ outer Lima VM intercept and emulate those accesses. This can affect
+ both the libguestfs appliance and the test target.
+
+ * For an existing VM, run ``sudo sh infra/lima/enable-gicv3-traps.sh``
+ in its Lima shell from the updated checkout.
+ * For a new VM, add ``--param gicv3Traps=true`` to the creation command
+ above. The template leaves the traps off by default.
+
+ If any of the three settings below is still missing from the Lima VM's
+ ``/proc/cmdline`` after the opt-in, restart the outer VM from the Mac:
+
+ .. code-block:: console
+
+ $ limactl stop shirudo-dev && limactl start shirudo-dev
+
+ The outer Linux kernel reads the settings at boot. Reopen the Lima
+ shell, confirm that ``cat /proc/cmdline`` shows all three settings, and
+ retry the step that stalled.
+ The settings are ``kvm-arm.vgic_v3_group0_trap=1``,
+ ``kvm-arm.vgic_v3_group1_trap=1``, and
+ ``kvm-arm.vgic_v3_common_trap=1``.
+
The test VM state path probe fails, or ``make vm`` cannot create the vTPM keys
If ``build/test-vm`` is a directory in the shared checkout, Lima leaves
it in place. Ubuntu's AppArmor profile denies ``swtpm`` writes there.
diff --git a/infra/lima/enable-gicv3-traps.sh b/infra/lima/enable-gicv3-traps.sh
new file mode 100644
index 00000000..35e80533
--- /dev/null
+++ b/infra/lima/enable-gicv3-traps.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+set -eu
+
+traps='kvm-arm.vgic_v3_group0_trap=1 kvm-arm.vgic_v3_group1_trap=1 kvm-arm.vgic_v3_common_trap=1'
+cfg=/etc/default/grub.d/90-shirudo-kvm.cfg
+
+line='GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT '"$traps"'"'
+if [ "$(cat "$cfg" 2>/dev/null)" != "$line" ] ||
+ ! grep -qF -- "$traps" /boot/grub/grub.cfg; then
+ command -v update-grub >/dev/null
+ printf '%s\n' "$line" >"$cfg"
+ update-grub
+fi
+
+cmdline=$(cat /proc/cmdline)
+for flag in $traps; do
+ case " $cmdline " in
+ *" $flag "*) ;;
+ *)
+ echo "Restart the Lima VM to boot with the GICv3 traps."
+ break
+ ;;
+ esac
+done
diff --git a/infra/lima/shirudo.yaml b/infra/lima/shirudo.yaml
index d02a4271..19b87a9b 100644
--- a/infra/lima/shirudo.yaml
+++ b/infra/lima/shirudo.yaml
@@ -19,6 +19,7 @@ disk: 100GiB
param:
checkout: ""
+ gicv3Traps: "false"
# The checkout is shared at the path it has on the Mac, so that limactl
# shell opens in it and the build writes there.
@@ -75,6 +76,13 @@ provision:
set -eux -o pipefail
sh "{{.Param.checkout}}/.github/scripts/install-clang.sh"
sh "{{.Param.checkout}}/.github/scripts/install-static-libcrypto-deps.sh"
+- mode: system
+ script: |
+ #!/bin/bash
+ set -eu
+ {{if eq .Param.gicv3Traps "true"}}
+ sh "{{.Param.checkout}}/infra/lima/enable-gicv3-traps.sh"
+ {{end}}
# The test VMs' TPM and console sockets cannot live on the shared checkout,
# and Ubuntu's AppArmor profile lets swtpm write only under the home
# directory, so build/test-vm points there.
@@ -86,6 +94,23 @@ provision:
if [ ! -e "{{.Param.checkout}}/build/test-vm" ]; then
ln -s ~/test-vm "{{.Param.checkout}}/build/test-vm"
fi
+ {{if ne .Param.gicv3Traps "true"}}
+ # Keep the appliance check on the guest disk and run it once.
+ log="$HOME/test-vm/kvm-appliance-check.log"
+ if [[ "{{.Param.checkout}}/build/test-vm" -ef "$HOME/test-vm" && ! -e "$log" ]]; then
+ echo "Checking nested KVM with a libguestfs appliance."
+ rc=0
+ TEST_VM_WORK_DIR="$HOME/test-vm" \
+ "{{.Param.checkout}}/testing/vm/build-bundle.sh" --check-kvm-appliance \
+ >"$log" 2>&1 || rc=$?
+ case "$rc" in
+ 0) echo "The nested KVM appliance is ready." ;;
+ 124) echo "The nested KVM appliance check timed out. Inspect $log for boot progress."
+ echo "If boot stalled, see Troubleshooting in Documentation/start/quickstart-macos.rst." ;;
+ *) echo "The nested KVM appliance check failed with status $rc. See $log." ;;
+ esac
+ fi
+ {{end}}
probes:
- description: nested KVM
@@ -128,3 +153,5 @@ message: |
------
Follow the test kernel artifact steps in Documentation/start/quickstart.rst
before building shirudo and booting the test target.
+ The first-start nested KVM check logs to ~/test-vm/kvm-appliance-check.log
+ inside the Lima VM.
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index f0f3a760..6ee71712 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -265,6 +265,32 @@ if [[ "$TEST_VM_ARCH" == aarch64 ]]; then
require_command objcopy binutils binutils
require_command zstd zstd zstd
fi
+if [[ "${1:-}" == --check-kvm-appliance ]]; then
+ require_command timeout coreutils coreutils
+ log "Waiting up to 150s for the KVM appliance, including kernel selection. Console follows."
+ status=0
+ timeout --kill-after=5s 150s "$BASH" "$0" --boot-kvm-appliance || status=$?
+ case "$status" in
+ 0) log "KVM appliance ready. Check target readiness separately with 'make -C testing vm'." ;;
+ 124) log "KVM appliance readiness check timed out. Inspect the console for boot progress."
+ log "If boot appears stalled in Lima, see Troubleshooting in Documentation/start/quickstart-macos.rst." ;;
+ *) log "KVM appliance check failed with status $status. See output above." ;;
+ esac
+ exit "$status"
+fi
+if [[ "${1:-}" == --boot-kvm-appliance ]]; then
+ require_command guestfish libguestfs-tools guestfs-tools
+ check_libguestfs_host
+ # shellcheck source=testing/vm/guestfs.sh
+ . "$REPO_ROOT/testing/vm/guestfs.sh"
+ export LIBGUESTFS_BACKEND=direct LIBGUESTFS_BACKEND_SETTINGS=force_kvm
+ export LIBGUESTFS_HV
+ LIBGUESTFS_HV="$(command -v "${TEST_VM_QEMU:-$TEST_VM_ARCH_QEMU}")" ||
+ die "could not find the test VM's QEMU"
+ guestfs_setup
+ guestfs_run guestfish -v -a /dev/null run
+ exit
+fi
check_libguestfs_host
resolve_rootfs()
diff --git a/testing/vm/run.sh b/testing/vm/run.sh
index 1ca96e83..c92f1c38 100755
--- a/testing/vm/run.sh
+++ b/testing/vm/run.sh
@@ -2057,8 +2057,11 @@ wait_for_guest_status()
while [[ ! -s "$RUN_DIR/result/status" ]]; do
kill -0 "$QEMU_PID" 2>/dev/null ||
die "QEMU exited before guest preparation completed"
- (( elapsed < VM_TIMEOUT )) ||
- die "VM timed out after ${VM_TIMEOUT}s"
+ if (( elapsed >= VM_TIMEOUT )); then
+ log "console log: $RUN_DIR/console.log"
+ log "qemu log: $RUN_DIR/qemu.log"
+ die "target readiness timed out after ${VM_TIMEOUT}s"
+ fi
sleep 1
elapsed=$((elapsed + 1))
done