1#!/bin/bash -e
2# A script which is called in a host-reboot path to check the host reboot count
3# and either initiate the host firmware boot, or move it to the quiesce state
4# (if the reboot count has been reached)
5
6# This script has a single required parameter, the instance number of the
7# host that is being rebooted (or quiesced)
8
9set -euo pipefail
10
11function get_reboot_count()
12{
13    busctl get-property xyz.openbmc_project.State.Host"$1" \
14        /xyz/openbmc_project/state/host"$1" \
15        xyz.openbmc_project.Control.Boot.RebootAttempts AttemptsLeft \
16        | cut -d ' ' -f2
17}
18
19function get_restart_cause()
20{
21    busctl get-property xyz.openbmc_project.State.Host"$1" \
22        /xyz/openbmc_project/state/host"$1" \
23        xyz.openbmc_project.State.Host RestartCause \
24        | cut -d '"' -f2
25}
26
27# host instance id is input parameter to function
28function host_quiesce()
29{
30    systemctl start obmc-host-quiesce@"$1".target
31}
32
33# host instance id is input parameter to function
34function host_reboot()
35{
36    systemctl start obmc-host-startmin@"$1".target
37}
38
39# This service is run as a part of a systemd target to reboot the host
40# As such, it has to first shutdown the host, and then reboot it. Due to
41# systemd complexities with Conflicts statements between the targets to
42# shutdown and to boot, need to start off by sleeping 5 seconds to ensure
43# the shutdown path has completed before starting the boot (or quiesce)
44sleep 5
45
46host_instance=$1
47
48# Normally the standard power on path will determine if the system has reached
49# its reboot count and halt in Quiesce if so. But in the host-crash path this
50# standard path is not utilized, as it goes only through systemd targets. To
51# ensure the system does not end up in an endless reboot loop, put host into
52# Quiesce if reboot count is exhausted and the reason for the reboot was a
53# HostCrash
54reboot_count=$(get_reboot_count "$host_instance")
55restart_cause=$(get_restart_cause "$host_instance")
56if [ "$reboot_count" -eq 0 ] && \
57    [ "$restart_cause" == "xyz.openbmc_project.State.Host.RestartCause.HostCrash" ];
58then
59    echo "reboot count is 0 and host crashed, go to host quiesce"
60    host_quiesce "$host_instance"
61else
62    echo "reboot count ($reboot_count) is greater then 0 or host did not" \
63        "crash so reboot host"
64    host_reboot "$host_instance"
65fi
66