diff --git a/scripts/test-util.sh b/scripts/test-util.sh index 8d4029f3c..b75dc4886 100644 --- a/scripts/test-util.sh +++ b/scripts/test-util.sh @@ -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=("$@")