2020-06-15 06:25:49 -05:00
|
|
|
#!/bin/bash
|
2020-01-28 23:55:50 -06:00
|
|
|
#
|
2021-10-17 11:03:02 -05:00
|
|
|
# Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES.
|
|
|
|
# Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2020-01-28 23:55:50 -06:00
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
this_script="$(basename $(realpath ${0}))"
|
|
|
|
lock_file="/var/run/${this_script%.*}.lock"
|
2020-01-28 23:55:50 -06:00
|
|
|
|
|
|
|
onie_mount=/mnt/onie-boot
|
2020-06-15 06:25:49 -05:00
|
|
|
onie_lib=/lib/onie
|
2020-01-28 23:55:50 -06:00
|
|
|
os_boot=/host
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
print_help() {
|
2020-01-28 23:55:50 -06:00
|
|
|
cat <<EOF
|
|
|
|
update
|
|
|
|
The 'update' command will reboot system to ONIE update mode
|
2020-06-15 06:25:49 -05:00
|
|
|
and ONIE will perform automatically update of previously
|
2020-01-28 23:55:50 -06:00
|
|
|
added (i.e. pending) FW (ONIE itself, BIOS or CPLD) image.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
enable_onie_access() {
|
|
|
|
if [[ ! -d "${onie_mount}" ]]; then
|
|
|
|
mkdir ${onie_mount}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! mountpoint -q "${onie_mount}"; then
|
|
|
|
mount LABEL="ONIE-BOOT" ${onie_mount}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -e "${onie_lib}" ]]; then
|
|
|
|
ln -s ${onie_mount}/onie/tools/lib/onie ${onie_lib}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
disable_onie_access() {
|
|
|
|
if [[ -e "${onie_lib}" ]]; then
|
|
|
|
unlink ${onie_lib}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if mountpoint -q "${onie_mount}"; then
|
|
|
|
umount -rf ${onie_mount}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -d "${onie_mount}" ]]; then
|
|
|
|
rmdir ${onie_mount}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_onie_fw_update_mode() {
|
|
|
|
if [[ ! -f ${os_boot}/grub/grubenv || ! -f ${onie_mount}/grub/grubenv ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
register_terminate_handler
|
|
|
|
|
|
|
|
grub-editenv ${os_boot}/grub/grubenv set onie_entry="ONIE" || return $?
|
|
|
|
grub-editenv ${onie_mount}/grub/grubenv set onie_mode="update" || return $?
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
disable_onie_fw_update_mode() {
|
|
|
|
if [[ ! -f ${os_boot}/grub/grubenv || ! -f ${onie_mount}/grub/grubenv ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
grub-editenv ${os_boot}/grub/grubenv unset onie_entry || return $?
|
|
|
|
grub-editenv ${onie_mount}/grub/grubenv set onie_mode="install" || return $?
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
show_pending() {
|
|
|
|
if [[ ! -d ${onie_mount}/onie/update/pending ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
num=$(find ${onie_mount}/onie/update/pending -type f | wc -l)
|
|
|
|
if [[ ${num} -ge 1 ]]; then
|
|
|
|
${onie_mount}/onie/tools/bin/onie-fwpkg show-pending
|
|
|
|
fi
|
|
|
|
|
|
|
|
return ${num}
|
|
|
|
}
|
|
|
|
|
|
|
|
system_reboot() {
|
|
|
|
echo "INFO: Rebooting in 5 sec..."
|
|
|
|
|
|
|
|
# Give user some time to cancel the update
|
|
|
|
sleep 5s
|
|
|
|
|
|
|
|
# Use SONiC reboot scenario
|
2020-09-20 22:16:42 -05:00
|
|
|
/usr/local/bin/reboot
|
2020-06-15 06:25:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
terminate_handler() {
|
|
|
|
local -r _rc="$?"
|
|
|
|
local -r _sig="${1}"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "WARNING: Interrupted by ${_sig}: disable ONIE firmware update mode"
|
|
|
|
echo
|
|
|
|
|
|
|
|
enable_onie_access
|
|
|
|
disable_onie_fw_update_mode
|
|
|
|
rc=$?
|
|
|
|
disable_onie_access
|
|
|
|
|
|
|
|
if [[ ${rc} -ne 0 ]]; then
|
|
|
|
echo "ERROR: failed to disable ONIE firmware update mode"
|
|
|
|
exit ${rc}
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit ${_rc}
|
|
|
|
}
|
|
|
|
|
|
|
|
register_terminate_handler() {
|
|
|
|
trap "terminate_handler SIGHUP" SIGHUP
|
|
|
|
trap "terminate_handler SIGINT" SIGINT
|
|
|
|
trap "terminate_handler SIGQUIT" SIGQUIT
|
|
|
|
trap "terminate_handler SIGTERM" SIGTERM
|
2020-01-28 23:55:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
unlock_handler() {
|
|
|
|
/usr/bin/flock -u ${1}
|
2020-01-28 23:55:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
register_unlock_handler() {
|
|
|
|
trap "unlock_handler ${1}" EXIT
|
2020-01-28 23:55:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
unlock_script_state_change() {
|
|
|
|
/usr/bin/flock -u ${lock_fd}
|
2020-01-28 23:55:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
lock_script_state_change(){
|
|
|
|
exec {lock_fd}>${lock_file}
|
|
|
|
/usr/bin/flock -x ${lock_fd}
|
|
|
|
register_unlock_handler ${lock_fd}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Multiprocessing synchronization
|
|
|
|
lock_script_state_change
|
|
|
|
|
2020-01-28 23:55:50 -06:00
|
|
|
# Process command arguments
|
2020-06-15 06:25:49 -05:00
|
|
|
cmd="${1}"
|
|
|
|
|
|
|
|
# Optional argument
|
|
|
|
arg="${2}"
|
2020-01-28 23:55:50 -06:00
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
if [[ -z "${cmd}" ]]; then
|
|
|
|
# Default to 'show' if no command is specified.
|
|
|
|
cmd="show"
|
2020-01-28 23:55:50 -06:00
|
|
|
fi
|
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
case "${cmd}" in
|
|
|
|
add|remove)
|
|
|
|
if [[ -z "${arg}" ]]; then
|
|
|
|
echo "ERROR: This command requires a firmware update file name"
|
|
|
|
echo "Run: '${this_script} help' for complete details"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
update)
|
|
|
|
enable_onie_access
|
|
|
|
show_pending
|
|
|
|
rc=$?
|
|
|
|
if [[ ${rc} -ne 0 ]]; then
|
|
|
|
enable_onie_fw_update_mode
|
|
|
|
rc=$?
|
|
|
|
disable_onie_access
|
|
|
|
if [[ ${rc} -eq 0 ]]; then
|
|
|
|
system_reboot
|
|
|
|
else
|
|
|
|
echo "ERROR: failed to enable ONIE firmware update mode"
|
|
|
|
exit ${rc}
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: No firmware images for update"
|
|
|
|
echo "Run: '${this_script} add <image>' before update"
|
|
|
|
disable_onie_access
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
purge|show-pending|show-results|show|show-log|help)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "ERROR: Unknown command: ${cmd}"
|
|
|
|
exit 1
|
|
|
|
;;
|
2020-01-28 23:55:50 -06:00
|
|
|
esac
|
|
|
|
|
|
|
|
enable_onie_access
|
2020-06-15 06:25:49 -05:00
|
|
|
${onie_mount}/onie/tools/bin/onie-fwpkg "$@"
|
2020-01-28 23:55:50 -06:00
|
|
|
rc=$?
|
2020-06-15 06:25:49 -05:00
|
|
|
if [[ "${cmd}" = "help" ]]; then
|
|
|
|
print_help
|
2020-01-28 23:55:50 -06:00
|
|
|
fi
|
2020-06-15 06:25:49 -05:00
|
|
|
disable_onie_access
|
2020-01-28 23:55:50 -06:00
|
|
|
|
2020-06-15 06:25:49 -05:00
|
|
|
exit ${rc}
|