forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreboot
executable file
·262 lines (227 loc) · 7.93 KB
/
reboot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
DEVPATH="/usr/share/sonic/device"
PLAT_REBOOT="platform_reboot"
PLATFORM_UPDATE_REBOOT_CAUSE="platform_update_reboot_cause"
REBOOT_CAUSE_FILE="/host/reboot-cause/reboot-cause.txt"
PLATFORM_REBOOT_PRE_CHECK="platform_reboot_pre_check"
REBOOT_TIME=$(date)
# Reboot immediately if we run the kdump capture kernel
VMCORE_FILE=/proc/vmcore
if [ -e $VMCORE_FILE -a -s $VMCORE_FILE ]; then
echo "We have a /proc/vmcore, then we just kdump'ed"
echo "Kernel Panic [Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE}
sync
PLATFORM=$(grep -oP 'sonic_platform=\K\S+' /proc/cmdline)
if [ ! -z "${PLATFORM}" -a -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
exec ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT}
fi
# If no platform-specific reboot tool, just run /sbin/reboot
/sbin/reboot
echo b > /proc/sysrq-trigger
fi
REBOOT_USER=$(logname)
PLATFORM=$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)
ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)
SUBTYPE=$(sonic-cfggen -d -v DEVICE_METADATA.localhost.subtype)
ASAN=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asan)
VERBOSE=no
EXIT_NEXT_IMAGE_NOT_EXISTS=4
EXIT_SONIC_INSTALLER_VERIFY_REBOOT=21
EXIT_PLATFORM_FW_AU_FAILURE=22
PLATFORM_FWUTIL_AU_REBOOT_HANDLE="platform_fw_au_reboot_handle"
REBOOT_SCRIPT_NAME=$(basename $0)
REBOOT_TYPE="${REBOOT_SCRIPT_NAME}"
TAG_LATEST=yes
function debug()
{
if [[ x"${VERBOSE}" == x"yes" ]]; then
echo `date` $@
fi
logger "$@"
}
function tag_images()
{
if test -f /usr/local/bin/ctrmgr_tools.py
then
if [[ x"${TAG_LATEST}" == x"yes" ]]; then
/usr/local/bin/ctrmgr_tools.py tag-all
fi
fi
}
function stop_pmon_service()
{
CONTAINER_STOP_RC=0
debug "Stopping pmon docker"
docker kill pmon &> /dev/null || CONTAINER_STOP_RC=$?
systemctl stop pmon || debug "Ignore stopping pmon error $?"
if [[ CONTAINER_STOP_RC -ne 0 ]]; then
debug "Failed killing container pmon RC $CONTAINER_STOP_RC ."
fi
}
function stop_sonic_services()
{
if [[ x"$SUBTYPE" == x"DualToR" ]]; then
debug "DualToR detected, stopping mux container before reboot..."
systemctl stop mux
fi
if [[ x"$ASIC_TYPE" != x"mellanox" ]]; then
ASIC_CONF=${DEVPATH}/$PLATFORM/asic.conf
if [ -f "$ASIC_CONF" ]; then
source $ASIC_CONF
fi
if [[ ($NUM_ASIC -gt 1) ]]; then
asic_num=0
while [[ ($asic_num -lt $NUM_ASIC) ]]; do
debug "Stopping syncd$asic_num process..."
docker exec -i syncd$asic_num /usr/bin/syncd_request_shutdown --cold > /dev/null
((asic_num = asic_num + 1))
done
else
debug "Stopping syncd process..."
docker exec -i syncd /usr/bin/syncd_request_shutdown --cold > /dev/null
fi
sleep 3
fi
stop_pmon_service
}
function stop_services_asan()
{
debug "Stopping swss for ASAN"
systemctl stop swss
}
function clear_warm_boot()
{
# If reboot is requested, make sure the outstanding warm-boot is cleared
# So the system will come up from a cold boot.
WARM_DIR="/host/warmboot"
REDIS_FILE=dump.rdb
TIMESTAMP=`date +%Y%m%d-%H%M%S`
if [[ -f ${WARM_DIR}/${REDIS_FILE} ]]; then
mv -f ${WARM_DIR}/${REDIS_FILE} ${WARM_DIR}/${REDIS_FILE}.${TIMESTAMP} || /bin/true
fi
/sbin/kexec -u || /bin/true
}
SCRIPT=$0
function show_help_and_exit()
{
echo "Usage ${SCRIPT} [options]"
echo " Request rebooting the device. Invoke platform-specific tool when available."
echo " This script will shutdown syncd before rebooting."
echo " "
echo " Available options:"
echo " -h, -? : getting this help"
exit 0
}
function setup_reboot_variables()
{
NEXT_SONIC_IMAGE=$(sonic-installer list | grep "Next: " | cut -d ' ' -f 2)
IMAGE_PATH="/host/image-${NEXT_SONIC_IMAGE#SONiC-OS-}"
}
function reboot_pre_check()
{
# Make sure that the file system is normal: read-write able
filename="/host/test-`date +%Y%m%d-%H%M%S`"
ERR=0
touch ${filename} || ERR=$?
if [[ ${ERR} -ne 0 ]]; then
# Continue rebooting in this case, but log the error
VERBOSE=yes debug "Filesystem might be read-only or full ..."
fi
rm ${filename}
if [ -x ${DEVPATH}/${PLATFORM}/${PLATFORM_REBOOT_PRE_CHECK} ]; then
${DEVPATH}/${PLATFORM}/${PLATFORM_REBOOT_PRE_CHECK}
[[ $? -ne 0 ]] && exit $?
fi
# Verify the next image by sonic-installer
local message=$(sonic-installer verify-next-image 2>&1)
if [ $? -ne 0 ]; then
VERBOSE=yes debug "Failed to verify next image: ${message}"
exit ${EXIT_SONIC_INSTALLER_VERIFY_REBOOT}
fi
}
function check_conflict_boot_in_fw_update()
{
# Make sure firmware auto update is not scheduled for a different reboot
FIRMWARE_AU_STATUS_DIR="/tmp/firmwareupdate"
FW_AU_TASK_FILE_REGEX="${FIRMWARE_AU_STATUS_DIR}/*_fw_au_task"
FW_AU_TASK_FILE_EXP="${FIRMWARE_AU_STATUS_DIR}/cold_fw_au_task"
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
fi
}
function parse_options()
{
while getopts "h?v" opt; do
case ${opt} in
h|\? )
show_help_and_exit
;;
v )
VERBOSE=yes
;;
t )
TAG_LATEST=no
;;
esac
done
}
parse_options $@
# Exit if not superuser
if [[ "$EUID" -ne 0 ]]; then
echo "This command must be run as root" >&2
exit 1
fi
debug "User requested rebooting device ..."
check_conflict_boot_in_fw_update
setup_reboot_variables
reboot_pre_check
# Tag remotely deployed images as local
tag_images
# Stop SONiC services gracefully.
stop_sonic_services
# Stop ASAN-enabled services so the report can be generated
if [[ x"$ASAN" == x"yes" ]]; then
stop_services_asan
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
echo "User issued 'reboot' command [User: ${REBOOT_USER}, Time: ${REBOOT_TIME}]" > ${REBOOT_CAUSE_FILE}
sync
/sbin/fstrim -av
sleep 3
# sync the current system time to CMOS
if [ -x /sbin/hwclock ]; then
/sbin/hwclock -w || /bin/true
fi
if [[ -x ${DEVPATH}/${PLATFORM}/${PLATFORM_FWUTIL_AU_REBOOT_HANDLE} ]]; then
debug "Handling task file for boot type ${REBOOT_TYPE}"
${DEVPATH}/${PLATFORM}/${PLATFORM_FWUTIL_AU_REBOOT_HANDLE} ${REBOOT_TYPE} || PLATFORM_FW_AU_RC=$?
if [[ $PLATFORM_FW_AU_RC -ne 0 ]]; then
debug "ERROR: Failed to handle the platform firmware auto-update for ${REBOOT_TYPE} Exit code: $PLATFORM_FW_AU_RC"
exit "${EXIT_PLATFORM_FW_AU_FAILURE}"
fi
fi
if [ -x ${DEVPATH}/${PLATFORM}/${PLATFORM_UPDATE_REBOOT_CAUSE} ]; then
debug "updating reboot cause for ${PLATFORM}"
${DEVPATH}/${PLATFORM}/${PLATFORM_UPDATE_REBOOT_CAUSE}
fi
if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
VERBOSE=yes debug "Rebooting with platform ${PLATFORM} specific tool ..."
${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} $@
# There are a couple reasons execution reaches here:
#
# 1. The vendor platform reboot returned after scheduled the platform specific reboot.
# This is a vendor platform reboot code bug but it happens.
# 2. The vendor platform reboot failed. e.g. due to platform driver didn't load properly.
#
# As result if the reboot script reaches here. We should make the reboot happen.
# Sleep 1 second before calling /sbin/reboot to accommodate situation #1 above.
sleep 1
VERBOSE=yes debug "Platform specific reboot failed!" >&2
fi
VERBOSE=yes debug "Issuing OS-level reboot ..." >&2
exec /sbin/reboot $@