22
33declare -r EXIT_SUCCESS=0
44declare -r EXIT_ERROR=1
5+ declare -r EXIT_TIMEOUT=2
56declare -r WATCHDOG_UTIL=" /usr/local/bin/watchdogutil"
67declare -r PRE_REBOOT_HOOK=" pre_reboot_hook"
78
@@ -13,6 +14,7 @@ PLATFORM_UPDATE_REBOOT_CAUSE="platform_update_reboot_cause"
1314REBOOT_CAUSE_FILE=" /host/reboot-cause/reboot-cause.txt"
1415PLATFORM_REBOOT_PRE_CHECK=" platform_reboot_pre_check"
1516REBOOT_TIME=$( date)
17+ REBOOT_CONFIG_FILE=" /etc/sonic/reboot.conf"
1618
1719# Reboot immediately if we run the kdump capture kernel
1820VMCORE_FILE=/proc/vmcore
@@ -53,6 +55,9 @@ SMART_SWITCH="no"
5355DPU_MODULE_NAME=" "
5456REBOOT_DPU=" no"
5557PRE_SHUTDOWN=" no"
58+ BLOCKING_MODE=" no"
59+ BLOCKING_MODE_TIMEOUT_IN_SECOND=180
60+ SHOW_TIMER=" no"
5661
5762function debug()
5863{
@@ -145,6 +150,8 @@ function show_help_and_exit()
145150 echo " -h, -? : getting this help"
146151 echo " -d : DPU module name on a smart switch, option is invalid when on DPU"
147152 echo " -p : Pre-shutdown steps on DPU, invalid on NPU"
153+ echo " -b : Enable the command to run in blocking mode"
154+ echo " -v : Print out detail logs. For blocking mode will print dots to identify the script is running"
148155
149156 exit ${EXIT_SUCCESS}
150157}
@@ -195,13 +202,14 @@ function check_conflict_boot_in_fw_update()
195202
196203function parse_options()
197204{
198- while getopts " h?vfpd:" opt; do
205+ while getopts " h?vfpd:b " opt; do
199206 case ${opt} in
200207 h|\? )
201208 show_help_and_exit
202209 ;;
203210 v )
204211 VERBOSE=yes
212+ SHOW_TIMER=" yes"
205213 ;;
206214 t )
207215 TAG_LATEST=no
@@ -216,6 +224,9 @@ function parse_options()
216224 p )
217225 PRE_SHUTDOWN=" yes"
218226 ;;
227+ b )
228+ BLOCKING_MODE=" yes"
229+ ;;
219230 esac
220231 done
221232}
@@ -239,7 +250,32 @@ function linecard_reboot_notify_supervisor()
239250 fi
240251}
241252
253+ function parse_config_file
254+ {
255+ if [ -f " $REBOOT_CONFIG_FILE " ]; then
256+ while IFS=' =' read -r key value; do
257+ key=$( echo " $key " | xargs)
258+ value=$( echo " $value " | xargs)
259+ case $key in
260+ blocking_mode)
261+ [ " ${value} " = " true" ] && BLOCKING_MODE=" yes"
262+ ;;
263+ blocking_mode_timeout)
264+ [[ " ${value} " =~ ^[0-9]+$ ]] && BLOCKING_MODE_TIMEOUT_IN_SECOND=$( value)
265+ ;;
266+ show_timer)
267+ [ " ${value} " = " true" ] && SHOW_TIMER=" yes"
268+ ;;
269+ * )
270+ debug " WARNING: Unknown config ${key} =${value} finded in config file ${REBOOT_CONFIG_FILE} "
271+ ;;
272+ esac
273+ done < " $REBOOT_CONFIG_FILE "
274+ fi
275+ }
276+
242277parse_options $@
278+ parse_config_file
243279
244280# Exit if not superuser
245281if [[ " $EUID " -ne 0 ]]; then
@@ -341,4 +377,37 @@ if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
341377fi
342378
343379VERBOSE=yes debug " Issuing OS-level reboot ..." >&2
344- exec /sbin/reboot ${REBOOT_FLAGS}
380+
381+ if [[ " ${BLOCKING_MODE} " != " yes" ]]; then
382+ # In non blocking mode, we will remain the original logic.
383+ exec /sbin/reboot ${REBOOT_FLAGS}
384+ else
385+
386+ REBOOT_START_TIME=$( date +%s)
387+ /sbin/reboot ${REBOOT_FLAGS}
388+ if [[ " ${BLOCKING_MODE} " == " yes" ]]; then
389+ # In blocking mode, we just deadloop here to avoid more output generated.
390+ BLOCKING_TIMER=1
391+ sleep 1
392+ while true ; do
393+ if [ " ${SHOW_TIMER} " == " yes" ]; then
394+ if [ " ${BLOCKING_TIMER} " -ge 50 ]; then
395+ BLOCKING_TIMER=1
396+ echo " ."
397+ else
398+ let BLOCKING_TIMER++
399+ echo -n " ."
400+ fi
401+ fi
402+
403+ CURRENT_TIME=$( date +%s)
404+ ELAPSED=$(( CURRENT_TIME - REBOOT_START_TIME))
405+ if (( ELAPSED >= BLOCKING_MODE_TIMEOUT_IN_SECOND )) ; then
406+ echo " [REBOOT FAILED] The reboot used $( ELAPSED) seconds while the timeout is configed as $( BLOCKING_MODE_TIMEOUT_IN_SECOND) seconds"
407+ exit ${EXIT_TIMEOUT}
408+ fi
409+
410+ sleep 10
411+ done
412+ fi
413+ fi
0 commit comments