From 3aa8d73684d992a373cdaa7595d218b01a7784bf Mon Sep 17 00:00:00 2001 From: virant Date: Thu, 29 Oct 2026 10:58:46 +0100 Subject: [PATCH] feat(packaging): boot the newest installed SmoothKernel unless overridden GRUB ranks kernels by release string, so the installer's old 7.2.10-smoothkernel sorts above the archive's 7.2.10-74-smoothkernel and a system upgraded from it keeps booting the old kernel. linux-image-smoothkernel now ships kernel hooks that, on every kernel install and removal, point GRUB_TOP_LEVEL at the installed SmoothKernel with the highest package version. GRUB_TOP_LEVEL or GRUB_DEFAULT set in /etc/default/grub take precedence. --- docs/CI_RELEASES.md | 7 ++++ recipes/build-kernel.sh | 58 +++++++++++++++++++++++++++++++-- templates/boot-newest-kernel.in | 44 +++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 templates/boot-newest-kernel.in diff --git a/docs/CI_RELEASES.md b/docs/CI_RELEASES.md index 37990b4..ec16995 100644 --- a/docs/CI_RELEASES.md +++ b/docs/CI_RELEASES.md @@ -44,6 +44,13 @@ every new build. Old kernels are removed by `apt autoremove`, which keeps the running and newest ones. The versioned headers package also `Provides: linux-headers`, for DKMS packages that depend on the generic name. +`linux-image-smoothkernel` also keeps GRUB booting the newest installed +SmoothKernel, by package version: its kernel hooks set `GRUB_TOP_LEVEL` in +`/etc/default/grub.d/50-smoothkernel.cfg` whenever a kernel is installed or +removed. GRUB's own ordering compares release strings and would rank an older +`7.2.10-smoothkernel` above `7.2.10-74-smoothkernel`. To boot another kernel, +set `GRUB_TOP_LEVEL` or `GRUB_DEFAULT` in `/etc/default/grub`; either wins. + The DKMS module version of OpenZFS stays the upstream version (`2.4.4`); only the package revision carries the build number. diff --git a/recipes/build-kernel.sh b/recipes/build-kernel.sh index 033117a..fae94a0 100755 --- a/recipes/build-kernel.sh +++ b/recipes/build-kernel.sh @@ -176,16 +176,70 @@ add_headers_virtual_provides() { rm -rf "$tmp" } +# Lay out, under , the boot-newest script, the /etc/kernel hooks that run +# it (before GRUB's zz-update-grub) and the maintainer scripts that apply it +# when the package itself is installed or removed. +add_boot_newest_hooks() { + local root="$1" dir + install -d "$root/usr/share/smoothkernel" + sed "s/@LOCALVERSION@/${LOCALVERSION}/g" "$ROOT/templates/boot-newest-kernel.in" \ + > "$root/usr/share/smoothkernel/boot-newest" + chmod 755 "$root/usr/share/smoothkernel/boot-newest" + sh -n "$root/usr/share/smoothkernel/boot-newest" + for dir in postinst.d postrm.d; do + install -d "$root/etc/kernel/$dir" + cat > "$root/etc/kernel/$dir/zy-smoothkernel-boot-newest" <<'HOOK' +#!/bin/sh +# linux-image-smoothkernel: keep GRUB on the newest installed SmoothKernel. +# zz-update-grub runs after this and writes it into grub.cfg. +[ -x /usr/share/smoothkernel/boot-newest ] || exit 0 +exec /usr/share/smoothkernel/boot-newest +HOOK + chmod 755 "$root/etc/kernel/$dir/zy-smoothkernel-boot-newest" + echo "/etc/kernel/$dir/zy-smoothkernel-boot-newest" >> "$root/DEBIAN/conffiles" + done + # The kernel this package depends on is configured before the package, so + # its hooks ran without this one; apply it now, as zz-update-grub would. + cat > "$root/DEBIAN/postinst" <<'POSTINST' +#!/bin/sh +set -e +if [ "$1" = configure ]; then + /usr/share/smoothkernel/boot-newest + if command -v update-grub >/dev/null 2>&1 && [ -e /boot/grub/grub.cfg ]; then + update-grub + fi +fi +POSTINST + cat > "$root/DEBIAN/postrm" <<'POSTRM' +#!/bin/sh +set -e +if [ "$1" = remove ] || [ "$1" = purge ]; then + rm -f /etc/default/grub.d/50-smoothkernel.cfg + if command -v update-grub >/dev/null 2>&1 && [ -e /boot/grub/grub.cfg ]; then + update-grub + fi +fi +POSTRM + chmod 755 "$root/DEBIAN/postinst" "$root/DEBIAN/postrm" +} + # Build a metapackage __.deb that depends on exactly # this build's . Systems install the metapackage; each new build's # metapackage pulls in that build's kernel through an ordinary apt upgrade. +# +# With "boot-newest" as a fourth argument it also carries the kernel hooks that +# keep GRUB booting the newest installed SmoothKernel (see +# templates/boot-newest-kernel.in). build_metapackage() { - local name="$1" target="$2" description="$3" + local name="$1" target="$2" description="$3" extra="${4:-}" local tmp deb maintainer # Same maintainer as the packages bindeb-pkg built in this run. maintainer="$(dpkg-deb -f "$OUT_DIR/linux-image-${KERNEL_RELEASE}_${PKG_VERSION}_${DEB_ARCH}.deb" Maintainer)" tmp="$(mktemp -d)" mkdir -p "$tmp/DEBIAN" + if [[ "$extra" == boot-newest ]]; then + add_boot_newest_hooks "$tmp" + fi cat > "$tmp/DEBIAN/control" < building metapackages" build_metapackage linux-image-smoothkernel "linux-image-${KERNEL_RELEASE}" \ - "SmoothKernel image (latest build)" + "SmoothKernel image (latest build)" boot-newest build_metapackage linux-headers-smoothkernel "linux-headers-${KERNEL_RELEASE}" \ "SmoothKernel headers (latest build)" echo "==> done. Built ${#moved[@]} .debs + 2 metapackages for ${KERNEL_RELEASE} in $OUT_DIR" diff --git a/templates/boot-newest-kernel.in b/templates/boot-newest-kernel.in new file mode 100644 index 0000000..9ff0bdc --- /dev/null +++ b/templates/boot-newest-kernel.in @@ -0,0 +1,44 @@ +#!/bin/sh +# Boot the newest installed SmoothKernel. Shipped by linux-image-smoothkernel +# and run from its kernel hooks on every kernel install and removal. +# +# GRUB ranks kernels by their release string, which puts an older build first +# when the names differ in shape: the installer's 7.2.10@LOCALVERSION@ sorts +# above the archive's 7.2.10-74@LOCALVERSION@. This picks the kernel with the +# highest package version instead and has GRUB put it first, through +# GRUB_TOP_LEVEL. +# +# To boot another kernel, set GRUB_TOP_LEVEL or GRUB_DEFAULT yourself in +# /etc/default/grub; either takes precedence over this. + +set -e + +cfg=/etc/default/grub.d/50-smoothkernel.cfg + +newest="" +newest_version="" +for entry in $(dpkg-query -W -f '${Package}=${Version}\n' 'linux-image-*@LOCALVERSION@' 2>/dev/null); do + pkg=${entry%%=*} + version=${entry#*=} + release=${pkg#linux-image-} + # A kernel being removed is still known to dpkg; one being installed is + # not configured yet. Its image in /boot is what counts. + [ -e "/boot/vmlinuz-${release}" ] || continue + if [ -z "$newest" ] || dpkg --compare-versions "$version" gt "$newest_version"; then + newest=$release + newest_version=$version + fi +done + +if [ -z "$newest" ]; then + rm -f "$cfg" + exit 0 +fi + +mkdir -p "${cfg%/*}" +cat > "${cfg}.new" << EOF +# Written by linux-image-smoothkernel: boot the newest installed SmoothKernel. +# To boot another kernel, set GRUB_TOP_LEVEL or GRUB_DEFAULT in /etc/default/grub. +GRUB_TOP_LEVEL="\${GRUB_TOP_LEVEL:-/boot/vmlinuz-${newest}}" +EOF +mv "${cfg}.new" "$cfg"