virtual-dsm/src/cpu.sh

73 lines
1.7 KiB
Bash
Raw Normal View History

2023-12-16 07:42:57 +01:00
#!/usr/bin/env bash
2023-12-16 04:36:00 +01:00
set -Eeuo pipefail
2023-12-16 05:08:34 +01:00
# Docker environment variables
2023-12-16 06:31:43 +01:00
2023-12-16 05:08:34 +01:00
: ${HOST_CPU:=''}
: ${CPU_MODEL:='host'}
2023-12-16 06:31:43 +01:00
: ${CPU_FEATURES:='+ssse3,+sse4.1,+sse4.2'}
2023-12-16 05:08:34 +01:00
2023-12-27 16:28:24 +01:00
[ "$ARCH" != "amd64" ] && KVM="N"
2023-12-16 04:36:00 +01:00
2023-12-27 16:28:24 +01:00
if [[ "$KVM" != [Nn]* ]]; then
KVM_ERR=""
2023-12-16 04:36:00 +01:00
if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then
if ! grep -q -e vmx -e svm /proc/cpuinfo; then
KVM_ERR="(vmx/svm disabled)"
fi
else
[ -e /dev/kvm ] && KVM_ERR="(no write access)" || KVM_ERR="(device file missing)"
fi
if [ -n "$KVM_ERR" ]; then
2023-12-27 16:28:24 +01:00
KVM="N"
2023-12-16 04:36:00 +01:00
error "KVM acceleration not detected $KVM_ERR, this will cause a major loss of performance."
2023-12-27 16:28:24 +01:00
error "See the FAQ on how to enable it, or continue without KVM by setting KVM=N (not recommended)."
2023-12-16 04:55:40 +01:00
[[ "$DEBUG" != [Yy1]* ]] && exit 88
2023-12-16 04:36:00 +01:00
fi
2023-12-27 16:28:24 +01:00
fi
if [[ "$KVM" != [Nn]* ]]; then
KVM_OPTS=",accel=kvm -enable-kvm"
if ! grep -qE '^flags.* (sse4_2)' /proc/cpuinfo; then
error "Your host CPU does not have the SSE4.2 instruction set that Virtual DSM requires to boot."
error "Disable KVM by setting KVM=N to emulate a compatible CPU, at the cost of performance."
[[ "$DEBUG" != [Yy1]* ]] && exit 89
2023-12-16 04:36:00 +01:00
fi
2023-12-16 04:55:40 +01:00
2023-12-16 04:36:00 +01:00
else
2023-12-16 04:55:40 +01:00
2023-12-27 16:28:24 +01:00
KVM_OPTS=""
2023-12-25 05:58:14 +01:00
if [[ "$CPU_MODEL" == "host"* ]]; then
if [[ "$ARCH" == "amd64" ]]; then
CPU_MODEL="max,$CPU_FEATURES"
else
CPU_MODEL="qemu64,$CPU_FEATURES"
fi
fi
2023-12-16 04:55:40 +01:00
2023-12-16 04:36:00 +01:00
fi
2023-12-16 06:31:43 +01:00
if [ -z "$HOST_CPU" ]; then
2023-12-16 05:08:34 +01:00
HOST_CPU=$(lscpu | grep 'Model name' | cut -f 2 -d ":" | awk '{$1=$1}1' | sed 's# @.*##g' | sed s/"(R)"//g | sed 's/[^[:alnum:] ]\+/ /g' | sed 's/ */ /g')
fi
2023-12-27 03:23:39 +01:00
if [ -n "$HOST_CPU" ]; then
HOST_CPU="${HOST_CPU%%,*},,"
else
2023-12-16 06:31:43 +01:00
HOST_CPU="QEMU, Virtual CPU,"
2023-12-16 05:08:34 +01:00
if [ "$ARCH" == "amd64" ]; then
2023-12-16 06:31:43 +01:00
HOST_CPU="$HOST_CPU X86_64"
2023-12-16 05:08:34 +01:00
else
2023-12-16 06:31:43 +01:00
HOST_CPU="$HOST_CPU $ARCH"
2023-12-16 05:08:34 +01:00
fi
fi
2023-12-16 04:36:00 +01:00
return 0