Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[201911] Backport: Add the pre_reboot_hook script execution, add the watchdog arm before the reboot #3760

Open
wants to merge 2 commits into
base: 201911
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions scripts/reboot
Original file line number Diff line number Diff line change
@@ -1,5 +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"

REBOOT_USER=$(logname)
REBOOT_TIME=$(date)
PLATFORM=$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)
Expand All @@ -13,6 +18,8 @@ PLATFORM_REBOOT_PRE_CHECK="platform_reboot_pre_check"
VERBOSE=no
EXIT_NEXT_IMAGE_NOT_EXISTS=4
SSD_FW_UPDATE="ssd-fw-upgrade"
REBOOT_FLAGS=""
FORCE_REBOOT="no"

function debug()
{
Expand Down Expand Up @@ -67,7 +74,7 @@ function show_help_and_exit()
echo " Available options:"
echo " -h, -? : getting this help"

exit 0
exit ${EXIT_SUCCESS}
}

function setup_reboot_variables()
Expand All @@ -89,8 +96,10 @@ function reboot_pre_check()
rm ${filename}

if [ -x ${DEVPATH}/${PLATFORM}/${PLATFORM_REBOOT_PRE_CHECK} ]; then
${DEVPATH}/${PLATFORM}/${PLATFORM_REBOOT_PRE_CHECK}
[[ $? -ne 0 ]] && exit
if ! ${DEVPATH}/${PLATFORM}/${PLATFORM_REBOOT_PRE_CHECK}; then
echo "Platform reboot pre-check failed, exiting..."
exit ${EXIT_ERROR}
fi
fi

# Make sure that the next image exists
Expand All @@ -102,14 +111,18 @@ function reboot_pre_check()

function parse_options()
{
while getopts "h?v" opt; do
while getopts "h?vf" opt; do
case ${opt} in
h|\? )
show_help_and_exit
;;
v )
VERBOSE=yes
;;
f )
REBOOT_FLAGS+=" -f"
FORCE_REBOOT="yes"
;;
esac
done
}
Expand All @@ -119,11 +132,28 @@ 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 ..."

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

setup_reboot_variables
reboot_pre_check

Expand Down Expand Up @@ -169,4 +199,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}