From c2edb081bb986cfe12644d99832ea77b1cd36f15 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 16 Dec 2023 07:41:19 +0100 Subject: [PATCH] feat: CPU configuration * feat: CPU configuration --- src/config.sh | 28 +++++----------------------- src/cpu.sh | 32 ++++++++++++++++++++++++++++++++ src/entry.sh | 1 + src/reset.sh | 3 ++- 4 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 src/cpu.sh diff --git a/src/config.sh b/src/config.sh index a80bb2b..0603f02 100644 --- a/src/config.sh +++ b/src/config.sh @@ -1,34 +1,16 @@ #!/usr/bin/env bash set -Eeuo pipefail -KVM_ERR="" -KVM_OPTS="" - -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 - if [ "$ARCH" == "amd64" ]; then - error "KVM acceleration not detected $KVM_ERR, see the FAQ about this." - [[ "$DEBUG" != [Yy1]* ]] && exit 88 - fi -else - KVM_OPTS=",accel=kvm -enable-kvm -cpu host" -fi - DEF_OPTS="-nodefaults" RAM_OPTS=$(echo "-m $RAM_SIZE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') -CPU_OPTS="-smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" +CPU_OPTS="-cpu $CPU_MODEL -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" MAC_OPTS="-machine type=q35,usb=off,dump-guest-core=off,hpet=off${KVM_OPTS}" SERIAL_OPTS="-serial mon:stdio -device virtio-serial-pci,id=virtio-serial0,bus=pcie.0,addr=0x3" -EXTRA_OPTS="-device virtio-balloon-pci,id=balloon0 -object rng-random,id=rng0,filename=/dev/urandom -device virtio-rng-pci,rng=rng0" +EXTRA_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" +EXTRA_OPTS="$EXTRA_OPTS -object rng-random,id=objrng0,filename=/dev/urandom" +EXTRA_OPTS="$EXTRA_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" -ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $DISPLAY_OPTS $BOOT_OPTS $EXTRA_OPTS $ARGUMENTS" +ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $BOOT_OPTS $EXTRA_OPTS $ARGUMENTS" ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') return 0 diff --git a/src/cpu.sh b/src/cpu.sh new file mode 100644 index 0000000..f1fd6dd --- /dev/null +++ b/src/cpu.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +# Docker environment variables +: ${CPU_MODEL:='host'} + +if [[ "$ARCH" == "amd64" && "$KVM" != [Nn]* ]]; then + + 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 + error "KVM acceleration not detected $KVM_ERR, this will cause a major loss of performance." + error "See the FAQ on how to enable it, or skip this error by setting KVM=N (not recommended)." + [[ "$DEBUG" != [Yy1]* ]] && exit 88 + [[ "$CPU_MODEL" == "host"* ]] && CPU_MODEL="max" + else + KVM_OPTS=",accel=kvm -enable-kvm" + fi + +else + + [[ "$CPU_MODEL" == "host"* ]] && CPU_MODEL="max" + +fi + +return 0 diff --git a/src/entry.sh b/src/entry.sh index cce345f..0072714 100755 --- a/src/entry.sh +++ b/src/entry.sh @@ -12,6 +12,7 @@ cd /run . display.sh # Initialize display . network.sh # Initialize network . boot.sh # Configure boot +. cpu.sh # Initialize processor . config.sh # Configure arguments trap - ERR diff --git a/src/reset.sh b/src/reset.sh index 6d49c7c..cbe2188 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -12,7 +12,8 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR # Docker environment variables : ${BOOT:=''} # URL of the ISO file -: ${DEBUG:='N'} # Enable debugging mode +: ${KVM:='Y'} # Enable KVM acceleration +: ${DEBUG:='N'} # Disable debugging mode : ${ALLOCATE:='Y'} # Preallocate diskspace : ${ARGUMENTS:=''} # Extra QEMU parameters : ${CPU_CORES:='1'} # Amount of CPU cores