From c02b7e70414f261030b79227bc923e1cbe96034c Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 19:04:04 +0100 Subject: [PATCH 1/6] feat: Close network on shutdown --- src/network.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/network.sh b/src/network.sh index 437f76eb..fd8f7292 100644 --- a/src/network.sh +++ b/src/network.sh @@ -193,6 +193,36 @@ configureNAT() { return 0 } +closeNetwork() { + + # Shutdown nginx + nginx -s stop 2> /dev/null + fWait "nginx" + + exec 30<&- || true + exec 40<&- || true + + if [[ "$DHCP" == [Yy1]* ]]; then + + ip link set "$VM_NET_TAP" down || true + ip link delete "$VM_NET_TAP" || true + + else + + local pid="/var/run/dnsmasq.pid" + [ -f "$pid" ] && pKill "$(<"$pid")" + + ip link set "$VM_NET_TAP" down promisc off || true + ip link delete "$VM_NET_TAP" || true + + ip link set dockerbridge down || true + ip link delete dockerbridge || true + + fi + + return 0 +} + getInfo() { if [ -z "$VM_NET_DEV" ]; then From b532f4f6cf46493aae7f76d49df4798e07fdd273 Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 19:14:25 +0100 Subject: [PATCH 2/6] Update boot.sh --- src/boot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boot.sh b/src/boot.sh index 2272710e..c66f1fc8 100644 --- a/src/boot.sh +++ b/src/boot.sh @@ -62,9 +62,9 @@ if [[ "${BOOT_MODE,,}" != "legacy" ]]; then mkdir -p /run/shm/tpm chmod 755 /run/shm/tpm - swtpm socket -t -d --tpmstate dir=/run/shm/tpm --ctrl type=unixio,path=/run/swtpm-sock --tpm2 + swtpm socket -t -d --tpmstate dir=/run/shm/tpm --ctrl type=unixio,path=/run/swtpm-sock --pid file=/run/tpm.pid --tpm2 - for (( i = 1; i < 50; i++ )); do + for (( i = 1; i < 20; i++ )); do [ -S "/run/swtpm-sock" ] && break From 86b43ab5736267ecd97bd1fb40e57197d43d034e Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 19:20:57 +0100 Subject: [PATCH 3/6] Update boot.sh --- src/boot.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/boot.sh b/src/boot.sh index c66f1fc8..2257d4da 100644 --- a/src/boot.sh +++ b/src/boot.sh @@ -60,9 +60,12 @@ if [[ "${BOOT_MODE,,}" != "legacy" ]]; then if [[ "$TPM" == [Yy1]* ]]; then + rm -rf /run/shm/tpm + rm -f /var/run/tpm.pid mkdir -p /run/shm/tpm chmod 755 /run/shm/tpm - swtpm socket -t -d --tpmstate dir=/run/shm/tpm --ctrl type=unixio,path=/run/swtpm-sock --pid file=/run/tpm.pid --tpm2 + + swtpm socket -t -d --tpmstate dir=/run/shm/tpm --ctrl type=unixio,path=/run/swtpm-sock --pid file=/var/run/tpm.pid --tpm2 for (( i = 1; i < 20; i++ )); do From a45676e10510b4ccf11196f5b5ad644bf23a577b Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 19:52:11 +0100 Subject: [PATCH 4/6] feat: Make serial monitor configurable --- src/config.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/config.sh b/src/config.sh index 8778de07..87031992 100644 --- a/src/config.sh +++ b/src/config.sh @@ -1,13 +1,16 @@ #!/usr/bin/env bash set -Eeuo pipefail +: "${SERIAL:="mon:stdio"}" +: "${MONITOR:="telnet:localhost:7100,server,nowait,nodelay"}" + DEF_OPTS="-nodefaults" -SERIAL_OPTS="-serial mon:stdio" +SERIAL_OPTS="-serial $SERIAL" +MON_OPTS="-monitor $MONITOR" USB_OPTS="-device qemu-xhci -device usb-tablet" -MON_OPTS="-monitor telnet:localhost:7100,server,nowait,nodelay" RAM_OPTS=$(echo "-m $RAM_SIZE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" -MAC_OPTS="-machine type=q35${SECURE},graphics=off,vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}" +MAC_OPTS="-machine type=q35${SECURE},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="$DEV_OPTS -object rng-random,id=objrng0,filename=/dev/urandom" DEV_OPTS="$DEV_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" From 5d3ecad59d29ea1ddce96890a42c4b71b287b6b0 Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 19:53:22 +0100 Subject: [PATCH 5/6] Update config.sh --- src/config.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.sh b/src/config.sh index 87031992..fe85cf28 100644 --- a/src/config.sh +++ b/src/config.sh @@ -10,7 +10,7 @@ MON_OPTS="-monitor $MONITOR" USB_OPTS="-device qemu-xhci -device usb-tablet" RAM_OPTS=$(echo "-m $RAM_SIZE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" -MAC_OPTS="-machine type=q35${SECURE},vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}" +MAC_OPTS="-machine type=q35${SECURE},graphics=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="$DEV_OPTS -object rng-random,id=objrng0,filename=/dev/urandom" DEV_OPTS="$DEV_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" From d2d044f7bc023bdd49785895c46661756ba27ded Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 23 Jan 2024 20:11:34 +0100 Subject: [PATCH 6/6] Update reset.sh --- src/reset.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/reset.sh b/src/reset.sh index 97b62818..73539cf4 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -24,7 +24,7 @@ echo : "${RAM_SIZE:="1G"}" # Maximum RAM amount : "${DISK_SIZE:="16G"}" # Initial data disk size : "${BOOT_INDEX:="10"}" # Boot index of CD drive - + # Helper variables STORAGE="/storage" @@ -48,10 +48,54 @@ else fi # Check folder -[ ! -d "$STORAGE" ] && error "Storage folder ($STORAGE) not found!" && exit 13 + +if [ ! -d "$STORAGE" ]; then + error "Storage folder ($STORAGE) not found!" && exit 13 +fi # Helper functions +isAlive() { + local pid=$1 + + if kill -0 "$pid" 2>/dev/null; then + return 0 + fi + + return 1 +} + +pKill() { + local pid=$1 + + { kill -15 "$pid" || true; } 2>/dev/null + + while isAlive "$pid"; do + sleep 0.2 + done + + return 0 +} + +fWait() { + local name=$1 + + while pgrep -f -l "$name" >/dev/null; do + sleep 0.2 + done + + return 0 +} + +fKill() { + local name=$1 + + { pkill -f "$name" || true; } 2>/dev/null + fWait "$name" + + return 0 +} + escape () { local s s=${1//&/\&} @@ -89,13 +133,12 @@ html() HTML="${HTML/\[5\]/$FOOTER2}" echo "$HTML" > "$PAGE" - echo "$body$script" > "$INFO" + echo "$body" > "$INFO" return 0 } -addPackage () { - +addPackage() { local pkg=$1 local desc=$2