opnsense-deps/rc.d/20-kernel-update
Jeremy Karst 5e215f5997 20-kernel-update: leave kernel.bin where the kernel set put it
The hook deleted /boot/kernel/kernel.bin once it was on the FAT
partition, so the health audit always reported it missing (seen on the
gateway after the 26.7.9 upgrade). It now leaves the kernel set's files
alone and returns early, without a space check or a log line, when the
kernel and device tree on the FAT partition are already the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 18:54:16 -05:00

121 lines
3.6 KiB
Bash

#!/bin/sh
#
# rc.syshook "stop" script: install a freshly updated kernel for U-Boot.
#
# U-Boot loads kernel.img and the device tree straight from the FAT boot
# partition (/boot/msdos), not from /boot/kernel. After opnsense-update has
# installed a kernel set, this copies the new kernel.bin and device tree onto
# the FAT partition before the reboot. They stay in /boot as the kernel set
# installed them (the health audit checks the set's files); a shutdown that
# finds them on the FAT partition already does nothing.
#
# kernel.img must stay bootable throughout: nothing on the FAT partition is
# touched unless there is room for the new files, the new kernel is copied
# and verified before the old one is replaced, the old one is kept as
# kernel.old, and it is restored if a step fails. If the update cannot be
# done, the next shutdown tries again.
#
# GW_ROOT prefixes every path (tests only).
#
# SPDX-License-Identifier: BSD-2-Clause
ROOT=${GW_ROOT:-}
BOOT=${ROOT}/boot/msdos
NEW_KERNEL=${ROOT}/boot/kernel/kernel.bin
NEW_DTB=${ROOT}/boot/dtb/freescale/mono-gateway-dk.dtb
MARGIN=1048576
log()
{
logger -t kernel-update -- "$*" 2>/dev/null
echo "kernel-update: $*" >&2
}
size()
{
if [ -f "${1}" ]; then wc -c < "${1}" | tr -d ' '; else echo 0; fi
}
free_bytes()
{
df -Pk "${BOOT}" | awk 'NR == 2 { print $4 * 1024 }'
}
# install_file NEW TARGET: replace TARGET with NEW, keeping TARGET.old.
# The old file is only renamed once the new copy is complete and verified.
# Sets REPLACED=1 if TARGET changed, 0 if it already was identical.
install_file()
{
_new=${1}
_target=${2}
_base=${_target%.*}
REPLACED=0
if [ -f "${_target}" ] && cmp -s "${_new}" "${_target}"; then
return 0
fi
# the previous backup makes room first; kernel.img itself is untouched
rm -f "${_base}.new" "${_base}.old"
if ! cp "${_new}" "${_base}.new" || ! sync || ! cmp -s "${_new}" "${_base}.new"; then
rm -f "${_base}.new"
log "copying ${_new} to ${_base}.new failed"
return 1
fi
if [ -f "${_target}" ]; then
if ! mv "${_target}" "${_base}.old"; then
rm -f "${_base}.new"
log "cannot rotate ${_target}"
return 1
fi
fi
if ! mv "${_base}.new" "${_target}"; then
mv "${_base}.old" "${_target}" 2>/dev/null
rm -f "${_base}.new"
log "cannot install ${_target}, restored the previous one"
return 1
fi
sync
REPLACED=1
return 0
}
[ -f "${NEW_KERNEL}" ] || exit 0
if [ ! -f "${BOOT}/kernel.img" ]; then
log "no ${BOOT}/kernel.img: FAT boot partition not mounted? new kernel not installed"
exit 1
fi
DTB_TARGET=${BOOT}/dtb/mono-gateway-dk.dtb
if cmp -s "${NEW_KERNEL}" "${BOOT}/kernel.img" &&
{ [ ! -f "${NEW_DTB}" ] || cmp -s "${NEW_DTB}" "${DTB_TARGET}"; }; then
exit 0
fi
# Room needed for the new copies; stale .old/.new files are given back first.
NEED=$(( $(size "${NEW_KERNEL}") + $(size "${NEW_DTB}") + MARGIN ))
HAVE=$(( $(free_bytes) + $(size "${BOOT}/kernel.old") + $(size "${BOOT}/kernel.new") \
+ $(size "${BOOT}/dtb/mono-gateway-dk.old") + $(size "${BOOT}/dtb/mono-gateway-dk.new") ))
if [ "${HAVE}" -lt "${NEED}" ]; then
log "only ${HAVE} bytes free on ${BOOT}, ${NEED} needed: new kernel not installed"
exit 1
fi
if ! install_file "${NEW_KERNEL}" "${BOOT}/kernel.img"; then
exit 1
fi
KERNEL_REPLACED=${REPLACED}
if [ -f "${NEW_DTB}" ]; then
mkdir -p "${BOOT}/dtb"
if ! install_file "${NEW_DTB}" "${DTB_TARGET}"; then
# keep kernel and device tree from the same build
if [ "${KERNEL_REPLACED}" = 1 ]; then
mv "${BOOT}/kernel.old" "${BOOT}/kernel.img" && sync
fi
log "device tree update failed, kept the previous kernel"
exit 1
fi
fi
log "installed new kernel and device tree on ${BOOT} (previous kept as kernel.old)"
exit 0