blob: 72ea6256f7eb14d26a715fd3b6d0b25bb89e2cdb [file]
#!/bin/sh
# Copyright 2022 Google LLC
#
# 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.
# Retrieved from https://cos.googlesource.com/cos/overlays/board-overlays/+/refs/heads/master/project-lakitu/dev-util/cos-dev-tools/files/
set -e
PKG=$1
if ! grep -q '^ID=cos$' /etc/os-release; then
echo "Does not appear to be a COS instance"
echo "Refusing to replace kernel"
exit 1
fi
if [ -z "${PKG}" ]; then
echo "Usage: $0 cos-kernel-A.B.C-arch.txz"
exit 1
fi
VERSION=$(tar tvfJ "${PKG}" boot/ | grep /config- | sed s/.*config-//)
if [ -z "${VERSION}" ]; then
echo "Could not find kernel version from /boot/config-*"
echo "Check if package is valid"
exit 1
fi
echo "Kernel version: ${VERSION}"
mount -o rw,remount / || true
if mount | grep "on / " | grep -q ro, ; then
echo "Could not remount rootfs as read-write"
exit 1
fi
echo "Remounted rootfs as read-write"
if [ -d "/lib/modules/${VERSION}" ]; then
rm -rf "/lib/modules/${VERSION}"
echo "Modules for ${VERSION} removed"
fi
tar -C / -x -J -f "${PKG}"
ln -sf "vmlinuz-${VERSION}" /boot/vmlinuz
echo "rootfs updated"
ESPMOUNT=$(mktemp -d)
mount /dev/disk/by-label/EFI-SYSTEM "${ESPMOUNT}"
BOOT_IMAGE="${ESPMOUNT}/$(sed 's/.*BOOT_IMAGE=\([^ ]*\) .*/\1/' /proc/cmdline)"
echo "Updating boot image ${BOOT_IMAGE}"
if [ ! -f "${BOOT_IMAGE}" ]; then
echo "Boot image '${BOOT_IMAGE}' is not a regular file"
umount "${ESPMOUNT}"
exit 1
fi
rm -f "${BOOT_IMAGE}"
cp -f "/boot/vmlinuz-${VERSION}" "${BOOT_IMAGE}"
umount "${ESPMOUNT}"
rm -rf "${ESPMOUNT}"
echo "EFI system partition updated"
mount -o ro,remount / || true
echo "Remounted rootfs as read-only"
echo "Rebooting the system to activate the new kernel"
reboot