forked from jordanhillis/pvekclean
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpvekclean.sh
350 lines (329 loc) · 11.7 KB
/
pvekclean.sh
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
: '
______________________________________________
PVE Kernel Cleaner
By Jordan Hillis
contact@jordanhillis.com
https://jordanhillis.com
______________________________________________
MIT License
Copyright (c) 2018 Jordan S. Hillis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
______________________________________________
'
# Percentage of used space in the /boot which would consider it critically full
boot_critical_percent="80"
# Current kernel
current_kernel=$(uname -r)
# Name of the program
program_name="pvekclean"
# Version
version="1.2"
# Check if force removal argument is added
if [ "$1" == "-f" ] || [ "$1" == "--force" ]; then
force_purge=true
else
force_purge=false
fi
# Check if script is ran as root, if not exit
function check_root {
if [[ $EUID -ne 0 ]]; then
printf "[!] Error: this script must be ran as the root user.\n"
exit 1
fi
}
# Shown current version
function version(){
printf $version"\n"
exit 1
}
# Header for PVE Kernel Cleaner
function header_info {
echo -e "
█▀▀█ ▀█ █▀ █▀▀ █ █ █▀▀ █▀▀█ █▀▀▄ █▀▀ █
█ █ █▄█ █▀▀ █▀▄ █▀▀ █▄▄▀ █ █ █▀▀ █
█▀▀▀ ▀ ▀▀▀ ▀ ▀ ▀▀▀ ▀ ▀▀ ▀ ▀ ▀▀▀ ▀▀▀
█▀▀ █ █▀▀ █▀▀█ █▀▀▄ █▀▀ █▀▀█
█ █ █▀▀ █▄▄█ █ █ █▀▀ █▄▄▀ ⎦˚◡˚⎣ v$version
▀▀▀ ▀▀▀ ▀▀▀ ▀ ▀ ▀ ▀ ▀▀▀ ▀ ▀▀
By Jordan Hillis [contact@jordanhillis.com]
___________________________________________
"
}
# Show current system information
function kernel_info {
# Lastest kernel installed
latest_kernel=$(dpkg --list| grep 'pve-kernel-.*-pve' | awk '{print $2}' | tac | head -n 1)
# Show operating system used
printf "OS: $(cat /etc/os-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/["]//g' | awk '{print $0}')\n"
# Get information about the /boot folder
boot_info=($(echo $(df -Ph /boot | tail -1) | sed 's/%//g'))
# Show information about the /boot
printf "Boot Disk: ${boot_info[4]}%% full [${boot_info[2]}/${boot_info[1]} used, ${boot_info[3]} free] \n"
# Show current kernel in use
printf "Current Kernel: pve-kernel-$current_kernel\n"
# Check if they are running a PVE kernel
if [[ "$current_kernel" == *"pve"* ]]; then
# Check if we are running the latest kernel, if not warn
if [[ "$latest_kernel" != *"$current_kernel"* ]]; then
printf "Latest Kernel: $latest_kernel\n"
fi
# Warn them that they aren't on a PVE kernel
else
printf "___________________________________________\n\n"
printf "[!] Warning, you're not running a PVE kernel\n"
# Ask them if they want to continue
read -p "[*] Would you like to continue [y/N] " -n 1 -r
printf "\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Continue on if they wish
printf "[-] Alright, we will continue on\n"
else
# Exit script
printf "\nGood bye!\n"
exit 1
fi
fi
printf "___________________________________________\n\n"
}
# Usage information on how to use PVE Kernel Clean
function show_usage {
# Skip showing usage when force_purge is enabled
if [ $force_purge == false ]; then
printf "Usage: $(basename $0) [OPTION]\n\n"
printf " -f, --force Remove all old PVE kernels without confirm prompts\n"
printf " -s --scheduler Have old PVE kernels removed on a scheduled basis\n"
printf " -v, --version Shows current version of $program_name\n"
printf " -r --remove Uninstall $program_name from the system\n"
printf "___________________________________________\n\n"
fi
}
# Schedule PVE Kernel Cleaner at a time desired
function scheduler(){
# Check if pvekclean is on the system, if not exit
if [ ! -f /usr/local/sbin/$program_name ]; then
printf "[!] Sorry $program_name is required to be installed on the system for this functionality.\n"
exit 1
fi
# Check if cron is installed
if ! [ -x "$(command -v crontab)" ]; then
printf "[*] Error, cron does not appear to be installed.\n"
printf " Please install cron with the command 'sudo apt-get install cron'\n\n"
exit 1
fi
# Check if the cronjob exists on the system
check_cron_exists=$(crontab -l | grep "$program_name")
# Cronjob exists
if [ -n "$check_cron_exists" ]; then
# Get the current cronjob scheduling
cron_current=$(crontab -l | grep "$program_name" | sed "s/[^a-zA-Z']/ /g" | sed -e "s/\b\(.\)/\u\1/g" | awk '{print $1;}')
# Ask the user if they would like to remove the scheduling
read -p "[-] Would you like to remove the currently scheduled PVE Kernel Cleaner? (Current: $cron_current) [y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Remove the cronjob
(crontab -l | grep -v "$program_name")| crontab -
printf "\n[*] Successfully removed the ${cron_current,,} scheduled PVE Kernel Cleaner!\n"
else
# Keep it
printf "\n\nAlright we will keep your current settings then.\n"
fi
# Cronjob does not exist
else
# Ask how often the would like to check for old PVE kernels
printf "[-] How often would you like to check for old PVE kernels?\n 1) Daily\n 2) Weekly\n 3) Monthly\n\n - Enter a number option above? "
read -r -p "" response
case "$response" in
1)
cron_time="daily"
;;
2)
cron_time="weekly"
;;
3)
cron_time="monthly"
;;
*)
printf "\nThat is not a valid option!\n"
exit 1
;;
esac
# Add the cronjob
(crontab -l ; echo "@$cron_time /usr/local/sbin/$program_name -f")| crontab -
printf "\n[-] Scheduled $cron_time PVE Kernel Cleaner successfully!\n"
fi
exit 1
}
# Installs PVE Kernel Cleaner for easier access
function install_program(){
force_pvekclean_update=false
# If pvekclean exists on the system
if [ -e /usr/local/sbin/$program_name ]; then
# Get current version of pvekclean
pvekclean_installed_version=$(/usr/local/sbin/$program_name -v | awk '{printf $0}')
# If the version differs, update it to the latest from the script
if [ $version != $pvekclean_installed_version ] && [ $force_purge == false ]; then
printf "[!] A new version of PVE Kernel Cleaner has been detected (Installed: $pvekclean_installed_version | New: $version).\n"
printf "[*] Installing update...\n"
force_pvekclean_update=true
fi
fi
# If pvekclean does not exist on the system or force_purge is enabled
if [ ! -f /usr/local/sbin/$program_name ] || [ $force_pvekclean_update == true ]; then
# Ask user if we can install it to their system
if [ $force_purge == true ]; then
REPLY="n"
else
# Ask if we can install it
read -p "[-] Can we install PVE Kernel Cleaner to your /usr/local/sbin for easier access [y/N] " -n 1 -r
printf "\n"
fi
# User agrees to have it installed
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Copy the script to /usr/local/sbin and set execution permissions
cp $0 /usr/local/sbin/$program_name
chmod +x /usr/local/sbin/$program_name
# Tell user how to use it
printf "[*] Installed PVE Kernel Cleaner to /usr/local/sbin/$program_name\n"
printf "[*] Run the command \"$program_name\" to begin using this program.\n"
printf "[-] Run the command \"$program_name -r\" to remove this program at any time.\n"
exit 1
fi
fi
}
# Uninstall pvekclean from the system
function uninstall_program(){
# If pvekclean exists on the system
if [ -e /usr/local/sbin/$program_name ]; then
# Confirm that they wish to remove it
read -p "[-] Are you sure that you would like to remove $program_name? [y/N] " -n 1 -r
printf "\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Remove the program
rm -f /usr/local/sbin/$program_name
printf "[*] Successfully removed PVE Kernel Cleaner from the system!\n"
printf "[-] Sorry to see you go :(\n"
else
printf "\nExiting...\nThat was a close one ⎦˚◡˚⎣\n"
fi
exit 1
else
# Tell the user that it is not installed
printf "[!] This program is not installed on the system.\n"
exit 1
fi
}
# PVE Kernel Clean main function
function pve_kernel_clean {
# Find all the PVE kernels on the system
kernels=$(dpkg --list| grep 'pve-kernel-.*-pve' | awk '{print $2}' | sort -V)
# List of kernels that will be removed (adds them as the script goes on)
kernels_to_remove=""
# Check the /boot used
printf "[*] Boot disk space used is "
# Warn user when the /boot is critically full
if [[ "${boot_info[4]}" -ge "$boot_critical_percent" ]]; then
printf "critically full "
# Tell them if it is at an acceptable percentage
else
printf "healthy "
fi
# Display percentage used and available space left
printf "at ${boot_info[4]}%% capacity (${boot_info[3]} free)\n"
printf "[-] Searching for old PVE kernels on your system...\n"
# For each kernel that was found via dpkg
for kernel in $kernels
do
# If the kernel listed from dpkg is our current then break
if [ "$(echo $kernel | grep $current_kernel)" ]; then
break
# Add kernel to the list of removal since it is old
else
printf "[*] \"$kernel\" has been added to the kernel remove list\n"
kernels_to_remove+=" $kernel"
fi
done
printf "[-] PVE kernel search complete!\n"
# If there are no kernels to be removed then exit
if [[ "$kernels_to_remove" != *"pve"* ]]; then
printf "[!] It appears there are no old PVE kernels on your system ⎦˚◡˚⎣\n"
printf "[-] Good bye!\n"
# Kernels found in removal list
else
# Check if force removal was passed
if [ $force_purge == true ]; then
REPLY="y"
# Ask the user if they want to remove the selected kernels found
else
read -p "[!] Would you like to remove the $(echo $kernels_to_remove | awk '{print NF}') selected PVE kernels listed above? [y/N]: " -n 1 -r
printf "\n"
fi
# User wishes to remove the kernels
if [[ $REPLY =~ ^[Yy]$ ]]; then
printf "[*] Removing $(echo $kernels_to_remove | awk '{print NF}') old PVE kernels..."
# Purge the old kernels via apt and suppress output
/usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1
printf "DONE!\n"
printf "[*] Updating GRUB..."
# Update grub after kernels are removed, suppress output
/usr/sbin/update-grub > /dev/null 2>&1
printf "DONE!\n"
# Script finished successfully
printf "[-] Have a nice day ⎦˚◡˚⎣\n"
# User wishes to not remove the kernels above, exit
else
printf "\nExiting...\n"
printf "See you later ⎦˚◡˚⎣\n"
fi
fi
}
function main {
# Check for root
check_root
# Show header information
header_info
# Script usage
show_usage
# Show kernel information
kernel_info
# Install program to /usr/local/sbin/
install_program
}
while true; do
case "$1" in
-r | --remove )
main
uninstall_program
;;
-s | --scheduler )
main
scheduler
;;
-v | --version )
version
;;
-h | --help )
main
exit 1
;;
* )
main
pve_kernel_clean
exit 1
;;
esac
shift
done