| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 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 |  | 
|  | 9 | set -euo pipefail | 
|  | 10 |  | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 11 | function get_reboot_count() | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 12 | { | 
| Potin Lai | 2685461 | 2022-08-19 09:24:45 +0000 | [diff] [blame] | 13 | busctl get-property xyz.openbmc_project.State.Host"$1" \ | 
|  | 14 | /xyz/openbmc_project/state/host"$1" \ | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 15 | xyz.openbmc_project.Control.Boot.RebootAttempts AttemptsLeft \ | 
|  | 16 | | cut -d ' ' -f2 | 
|  | 17 | } | 
|  | 18 |  | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 19 | function get_restart_cause() | 
| Andrew Geissler | 55324b4 | 2022-10-27 09:19:19 -0500 | [diff] [blame] | 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 |  | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 27 | # host instance id is input parameter to function | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 28 | function host_quiesce() | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 29 | { | 
|  | 30 | systemctl start obmc-host-quiesce@"$1".target | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | # host instance id is input parameter to function | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 34 | function host_reboot() | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 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) | 
|  | 44 | sleep 5 | 
|  | 45 |  | 
|  | 46 | host_instance=$1 | 
|  | 47 |  | 
| Andrew Geissler | 55324b4 | 2022-10-27 09:19:19 -0500 | [diff] [blame] | 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 | 
| Potin Lai | 2685461 | 2022-08-19 09:24:45 +0000 | [diff] [blame] | 54 | reboot_count=$(get_reboot_count "$host_instance") | 
| Andrew Geissler | 55324b4 | 2022-10-27 09:19:19 -0500 | [diff] [blame] | 55 | restart_cause=$(get_restart_cause "$host_instance") | 
|  | 56 | if [ "$reboot_count" -eq 0 ] && \ | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 57 | [ "$restart_cause" == "xyz.openbmc_project.State.Host.RestartCause.HostCrash" ]; | 
| Andrew Geissler | 55324b4 | 2022-10-27 09:19:19 -0500 | [diff] [blame] | 58 | then | 
|  | 59 | echo "reboot count is 0 and host crashed, go to host quiesce" | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 60 | host_quiesce "$host_instance" | 
|  | 61 | else | 
| Andrew Geissler | 55324b4 | 2022-10-27 09:19:19 -0500 | [diff] [blame] | 62 | echo "reboot count ($reboot_count) is greater then 0 or host did not" \ | 
| Patrick Williams | d182bff | 2022-12-08 06:39:12 -0600 | [diff] [blame] | 63 | "crash so reboot host" | 
| Andrew Geissler | 128ea8e | 2022-06-22 12:28:15 -0400 | [diff] [blame] | 64 | host_reboot "$host_instance" | 
|  | 65 | fi |