From 120171e8264163449024c4c8f0d82bc2fdad419f Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 15 Mar 2025 03:27:33 +0100 Subject: [PATCH] feat: Verify clock source (#203) --- src/boot.sh | 49 ++++++++++++++++++++++++++++++++++++------------- src/proc.sh | 25 ------------------------- src/reset.sh | 2 +- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/boot.sh b/src/boot.sh index a55db6f..c88c993 100644 --- a/src/boot.sh +++ b/src/boot.sh @@ -2,23 +2,14 @@ set -Eeuo pipefail # Docker environment variables -: "${BIOS:=""}" # BIOS file +: "${BIOS:=""}" # BIOS file +: "${SECURE:="off"}" # Secure boot -SECURE="off" -BOOT_OPTS="" BOOT_DESC="" - -if [ -n "$BIOS" ]; then - BOOT_MODE="custom" - BOOT_OPTS="-bios $BIOS" - BOOT_DESC=" with custom BIOS file" - return 0 -fi +BOOT_OPTS="" +[ -n "$BIOS" ] && BOOT_MODE="custom" case "${BOOT_MODE,,}" in - "legacy" ) - BOOT_DESC=" with SeaBIOS" - ;; "uefi" | "" ) BOOT_MODE="uefi" ROM="AAVMF_CODE.no-secboot.fd" @@ -42,6 +33,13 @@ case "${BOOT_MODE,,}" in VARS="AAVMF_VARS.ms.fd" BOOT_OPTS="-rtc base=localtime" ;; + "legacy" ) + BOOT_DESC=" with SeaBIOS" + ;; + "custom" ) + BOOT_OPTS="-bios $BIOS" + BOOT_DESC=" with custom BIOS file" + ;; *) error "Unknown BOOT_MODE, value \"${BOOT_MODE}\" is not recognized!" exit 33 @@ -74,4 +72,29 @@ case "${BOOT_MODE,,}" in ;; esac +MSRS="/sys/module/kvm/parameters/ignore_msrs" +if [ -e "$MSRS" ]; then + result=$(<"$MSRS") + if [[ "$result" == "0" ]] || [[ "${result^^}" == "N" ]]; then + echo 1 | tee "$MSRS" > /dev/null 2>&1 || true + fi +fi + +CLOCKSOURCE="tsc" +[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter" +CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource" + +if [ ! -f "$CLOCK" ]; then + warn "file \"$CLOCK\" cannot not found?" +else + result=$(<"$CLOCK") + case "${result,,}" in + "${CLOCKSOURCE,,}" ) ;; + "kvm-clock" ) info "Nested KVM virtualization detected.." ;; + "hyperv_clocksource_tsc_page" ) info "Nested Hyper-V virtualization detected.." ;; + "hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; + *) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; + esac +fi + return 0 diff --git a/src/proc.sh b/src/proc.sh index de2b7bb..e158d9d 100644 --- a/src/proc.sh +++ b/src/proc.sh @@ -24,31 +24,6 @@ if [[ "$CPU" == "Rockchip RK3588"* ]] && [[ "$CORES" == "8" ]]; then [ -z "$CPU_PIN" ] && CPU_PIN="4,5,6,7" fi -MSRS="/sys/module/kvm/parameters/ignore_msrs" -if [ -e "$MSRS" ]; then - result=$(<"$MSRS") - if [[ "$result" == "0" ]] || [[ "${result^^}" == "N" ]]; then - echo 1 | tee "$MSRS" > /dev/null 2>&1 || true - fi -fi - -CLOCKSOURCE="tsc" -[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter" -CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource" - -if [ ! -f "$CLOCK" ]; then - warn "file \"$CLOCK\" cannot not found?" -else - result=$(<"$CLOCK") - case "${result,,}" in - "${CLOCKSOURCE,,}" ) ;; - "kvm-clock" ) info "Nested KVM virtualization detected.." ;; - "hyperv_clocksource_tsc_page" ) info "Nested Hyper-V virtualization detected.." ;; - "hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; - *) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; - esac -fi - if [[ "${ARCH,,}" != "arm64" ]]; then KVM="N" warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for ARM64 instructions, this will cause a major loss of performance." diff --git a/src/reset.sh b/src/reset.sh index b033208..8151cbf 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -148,7 +148,7 @@ SPACE_GB=$(formatBytes "$SPACE" "down") AVAIL_MEM=$(formatBytes "$RAM_AVAIL" "down") TOTAL_MEM=$(formatBytes "$RAM_TOTAL" "up") -echo "❯ CPU: ${CPU} | RAM: ${AVAIL_MEM%% *}/$TOTAL_MEM | DISK: $SPACE_GB (${FS}) | KERNEL: ${SYS}..." +echo "❯ CPU: ${CPU} | RAM: ${AVAIL_MEM/ GB/}/$TOTAL_MEM | DISK: $SPACE_GB (${FS}) | KERNEL: ${SYS}..." echo # Check compatibilty