From 83092b7b734b2d90c22910b57d5b7f62110c7675 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 2 Sep 2023 17:28:40 +0100 Subject: [PATCH] add watchdog service --- devices/gl-mt300n-v2/default.nix | 7 +++--- examples/arhcive.nix | 40 ++++---------------------------- modules/watchdog/default.nix | 24 +++++++++++++++++++ modules/watchdog/gaspode.sh | 23 ++++++++++++++++++ modules/watchdog/watchdog.nix | 12 ++++++++++ 5 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 modules/watchdog/default.nix create mode 100755 modules/watchdog/gaspode.sh create mode 100644 modules/watchdog/watchdog.nix diff --git a/devices/gl-mt300n-v2/default.nix b/devices/gl-mt300n-v2/default.nix index 0b62e9c..896c9dd 100644 --- a/devices/gl-mt300n-v2/default.nix +++ b/devices/gl-mt300n-v2/default.nix @@ -146,10 +146,6 @@ NET_RALINK_SOC="y"; SWPHY = "y"; - WATCHDOG = "y"; - RALINK_WDT = "y"; # watchdog - MT7621_WDT = "y"; # or it might be this one - GPIOLIB="y"; GPIO_MT7621 = "y"; @@ -162,6 +158,9 @@ PRINTK_TIME = "y"; } // lib.optionalAttrs (config.system.service ? vlan) { SWCONFIG = "y"; + } // lib.optionalAttrs (config.system.service ? watchdog) { + RALINK_WDT = "y"; # watchdog + MT7621_WDT = "y"; # or it might be this one }; }; }; diff --git a/examples/arhcive.nix b/examples/arhcive.nix index 11601c9..b5430a7 100644 --- a/examples/arhcive.nix +++ b/examples/arhcive.nix @@ -29,6 +29,7 @@ in rec { ../modules/network ../modules/vlan ../modules/ssh + ../modules/watchdog ]; hostname = "arhcive"; @@ -67,42 +68,9 @@ in rec { services.sshd = svc.ssh.build { }; - services.watchdog = - let - watched = with config.services ; [ sshd dhcpc ]; - spinupGrace = 60; - script = pkgs.writeAshScript "gaspode" { - runtimeInputs = [ pkgs.s6 ]; - } '' - deadline=$(expr $(date +%s) + ${toString spinupGrace}) - services=$@ - echo started feeding the dog - exec 3> ''${WATCHDOG-/dev/watchdog} - - healthy(){ - test $(date +%s) -le $deadline && return 0 - - for i in $services; do - if test "$(s6-svstat -o up /run/service/$i)" != "true" ; then - echo "service $i is down" - return 1 - fi - done - } - - while healthy ;do - sleep 10 - echo >&3 - done - echo "stopped feeding the dog" - sleep 6000 # don't want s6-rc to restart - ''; - in longrun { - name = "watchdog"; - run = - "${script} ${lib.concatStringsSep " " (builtins.map (s: s.name) watched)}"; - }; - + services.watchdog = svc.watchdog.build { + watched = with config.services ; [ sshd dhcpc ]; + }; services.resolvconf = oneshot rec { dependencies = [ services.dhcpc ]; diff --git a/modules/watchdog/default.nix b/modules/watchdog/default.nix new file mode 100644 index 0000000..738c668 --- /dev/null +++ b/modules/watchdog/default.nix @@ -0,0 +1,24 @@ +{ lib, pkgs, config, ...}: +let + inherit (lib) mkOption types; + inherit (pkgs) liminix; +in +{ + options = { + system.service.watchdog = mkOption { + type = liminix.lib.types.serviceDefn; + }; + }; + config.system.service.watchdog = liminix.callService ./watchdog.nix { + watched = mkOption { + description = "services to watch"; + type = types.listOf liminix.lib.types.service; + }; + headStart = mkOption { + description = "delay in seconds before watchdog starts checking service health"; + default = 60; + type = types.int; + }; + }; + config.kernel.config.WATCHDOG = "y"; +} diff --git a/modules/watchdog/gaspode.sh b/modules/watchdog/gaspode.sh new file mode 100755 index 0000000..e9a9ed7 --- /dev/null +++ b/modules/watchdog/gaspode.sh @@ -0,0 +1,23 @@ +#!/bin/sh +deadline=$(expr $(date +%s) + ${HEADSTART}) +services=$@ +echo started feeding the dog +exec 3> ${WATCHDOG-/dev/watchdog} + +healthy(){ + test $(date +%s) -le $deadline && return 0 + + for i in $services; do + if test "$(s6-svstat -o up /run/service/$i)" != "true" ; then + echo "service $i is down" + return 1 + fi + done +} + +while healthy ;do + sleep 10 + echo >&3 +done +echo "stopped feeding the dog" +sleep 6000 # don't want s6-rc to restart diff --git a/modules/watchdog/watchdog.nix b/modules/watchdog/watchdog.nix new file mode 100644 index 0000000..b4119f0 --- /dev/null +++ b/modules/watchdog/watchdog.nix @@ -0,0 +1,12 @@ +{ + liminix +, lib +}: +{ watched, headStart } : +let + inherit (liminix.services) longrun; +in longrun { + name = "watchdog"; + run = + "HEADSTART=${toString headStart} ${./gaspode.sh} ${lib.concatStringsSep " " (builtins.map (s: s.name) watched)}"; +}