Add support for bmcrebootoff and bmcrebooton

In the view of having the system available in fail state, it would be a
necessity to have BMC stay in error state and not take reset due to any
recovery policy that is specified in the service files. This patch puts the
override for reboot, poweroff and halt requests to make those, a NO-OP

If the user needs to reboot the BMC, then "bmcrebooton" must be executed prior
to doing the reset.

Tested:

root@witherspoon:/tmp# cd /run/systemd/system/
root@witherspoon:/run/systemd/system# ls

root@witherspoon:/run/systemd/system# /tmp/obmcutil bmcrebootoff

root@witherspoon:/run/systemd/system# ls -l
drwxr-xr-x    2 root     root            60 Nov 13 08:32 halt.target.d
drwxr-xr-x    2 root     root            60 Nov 13 08:32 poweroff.target.d
drwxr-xr-x    2 root     root            60 Nov 13 08:32 reboot.target.d
------------------------------------------------------------------

root@witherspoon:/run/systemd/system# uptime
 08:33:41 up  2:19,  load average: 0.33, 0.40, 0.31

root@witherspoon:/run/systemd/system# reboot

root@witherspoon:/run/systemd/system# uptime
 08:35:21 up  2:21,  load average: 0.20, 0.31, 0.28

root@witherspoon:/run/systemd/system#
------------------------------------------------------------------
root@witherspoon:/run/systemd/system# halt

root@witherspoon:/run/systemd/system# uptime
 08:40:16 up  2:26,  load average: 0.23, 0.19, 0.23

root@witherspoon:/run/systemd/system#
-----------------------------------------------------------------
root@witherspoon:/run/systemd/system# /tmp/obmcutil bmcrebooton

root@witherspoon:/run/systemd/system# ls -l halt.target.d/*
ls: halt.target.d/*: No such file or directory

root@witherspoon:/run/systemd/system#
-----------------------------------------------------------------
root@witherspoon:/etc/systemd/system# /tmp/obmcutil recoveryoff
Created symlink /etc/systemd/system/obmc-host-timeout@0.target → /dev/null.
Created symlink /etc/systemd/system/obmc-host-crash@0.target → /dev/null.

root@witherspoon:/etc/systemd/system# ls -l /run/systemd/system/
halt.target.d/     poweroff.target.d/ reboot.target.d/

root@witherspoon:/etc/systemd/system# ls -l
/run/systemd/system/halt.target.d/reboot-guard.conf
-rw-r--r--    1 root     root            29 Nov 13 08:41
/run/systemd/system/halt.target.d/reboot-guard.conf
root@witherspoon:/etc/systemd/system#
-----------------------------------------------------------------
root@witherspoon:/etc/systemd/system# /tmp/obmcutil recoveryon
Removed /etc/systemd/system/obmc-host-timeout@0.target.
Removed /etc/systemd/system/obmc-host-crash@0.target.

root@witherspoon:/etc/systemd/system#

root@witherspoon:/run/systemd/system# ls -l halt.target.d/reboot-guard.conf
ls: halt.target.d/reboot-guard.conf: No such file or directory
-----------------------------------------------------------------

Change-Id: Ic43eeb8517859ae6dbf3b83d053b33758e5ae1b0
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/obmcutil b/obmcutil
index 24bce75..0afa678 100755
--- a/obmcutil
+++ b/obmcutil
@@ -3,7 +3,8 @@
 set -euo pipefail
 
 OPTS="bmcstate,bootprogress,chassiskill,chassisoff,chassison,chassisstate,hoststate,\
-osstate,power,poweroff,poweron,state,status,rebootoff,rebooton,recoveryoff,recoveryon"
+osstate,power,poweroff,poweron,state,status,hostrebootoff,hostrebooton,recoveryoff,recoveryon,\
+bmcrebootoff, bmcrebooton"
 
 USAGE="Usage: obmcutil [-h] [--wait] [--verbose]
                 {$OPTS}"
@@ -47,13 +48,17 @@
     echo ""
     echo "Examples:"
     echo ""
-    echo "obmcutil rebootoff    Disable auto reboot from Quiesce state"
-    echo "obmcutil rebooton     Enable auto reboot from Quiesce state"
+    echo "obmcutil hostrebootoff Disable auto reboot of Host from Quiesce state"
+    echo "obmcutil hostrebooton  Enable auto reboot of Host from Quiesce state"
     echo ""
-    echo "obmcutil recoveryoff  Disable handling boot watchdog timeout and host crash"
-    echo "                      Also, disable auto reboot from Quiesce state"
-    echo "obmcutil recoveryon   Enable handling boot watchdog timeout and host crash"
-    echo "                      Also, enable auto reboot from Quiesce state"
+    echo "obmcutil bmcrebootoff  Disable reboot of BMC"
+    echo "obmcutil bmcrebooton   Enable reboot of BMC"
+    echo ""
+    echo "obmcutil recoveryoff   Disable handling boot watchdog timeout and host crash"
+    echo "                       Also, disable BMC and Host auto reboots"
+    echo ""
+    echo "obmcutil recoveryon    Enable handling boot watchdog timeout and host crash"
+    echo "                       Also, enable BMC and Host auto reboots"
     echo ""
     echo "optional arguments:"
     echo "  -h, --help          show this help message and exit"
@@ -152,6 +157,30 @@
     systemctl unmask $target
 }
 
+disable_bmc_reboot ()
+{
+    dir="/run/systemd/system/"
+    file="reboot-guard.conf"
+    units=("reboot" "poweroff" "halt")
+
+    for unit in "${units[@]}"; do
+        mkdir -p ${dir}${unit}.target.d
+        echo -e "[Unit]\nRefuseManualStart=yes" >> ${dir}${unit}.target.d/${file}
+    done
+}
+
+enable_bmc_reboot ()
+{
+    dir="/run/systemd/system/"
+    file="reboot-guard.conf"
+    units=("reboot" "poweroff" "halt")
+
+    for unit in "${units[@]}"; do
+        rm -rf ${dir}${unit}.target.d/${file}
+        rm -rf ${dir}${unit}.target.d
+    done
+}
+
 handle_cmd ()
 {
     case "$1" in
@@ -254,7 +283,7 @@
         chassiskill)
             /usr/libexec/chassiskill
             ;;
-        rebootoff)
+        hostrebootoff)
             OBJECT=$CONTROL_OBJECT/host0/auto_reboot
             SERVICE=$(mapper get-service $OBJECT)
             INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
@@ -262,7 +291,7 @@
             VALUE=false
             set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "b" $VALUE
             ;;
-        rebooton)
+        hostrebooton)
             OBJECT=$CONTROL_OBJECT/host0/auto_reboot
             SERVICE=$(mapper get-service $OBJECT)
             INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
@@ -270,13 +299,21 @@
             VALUE=true
             set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "b" $VALUE
             ;;
+        bmcrebootoff)
+            disable_bmc_reboot
+            ;;
+        bmcrebooton)
+            enable_bmc_reboot
+            ;;
         recoveryoff)
-            handle_cmd rebootoff
+            handle_cmd hostrebootoff
+            handle_cmd bmcrebootoff
             mask_systemd_target $HOST_TIMEOUT_TARGET
             mask_systemd_target $HOST_CRASH_TARGET
             ;;
         recoveryon)
-            handle_cmd rebooton
+            handle_cmd hostrebooton
+            handle_cmd bmcrebooton
             unmask_systemd_target $HOST_TIMEOUT_TARGET
             unmask_systemd_target $HOST_CRASH_TARGET
             ;;