scripts: auto-detect hypervisor in test-util.sh

Make the --hypervisor argument optional in process_common_args().
When it is not supplied, detect_hypervisor() now selects the backend
from the host device node: /dev/mshv for MSHV, /dev/kvm for KVM, and
errors out when neither is present. An explicit --hypervisor still
overrides the detection.

Assisted-by: Claude:Opus-4.8
Signed-off-by: Muminul Islam <muislam@microsoft.com>
This commit is contained in:
Muminul Islam
2026-06-03 17:56:10 -07:00
committed by Bo Chen
parent b88d5de85e
commit 5932b11f13

View File

@@ -3,10 +3,23 @@
# shellcheck source=/dev/null
set -x
hypervisor="kvm"
test_filter=""
build_kernel=false
# Auto-detect the underlying hypervisor based on the device node exposed
# by the host kernel: /dev/mshv for MSHV, /dev/kvm for KVM.
# shellcheck disable=SC2034
detect_hypervisor() {
if [ -e /dev/mshv ]; then
hypervisor="mshv"
elif [ -e /dev/kvm ]; then
hypervisor="kvm"
else
echo "ERROR: no hypervisor device found (/dev/mshv or /dev/kvm)" >&2
exit 1
fi
}
# Download from a url with retries
# Args:
# $1: URL
@@ -98,7 +111,7 @@ cmd_help() {
echo ""
echo "Available arguments:"
echo ""
echo " --hypervisor Underlying hypervisor. Options kvm, mshv"
echo " --hypervisor Underlying hypervisor. Options kvm, mshv. Auto-detected from the host device node when omitted."
echo " --test-filter Tests to run"
echo " --build-guest-kernel Build guest kernel from source instead of downloading pre-built"
echo ""
@@ -140,8 +153,13 @@ process_common_args() {
esac
shift
done
# Auto-detect the hypervisor when it was not provided explicitly.
if [ -z "$hypervisor" ]; then
detect_hypervisor
fi
if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
die "Hypervisor value must be kvm or mshv"
echo "ERROR: hypervisor must be kvm or mshv" >&2
exit 1
fi
# shellcheck disable=SC2034
test_binary_args=("$@")