From a634b27a8dffb99ed2e3978f1a758467a9438bb5 Mon Sep 17 00:00:00 2001 From: Vadym Hlushko <62022266+vadymhlushko-mlnx@users.noreply.github.com> Date: Fri, 12 Apr 2024 19:50:32 +0200 Subject: [PATCH] [graceful reboot] Add watchdog, add execution of pre_reboot_hook, fix --force flag bug (#3191) Signed-off-by: vadymhlushko-mlnx --- scripts/reboot | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/scripts/reboot b/scripts/reboot index 2d1cd8a87..5009e6df6 100755 --- a/scripts/reboot +++ b/scripts/reboot @@ -1,4 +1,10 @@ #!/bin/bash + +declare -r EXIT_SUCCESS=0 +declare -r EXIT_ERROR=1 +declare -r WATCHDOG_UTIL="/usr/local/bin/watchdogutil" +declare -r PRE_REBOOT_HOOK="pre_reboot_hook" + DEVPATH="/usr/share/sonic/device" PLAT_REBOOT="platform_reboot" PLATFORM_UPDATE_REBOOT_CAUSE="platform_update_reboot_cause" @@ -34,6 +40,8 @@ PLATFORM_FWUTIL_AU_REBOOT_HANDLE="platform_fw_au_reboot_handle" REBOOT_SCRIPT_NAME=$(basename $0) REBOOT_TYPE="${REBOOT_SCRIPT_NAME}" TAG_LATEST=no +REBOOT_FLAGS="" +FORCE_REBOOT="no" function debug() { @@ -123,7 +131,7 @@ function show_help_and_exit() echo " -h, -? : getting this help" echo " -f : execute reboot force" - exit 0 + exit ${EXIT_SUCCESS} } function setup_reboot_variables() @@ -166,7 +174,7 @@ function check_conflict_boot_in_fw_update() FW_AU_TASK_FILE=$(compgen -G ${FW_AU_TASK_FILE_REGEX}) || true if [[ -n "${FW_AU_TASK_FILE}" ]] && [[ ! -f "${FW_AU_TASK_FILE_EXP}" ]]; then VERBOSE=yes debug "Firmware auto update scheduled for a different reboot: ${FW_AU_TASK_FILE}" - exit 1 + exit ${EXIT_ERROR} fi } @@ -183,6 +191,10 @@ function parse_options() t ) TAG_LATEST=no ;; + f ) + REBOOT_FLAGS+=" -f" + FORCE_REBOOT="yes" + ;; esac done } @@ -192,7 +204,7 @@ parse_options $@ # Exit if not superuser if [[ "$EUID" -ne 0 ]]; then echo "This command must be run as root" >&2 - exit 1 + exit ${EXIT_ERROR} fi debug "User requested rebooting device ..." @@ -242,6 +254,23 @@ if [ -x ${DEVPATH}/${PLATFORM}/${PLATFORM_UPDATE_REBOOT_CAUSE} ]; then ${DEVPATH}/${PLATFORM}/${PLATFORM_UPDATE_REBOOT_CAUSE} fi +if [ -x ${DEVPATH}/${PLATFORM}/${PRE_REBOOT_HOOK} ]; then + debug "Executing the pre-reboot script" + ${DEVPATH}/${PLATFORM}/${PRE_REBOOT_HOOK} + EXIT_CODE=$? + if [[ ${EXIT_CODE} != ${EXIT_SUCCESS} ]]; then + if [[ "${FORCE_REBOOT}" != "yes" ]]; then + echo "Reboot is interrupted: use -f (force) to override" + exit ${EXIT_ERROR} + fi + fi +fi + +if [ -x ${WATCHDOG_UTIL} ]; then + debug "Enabling the Watchdog before reboot" + ${WATCHDOG_UTIL} arm +fi + if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then VERBOSE=yes debug "Rebooting with platform ${PLATFORM} specific tool ..." ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} $@ @@ -260,4 +289,4 @@ if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then fi VERBOSE=yes debug "Issuing OS-level reboot ..." >&2 -exec /sbin/reboot $@ +exec /sbin/reboot ${REBOOT_FLAGS}