Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<arch>.conf` for arch-specific config |
| `files/` | Filesystem overlay copied verbatim into image |
| `patches/` | Patches applied to upstream OpenWrt feeds |
Expand All @@ -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/<feature>.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.

---
Expand Down
1 change: 1 addition & 0 deletions config/watchdog.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_PACKAGE_ns-watchdog=y
80 changes: 80 additions & 0 deletions docs/design/watchdog.md
Original file line number Diff line number Diff line change
@@ -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.
73 changes: 73 additions & 0 deletions packages/ns-watchdog/Makefile
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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))
114 changes: 114 additions & 0 deletions packages/ns-watchdog/README.md
Original file line number Diff line number Diff line change
@@ -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.
Loading
Loading