From 429fa7276c5a0167d46718c21ff4fc5e8a4545cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B6gl-Brunner=20Michel?= Date: Fri, 4 Dec 2026 14:15:44 +0100 Subject: [PATCH] haos-vm: stop when the chosen CPU model needs flags the host lacks A lower model than detected stays allowed; one the host cannot provide (e.g. x86-64-v3 on a Sandy Bridge host) would leave a VM that does not start, so the script exits before creating it. --- vm/haos-vm.sh | 53 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/vm/haos-vm.sh b/vm/haos-vm.sh index 96e7a35a2..0b1149abb 100644 --- a/vm/haos-vm.sh +++ b/vm/haos-vm.sh @@ -43,27 +43,51 @@ trap 'post_update_to_api "failed" "129"; exit 129' SIGHUP TEMP_DIR=$(mktemp -d) pushd $TEMP_DIR >/dev/null +# Whether the host CPU has every flag a QEMU CPU model needs. host and the +# kvm64 default (empty) run anywhere. +function host_supports_cpu_model() { + local flags f need=() + flags=" $(grep -m1 '^flags' /proc/cpuinfo | cut -d: -f2) " + local v2=(cx16 lahf_lm popcnt sse4_1 sse4_2 ssse3) + local v3=(avx avx2 bmi1 bmi2 f16c fma abm movbe xsave) + local v4=(avx512f avx512bw avx512cd avx512dq avx512vl) + case "$1" in + "" | host | kvm64) return 0 ;; + x86-64-v2) need=("${v2[@]}") ;; + x86-64-v2-AES) need=("${v2[@]}" aes) ;; + x86-64-v3) need=("${v2[@]}" "${v3[@]}") ;; + x86-64-v4) need=("${v2[@]}" "${v3[@]}" "${v4[@]}") ;; + *) return 0 ;; + esac + for f in "${need[@]}"; do + [[ "$flags" == *" $f "* ]] || return 1 + done +} + # Pick the highest portable CPU model the host supports instead of host: a # named model keeps live migration safe and avoids passing through flags of # unusual CPUs. x86-64-v3 exposes AVX2, which some add-ons need. x86-64-v4 # (AVX-512) is left out: nothing in Home Assistant needs it and it narrows the # nodes a VM can migrate to. Falls back to kvm64 (empty) on pre-v2 CPUs. function detect_cpu_model() { - local flags f - flags=" $(grep -m1 '^flags' /proc/cpuinfo | cut -d: -f2) " - for f in cx16 lahf_lm popcnt sse4_1 sse4_2 ssse3; do - [[ "$flags" == *" $f "* ]] || return 0 + local model + for model in x86-64-v3 x86-64-v2-AES x86-64-v2; do + if host_supports_cpu_model "$model"; then + echo "$model" + return 0 + fi done - local v3=1 - for f in avx avx2 bmi1 bmi2 f16c fma abm movbe xsave; do - [[ "$flags" == *" $f "* ]] || v3=0 - done - if ((v3)); then - echo "x86-64-v3" - elif [[ "$flags" == *" aes "* ]]; then - echo "x86-64-v2-AES" - else - echo "x86-64-v2" +} + +# A lower model than detected is fine (it only hides features); one the host +# cannot provide would leave a VM that does not start. +function check_cpu_model() { + [[ "$(vm_arch)" == "amd64" ]] || return 0 + local model="${CPU_TYPE# -cpu }" best + if ! host_supports_cpu_model "$model"; then + best="$(detect_cpu_model)" + msg_error "The host CPU ($(grep -m1 'model name' /proc/cpuinfo | cut -d: -f2 | xargs)) does not support the ${model} CPU model. Choose host, ${best:-kvm64} or a lower model." + exit 106 fi } @@ -153,6 +177,7 @@ Continue anyway?" 15 68; then vm_preflight vm_start_script "Use Default Settings?" 10 58 +check_cpu_model check_avx2 post_to_api_vm