forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroks
200 lines (174 loc) · 4.78 KB
/
roks
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/roks
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/roks
# Started On - Sun 17 Sep 23:32:47 BST 2017
# Last Change - Fri 11 Aug 13:08:38 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# Written as an improvement over the now-deprecated script called
# 'purge-old-kernels' found in the 'byobu' package. This small program will
# clear out your system of old kernel versions on an Ubuntu- or Debian-based
# system.
#
# By way of example, assume the following:
#
# 6.2.11
# 6.3.0 <-- Currently Loaded
# 6.3.1
# 6.3.2
# 6.3.3
# 6.3.4
#
# ROKs will ignore anything newer than 6.3.0, opting to keep 6.3.0, because it
# is currently loaded, and 6.2.11 will by default be kept as well, because it's
# the fallback. If no fallback is selected (keep 1), 6.2.11 will be selected
# for removal or purging.
#
# NOTE:
#
# Noticed on 2023-08-11, that linux-version(1) was actually incorrectly
# sorting the kernel version strings, which of course is a huge problem. To
# handle this issue, sort(1) was used, with its `-V` flag to sort version
# strings, which it appears to do correctly with at least regular MAJ.MIN.REV
# version strings.
#
# Features:
#
# N/A
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# apt (>= 1.6.12)
# bash (>= 4.1)
# coreutils (>= 8.25-2)
# linux-base (>= 4.5)
#------------------------------------------------------------------------------
CurVer='2023-08-11'
Progrm='roks'
ProgrmFancy='ROKs'
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS]
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
-k, --keep INT - Keep INT number of kernels, instead of 1.
-p, --purge - Don't just remove, but purge the kernels.
-t, --type TYPE - Work with TYPE kernels. Any by default.
Remember to check before saying yes to APT, in-case gathered packages
aren't as expected. By default, two kernel will be held back, which
includes the one currently loaded. Kernels newer than your currently
loaded kernel will be ignored. $ProgrmFancy may not be suitable for all Linux
systems or kernel types.
Unrecognised flags will be passed to apt-get(8).
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
KernKeep=2
Args=()
while [[ -n $1 ]]; do
case $1 in
-[^-]*)
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
} ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
NoRoot='False'
while [[ -n $1 ]]; do
case $1 in
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"
exit 0 ;;
--keep|-k)
if [[ $2 == +([0-9]) ]]; then
if (( $2 == 0 )); then
Err 1 "Unable to remove currently-loaded kernel."
else
KernKeep=$2
shift
fi
else
Err 1 "Option '$1' given non-numeric argument."
fi ;;
--type|-t)
shift; KernType="-$1" ;;
--purge|-p)
DoPurge='--purge' ;;
-*)
AptGetOpts+=" $1" ;;
*)
Err 1 "Incorrect argument(s) provided." ;;
esac
shift
done
DepCount=0
for CurDep in apt-get linux-version sort; {
if ! type -P "$CurDep" &> /dev/null; then
Err 0 "Dependency '$CurDep' not met."
(( DepCount++ ))
fi
}
(( DepCount > 0 )) && exit 1
(( UID == 0 )) || NoRoot='True'
ProcVerFile='/proc/version'
if type -P uname &> /dev/null; then
Current=`uname -r`
elif [[ -f $ProcVerFile && -r $ProcVerFile ]]; then
read _ _ Current _ < /proc/version
else
Err 1 'Unable to determine current kernel version.'
fi
readarray -t <<< "$(linux-version list | sort -V)"
Kerns=()
for Line in "${MAPFILE[@]}"; {
[[ $Line == $Current ]] && break
Kerns+=("$Line")
}
KernsAvail=${#Kerns[@]}
Kerns=("${Kerns[@]:0:${#Kerns[@]} - KernKeep + 1}")
if [[ $NoRoot == True ]]; then
printf "Found %d kernel installation(s):\n\n" ${#MAPFILE[@]}
for Kern in "${MAPFILE[@]}"; {
if [[ $Kern == $Current ]]; then
printf ' %s <-- Active\n' "$Kern"
else
printf ' %s\n' "$Kern"
fi
}
printf '\nUp to %d kernel(s) available for uninstallation.\n' $KernsAvail
if (( KernsAvail > 0 )); then
printf "Launch %s with 'root' privileges to take action.\n" "$ProgrmFancy"
fi
else
if (( ${#Kerns[@]} == 0 )); then
Err 1 'No suitable kernels found.'
fi
Packages=()
for Kern in "${Kerns[@]}"; {
Packages+=(linux-{image,headers}-"$Kern")
}
apt-get $AptGetOpts -o Dpkg::Progress=true\
-o Dpkg::Progress-Fancy=true remove $DoPurge "${Packages[@]}"
Exit=$?
if (( Exit > 0 )); then
Err 1 "Exit $Exit detected from apt-get(8)."
fi
fi