xref: /openbmc/phosphor-state-manager/scripts/host-reboot (revision 55324b49a0d9a27949809f06376404d6cd85cbd6)
1128ea8e3SAndrew Geissler#!/bin/bash -e
2128ea8e3SAndrew Geissler# A script which is called in a host-reboot path to check the host reboot count
3128ea8e3SAndrew Geissler# and either initiate the host firmware boot, or move it to the quiesce state
4128ea8e3SAndrew Geissler# (if the reboot count has been reached)
5128ea8e3SAndrew Geissler
6128ea8e3SAndrew Geissler# This script has a single required parameter, the instance number of the
7128ea8e3SAndrew Geissler# host that is being rebooted (or quiesced)
8128ea8e3SAndrew Geissler
9128ea8e3SAndrew Geisslerset -euo pipefail
10128ea8e3SAndrew Geissler
11128ea8e3SAndrew Geisslerget_reboot_count()
12128ea8e3SAndrew Geissler{
1326854617SPotin Lai    busctl get-property xyz.openbmc_project.State.Host"$1" \
1426854617SPotin Lai        /xyz/openbmc_project/state/host"$1" \
15128ea8e3SAndrew Geissler        xyz.openbmc_project.Control.Boot.RebootAttempts AttemptsLeft \
16128ea8e3SAndrew Geissler        | cut -d ' ' -f2
17128ea8e3SAndrew Geissler}
18128ea8e3SAndrew Geissler
19*55324b49SAndrew Geisslerget_restart_cause()
20*55324b49SAndrew Geissler{
21*55324b49SAndrew Geissler    busctl get-property xyz.openbmc_project.State.Host"$1" \
22*55324b49SAndrew Geissler        /xyz/openbmc_project/state/host"$1" \
23*55324b49SAndrew Geissler        xyz.openbmc_project.State.Host RestartCause \
24*55324b49SAndrew Geissler        | cut -d '"' -f2
25*55324b49SAndrew Geissler}
26*55324b49SAndrew Geissler
27128ea8e3SAndrew Geissler# host instance id is input parameter to function
28128ea8e3SAndrew Geisslerhost_quiesce()
29128ea8e3SAndrew Geissler{
30128ea8e3SAndrew Geissler    systemctl start obmc-host-quiesce@"$1".target
31128ea8e3SAndrew Geissler}
32128ea8e3SAndrew Geissler
33128ea8e3SAndrew Geissler# host instance id is input parameter to function
34128ea8e3SAndrew Geisslerhost_reboot()
35128ea8e3SAndrew Geissler{
36128ea8e3SAndrew Geissler    systemctl start obmc-host-startmin@"$1".target
37128ea8e3SAndrew Geissler}
38128ea8e3SAndrew Geissler
39128ea8e3SAndrew Geissler# This service is run as a part of a systemd target to reboot the host
40128ea8e3SAndrew Geissler# As such, it has to first shutdown the host, and then reboot it. Due to
41128ea8e3SAndrew Geissler# systemd complexities with Conflicts statements between the targets to
42128ea8e3SAndrew Geissler# shutdown and to boot, need to start off by sleeping 5 seconds to ensure
43128ea8e3SAndrew Geissler# the shutdown path has completed before starting the boot (or quiesce)
44128ea8e3SAndrew Geisslersleep 5
45128ea8e3SAndrew Geissler
46128ea8e3SAndrew Geisslerhost_instance=$1
47128ea8e3SAndrew Geissler
48*55324b49SAndrew Geissler# Normally the standard power on path will determine if the system has reached
49*55324b49SAndrew Geissler# its reboot count and halt in Quiesce if so. But in the host-crash path this
50*55324b49SAndrew Geissler# standard path is not utilized, as it goes only through systemd targets. To
51*55324b49SAndrew Geissler# ensure the system does not end up in an endless reboot loop, put host into
52*55324b49SAndrew Geissler# Quiesce if reboot count is exhausted and the reason for the reboot was a
53*55324b49SAndrew Geissler# HostCrash
5426854617SPotin Laireboot_count=$(get_reboot_count "$host_instance")
55*55324b49SAndrew Geisslerrestart_cause=$(get_restart_cause "$host_instance")
56*55324b49SAndrew Geisslerif [ "$reboot_count" -eq 0 ] && \
57*55324b49SAndrew Geissler   [ "$restart_cause" == "xyz.openbmc_project.State.Host.RestartCause.HostCrash" ];
58*55324b49SAndrew Geisslerthen
59*55324b49SAndrew Geissler    echo "reboot count is 0 and host crashed, go to host quiesce"
60128ea8e3SAndrew Geissler    host_quiesce "$host_instance"
61128ea8e3SAndrew Geisslerelse
62*55324b49SAndrew Geissler    echo "reboot count ($reboot_count) is greater then 0 or host did not" \
63*55324b49SAndrew Geissler         "crash so reboot host"
64128ea8e3SAndrew Geissler    host_reboot "$host_instance"
65128ea8e3SAndrew Geisslerfi
66