-
Notifications
You must be signed in to change notification settings - Fork 12
/
kernel-remove-old
executable file
·109 lines (95 loc) · 2.85 KB
/
kernel-remove-old
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash -ue
#
# Remove old kernels but keep the first, the current and the previous
#
# Copyright (C) 2012 Rodrigo Silva (MestreLion) <[email protected]>
#
# This program 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 3 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. See <http://www.gnu.org/licenses/gpl.html>
myname="${0##*/}"
verbose=1
keepfirst=1
keepprevious=1
fatal() { [[ "${1:-}" ]] && echo "$myname: $@" >&2 ; exit 1; }
message() { if ((verbose)); then printf "%s\n" "${1:-}"; fi; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid option: $1" ; }
usage() {
cat <<-USAGE
Usage: $myname [options]
USAGE
if [[ "${1:-}" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Remove old kernels but keep the first, the current and the previous
Options:
-h|--help - show this page.
-q|--quiet - do not print informative messages.
--remove-first - also remove first kernel
--remove-previous - also remove previous kernel
Copyright (C) 2012 Rodrigo Silva (MestreLion) <[email protected]>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
case "$1" in
-q|--quiet ) verbose=0 ;;
--remove-first ) keepfirst=0 ;;
--remove-previous) keepprevious=0 ;;
-*) invalid "$1" ;;
* ) argerr "$1" ;;
esac
shift
done
export current=$(uname -r | cut -d- -f-2)
list=$(
dpkg --list "linux-*" |
grep -E "^ii linux-(headers|image)-[2-5]\." |
cut -d' ' -f3 |
sort -V |
grep -v "$current"
)
message "Removing all kernels but keeping:"
keep=()
if ((keepfirst)); then
first=$(head -n 1 <<< "$list" | cut -d- -f3,4)
keep+=(-e "$first")
message "First : $first"
fi
if ((keepprevious)); then
previous=$(tail -n 1 <<< "$list" | cut -d- -f3,4)
keep+=(-e "$previous")
message "Previous: $previous"
fi
if ((${#keep[@]})); then
keep+=(-v)
else
keep=('')
fi
aptlist=$(grep "${keep[@]}" <<< "$list")
msglist=$(cut -d- -f3,4 <<< "$aptlist" | sort -u)
message "Current : $current"
message
message "Kernels to be removed:"
message "$msglist"
message
sudo apt-get purge $aptlist &&
message "Done, success!" ||
fatal "Error removing packages, check the output!" \
"(have you cancelled the operation?)"