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

Fixes the reboot/fast-reboot command args validation and gets the user confirmation before reboot. #698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ REBOOT_TYPE="${REBOOT_SCRIPT_NAME}"
VERBOSE=no
FORCE=no
STRICT=no
EXECUTE=no
REBOOT_METHOD="/sbin/kexec -e"
ASSISTANT_IP_LIST=""
ASSISTANT_SCRIPT="/usr/bin/neighbor_advertiser"
Expand Down Expand Up @@ -47,6 +48,7 @@ function showHelpAndExit()
echo " -h,-? : get this help"
echo " -v : turn on verbose"
echo " -f : force execution"
echo " -y : skip the user confirmation"
echo " -r : reboot with /sbin/reboot"
echo " -k : reboot with /sbin/kexec -e [default]"
echo " -x : execute script with -x flag"
Expand All @@ -59,7 +61,7 @@ function showHelpAndExit()

function parseOptions()
{
while getopts "vfh?rkxc:s" opt; do
while getopts "yvfh?rkxc:s" opt; do
case ${opt} in
h|\? )
showHelpAndExit
Expand All @@ -70,6 +72,9 @@ function parseOptions()
f )
FORCE=yes
;;
y )
EXECUTE=yes
;;
r )
REBOOT_METHOD="/sbin/reboot"
;;
Expand All @@ -87,6 +92,14 @@ function parseOptions()
;;
esac
done

shift `expr $OPTIND - 1 `

if [ $# -ge 1 ] ; then
echo "Unsupported reboot option"
showHelpAndExit
exit -1;
fi
}

function clear_fast_boot()
Expand Down Expand Up @@ -288,6 +301,25 @@ function unload_kernel()
fi
}

function confirm_reboot()
{

if [ ${EXECUTE} == 'yes' ]; then
return
fi

read -r -p "Are you sure you want to reboot? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
exit 0
;;
esac

}

# main starts here
parseOptions $@

Expand All @@ -298,6 +330,9 @@ then
exit "${EXIT_FAILURE}"
fi

#Confirm the device reboot
confirm_reboot

sonic_asic_type=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)

# Check reboot type supported
Expand Down
56 changes: 53 additions & 3 deletions scripts/reboot
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ DEVPATH="/usr/share/sonic/device"
PLAT_REBOOT="platform_reboot"
REBOOT_CAUSE_FILE="/host/reboot-cause/reboot-cause.txt"
VERBOSE=no
EXECUTE=no
FORCE=no
EXIT_NEXT_IMAGE_NOT_EXISTS=4

function debug()
Expand Down Expand Up @@ -50,6 +52,8 @@ function show_help_and_exit()
echo " "
echo " Available options:"
echo " -h, -? : getting this help"
echo " -y : skip the user confirmation"
echo " -f : force reboot"

exit 0
}
Expand Down Expand Up @@ -79,18 +83,61 @@ function reboot_pre_check()
fi
}

ARGS=""
function parse_options()
{
while getopts "h?v" opt; do
while getopts "h?vfy" opt; do
case ${opt} in
h|\? )
show_help_and_exit
;;
y )
EXECUTE=yes
;;
f )
FORCE=yes
ARGS=$(printf "%s -%s" "${ARGS}" "${opt}" )
;;
v )
VERBOSE=yes
ARGS=$(printf "%s -%s" "${ARGS}" "${opt}" )
;;
esac
done
shift `expr $OPTIND - 1 `


for arg in $@; do
ARGS=$(printf "%s %s" "${ARGS}" "${arg}" )
done


if [ $# -ge 1 ] ; then
echo "Unsupported reboot option"
show_help_and_exit
exit -1;
fi
}

function confirm_reboot()
{
if [ ${EXECUTE} == 'yes' ]; then
return
fi

echo -e "Warning: This command would cause the switch to reboot"
echo -e "and result in traffic disruption."

read -r -p "Are you sure you want to reboot? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
exit 0
;;
esac

}

parse_options $@
Expand All @@ -101,6 +148,9 @@ if [[ "$EUID" -ne 0 ]]; then
exit 1
fi

#Confirm the device reboot
confirm_reboot

debug "User requested rebooting device ..."

setup_reboot_variables
Expand All @@ -126,10 +176,10 @@ fi

if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
VERBOSE=yes debug "Rebooting with platform ${PLATFORM} specific tool ..."
exec ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} $@
exec ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ${ARGS}
else
# If no platform-specific reboot tool, just run /sbin/reboot
exec /sbin/reboot $@
exec /sbin/reboot ${ARGS}
fi

# Should never reach here
Expand Down