diff --git a/devices/gl-mt300n-v2/default.nix b/devices/gl-mt300n-v2/default.nix index 0b62e9c2..896c9dd9 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 11601c94..b5430a74 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 00000000..738c6686 --- /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 00000000..e9a9ed7b --- /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 00000000..b4119f01 --- /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)}"; +}