feat: detect if running in a VM and use an emulated CPU

This commit is contained in:
Martin Wimpress 2024-05-14 20:48:30 +01:00 committed by Martin Wimpress
parent 3d83553d62
commit 20684681c6
1 changed files with 33 additions and 16 deletions

View File

@ -284,16 +284,32 @@ function configure_cpu() {
HOST_CPU_SOCKETS=$(get_cpu_info 'Socket') HOST_CPU_SOCKETS=$(get_cpu_info 'Socket')
HOST_CPU_VENDOR=$(get_cpu_info 'Vendor') HOST_CPU_VENDOR=$(get_cpu_info 'Vendor')
# A CPU with Intel VT-x / AMD SVM support is required # Detect if running in a VM
if [ "${HOST_CPU_VENDOR}" == "GenuineIntel" ]; then if [ ${DARWIN} -eq 1 ]; then
if ! check_cpu_flag vmx; then MANUFACTURER=$(ioreg -l | grep -e Manufacturer | grep -v iMan | cut -d'"' -f4 | sort -u)
echo "ERROR! Intel VT-x support is required." else
#exit 1 MANUFACTURER=$(head -1 /sys/class/dmi/id/sys_vendor)
fi fi
elif [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
if ! check_cpu_flag svm; then case ${MANUFACTURER,,} in
echo "ERROR! AMD SVM support is required." qemu) CPU_MODEL="Penryn"
exit 1 IN_VM=0;;
*) CPU_MODEL="host"
IN_VM=1;;
esac
if [ ! ${IN_VM} ]; then
# A CPU with Intel VT-x / AMD SVM support is required
if [ "${HOST_CPU_VENDOR}" == "GenuineIntel" ]; then
if ! check_cpu_flag vmx; then
echo "ERROR! Intel VT-x support is required."
exit 1
fi
elif [ "${HOST_CPU_VENDOR}" == "AuthenticAMD" ]; then
if ! check_cpu_flag svm; then
echo "ERROR! AMD SVM support is required."
exit 1
fi
fi fi
fi fi
@ -1077,11 +1093,12 @@ function vm_boot() {
echo "#!/usr/bin/env bash" > "${VMDIR}/${VMNAME}.sh" echo "#!/usr/bin/env bash" > "${VMDIR}/${VMNAME}.sh"
# Changing process name is not supported on macOS # Changing process name is not supported on macOS
if [ ${DARWIN} -eq 1 ]; then if [ ${DARWIN} -eq 1 ]; then
# shellcheck disable=SC2054,SC2206,SC2140 if [ ! ${IN_VM} ]; then
args+=(-accel hvf) # shellcheck disable=SC2054,SC2206,SC2140
args+=(-accel hvf)
fi
else else
# shellcheck disable=SC2054,SC2206,SC2140 # shellcheck disable=SC2054,SC2206,SC2140
args+=(-name ${VMNAME},process=${VMNAME} -enable-kvm) args+=(-name ${VMNAME},process=${VMNAME} -enable-kvm)
@ -1799,13 +1816,13 @@ if command -v gstat &>/dev/null; then
fi fi
DARWIN=0 DARWIN=0
CPU_MODEL="host"
CPU_KVM=",kvm=on" CPU_KVM=",kvm=on"
CPU_KVM_UNHALT=",kvm_pv_unhalt" CPU_KVM_UNHALT=",kvm_pv_unhalt"
KVM_GUEST_TWEAKS="-global kvm-pit.lost_tick_policy=discard " KVM_GUEST_TWEAKS="-global kvm-pit.lost_tick_policy=discard "
# Detect macOS and disable KVM
if [ "$(uname -s)" == "Darwin" ]; then if [ "$(uname -s)" == "Darwin" ]; then
DARWIN=1 DARWIN=1
CPU_MODEL="Penryn"
CPU_KVM="" CPU_KVM=""
CPU_KVM_UNHALT="" CPU_KVM_UNHALT=""
KVM_GUEST_TWEAKS="" KVM_GUEST_TWEAKS=""