-
Notifications
You must be signed in to change notification settings - Fork 10
/
linux-kernel-autoremove
executable file
·88 lines (81 loc) · 3.15 KB
/
linux-kernel-autoremove
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
#!/bin/bash
# linux-kernel-autoremove (part of ossobv/vcutil) // wdoekes/2016-2019
# // Public Domain
#
# Automatically remove old unused kernels on Debian and Ubuntu platforms.
#
# On hosts with auto updating on, a large collection of old kernels can
# pile up and collect dust. This utility greps for all linux-image-*,
# linux-modules-* and linux-headers-* and keeps only two, apt-get
# removing the rest. It prefers to keep the latest and running kernels.
#
# Usage:
#
# linux-kernel-autoremove # lists what it would remove
# linux-kernel-autoremove -f # apt-get removes those packages
#
# Bugs: right now, it doesn't correlate headers or modules to images. So
# if you've stopped updating headers/modules, this utility won't clean
# up old versions for you.
#
set -ue
list_packages_other_than_current_and_latest_kernel() {
# Take <prefix> (linux-image) and add current uname version, but
# remove trailing stuff like "-generic".
prefix="$1" # e.g. "linux-image", "linux-headers"
current="$prefix-$(uname --kernel-release | sed -e 's/\(.*[0-9]\).*/\1/')"
latest=$(dpkg -l "$prefix-[0-9]*" 2>/dev/null | awk '/^ii/{print $2}' |
sort -V | sed -e 's/\(.*[0-9]\).*/\1/' | tail -n1)
alldpkg=$(dpkg -l "$prefix-[0-9]*" 2>/dev/null |
awk '/^[ir][ic] /{print $2}' | sort -V)
currentdpkg=$(dpkg -l "$current*" 2>/dev/null | awk '/^ii/{print $2}')
if test -z "$latest"; then
if test "$prefix" = "linux-headers" ||
test "$prefix" = "linux-modules"; then
# echo "(ignoring $prefix; doesn't exist)" >&2
return
else
echo "ERROR: $current not installed. Please fix manually." >&2
exit 1
fi
fi
if test -z "$currentdpkg"; then
echo "WARNING: $current not installed. Please double check." >&2
echo >&2
fi
# Is already latest? Then use the previous one as current, so we
# keep two.
if test "$latest" = "$current"; then
current=$(dpkg -l "$prefix-[0-9]*" 2>/dev/null |
awk '/^ii/{print $2}' | sed -e 's/\(.*[0-9]\).*/\1/' |
sort -uV | tail -n2 | head -n1)
fi
for pkg in $alldpkg; do
# bash-isms in the if-globbing here, so we can find both
# linux-headers-4.15.0-50 and linux-headers-4.15.0-50-generic.
if ! test "$pkg" = "$currentdpkg" &&
! [[ "$pkg" = "$current"* ]] &&
! [[ "$pkg" = "$latest"* ]]; then
echo $pkg
fi
done
}
# Run by importance first. If there is no image, we'll want to abort fast.
# Headers are not mandatory, nor are modules.
packages=$(
list_packages_other_than_current_and_latest_kernel linux-image
list_packages_other_than_current_and_latest_kernel linux-modules
list_packages_other_than_current_and_latest_kernel linux-headers
)
force="${1:-}"
if test "$force" = "-f"; then
test -n "$packages" && apt-get -y remove --purge $packages
true
elif test -n "$packages"; then
echo "Supply -f to remove these packages:"
echo "$packages" | sed -e 's/^/ /'
else
echo "Nothing to remove." >&2
false
fi
# vim: set ts=8 sw=4 sts=4 et ai: