Mohaimen Alsamarai | 82a11d8 | 2021-06-24 16:59:29 -0500 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # Usage of this utility |
| 3 | function usage() { |
| 4 | echo "usage: power-util mb [on|off|status|cycle|reset|graceful_shutdown|graceful_reset|force_reset]"; |
| 5 | } |
| 6 | |
| 7 | power_off() { |
| 8 | echo "Shutting down Server $2" |
| 9 | busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off |
| 10 | } |
| 11 | |
| 12 | power_on() { |
| 13 | echo "Powering on Server $2" |
| 14 | gpioset 6 11=1 |
| 15 | sleep 1 |
| 16 | gpioset 6 11=0 |
| 17 | busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On |
| 18 | } |
| 19 | |
| 20 | power_status() { |
| 21 | st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6) |
| 22 | if [ "$st" == "On\"" ]; then |
| 23 | echo "on" |
| 24 | else |
| 25 | echo "off" |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | power_reset() { |
| 30 | echo "Reset on server $2" |
| 31 | busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host RequestedHostTransition s xyz.openbmc_project.State.Host.Transition.Reboot |
| 32 | } |
| 33 | |
| 34 | graceful_shutdown() { |
| 35 | if [ -f "/run/openbmc/host@0-request" ]; then |
| 36 | echo "shutdown host immediately" |
| 37 | gpioset 6 11=1 |
| 38 | sleep 6 |
| 39 | gpioset 6 11=0 |
| 40 | power_off |
| 41 | else |
| 42 | echo "Triggering graceful shutdown" |
| 43 | gpioset -l 2 6=0 |
| 44 | sleep 3 |
| 45 | gpioset -l 2 6=1 |
| 46 | sleep 30s |
| 47 | power_off |
| 48 | fi |
| 49 | } |
| 50 | |
| 51 | force_reset() { |
| 52 | echo "Triggering sysreset pin" |
| 53 | gpioset -l 2 1=0 |
| 54 | sleep 1 |
| 55 | gpioset -l 2 1=1 |
| 56 | } |
| 57 | |
| 58 | if [ $# -lt 2 ]; then |
| 59 | echo "Total number of parameter=$#" |
| 60 | echo "Insufficient parameter" |
| 61 | usage; |
| 62 | exit 0; |
| 63 | fi |
| 64 | |
| 65 | if [ $1 != "mb" ]; then |
| 66 | echo "Invalid parameter1=$1" |
| 67 | usage; |
| 68 | exit 0; |
| 69 | fi |
| 70 | |
| 71 | if [ $2 = "on" ]; then |
| 72 | if [ $(power_status) == "off" ]; then |
| 73 | power_on |
| 74 | fi |
| 75 | elif [ $2 = "off" ]; then |
| 76 | if [ $(power_status) == "on" ]; then |
| 77 | power_off |
| 78 | fi |
| 79 | # If any request of graceful reset, need to power on |
| 80 | if [ -f "/run/openbmc/host@0-graceful-reset" ]; then |
| 81 | sleep 20s |
| 82 | power_on |
| 83 | rm -f "/run/openbmc/host@0-graceful-reset" |
| 84 | fi |
| 85 | elif [ $2 == "cycle" ]; then |
| 86 | if [ $(power_status) == "on" ]; then |
| 87 | echo "Power cycling server" |
| 88 | power_off |
| 89 | sleep 20s |
| 90 | power_on |
| 91 | else |
| 92 | echo "Host is already off, do nothing" |
| 93 | fi |
| 94 | elif [ $2 == "reset" ]; then |
| 95 | if [ $(power_status) == "on" ]; then |
| 96 | power_reset |
| 97 | else |
| 98 | echo "ERROR: Server not powered on" |
| 99 | fi |
| 100 | elif [[ $2 == "graceful_shutdown" ]]; then |
| 101 | graceful_shutdown |
| 102 | elif [ $2 == "graceful_reset" ]; then |
| 103 | mkdir -p "/run/openbmc/" |
| 104 | touch "/run/openbmc/host@0-graceful-reset" |
| 105 | graceful_shutdown |
| 106 | sleep 20s |
| 107 | elif [ $2 == "status" ]; then |
| 108 | power_status |
| 109 | elif [ $2 == "force_reset" ]; then |
| 110 | force_reset |
| 111 | else |
| 112 | echo "Invalid parameter2=$2" |
| 113 | usage; |
| 114 | fi |
| 115 | |
| 116 | exit 0; |
| 117 | |