Fix version sorting bugs in kernel-cleanup.sh

This commit is contained in:
Joshua Boniface 2023-09-01 15:42:25 -04:00
parent 6903627150
commit 4666db17cb
1 changed files with 8 additions and 12 deletions

View File

@ -6,16 +6,17 @@
# Determine the active running kernel # Determine the active running kernel
RUNNING_KERNEL="$( uname -v | awk '{ print $4 }' )" RUNNING_KERNEL="$( uname -v | awk '{ print $4 }' )"
# Determine the list of installed kernels (latest is always last) # Determine the list of installed kernels (latest first)
INSTALLED_KERNELS=( $( dpkg -l | grep 'linux-image-[0-9]' | awk '{ print $3 }' | sort -n ) ) INSTALLED_KERNEL_PACKAGES=( $( dpkg -l | grep 'linux-image-[0-9]' | awk '{ print $2 }' | sort -Vr ) )
NUM_INSTALLED=${{ '{#' }}INSTALLED_KERNELS[@]} NUM_INSTALLED=${{ '{#' }}INSTALLED_KERNEL_PACKAGES[@]}
if [[ ${NUM_INSTALLED} -le 1 ]]; then if [[ ${NUM_INSTALLED} -le 1 ]]; then
echo "A single kernel is installed, aborting cleanly." echo "A single kernel is installed, aborting cleanly."
exit 0 exit 0
fi fi
LATEST_KERNEL="${INSTALLED_KERNELS[-1]}" LATEST_KERNEL_PACKAGE="${INSTALLED_KERNEL_PACKAGES[0]}"
LATEST_KERNEL="$( dpkg -l | grep "${LATEST_KERNEL_PACKAGE}" | awk '{ print $3 }' )"
if [[ ${LATEST_KERNEL} == ${RUNNING_KERNEL} ]]; then if [[ ${LATEST_KERNEL} == ${RUNNING_KERNEL} ]]; then
force="" force=""
else else
@ -24,12 +25,7 @@ fi
# Remove the latest kernel from the array # Remove the latest kernel from the array
NUM_REMOVABLE=$(( ${NUM_INSTALLED} - 1 )) NUM_REMOVABLE=$(( ${NUM_INSTALLED} - 1 ))
REMOVABLE_KERNELS=( ${INSTALLED_KERNELS[@]:0:${NUM_REMOVABLE}} ) REMOVABLE_KERNEL_PACKAGES=( ${INSTALLED_KERNEL_PACKAGES[@]:1} )
PURGE_PACKAGES=()
for KERNEL in ${REMOVABLE_KERNELS[@]}; do
PURGE_PACKAGES+=( $( dpkg -l | grep ${KERNEL} | grep -v 'linux-image-amd64\|linux-headers-amd64' | awk '{ print $2 }' ) )
done
# Override the "linux-check-removal" script # Override the "linux-check-removal" script
mv /usr/bin/linux-check-removal /usr/bin/linux-check-removal.orig mv /usr/bin/linux-check-removal /usr/bin/linux-check-removal.orig
@ -37,8 +33,8 @@ echo -e '#!/bin/sh\necho "Overriding default linux-check-removal script!"\nexit
chmod +x /usr/bin/linux-check-removal chmod +x /usr/bin/linux-check-removal
# Remove the packages # Remove the packages
echo "Removing: ${PURGE_PACKAGES[@]}" echo "Removing: ${REMOVABLE_KERNEL_PACKAGES[@]}"
apt-get purge --yes ${force} ${PURGE_PACKAGES[@]} apt-get purge --yes ${force} ${REMOVABLE_KERNEL_PACKAGES[@]}
# Restore the "linux-check-removal" script # Restore the "linux-check-removal" script
mv /usr/bin/linux-check-removal.orig /usr/bin/linux-check-removal mv /usr/bin/linux-check-removal.orig /usr/bin/linux-check-removal