2023-02-07 19:15:22 +02:00
#!/usr/bin/env bash
# Copyright (c) 2021-2023 tteck
# Author: tteck (tteckster)
# License: MIT
# https://github.com/tteck/Proxmox/raw/main/LICENSE
2023-01-10 18:08:36 +02:00
function header_info {
cat <<"EOF"
__ __ __ ________
2023-05-16 09:10:49 +02:00
/ //_/__ _________ ___ / / / ____/ /__ ____ _____
2023-01-10 18:08:36 +02:00
/ ,< / _ \/ ___/ __ \/ _ \/ / / / / / _ \/ __ ` / __ \
/ /| / __/ / / / / / __/ / / /___/ / __/ /_/ / / / /
2023-05-16 09:10:49 +02:00
/_/ | _\_ __/_/ /_/ /_/\_ __/_/ \_ ___/_/\_ __/\_ _,_/_/ /_/
2023-01-10 18:08:36 +02:00
EOF
}
2022-04-23 17:17:44 +02:00
set -euo pipefail
shopt -s inherit_errexit nullglob
2022-10-30 03:04:45 +02:00
YW = $( echo "\033[33m" )
BL = $( echo "\033[36m" )
RD = $( echo "\033[01;31m" )
BGN = $( echo "\033[4;92m" )
GN = $( echo "\033[1;92m" )
DGN = $( echo "\033[32m" )
CL = $( echo "\033[m" )
2022-04-23 17:17:44 +02:00
BFR = "\\r\\033[K"
HOLD = "-"
CM = " ${ GN } ✓ ${ CL } "
CROSS = " ${ RD } ✗ ${ CL } "
PARTY = "🎉"
current_kernel = $( uname -r)
2023-01-10 18:08:36 +02:00
clear
header_info
2022-04-23 17:17:44 +02:00
while true; do
2022-07-13 17:45:24 +02:00
read -p "This will Clean Unused Kernel Images, USE AT YOUR OWN RISK. Proceed(y/n)?" yn
2022-04-23 17:17:44 +02:00
case $yn in
2022-10-30 03:04:45 +02:00
[ Yy] *) break ; ;
[ Nn] *) exit ; ;
*) echo -e " ${ RD } Please answer y/n ${ CL } " ; ;
2022-04-23 17:17:44 +02:00
esac
done
clear
function msg_info( ) {
local msg = " $1 "
echo -ne " ${ HOLD } ${ YW } ${ msg } ... "
}
function msg_ok( ) {
local msg = " $1 "
echo -e " ${ BFR } ${ CM } ${ GN } ${ msg } ${ CL } "
}
function check_root( ) {
2022-10-30 03:04:45 +02:00
if [ [ $EUID -ne 0 ] ] ; then
echo -e " ${ CROSS } ${ RD } Error: This script must be ran as the root user.\n ${ CL } "
exit 1
else
header_info
2023-01-16 18:02:06 +02:00
other_kernel
2022-10-30 03:04:45 +02:00
kernel_info
kernel_clean
fi
2022-04-23 17:17:44 +02:00
}
2023-06-12 17:42:12 +02:00
VERSION = " $( awk -F'=' '/^VERSION_CODENAME=/{ print $NF }' /etc/os-release) "
2023-01-16 18:02:06 +02:00
function other_kernel( ) {
2023-06-12 17:42:12 +02:00
if [ [ " ${ VERSION } " = = "bullseye" ] ] ; then
2022-07-13 18:56:23 +02:00
if [ [ " $current_kernel " = = *"edge" * ] ] ; then
2022-10-30 03:04:45 +02:00
echo -e " \n ${ CROSS } ${ RD } ERROR: ${ CL } Proxmox ${ BL } ${ current_kernel } ${ CL } Kernel Active "
echo -e "\nAn Active PVE Kernel is required to use Kernel Clean\n"
exit 1
2022-07-13 18:56:23 +02:00
fi
2023-05-16 09:10:49 +02:00
if [ [ " $current_kernel " = = *"6.1" * || " $current_kernel " = = *"6.2" * ] ] ; then
2023-01-16 18:02:06 +02:00
echo -e " \n ${ CROSS } ${ RD } ERROR: ${ CL } Proxmox ${ BL } ${ current_kernel } ${ CL } Kernel Active "
2023-05-16 09:10:49 +02:00
echo -e "\nThe script cannot be used when running opt-in kernels. \nProxmox VE's package management relies directly on the current default kernel, which is 5.15. \nTherefore, it is not possible to utilize this script. In this case, you should use autoremove instead. \n`apt-get autoremove`\n"
2023-01-16 18:02:06 +02:00
exit 1
fi
2023-06-12 17:42:12 +02:00
fi
2022-07-13 18:56:23 +02:00
}
2022-04-23 17:17:44 +02:00
function kernel_info( ) {
2022-10-30 03:04:45 +02:00
if [ [ " $MODE " != "PBS" ] ] ; then
echo -e " ${ YW } PVE Version: ${ BL } $( pveversion) \n ${ CL } "
fi
2022-04-23 17:17:44 +02:00
if [ [ " $current_kernel " = = *"pve" * ] ] ; then
2022-10-13 15:24:33 +02:00
echo -e " ${ YW } Current Kernel: ${ BL } $current_kernel \n ${ CL } "
2022-04-23 17:17:44 +02:00
else
2022-07-13 18:56:23 +02:00
echo -e " \n ${ CROSS } ${ RD } ERROR: No PVE Kernel Found\n ${ CL } "
2022-04-23 17:17:44 +02:00
exit 1
fi
}
function kernel_clean( ) {
2022-10-30 03:04:45 +02:00
kernels = $( dpkg --list | grep 'kernel-.*-pve' | awk '{print $2}' | sort -V)
2022-04-23 17:17:44 +02:00
remove_kernels = ""
2022-10-30 03:04:45 +02:00
for kernel in $kernels ; do
2022-04-23 17:17:44 +02:00
if [ " $( echo $kernel | grep $current_kernel ) " ] ; then
break
else
echo -e " ${ BL } ' $kernel ' ${ CL } ${ YW } has been added to the remove Kernel list\n ${ CL } "
2022-10-30 03:04:45 +02:00
remove_kernels += " $kernel "
2022-04-23 17:17:44 +02:00
fi
done
2022-10-30 03:04:45 +02:00
msg_ok "Kernel Search Completed\n"
2022-04-23 17:17:44 +02:00
if [ [ " $remove_kernels " != *"pve" * ] ] ; then
echo -e " ${ PARTY } ${ GN } It appears there are no old Kernels on your system. \n ${ CL } "
msg_info "Exiting"
sleep 2
msg_ok "Done"
else
2022-10-30 03:04:45 +02:00
read -p " Would you like to remove the $( echo $remove_kernels | awk '{print NF}' ) selected Kernels listed above? [y/n]: " -n 1 -r
2022-04-23 17:17:44 +02:00
echo
2022-10-30 03:04:45 +02:00
if [ [ $REPLY = ~ ^[ Yy] $ ] ] ; then
msg_info " Removing ${ CL } ${ RD } $( echo $remove_kernels | awk '{print NF}' ) ${ CL } ${ YW } old Kernels ${ CL } "
/usr/bin/apt purge -y $remove_kernels >/dev/null 2>& 1
msg_ok "Successfully Removed Kernels"
msg_info "Updating GRUB"
/usr/sbin/update-grub >/dev/null 2>& 1
msg_ok "Successfully Updated GRUB"
msg_info "Exiting"
sleep 2
msg_ok "Done"
else
msg_info "Exiting"
sleep 2
msg_ok "Done"
fi
2022-04-23 17:17:44 +02:00
fi
}
2022-09-10 03:03:20 +02:00
if ! command -v pveversion >/dev/null 2>& 1; then
2022-10-30 03:04:45 +02:00
echo -e " Switching to PBS mode"
MODE = "PBS"
sleep 2
else
MODE = "PVE"
2022-09-10 03:03:20 +02:00
fi
2022-04-23 17:17:44 +02:00
check_root