Harald Hoyer 460d2c
From 39d90012a652fa4d9d2534d0168ddad8cacec248 Mon Sep 17 00:00:00 2001
Harald Hoyer 460d2c
From: Kairui Song <kasong@redhat.com>
Harald Hoyer 460d2c
Date: Thu, 10 Sep 2020 02:18:02 +0800
Harald Hoyer 460d2c
Subject: [PATCH] 04watchdog: split the watchdog module install
Harald Hoyer 460d2c
Harald Hoyer 460d2c
In some cases, user only want to include the watchdog module, not the
Harald Hoyer 460d2c
wdctl or any other userspace helper. For example, systemd have a
Harald Hoyer 460d2c
RebootWatchdogSec option that use watchdog to prevent reboot hangs. And
Harald Hoyer 460d2c
it can help prevent machines hangs when reboot directly within the
Harald Hoyer 460d2c
initramfs stage. So split the module installation to a standlone module.
Harald Hoyer 460d2c
Harald Hoyer 460d2c
Also when watchdog-module get included, install driver for all loaded
Harald Hoyer 460d2c
watchdog instaed of only install driver for active watchdog. Both
Harald Hoyer 460d2c
watchdog and watchdog-module return 255 in check(), so it's enabled only
Harald Hoyer 460d2c
when manually included, the watchdog may get configured/activated later.
Harald Hoyer 460d2c
Harald Hoyer 460d2c
Signed-off-by: Kairui Song <kasong@redhat.com>
Harald Hoyer 460d2c
---
Harald Hoyer 460d2c
 modules.d/04watchdog-modules/module-setup.sh | 61 ++++++++++++++++++++++++++++
Harald Hoyer 460d2c
 modules.d/04watchdog/module-setup.sh         | 50 +----------------------
Harald Hoyer 460d2c
 2 files changed, 63 insertions(+), 48 deletions(-)
