diff --git a/AGENTS.md b/AGENTS.md index d786d4468..175122708 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,7 +67,7 @@ recent host Ruby (4.x), so the site is built in a `ruby:3.3` container and the | Directory | Content | |---|---| -| `packages/` | Custom OpenWrt feed; 37 `ns-*` packages | +| `packages/` | Custom OpenWrt feed: `ns-*` packages and forks of upstream packages | | `config/` | Diffconfig fragments per feature; `config/targets/.conf` for arch-specific config | | `files/` | Filesystem overlay copied verbatim into image | | `patches/` | Patches applied to upstream OpenWrt feeds | @@ -86,6 +86,7 @@ recent host Ruby (4.x), so the site is built in a `ruby:3.3` container and the - To add a package to the image, create a corresponding `config/.conf` fragment that enables it at build time. - Renovate manages external package versions via magic comments in Makefiles: `# renovate: datasource=github-tags depName=Org/Repo` - `packages/netifyd` is download-only: prebuilt per-arch binaries, filenames and hashes in `netify-dist.mk`, regenerated by `tools/netifyd-update/netifyd-update.py`. No compile step. +- `packages/ns-watchdog` selects the x86 watchdog driver in preinit (`/lib/preinit/83_ns_watchdog`) and blacklists the watchdog modules for kmodloader in `/etc/modules.conf` (postinst for the image copy, the hook rewrites the overlay copy at boot). Do not add watchdog kmods to the image outside it: the first registered device owns `/dev/watchdog`, and non-probing drivers such as `ib700wdt` take it on any hardware. - `packages/python3-nethsec/` (the `nethsec` Python library used by all `ns.*` API scripts) is vendored in this repo, not fetched from an external project — read `packages/python3-nethsec/src/nethsec/` directly rather than looking it up on GitHub. --- diff --git a/config/watchdog.conf b/config/watchdog.conf new file mode 100644 index 000000000..b93515343 --- /dev/null +++ b/config/watchdog.conf @@ -0,0 +1 @@ +CONFIG_PACKAGE_ns-watchdog=y diff --git a/docs/design/watchdog.md b/docs/design/watchdog.md new file mode 100644 index 000000000..7967ff064 --- /dev/null +++ b/docs/design/watchdog.md @@ -0,0 +1,80 @@ +--- +layout: default +title: Hardware watchdog +parent: Design +--- + +# Hardware watchdog + +On x86 the image includes the `ns-watchdog` package. +It ships the watchdog drivers and selects the one matching the hardware at boot. +procd then opens `/dev/watchdog`, sets a 30 seconds timeout and pings it every 5 seconds: +if the kernel or procd hangs, the machine resets. + +The watchdog is enabled by default, also on systems upgraded from previous releases. + +## Why a selector + +OpenWrt kmod packages load watchdog drivers from `/etc/modules-boot.d`. +Only the first device that registers gets `/dev/watchdog` (misc minor 130); +the others get `/dev/watchdogN` only and procd ignores them. + +Some drivers do not probe the hardware: `ib700wdt` always registers a device. +On a PC Engines APU with all upstream modules installed, `ib700wdt` took `/dev/watchdog` +and the real `sp5100_tco` and NCT6102 Super I/O watchdogs were left out: +procd was pinging a watchdog that could not reset the board. + +## Shipped drivers + +Only drivers that bind to a chipset or PCI device are shipped: +`i6300esb`, `iTCO_wdt`, `sp5100_tco`, plus `softdog` (used only when configured). +Super I/O drivers (`w83627hf_wdt`, `it87_wdt`, `f71808e_wdt`) are left out on purpose: +they probe shared configuration ports on every board, and the chipset watchdog covers +the same machines in most cases. `ib700wdt` and `mlx_wdt` are left out as well. + +## Boot sequence + +1. procd init loads `/etc/modules-boot.d`. Watchdog modules are skipped: + the `ns-watchdog` postinst blacklists them in `/etc/modules.conf` (a file owned by `ubox`, + edited inside a `# BEGIN ns-watchdog` / `# END ns-watchdog` block). + At this point kmodloader reads the copy in the read-only image, written at build time. +2. `/etc/preinit` runs the hooks; `/lib/preinit/83_ns_watchdog` runs after the overlay is mounted, + so it can read UCI. It first writes the blacklist block again in the overlay copy when it differs: + `/etc/modules.conf` is a conffile, saved and restored by sysupgrade and backups, + so a restore can bring back a file without the block or with an old module list. +3. The hook loads the modules with `insmod`, which ignores the blacklist, in priority order + (`i6300esb`, `iTCO_wdt`, `sp5100_tco`), and unloads the ones that registered no device. +4. procd enters its early state and opens `/dev/watchdog`. +5. `/etc/init.d/boot` runs kmodloader again on `/etc/modules.d`; the blacklist keeps it from + loading the watchdog modules that were left out. + +Without the blacklist, the first test images showed both problems: modules loaded before preinit, +and `softdog`, `iTCO_wdt` and `i6300esb` loaded again in step 5 after the selection. +On a QEMU Q35 machine `iTCO_wdt` could not even be unloaded in step 3, because its watchdog +was already running and the kernel holds a reference to the module in that case. +The hook keeps such a module, logs it and reports it as the device in use, +since it registered before any module loaded by the hook. + +Nothing has opened a watchdog device before step 4, so unloading modules in step 3 is safe. +The kernel is built with `CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED=y`: +a watchdog armed by the BIOS is pinged by the kernel until a program opens it. + +## Disabling + +- UCI: `system.@system[0].watchdog='0'`, applied at next boot. +- Kernel command line: `ns.watchdog=0`, to recover from a reset loop via the GRUB menu. +- Runtime, until reboot: `ubus call system watchdog '{"magicclose":true,"stop":true}'`. + +`system.@system[0].watchdog_module` forces a single module, e.g. `softdog`, +which is never selected automatically. + +Disabling only skips loading drivers. It does not stop a watchdog that was already running: + +- a watchdog armed by the BIOS has no driver, so the kernel cannot ping it and it resets the board; + this was already the case before `ns-watchdog`, and the same happens in failsafe mode; +- a built-in driver such as `xen_wdt` registers before init, and procd opens it before preinit. + +Loading the driver while keeping procd off it is not possible from preinit, +because procd opens `/dev/watchdog` right after it. + +See `packages/ns-watchdog/README.md` for supported hardware and limitations. diff --git a/packages/ns-watchdog/Makefile b/packages/ns-watchdog/Makefile new file mode 100644 index 000000000..95be8d81e --- /dev/null +++ b/packages/ns-watchdog/Makefile @@ -0,0 +1,73 @@ +# +# Copyright (C) 2026 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-2.0-only +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=ns-watchdog +PKG_VERSION:=0.0.3 +PKG_RELEASE:=1 + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) + +PKG_MAINTAINER:=Filippo Carletti +PKG_LICENSE:=GPL-2.0-only + +include $(INCLUDE_DIR)/package.mk + +define Package/ns-watchdog + SECTION:=base + CATEGORY:=NethSecurity + TITLE:=Hardware watchdog driver selection + URL:=https://github.com/NethServer/nethsecurity + DEPENDS:=@TARGET_x86 \ + +kmod-i6300esb-wdt \ + +kmod-itco-wdt \ + +kmod-sp5100-tco \ + +kmod-softdog + PKGARCH:=all +endef + +define Package/ns-watchdog/description + Ship x86 watchdog drivers and pick the one matching the hardware + at boot, so procd pings a watchdog that can actually reset the system. +endef + +define Build/Compile +endef + +# Blacklist the watchdog modules for kmodloader, so only the preinit hook +# loads them. /etc/modules.conf belongs to ubox: edit a marked block. +# The module list is read from the hook, which is the single source. +# This writes the copy in the read-only image, read before preinit; +# the hook rewrites the block in the overlay copy when it differs. +define Package/ns-watchdog/postinst +#!/bin/sh +conf="$${IPKG_INSTROOT}/etc/modules.conf" +hook="$${IPKG_INSTROOT}/lib/preinit/83_ns_watchdog" +sed -i '/^# BEGIN ns-watchdog$$/,/^# END ns-watchdog$$/d' "$$conf" 2>/dev/null +{ + echo "# BEGIN ns-watchdog" + for mod in $$(sed -n 's/^NS_WDT_ALL="\(.*\)"$$/\1/p' "$$hook"); do + echo "blacklist $$mod" + done + echo "# END ns-watchdog" +} >> "$$conf" +exit 0 +endef + +define Package/ns-watchdog/prerm +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ]; then + sed -i '/^# BEGIN ns-watchdog$$/,/^# END ns-watchdog$$/d' /etc/modules.conf +fi +exit 0 +endef + +define Package/ns-watchdog/install + $(INSTALL_DIR) $(1)/lib/preinit + $(INSTALL_DATA) ./files/83_ns_watchdog $(1)/lib/preinit/83_ns_watchdog +endef + +$(eval $(call BuildPackage,ns-watchdog)) diff --git a/packages/ns-watchdog/README.md b/packages/ns-watchdog/README.md new file mode 100644 index 000000000..140d277ec --- /dev/null +++ b/packages/ns-watchdog/README.md @@ -0,0 +1,114 @@ +# ns-watchdog + +Hardware watchdog support for NethSecurity on x86. + +procd opens `/dev/watchdog` at boot, sets a 30 seconds timeout and pings it every 5 seconds. +If the kernel or procd hangs, the pings stop and the watchdog resets the machine. +This package ships the watchdog drivers and picks the one that matches the hardware, +so `/dev/watchdog` is a device that can actually reset the system. + +## Supported hardware + +| Module | Hardware | +|---|---| +| `i6300esb` | Intel 6300ESB; virtual watchdog on QEMU/KVM/Proxmox | +| `iTCO_wdt` | Intel chipsets and SoCs served by `lpc_ich` (ICH, older PCH, many Atom/Celeron) | +| `sp5100_tco` | AMD chipsets (SP5100/SB800 and newer), e.g. PC Engines APU | +| `softdog` | Software watchdog, used only when configured | + +## How the driver is selected + +The first device that registers gets `/dev/watchdog`, so the result would depend on load order. +For this reason kmodloader must not load watchdog modules on its own. +The package adds a `blacklist` block between `# BEGIN ns-watchdog` and `# END ns-watchdog` to `/etc/modules.conf`. +kmodloader honours it both before preinit (`/etc/modules-boot.d`) and later in `/etc/init.d/boot` (`/etc/modules.d`); +`insmod` ignores it. + +Before preinit kmodloader reads the copy in the read-only image, which always has the block. +`/etc/init.d/boot` reads the overlay copy, which sysupgrade and backups save and restore, +so a restored file may miss the block or carry an old one. +The preinit hook writes the block again when it differs and logs it. + +The preinit hook `/lib/preinit/83_ns_watchdog` runs before procd opens the device and: + +1. writes the blacklist block again if it is missing or outdated; +2. unloads any watchdog module loaded anyway (a module whose watchdog is already running cannot be unloaded and is kept, + and it owns `/dev/watchdog`); +3. stops if the watchdog is disabled (see below); +4. if `watchdog_module` is set, loads only that module; +5. otherwise loads the modules in this order: + `i6300esb`, `iTCO_wdt`, `sp5100_tco`. + A module that registers no device is unloaded again. + The first one that registers a device owns `/dev/watchdog`. + Other devices found stay loaded as `watchdog1`, `watchdog2`, ...; + if the BIOS armed one of them, the kernel keeps pinging it. + +The choice is logged to the kernel log: + +``` +dmesg | grep ns-watchdog +``` + +Check the status: + +``` +ubus call system watchdog +ls -l /dev/watchdog* +``` + +## Configuration + +Options in the `system` section of `/etc/config/system`: + +| Option | Default | Description | +|---|---|---| +| `watchdog` | `1` | `0` disables the watchdog at next boot | +| `watchdog_module` | empty | load only this module, e.g. `softdog` | + +Disable the watchdog: + +``` +uci set system.@system[0].watchdog=0 +uci commit system +reboot +``` + +Use the software watchdog when the hardware has none: + +``` +uci set system.@system[0].watchdog_module=softdog +uci commit system +reboot +``` + +Stop the watchdog until next reboot: + +``` +ubus call system watchdog '{"magicclose":true,"stop":true}' +``` + +If the system resets in a loop before the configuration can be changed, +edit the boot entry in the GRUB menu and append `ns.watchdog=0` to the kernel command line. + +Disabling only keeps the hook from loading drivers: it does not stop a watchdog that is already running +(see Limitations). + +## Limitations + +- `iTCO_wdt` on Intel 100-series and newer PCH needs `i2c-i801`, which is not in the image. +- Super I/O watchdogs (`w83627hf_wdt`, `it87_wdt`, `f71808e_wdt`), `ib700wdt` and `mlx_wdt` + are not shipped. Super I/O drivers poke shared configuration ports on any board, + and `ib700wdt` always registers a device without probing the hardware. + If one of them is installed manually, the preinit hook unloads it unless `watchdog_module` names it. +- A watchdog armed by the BIOS through an unsupported chip (e.g. the NCT6102 Super I/O on PC Engines APU) + is not pinged by anyone: disable it in the BIOS, or the board resets after its timeout. +- When the watchdog is disabled, and in failsafe mode, no driver is loaded. + A watchdog armed by the BIOS then has nobody pinging it and resets the board after its timeout, + as it did before this package: disable it in the BIOS. +- A watchdog driver built into the kernel, such as `xen_wdt` on Xen guests, is opened by procd + before preinit runs: `watchdog=0` and `ns.watchdog=0` do not disable it. + Use `ubus call system watchdog '{"magicclose":true,"stop":true}'` after each boot. +- `softdog` runs inside the kernel: it does not catch hard kernel lockups. +- VMware and Hyper-V have no supported virtual watchdog. + On Proxmox, add a watchdog device to the VM, e.g. `watchdog: model=i6300esb,action=reset`. +- The kernel is built without `CONFIG_WATCHDOG_SYSFS`, so `/sys/class/watchdog/*` has no attributes. diff --git a/packages/ns-watchdog/files/83_ns_watchdog b/packages/ns-watchdog/files/83_ns_watchdog new file mode 100644 index 000000000..23148fc78 --- /dev/null +++ b/packages/ns-watchdog/files/83_ns_watchdog @@ -0,0 +1,157 @@ +# shellcheck shell=busybox +# +# Copyright (C) 2026 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-2.0-only +# +# Select the hardware watchdog driver before procd opens /dev/watchdog. +# +# Only the first registered device gets /dev/watchdog, so kmodloader must +# not load watchdog modules on its own: NS_WDT_ALL is blacklisted in +# /etc/modules.conf. kmodloader honours the blacklist both for +# /etc/modules-boot.d (before preinit, reading the copy in the read-only +# image, written by the package postinst) and for /etc/modules.d (from +# /etc/init.d/boot, reading the overlay copy), while insmod ignores it. +# /etc/modules.conf is a conffile kept by sysupgrade and backups, so a +# restore can bring back a copy without the block or with an old one: +# this hook writes the block again when it differs. +# This hook loads the modules in priority order and keeps only the ones +# that found hardware. procd opens /dev/watchdog later, when it enters its +# early state. +# +# Configuration: +# system.@system[0].watchdog='0' disable the watchdog +# system.@system[0].watchdog_module='' load only this module +# kernel cmdline ns.watchdog=0 disable the watchdog +# + +# Modules blacklisted for kmodloader (see Makefile postinst, which reads +# this line) and unloaded before selection if loaded anyway. Besides the +# shipped ones, this lists drivers not shipped on purpose: if an admin +# installs one, it would take /dev/watchdog at boot. They are loaded only +# if named in watchdog_module. +# Unload order matters: iTCO_vendor_support is used by iTCO_wdt +NS_WDT_ALL="i6300esb iTCO_wdt iTCO_vendor_support sp5100_tco softdog ib700wdt w83627hf_wdt it87_wdt f71808e_wdt mlx_wdt" +# Auto-detection priority; softdog is never picked automatically +NS_WDT_AUTO="i6300esb iTCO_wdt sp5100_tco" + +NS_WDT_CONF="/etc/modules.conf" + +ns_wdt_log() { + echo "ns-watchdog: $*" > /dev/kmsg +} + +ns_wdt_count() { + # shellcheck disable=SC2012 + ls /sys/class/watchdog 2>/dev/null | wc -l +} + +ns_wdt_load() { + [ "$1" = "iTCO_wdt" ] && insmod iTCO_vendor_support >/dev/null 2>&1 + insmod "$1" >/dev/null 2>&1 +} + +ns_wdt_unload() { + rmmod "$1" >/dev/null 2>&1 + [ "$1" = "iTCO_wdt" ] && rmmod iTCO_vendor_support >/dev/null 2>&1 + return 0 +} + +ns_wdt_blacklist() { + local mod block="# BEGIN ns-watchdog" + + for mod in $NS_WDT_ALL; do + block="$block +blacklist $mod" + done + block="$block +# END ns-watchdog" + + [ "$(sed -n '/^# BEGIN ns-watchdog$/,/^# END ns-watchdog$/p' "$NS_WDT_CONF" 2>/dev/null)" = "$block" ] && return 0 + sed -i '/^# BEGIN ns-watchdog$/,/^# END ns-watchdog$/d' "$NS_WDT_CONF" 2>/dev/null + echo "$block" >> "$NS_WDT_CONF" + ns_wdt_log "restored the modules blacklist in $NS_WDT_CONF" +} + +ns_wdt_disabled() { + ns_wdt_log "disabled by $1" + [ -n "$kept" ] && ns_wdt_log "$kept is still loaded, procd will use it" + return 0 +} + +ns_watchdog_select() { + local mod before kept="" chosen="" + + [ "$FAILSAFE" = "true" ] && return 0 + + ns_wdt_blacklist + + # Safety net: modules should not be loaded here, thanks to the blacklist. + # A module whose watchdog is running cannot be unloaded: keep it. + # The first one kept was loaded before any other, so it owns + # /dev/watchdog. + for mod in $NS_WDT_ALL; do + [ -d "/sys/module/$mod" ] || continue + # Still in use while iTCO_wdt is loaded + [ "$mod" = "iTCO_vendor_support" ] && [ -d /sys/module/iTCO_wdt ] && continue + if ! rmmod "$mod" >/dev/null 2>&1; then + ns_wdt_log "cannot unload $mod, keeping it" + [ -z "$kept" ] && kept="$mod" + fi + done + + case " $(cat /proc/cmdline) " in + *" ns.watchdog=0 "*) + ns_wdt_disabled "kernel cmdline" + return 0 + ;; + esac + + if [ "$(uci -q get system.@system[0].watchdog)" = "0" ]; then + ns_wdt_disabled "configuration" + return 0 + fi + + mod="$(uci -q get system.@system[0].watchdog_module)" + if [ -n "$mod" ]; then + if [ -d "/sys/module/$mod" ]; then + ns_wdt_log "using $mod (configured, already loaded)" + else + before="$(ns_wdt_count)" + ns_wdt_load "$mod" + if [ "$(ns_wdt_count)" -gt "$before" ]; then + ns_wdt_log "using $mod (configured)" + else + ns_wdt_unload "$mod" + ns_wdt_log "configured module $mod found no device" + fi + fi + if [ -n "$kept" ] && [ "$kept" != "$mod" ]; then + ns_wdt_log "$kept is still loaded, procd will use it" + fi + return 0 + fi + + chosen="$kept" + for mod in $NS_WDT_AUTO; do + [ -d "/sys/module/$mod" ] && continue + before="$(ns_wdt_count)" + ns_wdt_load "$mod" + if [ "$(ns_wdt_count)" -gt "$before" ]; then + if [ -z "$chosen" ]; then + chosen="$mod" + else + ns_wdt_log "keeping $mod as secondary device" + fi + else + ns_wdt_unload "$mod" + fi + done + + if [ -n "$chosen" ]; then + ns_wdt_log "using $chosen" + else + ns_wdt_log "no hardware watchdog found" + fi +} + +boot_hook_add preinit_main ns_watchdog_select