forked from openbmc/phosphor-state-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obmcutil
executable file
·503 lines (460 loc) · 15.7 KB
/
obmcutil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/bash -e
set -euo pipefail
OPTS="bmcstate,bootprogress,chassiskill,chassisoff,chassison,chassisstate,hoststate,\
osstate,power,poweroff,poweron,state,status,hostrebootoff,hostrebooton,recoveryoff,recoveryon,\
bmcrebootoff, bmcrebooton, listbootblock listlogs showlog deletelogs, stopofftargets"
USAGE="Usage: obmcutil [-h] [--wait] [--verbose] [--id=<INSTANCE_ID>]
{$OPTS}"
INTERFACE_ROOT=xyz.openbmc_project
STATE_INTERFACE=$INTERFACE_ROOT.State
CONTROL_INTERFACE=$INTERFACE_ROOT.Control
OBJECT_ROOT=/xyz/openbmc_project
STATE_OBJECT=$OBJECT_ROOT/state
CONTROL_OBJECT=$OBJECT_ROOT/control
## NOTE: The following global variables are used only in the run_timeout cmd.
## By declaring these globally instead of passing them through the
## intermediary functions, which may not be "best practice", the readability
## and cleanliness of the code should at least be increased.
# The command passed in to be executed (e.g. poweron/off, status, etc.)
# This will be be used in some instances of error reporting
G_ORIG_CMD=
# The state an interface should be in after executing the requested command.
G_REQUESTED_STATE=
# The query to run during a poweron/off or chassison/off to check that
# the requested state (G_REQUESTED_STATE) of the interface has been reached.
G_QUERY=
# Wait the set period of time for state transitions to be successful before
# continuing on with the program or reporting an error if timeout reached.
G_WAIT=
# Print the journal to the console
G_VERBOSE=
# Instance id, default 0
G_INSTANCE_ID="0"
function print_help()
{
echo "$USAGE"
echo ""
echo "positional arguments:"
echo " {$OPTS}"
echo ""
echo "Examples:"
echo ""
echo "obmcutil hostrebootoff Disable auto reboot of Host from Quiesce state"
echo "obmcutil hostrebootoffonetime Disable auto reboot of Host from"
echo " Quiesce state for a single boot"
echo "obmcutil hostrebooton Enable auto reboot of Host from Quiesce state"
echo ""
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 "obmcutil listbootblock Check for and list any errors blocking the boot"
echo " of the system"
echo ""
echo "obmcutil listlogs List all phosphor-logging entries on the"
echo " system"
echo ""
echo "obmcutil showlog <log> Display details of input log. Format of <log>"
echo " should match listlogs output"
echo ""
echo "obmcutil deletelogs Delete all phosphor-logging entries from"
echo " system"
echo "obmcutil stopofftargets Manually stop all obmc targets in power off"
echo " path"
echo ""
echo "optional arguments (must precede the positional options above):"
echo " -h, --help show this help message and exit"
echo " -w, --wait block until state transition succeeds or fails"
echo " -v, --verbose print the journal to stdout if --wait is supplied"
echo " -i, -id instance id, default 0"
exit 0
}
function run_timeout()
{
local timeout="$1"; shift
local cmd="$*"
local verbose_child=
if [ -n "$G_VERBOSE" ]; then
journalctl -f &
verbose_child=$!
fi
$cmd
# Run a background query for the transition to the expected state
# This will be killed if the transition doesn't succeed within
# a timeout period.
(
while ! grep -q "$G_REQUESTED_STATE" <<< "$(handle_cmd "$G_QUERY")" ; do
sleep 1
done
) &
wait_child=$!
# Could be bad if process is killed before 'timeout' occurs if
# transition doesn't succeed.
trap -- "" SIGTERM
# Workaround for lack of 'timeout' command.
(
sleep "$timeout"
kill $wait_child
) > /dev/null 2>&1 &
if ! wait $wait_child; then
echo "Unable to confirm '$G_ORIG_CMD' success" \
"within timeout period (${timeout}s)"
fi
if [ -n "$verbose_child" ]; then
kill $verbose_child
fi
}
function run_cmd()
{
local cmd="$*";
if [ -n "$G_WAIT" ]; then
run_timeout "$G_WAIT" "$cmd"
else
$cmd
fi
}
function set_property()
{
run_cmd busctl set-property "$@"
}
function get_property()
{
G_WAIT=""
run_cmd busctl get-property "$@"
}
function state_query()
{
local state
state=$(get_property "$@" | cut -d '"' -f2)
printf "%-20s: %s\n" "$4" "$state"
}
function print_usage_err()
{
echo "ERROR: $1" >&2
echo "$USAGE"
exit 1
}
function mask_systemd_target()
{
target="$*"
systemctl mask "$target"
}
function unmask_systemd_target()
{
target="$*"
systemctl unmask "$target"
}
function 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
}
function 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
}
# will write blocking errors to stdout
function check_boot_block_errors()
{
# array of boot block objects
blockArray=()
# Look for any objects under logging that implement the
# xyz.openbmc_project.Logging.ErrorBlocksTransition
subtree="$(busctl call xyz.openbmc_project.ObjectMapper \
/xyz/openbmc_project/object_mapper \
xyz.openbmc_project.ObjectMapper \
GetSubTree sias "/xyz/openbmc_project/logging/" 0 1 \
xyz.openbmc_project.Logging.ErrorBlocksTransition)"
# remove quotation marks
# shellcheck disable=SC2001
subtree="$(echo "$subtree" | sed 's/\"//g')"
for entry in $subtree; do
if [[ ${entry} =~ "xyz/openbmc_project/logging/block"* ]]; then
blockArray+=( "$entry" )
fi
done
# now find associated error log for each boot block error
for berror in "${blockArray[@]}"; do
assocs="$(busctl call xyz.openbmc_project.Logging "$berror" \
org.freedesktop.DBus.Properties Get \
ss xyz.openbmc_project.Association.Definitions Associations)"
# remove quotation marks
# shellcheck disable=SC2001
assocs="$(echo "$assocs" | sed 's/\"//g')"
for entry in $assocs; do
if [[ ${entry} =~ "xyz/openbmc_project/logging/entry"* ]]; then
echo "Blocking Error: $entry"
fi
done
done
}
# helper function to check for boot block errors and notify user
function check_and_warn_boot_block()
{
blockingErrors=$(check_boot_block_errors)
if [ -n "$blockingErrors" ]; then
echo !!!!!!!!!!
echo "WARNING! System has blocking errors that will prevent boot"
echo "$blockingErrors"
echo !!!!!!!!!!
fi
}
# list all phosphor-logging entries
function list_logs()
{
# Look for any objects under logging that implement the
# xyz.openbmc_project.Logging.Entry
busctl -j call xyz.openbmc_project.ObjectMapper \
/xyz/openbmc_project/object_mapper \
xyz.openbmc_project.ObjectMapper \
GetSubTreePaths sias "/xyz/openbmc_project/logging/" 0 1 \
xyz.openbmc_project.Logging.Entry
}
# display input log details
function show_log()
{
busctl -j call xyz.openbmc_project.Logging \
"$1" \
org.freedesktop.DBus.Properties \
GetAll s xyz.openbmc_project.Logging.Entry
}
# delete all phosphor-logging entries
function delete_logs()
{
busctl call xyz.openbmc_project.Logging \
/xyz/openbmc_project/logging \
xyz.openbmc_project.Collection.DeleteAll DeleteAll
}
# stop all targets associated with powering off a system
function stop_off_targets()
{
systemctl stop \
}
function handle_cmd()
{
case "$1" in
chassisoff)
OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Chassis
PROPERTY=RequestedPowerTransition
VALUE=$INTERFACE.Transition.Off
G_REQUESTED_STATE=$INTERFACE.PowerState.Off
G_QUERY="chassisstate"
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
chassison)
check_and_warn_boot_block
OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Chassis
PROPERTY=RequestedPowerTransition
VALUE=$INTERFACE.Transition.On
G_REQUESTED_STATE=$INTERFACE.PowerState.On
G_QUERY="chassisstate"
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
poweroff)
OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Host
PROPERTY=RequestedHostTransition
VALUE=$INTERFACE.Transition.Off
G_REQUESTED_STATE=$INTERFACE.HostState.Off
G_QUERY="hoststate"
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
poweron)
check_and_warn_boot_block
OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Host
PROPERTY=RequestedHostTransition
VALUE=$INTERFACE.Transition.On
G_REQUESTED_STATE=$INTERFACE.HostState.Running
G_QUERY="hoststate"
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
bmcstate)
OBJECT=$STATE_OBJECT/bmc0
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.BMC
PROPERTY=CurrentBMCState
state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
;;
chassisstate)
OBJECT=$STATE_OBJECT/chassis$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Chassis
PROPERTY=CurrentPowerState
state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
;;
hoststate)
OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Host
PROPERTY=CurrentHostState
state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
;;
osstate)
OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.OperatingSystem.Status
PROPERTY=OperatingSystemState
state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
;;
state|status)
for query in bmcstate chassisstate hoststate bootprogress osstate
do
handle_cmd $query
done
check_and_warn_boot_block
;;
bootprogress)
OBJECT=$STATE_OBJECT/host$G_INSTANCE_ID
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$STATE_INTERFACE.Boot.Progress
PROPERTY=BootProgress
state_query "$SERVICE" $OBJECT $INTERFACE $PROPERTY
;;
power)
OBJECT=/org/openbmc/control/power0
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=org.openbmc.control.Power
for property in pgood state pgood_timeout; do
# get_property can potentially return several
# different formats of values, so we do the parsing outside
# of get_property depending on the query. These queries
# return 'i VALUE' formatted strings.
STATE=$(get_property "$SERVICE" "$OBJECT" "$INTERFACE" "$property" | sed 's/i[ ^I]*//')
printf "%s = %s\n" $property "$STATE"
done
;;
chassiskill)
/usr/libexec/chassiskill
;;
hostrebootoff)
OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
PROPERTY=AutoReboot
VALUE=false
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "b" $VALUE
;;
hostrebootoffonetime)
OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot/one_time
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
PROPERTY=AutoReboot
VALUE=false
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "b" $VALUE
;;
hostrebooton)
OBJECT=$CONTROL_OBJECT/host$G_INSTANCE_ID/auto_reboot
SERVICE=$(mapper get-service $OBJECT)
INTERFACE=$CONTROL_INTERFACE.Boot.RebootPolicy
PROPERTY=AutoReboot
VALUE=true
set_property "$SERVICE" $OBJECT $INTERFACE $PROPERTY "b" $VALUE
;;
bmcrebootoff)
disable_bmc_reboot
;;
bmcrebooton)
enable_bmc_reboot
;;
recoveryoff)
handle_cmd hostrebootoff
handle_cmd bmcrebootoff
mask_systemd_target $HOST_TIMEOUT_TARGET
mask_systemd_target $HOST_CRASH_TARGET
;;
recoveryon)
handle_cmd hostrebooton
handle_cmd bmcrebooton
unmask_systemd_target $HOST_TIMEOUT_TARGET
unmask_systemd_target $HOST_CRASH_TARGET
;;
listbootblock)
blockingErrors=$(check_boot_block_errors)
if [ -z "$blockingErrors" ]; then
echo "No blocking errors present"
else
echo "$blockingErrors"
fi
;;
listlogs)
list_logs
;;
showlog)
show_log "$2"
;;
deletelogs)
delete_logs
;;
stopofftargets)
stop_off_targets
;;
*)
print_usage_err "Invalid command '$1'"
;;
esac
}
shiftcnt=0
for arg in "$@"; do
case $arg in
-w|--wait)
G_WAIT=30
shiftcnt=$((shiftcnt+1))
continue
;;
-h|--help)
print_help
;;
-v|--verbose)
G_VERBOSE=y
shiftcnt=$((shiftcnt+1))
;;
-i=*|--id=*)
G_INSTANCE_ID="${arg#*=}"
shiftcnt=$((shiftcnt+1))
;;
-*)
print_usage_err "Unknown option: $arg"
;;
*)
G_ORIG_CMD=$arg
# shift out the optional parameters
shift $shiftcnt
# pass all arguments to handle_cmd in case command takes additional
# parameters
handle_cmd "$@"
break
;;
esac
done