3
# purge-old-kernels - remove old kernel packages
4
# Copyright (C) 2012 Dustin Kirkland <kirkland@ubuntu.com>
6
# Authors: Dustin Kirkland <kirkland@ubuntu.com>
7
# Kees Cook <kees@ubuntu.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, version 3 of the License.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
# Ensure we're running as root
22
if [ "$(id -u)" != 0 ]; then
23
echo "ERROR: This script must run as root. Hint..." 1>&2
24
echo " sudo $0 $@" 1>&2
28
# NOTE: This script will ALWAYS keep the currently running kernel
29
# NOTE: Default is to keep 2 more, user overrides with --keep N
31
# NOTE: Any unrecognized option will be passed straight through to apt-get
33
while [ ! -z "$1" ]; do
36
# User specified the number of kernels to keep
41
APT_OPTS="$APT_OPTS $1"
47
# Build our list of kernel packages to purge
48
PURGE=$(ls -tr /boot/vmlinuz-* | head -n -${KEEP} | grep -v "$(uname -r)$" | cut -d- -f2- | awk '{print "linux-image-" $0}')
50
if [ -z "$PURGE" ]; then
51
echo "No kernels are eligible for removal"
55
apt-get $APT_OPTS remove --purge $PURGE