Files
leleka/scripts/ops/server-hang-autoreboot.sh
T

118 lines
5.6 KiB
Bash

#!/usr/bin/env bash
# rnr5090: make the box reboot itself instead of hanging when the root NVMe stops answering.
# Owner-run (changes system settings). Default is a dry run that only prints the plan.
#
# sudo bash server-hang-autoreboot.sh # print the plan
# sudo bash server-hang-autoreboot.sh --autoreboot # step 1: panic-on-hung-task + auto reboot (+ hardware watchdog if present)
# sudo bash server-hang-autoreboot.sh --swapoff # step 2: no swap (comments the swap line in /etc/fstab, keeps the partition)
# sudo bash server-hang-autoreboot.sh --nvme-params # step 3: kernel params for the NVMe (APST off, ASPM off); needs a reboot
#
# Why: 2026-10-28/29/30 the root NVMe (Lexar NM610PRO) stopped answering for minutes ("I/O tag N timeout, aborting",
# "disable controller"; fsck blocked >122 s at boot). The kernel stays alive (ping answers) but sshd cannot read any
# file, so the machine is unusable until someone toggles the power.
set -euo pipefail
want_auto=0; want_swap=0; want_nvme=0; want_wd=0
for a in "$@"; do
case "$a" in
--autoreboot) want_auto=1 ;;
--swapoff) want_swap=1 ;;
--nvme-params) want_nvme=1 ;;
--watchdog-boot) want_wd=1 ;;
*) echo "unknown argument: $a" >&2; exit 2 ;;
esac
done
if [ "$(id -u)" -ne 0 ]; then echo "run as root (sudo)" >&2; exit 1; fi
say() { printf '%s\n' "$*"; }
if [ $((want_auto + want_swap + want_nvme + want_wd)) -eq 0 ]; then
say "DRY RUN. Nothing is changed. Steps (run them in this order, one at a time):"
say " --autoreboot /etc/sysctl.d/90-leleka-hang.conf: kernel.hung_task_panic=1, hung_task_timeout_secs=120, kernel.panic=10, kernel.panic_on_oops=1;"
say " disables kdump-tools (a crash kernel writing a vmcore to the stalled disk would hang the reboot);"
say " loads the AMD hardware watchdog (sp5100_tco) if the board exposes one and sets systemd RuntimeWatchdogSec=30."
say " --swapoff swapoff -a and comments the swap line in /etc/fstab (partition nvme0n1p5 is left alone)."
say " --nvme-params adds nvme_core.default_ps_max_latency_us=0 pcie_aspm=off to GRUB_CMDLINE_LINUX_DEFAULT, update-grub; reboot to apply."
say "Current:"
sysctl kernel.panic kernel.hung_task_panic kernel.hung_task_timeout_secs kernel.panic_on_oops || true
ls -l /dev/watchdog* 2>&1 | head -2 || true
swapon --show || true
exit 0
fi
if [ "$want_auto" -eq 1 ]; then
install -d /etc/sysctl.d
cat > /etc/sysctl.d/90-leleka-hang.conf <<'EOF'
# a task blocked in D state for 120 s (the stalled NVMe) panics the kernel, which reboots 10 s later
kernel.hung_task_panic = 1
kernel.hung_task_timeout_secs = 120
kernel.panic = 10
kernel.panic_on_oops = 1
EOF
sysctl --system >/dev/null
say "sysctl: hung_task_panic=1, panic=10 applied"
if systemctl is-enabled kdump-tools >/dev/null 2>&1 || systemctl is-active kdump-tools >/dev/null 2>&1; then
systemctl disable --now kdump-tools || true
say "kdump-tools disabled (so a panic reboots instead of dumping to the stalled disk)"
fi
if modprobe sp5100_tco 2>/dev/null && ls /dev/watchdog0 >/dev/null 2>&1; then
echo sp5100_tco > /etc/modules-load.d/sp5100_tco.conf
install -d /etc/systemd/system.conf.d
cat > /etc/systemd/system.conf.d/90-leleka-watchdog.conf <<'EOF'
[Manager]
RuntimeWatchdogSec=30
RebootWatchdogSec=2min
EOF
systemctl daemon-reexec
say "hardware watchdog: /dev/watchdog0 present, systemd RuntimeWatchdogSec=30"
else
say "no hardware watchdog available (sp5100_tco did not load or /dev/watchdog0 is missing); only the panic-on-hung-task path is active."
say " if the BIOS has a watchdog / 'SP5100 TCO' option, enable it and rerun this step."
fi
fi
if [ "$want_swap" -eq 1 ]; then
swapoff -a
[ -e /etc/fstab.pre-leleka-noswap ] || cp /etc/fstab /etc/fstab.pre-leleka-noswap
sed -i -E 's|^([^#].*[[:space:]]swap[[:space:]].*)$|# \1 # disabled by server-hang-autoreboot.sh|' /etc/fstab
say "swap off; fstab line commented (backup /etc/fstab.pre-leleka-noswap)"
say "memory note: without swap a memory shortage ends in the OOM killer instead of a stall; with 121 GB RAM and our ~110 GB resident, keep an eye on MemAvailable."
fi
if [ "$want_nvme" -eq 1 ]; then
f=/etc/default/grub
[ -e /etc/default/grub.pre-leleka-nvme ] || cp "$f" /etc/default/grub.pre-leleka-nvme
if grep -q 'nvme_core.default_ps_max_latency_us' "$f"; then
say "grub already has nvme_core.default_ps_max_latency_us"
else
sed -i -E 's|^(GRUB_CMDLINE_LINUX_DEFAULT=")(.*)(")|\1\2 nvme_core.default_ps_max_latency_us=0 pcie_aspm=off\3|' "$f"
sed -i -E 's|^(GRUB_CMDLINE_LINUX_DEFAULT=") |\1|' "$f"
fi
update-grub
say "grub updated (backup /etc/default/grub.pre-leleka-nvme). Reboot to apply; check: cat /proc/cmdline"
fi
if [ "$want_wd" -eq 1 ]; then
# Ubuntu deny-lists every watchdog module and systemd-modules-load honours that, so modules-load.d does nothing.
# Load it from a unit instead (explicit modprobe works) and re-exec systemd so it opens /dev/watchdog0.
rm -f /etc/modules-load.d/sp5100_tco.conf
cat > /etc/systemd/system/leleka-watchdog-load.service <<'EOF2'
[Unit]
Description=Load the AMD SP5100 TCO hardware watchdog (Ubuntu deny-lists it) and hand it to systemd
DefaultDependencies=no
After=systemd-modules-load.service
Before=sysinit.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/modprobe sp5100_tco
ExecStartPost=/bin/systemctl daemon-reexec
[Install]
WantedBy=sysinit.target
EOF2
systemctl daemon-reload
systemctl enable leleka-watchdog-load.service
if ! ls /dev/watchdog0 >/dev/null 2>&1; then systemctl start leleka-watchdog-load.service; fi
say "watchdog loader enabled; /dev/watchdog0: $(ls /dev/watchdog0 2>&1)"
fi