diff --git a/src/config.sh b/src/config.sh index c2a5415..0c30dc4 100644 --- a/src/config.sh +++ b/src/config.sh @@ -2,15 +2,62 @@ set -Eeuo pipefail DEF_OPTS="-nodefaults -boot strict=on" -RAM_OPTS=$(echo "-m ${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') -MON_OPTS="-name $PROCESS,process=$PROCESS,debug-threads=on -pidfile $QEMU_PID" -CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" -MAC_OPTS="-machine type=$MACHINE,smm=off,usb=off,vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}" -DEV_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" -DEV_OPTS+=" -object rng-random,id=objrng0,filename=/dev/urandom" -DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" +DEV_OPTS="" -ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $DEV_OPTS $ARGUMENTS" -ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') +configureProcessor() { + + CPU_OPTS="-cpu $CPU_FLAGS" + CPU_OPTS+=" -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" + + return 0 +} + +configureMemory() { + + RAM_OPTS=$(echo "-m ${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') + + return 0 +} + +configureMonitor() { + + MON_OPTS="-name $PROCESS,process=$PROCESS,debug-threads=on" + MON_OPTS+=" -pidfile $QEMU_PID" + + return 0 +} + +configureMachine() { + + MAC_OPTS="-machine type=$MACHINE,smm=off,usb=off" + MAC_OPTS+=",vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}" + + return 0 +} + +configureVirtioDevices() { + + DEV_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" + DEV_OPTS+=" -object rng-random,id=objrng0,filename=/dev/urandom" + DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" + + return 0 +} + +buildArguments() { + + ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $DEV_OPTS $ARGUMENTS" + ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') + + return 0 +} + +configureMemory +configureMonitor +configureMachine +configureProcessor +configureVirtioDevices + +buildArguments return 0 diff --git a/src/proc.sh b/src/proc.sh index 9803680..dd5a1c3 100644 --- a/src/proc.sh +++ b/src/proc.sh @@ -45,7 +45,7 @@ checkClocksource() { checkSse42() { - if ! grep -qw "sse4_2" <<< "$flags"; then + if ! hasFlag "sse4_2"; then error "Your CPU does not have the SSE4 instruction set that Virtual DSM requires!" ! enabled "$DEBUG" && exit 88 fi @@ -94,10 +94,10 @@ configureKvmCpuModel() { appendKvmInvtscFeature() { - if grep -qw "svm" <<< "$flags"; then + if hasFlag "svm"; then # AMD processor - if grep -qw "tsc_scale" <<< "$flags"; then + if hasFlag "tsc_scale"; then CPU_FEATURES+=",+invtsc" fi @@ -186,8 +186,6 @@ configureHostCpuName() { selectClocksource checkClocksource -flags=$(sed -ne '/^flags/s/^.*: //p' /proc/cpuinfo) - if ! disabled "$KVM"; then configureKvm else diff --git a/src/utils.sh b/src/utils.sh index 3ca719f..7931c04 100644 --- a/src/utils.sh +++ b/src/utils.sh @@ -7,6 +7,29 @@ info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; } error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; } warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; } +hasFlag() { + + # Match a whitespace-delimited token in /proc/cpuinfo + grep -m1 '^flags[[:space:]]*:' /proc/cpuinfo | grep -Fqw -- "$1" + +} + +hasFeature() { + + # Match a whitespace-delimited token in /proc/cpuinfo + grep -m1 '^Features[[:space:]]*:' /proc/cpuinfo | grep -Fqw -- "$1" + +} + +isAmdCpu() { + + local vendor + vendor=$(lscpu | awk '/Vendor ID/{print $3}') + + [[ "$vendor" == "AuthenticAMD" ]] + +} + interactive() { [ -t 1 ] &&