Harald Hoyer 460d2c
Harald Hoyer 460d2c
diff --git a/modules.d/04watchdog-modules/module-setup.sh b/modules.d/04watchdog-modules/module-setup.sh
Harald Hoyer 460d2c
new file mode 100755
Harald Hoyer 460d2c
index 00000000..5fbd769b
Harald Hoyer 460d2c
--- /dev/null
Harald Hoyer 460d2c
+++ b/modules.d/04watchdog-modules/module-setup.sh
Harald Hoyer 460d2c
@@ -0,0 +1,61 @@
Harald Hoyer 460d2c
+#!/bin/bash
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+# called by dracut
Harald Hoyer 460d2c
+check() {
Harald Hoyer 460d2c
+    return 255
Harald Hoyer 460d2c
+}
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+# called by dracut
Harald Hoyer 460d2c
+depends() {
Harald Hoyer 460d2c
+    return 0
Harald Hoyer 460d2c
+}
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+# called by dracut
Harald Hoyer 460d2c
+install() {
Harald Hoyer 460d2c
+    return 0
Harald Hoyer 460d2c
+}
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+installkernel() {
Harald Hoyer 460d2c
+    local -A _drivers
Harald Hoyer 460d2c
+    local _alldrivers _wdtdrv _wdtppath _dir
Harald Hoyer 460d2c
+    [[ -d /sys/class/watchdog/ ]] || return
Harald Hoyer 460d2c
+    for _dir in /sys/class/watchdog/*; do
Harald Hoyer 460d2c
+        [[ -d "$_dir" ]] || continue
Harald Hoyer 460d2c
+        [[ -f "$_dir/state" ]] || continue
Harald Hoyer 460d2c
+        # device/modalias will return driver of this device
Harald Hoyer 460d2c
+        _wdtdrv=$(< "$_dir/device/modalias")
Harald Hoyer 460d2c
+        # There can be more than one module represented by same
Harald Hoyer 460d2c
+        # modalias. Currently load all of them.
Harald Hoyer 460d2c
+        # TODO: Need to find a way to avoid any unwanted module
Harald Hoyer 460d2c
+        # represented by modalias
Harald Hoyer 460d2c
+        _wdtdrv=$(modprobe --set-version "$kernel" -R $_wdtdrv 2>/dev/null)
Harald Hoyer 460d2c
+        if [[ $_wdtdrv ]]; then
Harald Hoyer 460d2c
+            instmods $_wdtdrv
Harald Hoyer 460d2c
+            for i in $_wdtdrv; do
Harald Hoyer 460d2c
+                _drivers[$i]=1
Harald Hoyer 460d2c
+            done
Harald Hoyer 460d2c
+        fi
Harald Hoyer 460d2c
+        # however in some cases, we also need to check that if there is
Harald Hoyer 460d2c
+        # a specific driver for the parent bus/device.  In such cases
Harald Hoyer 460d2c
+        # we also need to enable driver for parent bus/device.
Harald Hoyer 460d2c
+        _wdtppath=$(readlink -f "$_dir/device")
Harald Hoyer 460d2c
+        while [[ -d "$_wdtppath" ]] && [[ "$_wdtppath" != "/sys" ]]; do
Harald Hoyer 460d2c
+            _wdtppath=$(readlink -f "$_wdtppath/..")
Harald Hoyer 460d2c
+            [[ -f "$_wdtppath/modalias" ]] || continue
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+            _wdtdrv=$(< "$_wdtppath/modalias")
Harald Hoyer 460d2c
+            _wdtdrv=$(modprobe --set-version "$kernel" -R $_wdtdrv 2>/dev/null)
Harald Hoyer 460d2c
+            if [[ $_wdtdrv ]]; then
Harald Hoyer 460d2c
+                instmods $_wdtdrv
Harald Hoyer 460d2c
+                for i in $_wdtdrv; do
Harald Hoyer 460d2c
+                    _drivers[$i]=1
Harald Hoyer 460d2c
+                done
Harald Hoyer 460d2c
+            fi
Harald Hoyer 460d2c
+        done
Harald Hoyer 460d2c
+    done
Harald Hoyer 460d2c
+    # ensure that watchdog module is loaded as early as possible
Harald Hoyer 460d2c
+    _alldrivers="${!_drivers[*]}"
Harald Hoyer 460d2c
+    [[ $_alldrivers ]] && echo "rd.driver.pre=${_alldrivers// /,}" > ${initdir}/etc/cmdline.d/00-watchdog.conf
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
+    return 0
Harald Hoyer 460d2c
+}
Harald Hoyer 460d2c
diff --git a/modules.d/04watchdog/module-setup.sh b/modules.d/04watchdog/module-setup.sh
Harald Hoyer 460d2c
index 7566d651..15bcd897 100755
Harald Hoyer 460d2c
--- a/modules.d/04watchdog/module-setup.sh
Harald Hoyer 460d2c
+++ b/modules.d/04watchdog/module-setup.sh
Harald Hoyer 460d2c
@@ -7,7 +7,7 @@ check() {
Harald Hoyer 460d2c
 
Harald Hoyer 460d2c
 # called by dracut
Harald Hoyer 460d2c
 depends() {
Harald Hoyer 460d2c
-    return 0
Harald Hoyer 460d2c
+    return "watchdog-modules"
Harald Hoyer 460d2c
 }
Harald Hoyer 460d2c
 
Harald Hoyer 460d2c
 # called by dracut
Harald Hoyer 460d2c
@@ -27,53 +27,7 @@ install() {
Harald Hoyer 460d2c
         inst_hook cleanup   00 "$moddir/watchdog.sh"
Harald Hoyer 460d2c
         inst_hook cleanup   99 "$moddir/watchdog.sh"
Harald Hoyer 460d2c
     fi
Harald Hoyer 460d2c
+
Harald Hoyer 460d2c
     inst_hook emergency 02 "$moddir/watchdog-stop.sh"
Harald Hoyer 460d2c
     inst_multiple -o wdctl
Harald Hoyer 460d2c
 }
Harald Hoyer 460d2c
-
Harald Hoyer 460d2c
-installkernel() {
Harald Hoyer 460d2c
-    local -A _drivers
Harald Hoyer 460d2c
-    local _alldrivers _active _wdtdrv _wdtppath _dir
Harald Hoyer 460d2c
-    [[ -d /sys/class/watchdog/ ]] || return
Harald Hoyer 460d2c
-    for _dir in /sys/class/watchdog/*; do
Harald Hoyer 460d2c
-        [[ -d "$_dir" ]] || continue
Harald Hoyer 460d2c
-        [[ -f "$_dir/state" ]] || continue
Harald Hoyer 460d2c
-        _active=$(< "$_dir/state")
Harald Hoyer 460d2c
-        ! [[ $hostonly ]] || [[ "$_active" =  "active" ]] || continue
Harald Hoyer 460d2c
-        # device/modalias will return driver of this device
Harald Hoyer 460d2c
-        _wdtdrv=$(< "$_dir/device/modalias")
Harald Hoyer 460d2c
-        # There can be more than one module represented by same
Harald Hoyer 460d2c
-        # modalias. Currently load all of them.
Harald Hoyer 460d2c
-        # TODO: Need to find a way to avoid any unwanted module
Harald Hoyer 460d2c
-        # represented by modalias
Harald Hoyer 460d2c
-        _wdtdrv=$(modprobe --set-version "$kernel" -R $_wdtdrv 2>/dev/null)
Harald Hoyer 460d2c
-        if [[ $_wdtdrv ]]; then
Harald Hoyer 460d2c
-            instmods $_wdtdrv
Harald Hoyer 460d2c
-            for i in $_wdtdrv; do
Harald Hoyer 460d2c
-                _drivers[$i]=1
Harald Hoyer 460d2c
-            done
Harald Hoyer 460d2c
-        fi
Harald Hoyer 460d2c
-        # however in some cases, we also need to check that if there is
Harald Hoyer 460d2c
-        # a specific driver for the parent bus/device.  In such cases
Harald Hoyer 460d2c
-        # we also need to enable driver for parent bus/device.
Harald Hoyer 460d2c
-        _wdtppath=$(readlink -f "$_dir/device")
Harald Hoyer 460d2c
-        while [[ -d "$_wdtppath" ]] && [[ "$_wdtppath" != "/sys" ]]; do
Harald Hoyer 460d2c
-            _wdtppath=$(readlink -f "$_wdtppath/..")
Harald Hoyer 460d2c
-            [[ -f "$_wdtppath/modalias" ]] || continue
Harald Hoyer 460d2c
-
Harald Hoyer 460d2c
-            _wdtdrv=$(< "$_wdtppath/modalias")
Harald Hoyer 460d2c
-            _wdtdrv=$(modprobe --set-version "$kernel" -R $_wdtdrv 2>/dev/null)
Harald Hoyer 460d2c
-            if [[ $_wdtdrv ]]; then
Harald Hoyer 460d2c
-                instmods $_wdtdrv
Harald Hoyer 460d2c
-                for i in $_wdtdrv; do
Harald Hoyer 460d2c
-                    _drivers[$i]=1
Harald Hoyer 460d2c
-                done
Harald Hoyer 460d2c
-            fi
Harald Hoyer 460d2c
-        done
Harald Hoyer 460d2c
-    done
Harald Hoyer 460d2c
-    # ensure that watchdog module is loaded as early as possible
Harald Hoyer 460d2c
-    _alldrivers="${!_drivers[*]}"
Harald Hoyer 460d2c
-    [[ $_alldrivers ]] && echo "rd.driver.pre=${_alldrivers// /,}" > ${initdir}/etc/cmdline.d/00-watchdog.conf
Harald Hoyer 460d2c
-
Harald Hoyer 460d2c
-    return 0
Harald Hoyer 460d2c
-}
Harald Hoyer 460d2c