update-kernel/remove-old-kernels
Vitaly Chikunov 7d47e9beb8 remove-old-kernels: Better show to user what is happing
Also show what is not deleted and why. So user see decision about all the
kernels in the system.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
2022-06-26 07:50:41 +03:00

155 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# Copyright (C) 2008 Vladimir V. Kamarzin <vvk@altlinux.org>
#
# Remove all kernels except current
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
PROG=${0##*/}
message() {
echo >&2 "$PROG: $*"
}
fatal() {
message "$@"
exit 1
}
show_usage()
{
[ -z "$*" ] || message "$*"
echo >&2 "Try \`$PROG --help' for more information."
exit 1
}
show_help()
{
cat <<EOF
Usage: $PROG [options]
Valid options are:
-f, -y, --force non-interactive kernel removal (default is interactive)
-n, --dry-run just simulate removal
-t, --type remove kernels with specified flavour (ovz-smp, std-def, etc)
-a, --all remove kernels with all flavours
-h, --help show this text and exit
EOF
exit 0
}
#parse command line options
TEMP=$(getopt -n "$PROG" -o f,y,n,t:,h,a,v -l force,dry-run,type:,help,all,verbose -- "$@") || show_help
eval set -- "$TEMP"
while :; do
case "$1" in
--) shift; break
;;
-f|-y|--force) force="-y"
;;
-n|--dry-run) dryrun="--no-remove"
;;
-t|--type) shift ; kernel_flavour="$1"
;;
-a|--all) all=1
;;
-v|--verbose) verbose=1
;;
-h|--help) show_help
esac
shift
done
if [ -n "$force" ] && [ -n "$dryrun" ]; then
show_usage '--force and --dry-run are mutually exclusive options.'
fi
if [ -n "$kernel_flavour" ] && [ -n "$all" ]; then
show_usage '--type and --all are mutually exclusive options.'
fi
if [ -n "$verbose" ]; then
V() { echo >&2 "+ $*"; "$@"; }
else
V() { "$@"; }
fi
cr=$'\n'
uname_r=$(uname -r)
# get running kernel version
current_kernel_package=$(rpmquery -qf "/lib/modules/$uname_r/kernel" 2>/dev/null)
if [ -n "$current_kernel_package" ] ; then
current_kernel_pkgname=$(rpmquery --queryformat "%{NAME}-%{VERSION}-%{RELEASE}\n" -q "$current_kernel_package")
echo "Currently booted kernel package: $current_kernel_pkgname"
unset current_kernel_pkgname
else
echo "Running kernel version: $uname_r (package not found)"
fi
# set kernel flavour. if not defined with -t option, use current
current_kernel_flavour="$uname_r"
current_kernel_flavour="${current_kernel_flavour#*-}"
current_kernel_flavour="${current_kernel_flavour%-*}"
if [ -n "$all" ] ; then
flavours="$(rpm -qa --queryformat '%{NAME}\n' 'kernel-image-*' | grep -v -w -e domU -e debuginfo | sort -u | cut -d- -f3-)"
else
flavours="${kernel_flavour:-$current_kernel_flavour}"
fi
# Sort the kernels
keep_kernels=
for kernel_flavour in $flavours; do
all_kernels="$(rpm -qa "kernel-image-$kernel_flavour" | sort -V)"
newest_kernel=$(echo "$all_kernels" | tail -1)
for kernel in $all_kernels; do
reason=
if [ "$kernel" = "$newest_kernel" ]; then
reason="$reason,latest for $kernel_flavour"
fi
if [ "$kernel" = "$current_kernel_package" ]; then
reason="$reason,currently booted"
fi
if [ -n "$reason" ]; then
keep_kernels="$keep_kernels $kernel (${reason#,})$cr"
else
remove_kernels="$remove_kernels $kernel$cr"
apt_args_list="$apt_args_list $(rpm -q --queryformat '%{NAME}=%{EPOCH}:%{VERSION}-%{RELEASE}\n' "$kernel" \
| sed -e "s,(none):,,g")"
fi
done
done
echo "Not removing these kernels (reason why keeping):"
echo "$keep_kernels"
echo "Removing these kernels:"
echo "$remove_kernels"
if [ -z "$apt_args_list" ] ; then
echo "Nothing to remove."
exit 0
fi
if [ "$UID" != "0" ]; then
echo >&2 "This program requires root privileges."
fi
# shellcheck disable=SC2086
V apt-get $force $dryrun remove $apt_args_list
# Mask non-zero apt exit code on dry run:
if [ -n "$dryrun" ]; then
exit 0
fi