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