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 11*d182bff5SPatrick Williamsfunction get_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*d182bff5SPatrick Williamsfunction get_restart_cause() 2055324b49SAndrew Geissler{ 2155324b49SAndrew Geissler busctl get-property xyz.openbmc_project.State.Host"$1" \ 2255324b49SAndrew Geissler /xyz/openbmc_project/state/host"$1" \ 2355324b49SAndrew Geissler xyz.openbmc_project.State.Host RestartCause \ 2455324b49SAndrew Geissler | cut -d '"' -f2 2555324b49SAndrew Geissler} 2655324b49SAndrew Geissler 27128ea8e3SAndrew Geissler# host instance id is input parameter to function 28*d182bff5SPatrick Williamsfunction host_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 34*d182bff5SPatrick Williamsfunction host_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 4855324b49SAndrew Geissler# Normally the standard power on path will determine if the system has reached 4955324b49SAndrew Geissler# its reboot count and halt in Quiesce if so. But in the host-crash path this 5055324b49SAndrew Geissler# standard path is not utilized, as it goes only through systemd targets. To 5155324b49SAndrew Geissler# ensure the system does not end up in an endless reboot loop, put host into 5255324b49SAndrew Geissler# Quiesce if reboot count is exhausted and the reason for the reboot was a 5355324b49SAndrew Geissler# HostCrash 5426854617SPotin Laireboot_count=$(get_reboot_count "$host_instance") 5555324b49SAndrew Geisslerrestart_cause=$(get_restart_cause "$host_instance") 5655324b49SAndrew Geisslerif [ "$reboot_count" -eq 0 ] && \ 5755324b49SAndrew Geissler [ "$restart_cause" == "xyz.openbmc_project.State.Host.RestartCause.HostCrash" ]; 5855324b49SAndrew Geisslerthen 5955324b49SAndrew Geissler echo "reboot count is 0 and host crashed, go to host quiesce" 60128ea8e3SAndrew Geissler host_quiesce "$host_instance" 61128ea8e3SAndrew Geisslerelse 6255324b49SAndrew Geissler echo "reboot count ($reboot_count) is greater then 0 or host did not" \ 6355324b49SAndrew Geissler "crash so reboot host" 64128ea8e3SAndrew Geissler host_reboot "$host_instance" 65128ea8e3SAndrew Geisslerfi 66