This commit is contained in:
crowetic 2025-04-16 21:23:23 +00:00
parent d17e375da4
commit 2ed3142433

View File

@ -2,7 +2,7 @@
set -euo pipefail set -euo pipefail
echo "=== [1/5] Updating system and installing tools ===" echo "=== [1/5] Installing essentials ==="
sudo apt update sudo apt update
sudo apt -y upgrade sudo apt -y upgrade
sudo apt install -y qemu-guest-agent util-linux haveged ethtool lshw dmidecode sudo apt install -y qemu-guest-agent util-linux haveged ethtool lshw dmidecode
@ -13,23 +13,35 @@ sudo systemctl enable --now qemu-guest-agent
echo "=== [3/5] Trimming filesystems ===" echo "=== [3/5] Trimming filesystems ==="
sudo fstrim -av || true sudo fstrim -av || true
echo "=== [4/5] Detecting VM NICs ===" echo "=== [4/5] Creating conditional NIC tuning script ==="
NIC_LIST=() TUNER_SCRIPT="/usr/local/sbin/conditional-nic-tuning.sh"
for nic in /sys/class/net/*; do
nicname=$(basename "$nic")
if [[ "$nicname" != "lo" && "$nicname" != vmbr* && "$nicname" != tap* && "$nicname" != veth* && "$nicname" != docker* ]]; then
NIC_LIST+=("$nicname")
fi
done
if [[ ${#NIC_LIST[@]} -eq 0 ]]; then sudo tee "$TUNER_SCRIPT" > /dev/null <<'EOSCRIPT'
echo "⚠️ No usable NICs detected. Exiting." #!/bin/bash
exit 1 set -euo pipefail
HYPERVISOR=$(systemd-detect-virt)
BROADCOM_FOUND=$(lspci | grep -i "broadcom" || true)
if [[ "$HYPERVISOR" == "kvm" && -n "$BROADCOM_FOUND" ]]; then
echo "✅ Broadcom NIC on KVM detected — applying NIC tuning"
for nic in /sys/class/net/*; do
name=$(basename "$nic")
[[ "$name" =~ ^(lo|vmbr|tap|veth|docker) ]] && continue
echo "→ Tuning $name"
/usr/sbin/ethtool -K "$name" tx off rx off tso off gso off gro off || true
/sbin/ip link set "$name" txqueuelen 10000 || true
done
else
echo "❌ Skipping NIC tuning — not KVM with Broadcom NIC"
fi fi
EOSCRIPT
echo "VM NICs detected: ${NIC_LIST[*]}" sudo chmod +x "$TUNER_SCRIPT"
echo "=== [5/5] Creating systemd service with conditional logic ===" echo "=== [5/5] Creating systemd service file ==="
SERVICE_FILE="/etc/systemd/system/conditional-nic-offload.service" SERVICE_FILE="/etc/systemd/system/conditional-nic-offload.service"
sudo tee "$SERVICE_FILE" > /dev/null <<EOF sudo tee "$SERVICE_FILE" > /dev/null <<EOF
@ -40,19 +52,7 @@ Wants=network-online.target
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=/bin/bash -c ' ExecStart=$TUNER_SCRIPT
HYPERVISOR=\$(systemd-detect-virt)
BROADCOM_FOUND=\$(lspci | grep -i "broadcom" || true)
if [[ "\$HYPERVISOR" == "kvm" && -n "\$BROADCOM_FOUND" ]]; then
echo "✅ Broadcom NIC on KVM detected — applying NIC tuning";
$(for nic in "${NIC_LIST[@]}"; do
echo "/usr/sbin/ethtool -K $nic tx off rx off tso off gso off gro off;"
echo "/sbin/ip link set $nic txqueuelen 10000;"
done)
else
echo "❌ Skipping NIC tuning — not KVM with Broadcom NIC"
fi
'
RemainAfterExit=yes RemainAfterExit=yes
[Install] [Install]
@ -65,8 +65,3 @@ sudo systemctl daemon-reload
sudo systemctl enable --now conditional-nic-offload.service sudo systemctl enable --now conditional-nic-offload.service
echo "=== ✅ VM NIC conditional optimization complete ===" echo "=== ✅ VM NIC conditional optimization complete ==="
for nic in "${NIC_LIST[@]}"; do
echo "--- $nic ---"
ethtool -k "$nic" | grep -E 'segmentation|offload|scatter|checksum' || true
ip link show "$nic" | grep txqueuelen || true
done