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
11get_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
19# host instance id is input parameter to function
20host_quiesce()
21{
22    systemctl start obmc-host-quiesce@"$1".target
23}
24
25# host instance id is input parameter to function
26host_reboot()
27{
28    systemctl start obmc-host-startmin@"$1".target
29}
30
31# This service is run as a part of a systemd target to reboot the host
32# As such, it has to first shutdown the host, and then reboot it. Due to
33# systemd complexities with Conflicts statements between the targets to
34# shutdown and to boot, need to start off by sleeping 5 seconds to ensure
35# the shutdown path has completed before starting the boot (or quiesce)
36sleep 5
37
38host_instance=$1
39
40reboot_count=$(get_reboot_count "$host_instance")
41if [ "$reboot_count" -eq 0 ]; then
42    echo "reboot count is 0, go to host quiesce"
43    host_quiesce "$host_instance"
44else
45    echo "reboot count is $reboot_count so reboot host"
46    host_reboot "$host_instance"
47fi
48