#!/bin/bash # Written by Didier Spaier this script is dedicated to the public domain. # # This script is called by slapt-get --upgrade like this: # # Its main purpose is to perform specific actions when upgrading kernels # Rationale: we want to make sure that a working kernel be always installed, but # we don't want to keep more kernels than necessary, to save space on disk and # not clutter the GRUB boot menu. In that aim: # 1. When installing a new kernel, we will keep the running one. # 2. When the new kernel runs, we remove the one that was running when # installing it. # Additionally, this script uses spkg -i then spkg -d for packages that ship # many files, namely kernel-modules and kernel-source, to save time removing # them as spkg -d is faster that removepkg. This could be used to upgrade other # packages with many files that do not need a pre-installation. In case of # packages kernel-generic or kernel-modules it calls update-portable that will # make a specific initrd and grub.cfg case occuring, else make an initrd and # update grub directly. # Incidentally, we plan to not upgrade the huge kernel anymore. As soon as a # generic one will be running we will remove the one initially installed upon # next upgrade of a generic kernel. LOG=/tmp/wrapupgradepkglog date >>$LOG RUNNINGVERSION=$(uname -r) # We are called with the patch to the new package as $2 NEWKERNELPATH=$2 NEWPKGNAME="$(basename "$NEWKERNELPATH")" FRAG='[^-]\{1,\}' NEWSHORTNAME="$(echo "$NEWPKGNAME"|sed "s/\(.*\)-$FRAG-$FRAG-$FRAG$/\1/")" NEWVERSION="$(echo "$NEWPKGNAME"|sed "s/.*-\($FRAG\)-$FRAG-$FRAG$/\1/")" KEYFILE=crypto_keyfile.bin unset ENCRYPT if [ "$(lsblk -lno type $(findmnt -lno source /|cut -d[ -f1))" = "crypt" ]; then if [ ! -f /etc/keys/$KEYFILE ]; then echo \ "WARNING $KEYFILE not found! You will have to type the passphrase during boot to unlock the root partition! Read \"man dracut\" to know more." sleep fi ENCRYPT=yes fi # We assume that only one kernel package at a given version is installed. As a # consequence, if we need to reconfigure a kernel, better wait for a new version # or at least do not do that twice in a row, else we won't know which kernel # package to remove short of checking the oldest installed according to # "stat -c %Y $i" that you could insert below, probably an overkill. usepkgtools() { # This function is only called to upgrade kernel-generic or kernel-headers if [ "$NEWSHORTNAME" = "kernel-generic" ]; then INSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep -e "kernel-generic" -e "kernel-huge") else INSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep -e "$NEWSHORTNAME") fi installpkg $NEWKERNELPATH echo "$INSTALLED"|while read i; do iVERSION=$(echo $i|sed "s/.*-\($FRAG\)-$FRAG-$FRAG$/\1/") if [ "$iVERSION" = "$RUNNINGVERSION" ]; then echo "not removing $i" >>$LOG continue fi echo "removepkg $i" >>$LOG removepkg $i if [ -f /boot/initrd-$iVERSION ]; then rm /boot/initrd-$iVERSION fi done # Remove the symlinks we won't use any more ( cd /boot rm -f System.map config config-generic vmlinuz{,-generic,-huge} ) } usespkg() { INSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep ${NEWSHORTNAME}-[[:digit:]]) installpkg $NEWKERNELPATH echo "$INSTALLED"|while read i; do iVERSION=$(echo $i|sed "s/.*-\($FRAG\)-$FRAG-$FRAG$/\1/") if [ "$iVERSION" = "$RUNNINGVERSION" ]; then echo "not removing $i" >>$LOG continue fi echo "spkg -d /var/lib/pkgtools/packages/$i" spkg -d /var/lib/pkgtools/packages/$i [ "$NEWSHORTNAME" = "kernel-modules" ] && \ [ -f /boot/initrd-generic-$iVERSION ] && \ rm /boot/initrd-generic-$iVERSION done } initrdandgrub() { # Called to upgrade kernel-generic or kernel-headers, do something as soon # as both are installed at the same version, or kernel (bundle of both) is installed. MODULESINSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep ^kernel-modules-$NEWVERSION) KERNEGENERICINSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep ^kernel-generic-$NEWVERSION) KERNELINSTALLED=$(ls -1 /var/lib/pkgtools/packages|grep ^kernel-$NEWVERSION) # Remove the huge kernel and the symlinks we won't use, just in case a user # upgraded from an previous version or migrated from Slackware if ls -1 /boot|grep -q vmlinuz-huge; then removepkg kernel-huge fi ( cd /boot rm -f System.map config config-generic vmlinuz{,-generic,-huge} ) if [ "$MODULESINSTALLED" ] && [ "$KERNEGENERICINSTALLED" ]; then if [ "$ENCRYPT" ]; then dracut -q --zstd --install " /etc/keys/$KEYFILE " --kver $NEWVERSION 2>>$LOG else dracut -q --zstd --kver $NEWVERSION 2>>$LOG fi updategrub fi if [ "$KERNELINSTALLED" ]; then if [ "$ENCRYPT" ]; then dracut -q --zstd --install " /etc/keys/$KEYFILE " --kver $NEWVERSION 2>>$LOG else dracut -q --zstd --kver $NEWVERSION 2>>$LOG fi updategrub fi } updategrub() { if [ ! -f /boot/grub/grub.cfg ]; then echo gettext "It seems that you didn't install grub in Slint. If you installed it from another distribution you should now run update-grub from this other distribution, else Slint won't boot from the grub menu. If you used another boot manager that grub we suggest that you install grub now." echo else gettext "Updating GRUB..." echo update-grub echo "update-grub" >>$LOG echo fi } case $NEWSHORTNAME in kernel) usespkg;initrdandgrub; echo "usepkg and initrdandgrub ran." >>$LOG;; kernel-generic) usepkgtools;initrdandgrub; echo "usespktools and initrdandgrub ran." >>$LOG;; kernel-modules) usespkg;initrdandgrub; echo "usepkg and initrdandgrub ran." >>$LOG;; kernel-headers) usepkgtools; echo "usespkgtools ran." >>$LOG;; kernel-source|libreoffice|mozilla-firefox|mozilla-thunderbird) usespkg; echo "usespkg ran." >>$LOG;; *) upgradepkg --reinstall $NEWKERNELPATH; echo "upgradepkg --reinstall ran." >>$LOG;; esac