#!/bin/bash # Copyright (C) 2008 Vladimir V. Kamarzin # # 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 <&2 "+ $*"; "$@"; } else V() { "$@"; } fi 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 "Running kernel package: $current_kernel_pkgname (keeping)" 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 echo "For removing:" for kernel_flavour in $flavours; do # rpmevrcmp is not needed since we don't output epoch # Output all kernels, sort by a version, skip last (highest version) one, # then filter out kernels we want to keep (like booted one). old_kernels="$(rpm -qa "kernel-image-$kernel_flavour" | sort -V | head -n-1)" for kernel in $old_kernels; do [ "$kernel" = "$current_kernel_package" ] && continue echo " $kernel" apt_args_list="$apt_args_list $(rpm -q --queryformat '%{NAME}=%{EPOCH}:%{VERSION}-%{RELEASE}\n' "$kernel" \ | sed -e "s,(none):,,g")" done done echo 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