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