added changes - detect if underlying hardware is broadcom and apply different fixes if so.

This commit is contained in:
crowetic 2025-04-16 21:21:25 +00:00
parent efd0461813
commit d17e375da4

View File

@ -5,7 +5,7 @@ set -euo pipefail
echo "=== [1/5] Updating system and installing tools ===" echo "=== [1/5] Updating system and installing tools ==="
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 sudo apt install -y qemu-guest-agent util-linux haveged ethtool lshw dmidecode
echo "=== [2/5] Enabling qemu-guest-agent ===" echo "=== [2/5] Enabling qemu-guest-agent ==="
sudo systemctl enable --now qemu-guest-agent sudo systemctl enable --now qemu-guest-agent
@ -13,7 +13,7 @@ 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 network interfaces ===" echo "=== [4/5] Detecting VM NICs ==="
NIC_LIST=() NIC_LIST=()
for nic in /sys/class/net/*; do for nic in /sys/class/net/*; do
nicname=$(basename "$nic") nicname=$(basename "$nic")
@ -29,33 +29,44 @@ fi
echo "VM NICs detected: ${NIC_LIST[*]}" echo "VM NICs detected: ${NIC_LIST[*]}"
echo "=== [5/5] Creating persistent systemd service to disable offloads ===" echo "=== [5/5] Creating systemd service with conditional logic ==="
SERVICE_FILE="/etc/systemd/system/disable-vm-nic-offloads.service" SERVICE_FILE="/etc/systemd/system/conditional-nic-offload.service"
{ sudo tee "$SERVICE_FILE" > /dev/null <<EOF
echo "[Unit]" [Unit]
echo "Description=Disable all VM NIC offloads at boot" Description=Disable NIC offloads only if KVM + Broadcom detected
echo "After=network.target" After=network-online.target
echo "" Wants=network-online.target
echo "[Service]"
echo "Type=oneshot"
for nic in "${NIC_LIST[@]}"; do
echo "ExecStart=/usr/sbin/ethtool -K $nic tx off rx off tso off gso off gro off"
echo "ExecStart=/sbin/ip link set $nic txqueuelen 10000"
done
echo "RemainAfterExit=yes"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
} | sudo tee "$SERVICE_FILE" > /dev/null
echo "=== Enabling and starting NIC offload disable service ===" [Service]
Type=oneshot
ExecStart=/bin/bash -c '
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
[Install]
WantedBy=multi-user.target
EOF
echo "=== Reloading systemd and enabling service ==="
sudo systemctl daemon-reexec
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now disable-vm-nic-offloads.service sudo systemctl enable --now conditional-nic-offload.service
echo "=== ✅ VM NIC setup complete ===" echo "=== ✅ VM NIC conditional optimization complete ==="
for nic in "${NIC_LIST[@]}"; do for nic in "${NIC_LIST[@]}"; do
echo "--- $nic ---" echo "--- $nic ---"
ethtool -k "$nic" | grep -E 'segmentation|offload|scatter|checksum' ethtool -k "$nic" | grep -E 'segmentation|offload|scatter|checksum' || true
ip link show "$nic" | grep txqueuelen ip link show "$nic" | grep txqueuelen || true
done done