From 23dbc9344ce4df7f4f1d3cda75dfac7ec789572d Mon Sep 17 00:00:00 2001 From: Shyam Kumar Date: Mon, 5 Feb 2024 22:01:40 -0800 Subject: [PATCH 1/2] reboot mechanism enhanced with reboot cause - Prior to this change, reboot script is only externally trigged where reboot cause is always fixed i.e. (default's to user issued reboot command) - In this PR (changeset), SONiC reboot is enhanced to accept reboot cause (reason) as an option (argument) - This would enable system applications' (e.g. Fault Manager) to pass reboot cause as an argument while initiating (system level) reboot - This reboot cause in turn is saved to REBOOT_CAUSE_FILE for tracking and debugging purposes Signed-off-by: Shyam Kumar --- scripts/reboot | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/scripts/reboot b/scripts/reboot index 2d1cd8a87c..7b39cb22ab 100755 --- a/scripts/reboot +++ b/scripts/reboot @@ -34,6 +34,7 @@ PLATFORM_FWUTIL_AU_REBOOT_HANDLE="platform_fw_au_reboot_handle" REBOOT_SCRIPT_NAME=$(basename $0) REBOOT_TYPE="${REBOOT_SCRIPT_NAME}" TAG_LATEST=no +REBOOT_CAUSE=null function debug() { @@ -121,8 +122,8 @@ function show_help_and_exit() echo " " echo " Available options:" echo " -h, -? : getting this help" - echo " -f : execute reboot force" - + echo " -f : execute reboot force" + echo " -r : specify reboot cause (reason) string in quotes" exit 0 } @@ -172,22 +173,33 @@ function check_conflict_boot_in_fw_update() function parse_options() { - while getopts "h?vf" opt; do + while getopts "h?vftr:" opt; do case ${opt} in h|\? ) show_help_and_exit ;; + f ) + REBOOT_FORCE=true + echo "force: $REBOOT_FORCE" + ;; v ) VERBOSE=yes + echo "verbose: $VERBOSE" ;; t ) TAG_LATEST=no + echo "tag_latest: $TAG_LATEST" + ;; + r ) + REBOOT_CAUSE="$OPTARG" + # alternatives: $OPTARG or ${OPTARG} or "${OPTARG[@]}" + echo "reboot_cause: ${REBOOT_CAUSE}" ;; esac done } -parse_options $@ +parse_options "$@" # Exit if not superuser if [[ "$EUID" -ne 0 ]]; then @@ -218,7 +230,11 @@ clear_warm_boot # Update the reboot cause file to reflect that user issued 'reboot' command # Upon next boot, the contents of this file will be used to determine the # cause of the previous reboot -echo "User issued 'reboot' command [User: ${REBOOT_USER}, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE} +if [[ ${REBOOT_CAUSE} != null ]]; then + echo "System triggered reboot. reason: '${REBOOT_CAUSE}' [User: system, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE} +else + echo "User issued 'reboot' command [User: ${REBOOT_USER}, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE} +fi sync /sbin/fstrim -av sleep 3 @@ -260,4 +276,18 @@ if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then fi VERBOSE=yes debug "Issuing OS-level reboot ..." >&2 -exec /sbin/reboot $@ +echo "Going to issue reboot command..." +if [ $# -eq 0 ]; then + echo "no argument passed" + exec /sbin/reboot $@ +elif [ $# -eq 1 ] && [[ $REBOOT_FORCE == true ]]; then + echo "force reboot" + exec /sbin/reboot $@ +elif [ $# -eq 2 ] && [[ ${REBOOT_CAUSE} != null ]]; then + echo "reboot cause specified" + exec /sbin/reboot +elif [ $# -gt 2 ] && [[ $REBOOT_FORCE == true ]] && [[ ${REBOOT_CAUSE} != null ]]; then + echo "both options - force reboot and reboot cause - specified" + exec /sbin/reboot -f +else echo "No appropriate match found, exiting" +fi From b83ac403b0ce213d78ecd4233916653a8d702aae Mon Sep 17 00:00:00 2001 From: Shyam Kumar Date: Tue, 13 Feb 2024 17:58:23 -0800 Subject: [PATCH 2/2] reboot flow enhancement to support -r option - Addressed PR review comments Signed-off-by: Shyam Kumar --- scripts/reboot | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/scripts/reboot b/scripts/reboot index 7b39cb22ab..bc78cdee92 100755 --- a/scripts/reboot +++ b/scripts/reboot @@ -34,7 +34,7 @@ PLATFORM_FWUTIL_AU_REBOOT_HANDLE="platform_fw_au_reboot_handle" REBOOT_SCRIPT_NAME=$(basename $0) REBOOT_TYPE="${REBOOT_SCRIPT_NAME}" TAG_LATEST=no -REBOOT_CAUSE=null +REBOOT_CAUSE="" function debug() { @@ -193,7 +193,7 @@ function parse_options() r ) REBOOT_CAUSE="$OPTARG" # alternatives: $OPTARG or ${OPTARG} or "${OPTARG[@]}" - echo "reboot_cause: ${REBOOT_CAUSE}" + echo "reboot_cause: $REBOOT_CAUSE" ;; esac done @@ -227,10 +227,13 @@ fi clear_warm_boot -# Update the reboot cause file to reflect that user issued 'reboot' command -# Upon next boot, the contents of this file will be used to determine the -# cause of the previous reboot -if [[ ${REBOOT_CAUSE} != null ]]; then +# Update the 'reboot cause' file to reflect the right reboot reason. +# If 'reboot cause' is specified via the -r option, then update the same to +# this file; else update the 'reboot cause' to reflect that user issued the +# 'reboot' command. +# Upon next boot, the contents of this file will be used to determine +# the cause of the previous reboot. +if [[ -n $REBOOT_CAUSE ]]; then echo "System triggered reboot. reason: '${REBOOT_CAUSE}' [User: system, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE} else echo "User issued 'reboot' command [User: ${REBOOT_USER}, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE} @@ -276,18 +279,12 @@ if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then fi VERBOSE=yes debug "Issuing OS-level reboot ..." >&2 -echo "Going to issue reboot command..." -if [ $# -eq 0 ]; then - echo "no argument passed" - exec /sbin/reboot $@ -elif [ $# -eq 1 ] && [[ $REBOOT_FORCE == true ]]; then - echo "force reboot" - exec /sbin/reboot $@ -elif [ $# -eq 2 ] && [[ ${REBOOT_CAUSE} != null ]]; then +if [ $# -eq 2 ] && [ -n "$REBOOT_CAUSE" ]; then echo "reboot cause specified" exec /sbin/reboot -elif [ $# -gt 2 ] && [[ $REBOOT_FORCE == true ]] && [[ ${REBOOT_CAUSE} != null ]]; then +elif [ $# -gt 2 ] && [[ $REBOOT_FORCE == true ]] && [[ -n $REBOOT_CAUSE ]]; then echo "both options - force reboot and reboot cause - specified" exec /sbin/reboot -f -else echo "No appropriate match found, exiting" +else + exec /sbin/reboot $@ fi