Files
RedBear-OS/local/scripts/test-iommu-qemu.sh
T
vasilito 9ec00d4c81 tests: replace expect with stdlib python helper for QEMU login proofs
Drop the host expect dependency from the PS/2, timer, IOMMU, and USB
storage (BOT) runtime proofs. local/scripts/qemu-login-expect.py drives
the guest over serial-on-stdio with ordered expect/send steps, per-step
timeouts (matching expect's per-pattern semantics), and pass/fail markers.
It distinguishes premature QEMU death (exit 3) from genuine check failure
(exit 1) so the scripts retry external interruptions without retrying real
failures. Scripts use headless -display none -vga none (fbcond serial
mirror), a 3600s per-step timeout, and a retry loop (max 5 attempts).
2026-07-20 05:38:44 +09:00

171 lines
5.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Launch or validate the IOMMU path in QEMU with an AMD IOMMU device.
set -euo pipefail
find_uefi_firmware() {
local candidates=(
"/usr/share/ovmf/x64/OVMF.4m.fd"
"/usr/share/OVMF/x64/OVMF.4m.fd"
"/usr/share/ovmf/x64/OVMF_CODE.4m.fd"
"/usr/share/OVMF/x64/OVMF_CODE.4m.fd"
"/usr/share/ovmf/OVMF.fd"
"/usr/share/OVMF/OVMF_CODE.fd"
"/usr/share/qemu/edk2-x86_64-code.fd"
)
local path
for path in "${candidates[@]}"; do
if [[ -f "$path" ]]; then
printf '%s\n' "$path"
return 0
fi
done
return 1
}
usage() {
cat << USAGE
Usage: $(basename "$0") [--check] [config] [extra qemu args...]
Launch or validate QEMU with an AMD IOMMU device.
Options:
--help Show this help message
--check Boot and verify the guest reaches a login prompt
Arguments:
config Optional config name (default: redbear-mini)
extra qemu args Additional arguments appended to the QEMU command
Environment:
QEMUFLAGS Additional flags (prepended to device amd-iommu)
Examples:
$(basename "$0")
$(basename "$0") --check
$(basename "$0") redbear-mini -m 4G
USAGE
exit 0
}
check_mode=0
filtered_args=()
config="redbear-mini"
for arg in "$@"; do
case "$arg" in
--help|-h|help)
usage
;;
--check)
check_mode=1
;;
redbear-*)
config="$arg"
;;
*)
filtered_args+=("$arg")
;;
esac
done
if [[ "$config" == "redbear-mini" ]]; then
config="redbear-mini"
fi
firmware="$(find_uefi_firmware)" || {
echo "ERROR: no usable x86_64 UEFI firmware found" >&2
exit 1
}
arch="${ARCH:-$(uname -m)}"
iso="build/$arch/$config.iso"
image="build/$arch/$config/harddrive.img"
extra="build/$arch/$config/extra.img"
extra_qemu_args="${filtered_args[*]:-}"
boot_media=""
if [[ -f "$iso" ]]; then
boot_media="iso"
elif [[ -f "$image" ]]; then
boot_media="harddrive"
else
echo "ERROR: no bootable image found ($iso or $image)" >&2
echo "Build it first with: ./local/scripts/build-redbear.sh $config" >&2
exit 1
fi
if [[ ! -f "$extra" ]]; then
truncate -s 1g "$extra"
fi
if [[ "$boot_media" == "iso" ]]; then
boot_args=(-cdrom "$iso")
else
boot_args=(-drive file="$image",format=raw,if=none,id=drv0,snapshot=on -device nvme,drive=drv0,serial=NVME_SERIAL)
fi
pkill -f "qemu-system-x86_64.*$config" 2>/dev/null || true
sleep 1
if [[ "$check_mode" -eq 1 ]]; then
attempt=1
while true; do
rc=0
python3 local/scripts/qemu-login-expect.py \
--timeout 3600 \
--log "build/$arch/$config/iommu-check.log" \
--step "expect:1022:1419" \
--step "expect:login:" \
--step "send:root" \
--step "expect:assword:" \
--step "send:password" \
--step "expect:Type 'help' for available commands." \
--step "send:redbear-phase-iommu-check" \
--step "expect:Red Bear OS IOMMU Runtime Check" \
--step "expect:units_detected=" \
--step "expect:units_initialized_now=" \
--step "expect:units_initialized_after=" \
--step "expect:events_drained=" \
--step "send:shutdown" \
--pass_marker "events_drained=" \
-- \
qemu-system-x86_64 -name "Red Bear OS x86_64" -device qemu-xhci -device amd-iommu -smp 4 -m 2048 -bios "$firmware" -chardev stdio,id=debug,signal=off,mux=on -serial chardev=debug -mon chardev=debug -machine q35 -device ich9-intel-hda -device hda-output -device virtio-net,netdev=net0 -netdev user,id=net0 -object filter-dump,id=f1,netdev=net0,file="build/$arch/$config/network.pcap" -display none -vga none "${boot_args[@]}" -drive file="$extra",format=raw,if=none,id=drv1,snapshot=on -device nvme,drive=drv1,serial=NVME_EXTRA -enable-kvm -cpu host $extra_qemu_args || rc=$?
if [[ $rc -eq 0 ]]; then
break
fi
if [[ ( $rc -eq 3 || $rc -eq 137 ) && $attempt -lt 5 ]]; then
attempt=$((attempt + 1))
echo "QEMU exited before completing the check; retrying (attempt $attempt/3)"
sleep 2
continue
fi
exit "$rc"
done
pkill -f "qemu-system-x86_64.*$image" 2>/dev/null || true
echo "IOMMU first-use validation path completed via guest runtime check"
exit 0
fi
exec qemu-system-x86_64 \
-name "Red Bear OS x86_64" \
-device qemu-xhci \
-device amd-iommu \
-smp 4 \
-m 2048 \
-bios "$firmware" \
-chardev stdio,id=debug,signal=off,mux=on \
-serial chardev:debug \
-mon chardev=debug \
-machine q35 \
-device ich9-intel-hda -device hda-output \
-device virtio-net,netdev=net0 \
-netdev user,id=net0 \
-object filter-dump,id=f1,netdev=net0,file="build/$arch/$config/network.pcap" \
-display none -vga none \
"${boot_args[@]}" \
-drive file="$extra",format=raw,if=none,id=drv1,snapshot=on \
-device nvme,drive=drv1,serial=NVME_EXTRA \
-enable-kvm -cpu host \
$extra_qemu_args