From 9b1a716aa82456c17da3271f3386a3908d63e356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ryan4yin=20=7C=20=E4=BA=8C=E8=8A=B1?= Date: Tue, 17 Nov 2026 21:04:51 +0800 Subject: [PATCH] feat(nixos): capture kernel lockups as panics on headless servers (#529) Assisted-by: DeepSeek Harness (claude-opus-5-5) --- hosts/idols-aquamarine/default.nix | 1 + lib/genVmHostModule.nix | 1 + modules/nixos/headless-recovery.nix | 26 ++++++++++++++++++++++++++ modules/nixos/watchdog.nix | 2 ++ 4 files changed, 30 insertions(+) create mode 100644 modules/nixos/headless-recovery.nix diff --git a/hosts/idols-aquamarine/default.nix b/hosts/idols-aquamarine/default.nix index 41bf1710..ecebf4ed 100644 --- a/hosts/idols-aquamarine/default.nix +++ b/hosts/idols-aquamarine/default.nix @@ -19,6 +19,7 @@ in { imports = [ disko.nixosModules.default + ../../modules/nixos/headless-recovery.nix ../../modules/nixos/power.nix ../../modules/nixos/watchdog.nix # disks diff --git a/lib/genVmHostModule.nix b/lib/genVmHostModule.nix index a98511de..60fc276e 100644 --- a/lib/genVmHostModule.nix +++ b/lib/genVmHostModule.nix @@ -12,6 +12,7 @@ in { # CPU policy belongs to physical hosts; guests do not control hardware frequency. imports = [ + ../modules/nixos/headless-recovery.nix ../modules/nixos/power.nix ../modules/nixos/watchdog.nix ]; diff --git a/modules/nixos/headless-recovery.nix b/modules/nixos/headless-recovery.nix new file mode 100644 index 00000000..69daac1e --- /dev/null +++ b/modules/nixos/headless-recovery.nix @@ -0,0 +1,26 @@ +{ pkgs, ... }: +{ + # Headless physical servers: turn silent kernel failures into panics, so efi_pstore keeps the + # dmesg and kernel.panic reboots. Desktops stay out: a GPU driver oops would drop the session. + # hung_task_panic stays off: a stalled USB disk would trip it. + # Armed 5 minutes after boot, so an oops during boot cannot loop panic-reboots before SSH is up. + systemd.services.panic-on-lockup = { + description = "Panic on kernel oops, lockup or RCU stall"; + path = [ pkgs.procps ]; + serviceConfig.Type = "oneshot"; + script = '' + sysctl -w kernel.panic_on_oops=1 + sysctl -w kernel.softlockup_panic=1 + sysctl -w kernel.hardlockup_panic=1 + sysctl -w kernel.panic_on_rcu_stall=1 + ''; + }; + systemd.timers.panic-on-lockup = { + wantedBy = [ "timers.target" ]; + timerConfig.OnBootSec = "5min"; + }; + # Keep the panic text visible on an attached monitor. + boot.kernelParams = [ "consoleblank=0" ]; + # A failed mount continues the boot with SSH instead of waiting on a console sulogin. + systemd.enableEmergencyMode = false; +} diff --git a/modules/nixos/watchdog.nix b/modules/nixos/watchdog.nix index dc750630..79a758e0 100644 --- a/modules/nixos/watchdog.nix +++ b/modules/nixos/watchdog.nix @@ -3,4 +3,6 @@ # a host without a hardware watchdog only logs that /dev/watchdog is missing. systemd.settings.Manager.RuntimeWatchdogSec = "30s"; boot.kernel.sysctl."kernel.panic" = 20; # seconds; the default 0 hangs forever + # A hard reset keeps only what journald fsynced; the default 5min loses the lead-up to a freeze. + services.journald.settings.Journal.SyncIntervalSec = "5s"; }