2022-04-03 03:12:12 +03:00
#!/bin/bash
2008-12-28 15:20:56 +03:00
# 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.
2022-04-06 02:09:07 +03:00
PROG=${0##*/}
message() {
echo >&2 "$PROG: $*"
}
fatal() {
message "$@"
exit 1
}
show_usage()
{
[ -z "$*" ] || message "$*"
echo >&2 "Try \`$PROG --help' for more information."
exit 1
}
2016-01-23 06:56:45 +03:00
show_help()
{
cat <<EOF
Usage: $PROG [options]
Valid options are:
2018-08-22 16:44:27 +03:00
-f, -y, --force non-interactive kernel removal (default is interactive)
2016-01-23 06:56:45 +03:00
-n, --dry-run just simulate removal
2016-04-24 03:42:48 +03:00
-t, --type remove kernels with specified flavour (ovz-smp, std-def, etc)
2019-12-19 16:13:08 +03:00
-a, --all remove kernels with all flavours
2016-01-23 06:56:45 +03:00
-h, --help show this text and exit
EOF
exit 1
}
#parse command line options
2022-04-03 03:12:12 +03:00
TEMP=$(getopt -n "$PROG" -o f,y,n,t:,h,a -l force,dry-run,type:,help,all -- "$@") || show_help
2016-01-23 06:56:45 +03:00
eval set -- "$TEMP"
while :; do
case "$1" in
--) shift; break
;;
2018-08-22 16:44:27 +03:00
-f|-y|--force) force="-y"
2016-01-23 06:56:45 +03:00
;;
-n|--dry-run) dryrun="--no-remove"
;;
2016-04-24 03:42:48 +03:00
-t|--type) shift ; kernel_flavour="$1"
;;
2019-12-19 16:13:08 +03:00
-a|--all) all=1
;;
2016-01-23 06:56:45 +03:00
-h|--help) show_help
esac
shift
done
2022-04-03 03:12:12 +03:00
if [ -n "$force" ] && [ -n "$dryrun" ]; then
2016-01-23 06:56:45 +03:00
show_usage '--force and --dry-run are mutually exclusive options.'
fi
2022-04-03 03:12:12 +03:00
if [ -n "$kernel_flavour" ] && [ -n "$all" ]; then
2019-12-19 16:13:08 +03:00
show_usage '--type and --all are mutually exclusive options.'
fi
2022-06-26 01:15:45 +03:00
uname_r=$(uname -r)
2016-04-24 03:42:48 +03:00
# get running kernel version
2022-06-26 01:15:45 +03:00
current_kernel_package=$(rpmquery -qf "/lib/modules/$uname_r/kernel" 2>/dev/null)
2016-04-24 03:42:48 +03:00
if [ -n "$current_kernel_package" ] ; then
2022-04-03 03:12:12 +03:00
current_kernel_pkgname=$(rpmquery --queryformat "%{NAME}-%{SERIAL}:%{VERSION}-%{RELEASE}\n" -q "$current_kernel_package")
2022-06-26 01:12:49 +03:00
current_kernel_pkgname=${current_kernel_pkgname/\(none\):/}
2016-04-24 03:42:48 +03:00
else
current_kernel_pkgname="UNKNOWN"
fi
echo "Running kernel version: $current_kernel_pkgname"
# set kernel flavour. if not defined with -t option, use current
2022-06-26 01:15:45 +03:00
current_kernel_flavour="$uname_r"
2020-02-12 16:30:43 +03:00
current_kernel_flavour="${current_kernel_flavour#*-}"
current_kernel_flavour="${current_kernel_flavour%-*}"
2019-12-19 16:13:08 +03:00
if [ -n "$all" ] ; then
2022-06-26 01:37:52 +03:00
flavours="$(rpm -qa --queryformat '%{NAME}\n' 'kernel-image-*' | grep -v -w -e domU -e debuginfo | sort -u | cut -d- -f3-)"
2019-12-19 16:13:08 +03:00
else
flavours="${kernel_flavour:-$current_kernel_flavour}"
fi
2016-04-24 03:42:48 +03:00
echo "Checking for installed kernel packages..."
2022-06-26 01:29:18 +03:00
# Rewrite `uname -r's kernelrelease (V-F-R) to package's F-V-R format
tver=${uname_r%%-*}
trel=${uname_r##*-}
tflv=${uname_r#"$tver"-}
tflv=${tflv%-"$trel"}
flavour_version_release=$tflv-$tver-$trel
unset tver trel tflv
2008-12-28 15:41:11 +03:00
2016-04-24 03:42:48 +03:00
echo "For removing:"
2019-12-19 16:13:08 +03:00
for kernel_flavour in $flavours
2008-12-28 15:41:11 +03:00
do
2022-06-26 01:31:58 +03:00
old_kernels="$(rpm -qa | grep -F "kernel-image-$kernel_flavour" | grep -F -w -v "$flavour_version_release" | sort | tr '\n' ' ')"
2019-12-19 16:13:08 +03:00
if [ -n "$all" ] ; then
echo "$kernel_flavour:"
2016-04-24 03:42:48 +03:00
fi
2019-12-19 16:13:08 +03:00
for kernel in $old_kernels
do
if [ "$current_kernel_flavour" = "$kernel_flavour" ] ; then
comparever="$(rpmevrcmp "$current_kernel_package" "$kernel")"
[ "$comparever" -lt 0 ] && continue
else
# check if kernel is latest with given flavour
latest=1
for kernel2 in $old_kernels
do
comparever="$(rpmevrcmp "$kernel" "$kernel2")"
[ "$comparever" -lt 0 ] && latest=0
done
[ "$latest" -eq 1 ] && continue
fi
echo " $kernel"
2022-04-03 03:12:12 +03:00
apt_args_list="$apt_args_list $(rpm -q --queryformat '%{NAME}=%{EPOCH}:%{VERSION}-%{RELEASE}\n' "$kernel" \
2019-12-19 16:13:08 +03:00
| sed -e "s,(none):,,g")"
done
2008-12-28 15:41:11 +03:00
done
2016-04-24 03:42:48 +03:00
echo
2008-12-28 15:20:56 +03:00
2019-12-20 14:48:28 +03:00
if [ -z "$apt_args_list" ] ; then
echo "Nothing to remove."
exit 0
fi
2021-07-10 10:30:51 +03:00
if [ "$UID" != "0" ]; then
echo >&2 "This program requires root privileges."
fi
2008-12-28 15:20:56 +03:00
2022-04-03 03:12:12 +03:00
# shellcheck disable=SC2086
2021-07-10 10:30:51 +03:00
apt-get $force $dryrun remove $apt_args_list
2016-01-23 06:56:45 +03:00
# Mask non-zero apt exit code on dry run:
if [ -n "$dryrun" ]; then
exit 0
fi