-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge-old-kernels
executable file
·47 lines (38 loc) · 1.75 KB
/
purge-old-kernels
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# script purge-old-kernels
# script written by: Grant Bowman <[email protected]>
# script Version 1.1
# copyright 2010
# Licensed under the GPL v2 or greater
# runs apt-get purge on old linux-image packages and their headers
# This script could easily be modified for other systems
# however it has only been tested on Ubuntu 9.10 Karmic Koala
# Information about other systems would be most appreciated.
kernels=`dpkg -l | egrep ^ii.*linux-image-\[23\] | cut --d=" " --fields=3 | cut --d="-" --fields=3-4`
anyprocessed=0
if (( $EUID )) ; then
echo "Warning: not run with root privileges. Kernels can not be purged. Rerun with \" sudo !!\"";
fi
for kernel in $kernels ; do
if [[ ( $kernel-generic != `uname -r` ) && ( $kernel-generic < `uname -r` ) ]] ; then
# don't offer to remove the currently running or newer kernels
echo ' ***** Do you want to run the following command? [Y/n] *****';
anyprocessed=1
while true; do
read -s -N 1 -p " apt-get purge linux-image-$kernel-generic linux-headers-$kernel linux-headers-$kernel-generic" yn ;
case $yn in
[Yy]* ) printf "\n\n"; echo "apt-get purge linux-image-$kernel-generic linux-headers-$kernel linux-headers-$kernel-generic"; printf "\n";;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
printf "\n";
fi
done
if ! (( $anyprocessed )) ; then
echo "No old kernels found.";
fi
printf "\n";
# takes care of kernels? via comments from http://www.omgubuntu.co.uk/2011/07/kernel-entries-gone/
#
# dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge