blob: 1aa75c9282f67aa81f9bc485268573df6b973c30 [file] [log] [blame]
Patrick Williamsb2398202021-04-13 20:53:26 -05001#!/bin/bash -e
Anthony Wilson79f697e2018-09-13 13:48:52 -05002
3set -euo pipefail
4
Anthony Wilson189cf242018-10-23 01:18:21 -05005OPTS="bmcstate,bootprogress,chassiskill,chassisoff,chassison,chassisstate,hoststate,\
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -06006osstate,power,poweroff,poweron,state,status,hostrebootoff,hostrebooton,recoveryoff,recoveryon,\
Andrew Geisslerad1afe52022-02-23 16:07:06 -06007bmcrebootoff, bmcrebooton, listbootblock listlogs showlog deletelogs, stopofftargets"
Anthony Wilson0f359832018-09-13 14:28:07 -05008
Potin Lai0e044c42022-05-13 23:42:31 +08009USAGE="Usage: obmcutil [-h] [--wait] [--verbose] [--id=<INSTANCE_ID>]
Patrick Williamsd182bff2022-12-08 06:39:12 -060010{$OPTS}"
Anthony Wilson79f697e2018-09-13 13:48:52 -050011
12INTERFACE_ROOT=xyz.openbmc_project
13STATE_INTERFACE=$INTERFACE_ROOT.State
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -050014CONTROL_INTERFACE=$INTERFACE_ROOT.Control
Anthony Wilson79f697e2018-09-13 13:48:52 -050015
16OBJECT_ROOT=/xyz/openbmc_project
17STATE_OBJECT=$OBJECT_ROOT/state
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -050018CONTROL_OBJECT=$OBJECT_ROOT/control
Anthony Wilson79f697e2018-09-13 13:48:52 -050019
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -050020HOST_TIMEOUT_TARGET=obmc-host-timeout@0.target
Vishwanatha Subbanna84b3b292019-11-04 05:43:19 -060021HOST_CRASH_TARGET=obmc-host-crash@0.target
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -050022
Anthony Wilsonacf54d02018-09-20 15:19:28 -050023## NOTE: The following global variables are used only in the run_timeout cmd.
24## By declaring these globally instead of passing them through the
25## intermediary functions, which may not be "best practice", the readability
26## and cleanliness of the code should at least be increased.
27
28# The command passed in to be executed (e.g. poweron/off, status, etc.)
29# This will be be used in some instances of error reporting
30G_ORIG_CMD=
31# The state an interface should be in after executing the requested command.
32G_REQUESTED_STATE=
33# The query to run during a poweron/off or chassison/off to check that
34# the requested state (G_REQUESTED_STATE) of the interface has been reached.
35G_QUERY=
36# Wait the set period of time for state transitions to be successful before
37# continuing on with the program or reporting an error if timeout reached.
38G_WAIT=
Andrew Jeffery60c3ac82019-10-02 09:29:29 +093039# Print the journal to the console
40G_VERBOSE=
Potin Lai0e044c42022-05-13 23:42:31 +080041# Instance id, default 0
42G_INSTANCE_ID="0"
Anthony Wilsonacf54d02018-09-20 15:19:28 -050043
Patrick Williamsd182bff2022-12-08 06:39:12 -060044function print_help()
Anthony Wilsonf3f16fa2018-09-13 14:10:52 -050045{
46 echo "$USAGE"
47 echo ""
48 echo "positional arguments:"
Anthony Wilson0f359832018-09-13 14:28:07 -050049 echo " {$OPTS}"
Anthony Wilsonf3f16fa2018-09-13 14:10:52 -050050 echo ""
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -050051 echo "Examples:"
52 echo ""
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -060053 echo "obmcutil hostrebootoff Disable auto reboot of Host from Quiesce state"
Andrew Geissler3191be82020-10-23 10:45:55 -050054 echo "obmcutil hostrebootoffonetime Disable auto reboot of Host from"
55 echo " Quiesce state for a single boot"
Andrew Geisslerad1afe52022-02-23 16:07:06 -060056 echo "obmcutil hostrebooton Enable auto reboot of Host from Quiesce state"
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -050057 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060058 echo "obmcutil bmcrebootoff Disable reboot of BMC"
59 echo "obmcutil bmcrebooton Enable reboot of BMC"
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -060060 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060061 echo "obmcutil recoveryoff Disable handling boot watchdog timeout and host crash"
62 echo " Also, disable BMC and Host auto reboots"
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -060063 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060064 echo "obmcutil recoveryon Enable handling boot watchdog timeout and host crash"
65 echo " Also, enable BMC and Host auto reboots"
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -050066 echo ""
Anusha Dathatri24b25a42024-05-31 05:57:45 -050067 echo "obmcutil recoverystatus Display the status of handling boot watchdog timeout and host crash"
68 echo " and also the status of BMC and Host auto reboots setting"
69 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060070 echo "obmcutil listbootblock Check for and list any errors blocking the boot"
71 echo " of the system"
Andrew Geissler3b7b5612020-05-14 10:45:29 -050072 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060073 echo "obmcutil listlogs List all phosphor-logging entries on the"
74 echo " system"
Andrew Geissler295ee4f2020-05-14 14:14:05 -050075 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060076 echo "obmcutil showlog <log> Display details of input log. Format of <log>"
77 echo " should match listlogs output"
Andrew Geisslerd8c63202020-05-14 15:06:31 -050078 echo ""
Andrew Geisslerad1afe52022-02-23 16:07:06 -060079 echo "obmcutil deletelogs Delete all phosphor-logging entries from"
80 echo " system"
81 echo "obmcutil stopofftargets Manually stop all obmc targets in power off"
82 echo " path"
Andrew Geissler42f28982020-05-14 15:12:42 -050083 echo ""
Andrew Geisslerd8779cd2020-06-11 10:48:40 -050084 echo "optional arguments (must precede the positional options above):"
Anthony Wilsonf3f16fa2018-09-13 14:10:52 -050085 echo " -h, --help show this help message and exit"
Anthony Wilsonacf54d02018-09-20 15:19:28 -050086 echo " -w, --wait block until state transition succeeds or fails"
Andrew Jeffery60c3ac82019-10-02 09:29:29 +093087 echo " -v, --verbose print the journal to stdout if --wait is supplied"
Potin Lai0e044c42022-05-13 23:42:31 +080088 echo " -i, -id instance id, default 0"
Anthony Wilsonf3f16fa2018-09-13 14:10:52 -050089 exit 0
90}
91
Patrick Williamsd182bff2022-12-08 06:39:12 -060092function run_timeout()
Anthony Wilsonacf54d02018-09-20 15:19:28 -050093{
94 local timeout="$1"; shift
Patrick Williamsb2398202021-04-13 20:53:26 -050095 local cmd="$*"
Andrew Jeffery60c3ac82019-10-02 09:29:29 +093096 local verbose_child=
97
Andrew Jeffery2869a922019-10-18 14:42:34 +103098 if [ -n "$G_VERBOSE" ]; then
Andrew Jeffery60c3ac82019-10-02 09:29:29 +093099 journalctl -f &
100 verbose_child=$!
101 fi
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500102
103 $cmd
104
105 # Run a background query for the transition to the expected state
106 # This will be killed if the transition doesn't succeed within
107 # a timeout period.
108 (
Patrick Williamsb2398202021-04-13 20:53:26 -0500109 while ! grep -q "$G_REQUESTED_STATE" <<< "$(handle_cmd "$G_QUERY")" ; do
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500110 sleep 1
111 done
112 ) &
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930113 wait_child=$!
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500114
115 # Could be bad if process is killed before 'timeout' occurs if
116 # transition doesn't succeed.
117 trap -- "" SIGTERM
118
119 # Workaround for lack of 'timeout' command.
120 (
Patrick Williamsb2398202021-04-13 20:53:26 -0500121 sleep "$timeout"
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930122 kill $wait_child
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500123 ) > /dev/null 2>&1 &
124
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930125 if ! wait $wait_child; then
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500126 echo "Unable to confirm '$G_ORIG_CMD' success" \
Patrick Williamsd182bff2022-12-08 06:39:12 -0600127 "within timeout period (${timeout}s)"
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500128 fi
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930129
Andrew Jeffery8be70292019-11-01 08:56:49 +1030130 if [ -n "$verbose_child" ]; then
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930131 kill $verbose_child
132 fi
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500133}
134
Patrick Williamsd182bff2022-12-08 06:39:12 -0600135function run_cmd()
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500136{
Patrick Williamsb2398202021-04-13 20:53:26 -0500137 local cmd="$*";
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500138
139 if [ -n "$G_WAIT" ]; then
Patrick Williamsb2398202021-04-13 20:53:26 -0500140 run_timeout "$G_WAIT" "$cmd"
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500141 else
142 $cmd
143 fi
144}
145
Patrick Williamsd182bff2022-12-08 06:39:12 -0600146function set_property()
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500147{
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500148 run_cmd busctl set-property "$@"
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500149}
150
Patrick Williamsd182bff2022-12-08 06:39:12 -0600151function get_property()
Anthony Wilson79f697e2018-09-13 13:48:52 -0500152{
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500153 G_WAIT=""
154 run_cmd busctl get-property "$@"
Anthony Wilson79f697e2018-09-13 13:48:52 -0500155}
156
Patrick Williamsd182bff2022-12-08 06:39:12 -0600157function state_query()
Anthony Wilson79f697e2018-09-13 13:48:52 -0500158{
Patrick Williamsb2398202021-04-13 20:53:26 -0500159 local state
160 state=$(get_property "$@" | cut -d '"' -f2)
161 printf "%-20s: %s\n" "$4" "$state"
Anthony Wilson79f697e2018-09-13 13:48:52 -0500162}
163
Patrick Williamsd182bff2022-12-08 06:39:12 -0600164function print_usage_err()
Anthony Wilsonea87db42018-09-26 16:06:38 -0500165{
166 echo "ERROR: $1" >&2
167 echo "$USAGE"
168 exit 1
169}
170
Patrick Williamsd182bff2022-12-08 06:39:12 -0600171function mask_systemd_target()
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500172{
Patrick Williamsb2398202021-04-13 20:53:26 -0500173 target="$*"
174 systemctl mask "$target"
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500175}
176
Patrick Williamsd182bff2022-12-08 06:39:12 -0600177function unmask_systemd_target()
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500178{
Patrick Williamsb2398202021-04-13 20:53:26 -0500179 target="$*"
180 systemctl unmask "$target"
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500181}
182
Anusha Dathatri24b25a42024-05-31 05:57:45 -0500183function get_systemd_target_state()
184{
185 target="$*"
186 enabled_state=$(systemctl is-enabled "$target")
187 echo "$enabled_state"
188}
189
Patrick Williamsd182bff2022-12-08 06:39:12 -0600190function disable_bmc_reboot()
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600191{
192 dir="/run/systemd/system/"
193 file="reboot-guard.conf"
194 units=("reboot" "poweroff" "halt")
195
196 for unit in "${units[@]}"; do
Patrick Williamsb2398202021-04-13 20:53:26 -0500197 mkdir -p "${dir}${unit}.target.d"
198 echo -e "[Unit]\nRefuseManualStart=yes" >> "${dir}${unit}.target.d/${file}"
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600199 done
200}
201
Patrick Williamsd182bff2022-12-08 06:39:12 -0600202function enable_bmc_reboot()
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600203{
204 dir="/run/systemd/system/"
205 file="reboot-guard.conf"
206 units=("reboot" "poweroff" "halt")
207
208 for unit in "${units[@]}"; do
Patrick Williamsb2398202021-04-13 20:53:26 -0500209 rm -rf "${dir}${unit}.target.d/${file}"
210 rm -rf "${dir}${unit}.target.d"
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600211 done
212}
213
Anusha Dathatri24b25a42024-05-31 05:57:45 -0500214function get_bmc_reboot_status()
215{
216 dir="/run/systemd/system/"
217 file="reboot-guard.conf"
218 units=("reboot" "poweroff" "halt")
219
220 # if file do
221 for unit in "${units[@]}"; do
222 if [ -e "${dir}${unit}.target.d/${file}" ]; then
223 echo "off"
224 return 0
225 fi
226 done
227 echo "on"
228 return 0
229}
230
231function get_host_reboot_status()
232{
233 OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
234 SERVICE=$(mapper get-service "$OBJECT")
235 INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
236 PROPERTY=AutoReboot
237 output="$(get_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY)"
238 echo "${output//b /}"
239}
240
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500241# will write blocking errors to stdout
Patrick Williamsd182bff2022-12-08 06:39:12 -0600242function check_boot_block_errors()
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500243{
244 # array of boot block objects
245 blockArray=()
246
247 # Look for any objects under logging that implement the
248 # xyz.openbmc_project.Logging.ErrorBlocksTransition
249 subtree="$(busctl call xyz.openbmc_project.ObjectMapper \
250 /xyz/openbmc_project/object_mapper \
251 xyz.openbmc_project.ObjectMapper \
252 GetSubTree sias "/xyz/openbmc_project/logging/" 0 1 \
253 xyz.openbmc_project.Logging.ErrorBlocksTransition)"
254
255 # remove quotation marks
Patrick Williamsb2398202021-04-13 20:53:26 -0500256 # shellcheck disable=SC2001
257 subtree="$(echo "$subtree" | sed 's/\"//g')"
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500258
259 for entry in $subtree; do
260 if [[ ${entry} =~ "xyz/openbmc_project/logging/block"* ]]; then
Patrick Williamsb2398202021-04-13 20:53:26 -0500261 blockArray+=( "$entry" )
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500262 fi
263 done
264
265 # now find associated error log for each boot block error
266 for berror in "${blockArray[@]}"; do
Patrick Williamsb2398202021-04-13 20:53:26 -0500267 assocs="$(busctl call xyz.openbmc_project.Logging "$berror" \
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500268 org.freedesktop.DBus.Properties Get \
269 ss xyz.openbmc_project.Association.Definitions Associations)"
270
271 # remove quotation marks
Patrick Williamsb2398202021-04-13 20:53:26 -0500272 # shellcheck disable=SC2001
273 assocs="$(echo "$assocs" | sed 's/\"//g')"
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500274
275 for entry in $assocs; do
276 if [[ ${entry} =~ "xyz/openbmc_project/logging/entry"* ]]; then
277 echo "Blocking Error: $entry"
278 fi
279 done
280 done
281}
282
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500283# helper function to check for boot block errors and notify user
Patrick Williamsd182bff2022-12-08 06:39:12 -0600284function check_and_warn_boot_block()
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500285{
286 blockingErrors=$(check_boot_block_errors)
Patrick Williamsb2398202021-04-13 20:53:26 -0500287 if [ -n "$blockingErrors" ]; then
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500288 echo !!!!!!!!!!
289 echo "WARNING! System has blocking errors that will prevent boot"
290 echo "$blockingErrors"
291 echo !!!!!!!!!!
292 fi
293}
294
Andrew Geissler295ee4f2020-05-14 14:14:05 -0500295# list all phosphor-logging entries
Patrick Williamsd182bff2022-12-08 06:39:12 -0600296function list_logs()
Andrew Geissler295ee4f2020-05-14 14:14:05 -0500297{
298 # Look for any objects under logging that implement the
299 # xyz.openbmc_project.Logging.Entry
300 busctl -j call xyz.openbmc_project.ObjectMapper \
Patrick Williamsd182bff2022-12-08 06:39:12 -0600301 /xyz/openbmc_project/object_mapper \
302 xyz.openbmc_project.ObjectMapper \
303 GetSubTreePaths sias "/xyz/openbmc_project/logging/" 0 1 \
304 xyz.openbmc_project.Logging.Entry
Andrew Geissler295ee4f2020-05-14 14:14:05 -0500305}
306
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500307# display input log details
Patrick Williamsd182bff2022-12-08 06:39:12 -0600308function show_log()
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500309{
310 busctl -j call xyz.openbmc_project.Logging \
Patrick Williamsd182bff2022-12-08 06:39:12 -0600311 "$1" \
312 org.freedesktop.DBus.Properties \
313 GetAll s xyz.openbmc_project.Logging.Entry
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500314}
315
Andrew Geissler42f28982020-05-14 15:12:42 -0500316# delete all phosphor-logging entries
Patrick Williamsd182bff2022-12-08 06:39:12 -0600317function delete_logs()
Andrew Geissler42f28982020-05-14 15:12:42 -0500318{
319 busctl call xyz.openbmc_project.Logging \
Patrick Williamsd182bff2022-12-08 06:39:12 -0600320 /xyz/openbmc_project/logging \
321 xyz.openbmc_project.Collection.DeleteAll DeleteAll
Andrew Geissler42f28982020-05-14 15:12:42 -0500322}
323
Andrew Geisslerad1afe52022-02-23 16:07:06 -0600324# stop all targets associated with powering off a system
Patrick Williamsd182bff2022-12-08 06:39:12 -0600325function stop_off_targets()
Andrew Geisslerad1afe52022-02-23 16:07:06 -0600326{
327 systemctl stop \
328 obmc-chassis-powered-off@0.target \
329 obmc-host-stop-pre@0.target \
330 obmc-host-stopped@0.target \
331 obmc-host-stopping@0.target \
332 obmc-power-off@0.target \
333 obmc-power-stop-pre@0.target \
334 obmc-power-stop@0.target
335}
336
Patrick Williamsd182bff2022-12-08 06:39:12 -0600337function handle_cmd()
Anthony Wilson79f697e2018-09-13 13:48:52 -0500338{
339 case "$1" in
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500340 chassisoff)
Potin Lai0e044c42022-05-13 23:42:31 +0800341 OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600342 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500343 INTERFACE=$STATE_INTERFACE.Chassis
344 PROPERTY=RequestedPowerTransition
345 VALUE=$INTERFACE.Transition.Off
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500346 G_REQUESTED_STATE=$INTERFACE.PowerState.Off
347 G_QUERY="chassisstate"
Andrew Geissler8aca0502023-05-01 15:51:39 -0600348 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500349 ;;
350 chassison)
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500351 check_and_warn_boot_block
Potin Lai0e044c42022-05-13 23:42:31 +0800352 OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600353 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500354 INTERFACE=$STATE_INTERFACE.Chassis
355 PROPERTY=RequestedPowerTransition
356 VALUE=$INTERFACE.Transition.On
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500357 G_REQUESTED_STATE=$INTERFACE.PowerState.On
358 G_QUERY="chassisstate"
Andrew Geissler8aca0502023-05-01 15:51:39 -0600359 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500360 ;;
361 poweroff)
Potin Lai0e044c42022-05-13 23:42:31 +0800362 OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600363 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500364 INTERFACE=$STATE_INTERFACE.Host
365 PROPERTY=RequestedHostTransition
366 VALUE=$INTERFACE.Transition.Off
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500367 G_REQUESTED_STATE=$INTERFACE.HostState.Off
368 G_QUERY="hoststate"
Andrew Geissler8aca0502023-05-01 15:51:39 -0600369 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500370 ;;
371 poweron)
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500372 check_and_warn_boot_block
Potin Lai0e044c42022-05-13 23:42:31 +0800373 OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600374 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500375 INTERFACE=$STATE_INTERFACE.Host
376 PROPERTY=RequestedHostTransition
377 VALUE=$INTERFACE.Transition.On
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500378 G_REQUESTED_STATE=$INTERFACE.HostState.Running
379 G_QUERY="hoststate"
Andrew Geissler8aca0502023-05-01 15:51:39 -0600380 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "s" $VALUE
Anthony Wilson3ae0a352018-09-13 14:47:56 -0500381 ;;
Anthony Wilson79f697e2018-09-13 13:48:52 -0500382 bmcstate)
383 OBJECT=$STATE_OBJECT/bmc0
384 SERVICE=$(mapper get-service $OBJECT)
385 INTERFACE=$STATE_INTERFACE.BMC
386 PROPERTY=CurrentBMCState
Patrick Williamsb2398202021-04-13 20:53:26 -0500387 state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
Anthony Wilson79f697e2018-09-13 13:48:52 -0500388 ;;
389 chassisstate)
Potin Lai0e044c42022-05-13 23:42:31 +0800390 OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600391 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson79f697e2018-09-13 13:48:52 -0500392 INTERFACE=$STATE_INTERFACE.Chassis
393 PROPERTY=CurrentPowerState
Andrew Geissler8aca0502023-05-01 15:51:39 -0600394 state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
Anthony Wilson79f697e2018-09-13 13:48:52 -0500395 ;;
396 hoststate)
Potin Lai0e044c42022-05-13 23:42:31 +0800397 OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600398 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson79f697e2018-09-13 13:48:52 -0500399 INTERFACE=$STATE_INTERFACE.Host
400 PROPERTY=CurrentHostState
Andrew Geissler8aca0502023-05-01 15:51:39 -0600401 state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
Anthony Wilson79f697e2018-09-13 13:48:52 -0500402 ;;
Alexander Filippov86cffd92019-04-03 16:29:57 +0300403 osstate)
Potin Lai0e044c42022-05-13 23:42:31 +0800404 OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600405 SERVICE=$(mapper get-service "$OBJECT")
Alexander Filippov86cffd92019-04-03 16:29:57 +0300406 INTERFACE=$STATE_INTERFACE.OperatingSystem.Status
407 PROPERTY=OperatingSystemState
Andrew Geissler8aca0502023-05-01 15:51:39 -0600408 state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
Alexander Filippov86cffd92019-04-03 16:29:57 +0300409 ;;
Anthony Wilson79f697e2018-09-13 13:48:52 -0500410 state|status)
Alexander Filippov86cffd92019-04-03 16:29:57 +0300411 for query in bmcstate chassisstate hoststate bootprogress osstate
Anthony Wilson79f697e2018-09-13 13:48:52 -0500412 do
413 handle_cmd $query
414 done
Andrew Geisslerdeb6bb42020-05-14 13:44:57 -0500415 check_and_warn_boot_block
Anthony Wilson79f697e2018-09-13 13:48:52 -0500416 ;;
Anthony Wilson50c5f882018-09-13 14:19:37 -0500417 bootprogress)
Potin Lai0e044c42022-05-13 23:42:31 +0800418 OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
Andrew Geissler8aca0502023-05-01 15:51:39 -0600419 SERVICE=$(mapper get-service "$OBJECT")
Anthony Wilson50c5f882018-09-13 14:19:37 -0500420 INTERFACE=$STATE_INTERFACE.Boot.Progress
421 PROPERTY=BootProgress
Andrew Geissler8aca0502023-05-01 15:51:39 -0600422 state_query "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY
Anthony Wilson50c5f882018-09-13 14:19:37 -0500423 ;;
Anthony Wilson0f359832018-09-13 14:28:07 -0500424 power)
425 OBJECT=/org/openbmc/control/power0
426 SERVICE=$(mapper get-service $OBJECT)
427 INTERFACE=org.openbmc.control.Power
Patrick Williamsd182bff2022-12-08 06:39:12 -0600428 for property in pgood state pgood_timeout; do
Anthony Wilson0f359832018-09-13 14:28:07 -0500429 # get_property can potentially return several
430 # different formats of values, so we do the parsing outside
431 # of get_property depending on the query. These queries
432 # return 'i VALUE' formatted strings.
Patrick Williamsd182bff2022-12-08 06:39:12 -0600433 STATE=$(get_property "$SERVICE" "$OBJECT" "$INTERFACE" "$property" | sed 's/i[ ^I]*//')
Patrick Williamsb2398202021-04-13 20:53:26 -0500434 printf "%s = %s\n" $property "$STATE"
Anthony Wilson0f359832018-09-13 14:28:07 -0500435 done
436 ;;
Anthony Wilson189cf242018-10-23 01:18:21 -0500437 chassiskill)
438 /usr/libexec/chassiskill
439 ;;
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600440 hostrebootoff)
Potin Lai0e044c42022-05-13 23:42:31 +0800441 OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
Andrew Geissler8aca0502023-05-01 15:51:39 -0600442 SERVICE=$(mapper get-service "$OBJECT")
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -0500443 INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
444 PROPERTY=AutoReboot
445 VALUE=false
Andrew Geissler8aca0502023-05-01 15:51:39 -0600446 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -0500447 ;;
Andrew Geissler3191be82020-10-23 10:45:55 -0500448 hostrebootoffonetime)
Potin Lai0e044c42022-05-13 23:42:31 +0800449 OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot/one_time
Andrew Geissler8aca0502023-05-01 15:51:39 -0600450 SERVICE=$(mapper get-service "$OBJECT")
Andrew Geissler3191be82020-10-23 10:45:55 -0500451 INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
452 PROPERTY=AutoReboot
453 VALUE=false
Andrew Geissler8aca0502023-05-01 15:51:39 -0600454 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
Patrick Williamsd182bff2022-12-08 06:39:12 -0600455 ;;
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600456 hostrebooton)
Potin Lai0e044c42022-05-13 23:42:31 +0800457 OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
Andrew Geissler8aca0502023-05-01 15:51:39 -0600458 SERVICE=$(mapper get-service "$OBJECT")
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -0500459 INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
460 PROPERTY=AutoReboot
461 VALUE=true
Andrew Geissler8aca0502023-05-01 15:51:39 -0600462 set_property "$SERVICE" "$OBJECT" $INTERFACE $PROPERTY "b" $VALUE
Vishwanatha Subbanna6d3a2c52019-10-24 07:21:30 -0500463 ;;
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600464 bmcrebootoff)
465 disable_bmc_reboot
466 ;;
467 bmcrebooton)
468 enable_bmc_reboot
469 ;;
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500470 recoveryoff)
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600471 handle_cmd hostrebootoff
472 handle_cmd bmcrebootoff
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500473 mask_systemd_target $HOST_TIMEOUT_TARGET
Vishwanatha Subbanna84b3b292019-11-04 05:43:19 -0600474 mask_systemd_target $HOST_CRASH_TARGET
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500475 ;;
476 recoveryon)
Vishwanatha Subbannaa65d30d2019-11-13 01:26:12 -0600477 handle_cmd hostrebooton
478 handle_cmd bmcrebooton
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500479 unmask_systemd_target $HOST_TIMEOUT_TARGET
Vishwanatha Subbanna84b3b292019-11-04 05:43:19 -0600480 unmask_systemd_target $HOST_CRASH_TARGET
Vishwanatha Subbanna7a787dd2019-10-31 06:02:31 -0500481 ;;
Anusha Dathatri24b25a42024-05-31 05:57:45 -0500482 recoverystatus)
483 host_reboot_state=$(get_host_reboot_status)
484 if [[ $host_reboot_state == "true" ]]; then
485 host_reboot_status=1
486 else
487 host_reboot_status=0
488 fi
489
490 bmc_reboot_state=$(get_bmc_reboot_status)
491 if [[ $bmc_reboot_state == "on" ]]; then
492 bmc_reboot_status=1
493 else
494 bmc_reboot_status=0
495 fi
496
497 host_timeout_target_state=$(get_systemd_target_state $HOST_TIMEOUT_TARGET)
498 if [[ $host_timeout_target_state == "masked" ]]; then
499 host_timeout_status=0
500 else
501 host_timeout_status=1
502 fi
503
504 host_crash_target_state=$(get_systemd_target_state $HOST_CRASH_TARGET)
505 if [[ $host_crash_target_state == "masked" ]]; then
506 host_crash_status=0
507 else
508 host_crash_status=1
509 fi
510
511 if (( host_reboot_status && bmc_reboot_status && host_timeout_status && host_crash_status )); then
512 echo "recovery: On"
513 elif (( !host_reboot_status && !bmc_reboot_status && !host_timeout_status && !host_crash_status )); then
514 echo "recovery: Off"
515 else
516 echo "recovery: Undefined"
517 fi
518
519 declare -A status
520 status[0]="Off"
521 status[1]="On"
522
523 printf " %-11s: %s\n" "hostReboot" "${status[$host_reboot_status]}"
524 printf " %-11s: %s\n" "bmcReboot" "${status[$bmc_reboot_status]}"
525 printf " %-11s: %s\n" "hostTimeout" "${status[$host_timeout_status]}"
526 printf " %-11s: %s\n" "hostCrash" "${status[$host_crash_status]}"
527 ;;
Andrew Geissler3b7b5612020-05-14 10:45:29 -0500528 listbootblock)
529 blockingErrors=$(check_boot_block_errors)
530 if [ -z "$blockingErrors" ]; then
531 echo "No blocking errors present"
532 else
533 echo "$blockingErrors"
534 fi
535 ;;
Andrew Geissler295ee4f2020-05-14 14:14:05 -0500536 listlogs)
537 list_logs
538 ;;
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500539 showlog)
Patrick Williamsb2398202021-04-13 20:53:26 -0500540 show_log "$2"
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500541 ;;
Andrew Geissler42f28982020-05-14 15:12:42 -0500542 deletelogs)
543 delete_logs
544 ;;
Andrew Geisslerad1afe52022-02-23 16:07:06 -0600545 stopofftargets)
546 stop_off_targets
547 ;;
Anthony Wilson79f697e2018-09-13 13:48:52 -0500548 *)
Anthony Wilsonea87db42018-09-26 16:06:38 -0500549 print_usage_err "Invalid command '$1'"
Anthony Wilson79f697e2018-09-13 13:48:52 -0500550 ;;
551 esac
552}
553
Andrew Geisslerd8779cd2020-06-11 10:48:40 -0500554shiftcnt=0
Anthony Wilsonea87db42018-09-26 16:06:38 -0500555for arg in "$@"; do
556 case $arg in
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500557 -w|--wait)
558 G_WAIT=30
Andrew Geisslerd8779cd2020-06-11 10:48:40 -0500559 shiftcnt=$((shiftcnt+1))
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500560 continue
561 ;;
Anthony Wilsonea87db42018-09-26 16:06:38 -0500562 -h|--help)
563 print_help
564 ;;
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930565 -v|--verbose)
566 G_VERBOSE=y
Andrew Geisslerd8779cd2020-06-11 10:48:40 -0500567 shiftcnt=$((shiftcnt+1))
Andrew Jeffery60c3ac82019-10-02 09:29:29 +0930568 ;;
Potin Lai0e044c42022-05-13 23:42:31 +0800569 -i=*|--id=*)
570 G_INSTANCE_ID="${arg#*=}"
571 shiftcnt=$((shiftcnt+1))
572 ;;
Anthony Wilsonea87db42018-09-26 16:06:38 -0500573 -*)
574 print_usage_err "Unknown option: $arg"
575 ;;
576 *)
Anthony Wilsonacf54d02018-09-20 15:19:28 -0500577 G_ORIG_CMD=$arg
Andrew Geisslerd8779cd2020-06-11 10:48:40 -0500578 # shift out the optional parameters
579 shift $shiftcnt
Andrew Geisslerd8c63202020-05-14 15:06:31 -0500580 # pass all arguments to handle_cmd in case command takes additional
581 # parameters
582 handle_cmd "$@"
Anthony Wilsonea87db42018-09-26 16:06:38 -0500583 break
584 ;;
585 esac
586done