nut: Handle FSD properly
authorDaniel F. Dickinson <cshored@thecshore.com>
Tue, 27 Aug 2019 04:56:42 +0000 (00:56 -0400)
committerDaniel F. Dickinson <cshored@thecshore.com>
Tue, 27 Aug 2019 04:56:42 +0000 (00:56 -0400)
Make sure we force shutdown of UPS only when we should, and when
we should that shutdown happens.

Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
net/nut/files/nutshutdown

index e4fd962cd79074ec773120cd30fe8781ca2e9a61..86fe01bb957e0eaad442830e21f5f16014f451f8 100755 (executable)
@@ -4,11 +4,6 @@
 # See /LICENSE for more information.
 #
 
-. /lib/functions.sh
-
-mount -o remount,ro /overlay /overlay
-mount -o remount,ro / /
-
 stop_instance() {
        /etc/init.d/nut-server stop "$1"
 }
@@ -16,26 +11,41 @@ stop_instance() {
 shutdown_instance() {
        local cfg="$1"
        config_get driver "$cfg" driver "usbhid-ups"
-       /lib/nut/${driver} -a "$cfg" -k
+
+       # Only FSD if killpower was indicated
+       if [ -f /var/run/killpower ]; then
+               /lib/nut/"${driver}" -a "$cfg" -k
+       fi
 }
 
-[ -f /var/run/killpower ] && {
-       [ -f /etc/config/nut_server ] && {
-               config_load nut_server
+do_fsd() {
+       if [ -f /var/run/killpower ]; then
+               # Only make FS readonly if we are doing an FSD
+               mount -o remount,ro /overlay /overlay
+               mount -o remount,ro / /
 
-               # Can't FSD unless drivers are stopped
-               config_foreach stop_instance driver
-               # Driver will  wait 'offdelay' before shutting down
-               config_foreach shutdown_instance driver
-               # So this can happen
-               poweroff
-               # And just in case
-               sleep 120
-               # Uh-oh failed to poweroff UPS
-               reboot -f
-       } || {
+               . ${IPKG_INSTOOT}/lib/functions.sh
+
+               if [ -f /etc/config/nut_server ]; then
+                       config_load nut_server
+
+                       # Can't FSD unless drivers are stopped
+                       config_foreach stop_instance driver
+                       # Driver will  wait 'offdelay' before shutting down
+                       config_foreach shutdown_instance driver
+                       # So this can happen
+                       rm -f /var/run/killpower
+                       poweroff
+                       # And just in case
+                       sleep 120
+                       # Uh-oh failed to poweroff UPS
+                       reboot -f
+               else
+                       poweroff
+               fi
+       else
                poweroff
-       }
-} || {
-       poweroff
+       fi
 }
+
+do_fsd