ProxmoxHelperScripts/misc/kernel-clean.sh

101 lines
2.8 KiB
Bash
Raw Normal View History

2022-03-04 16:41:56 +01:00
#!/bin/bash
2022-04-15 19:19:42 +02:00
RD=$(tput setaf 1)
GN=$(tput setaf 2)
LYW=$(tput setaf 190)
WH=$(tput setaf 7)
BRT=$(tput bold)
CL=$(tput sgr0)
UL=$(tput smul)
current_kernel=$(uname -r)
pve=$(pveversion)
while true; do
2022-04-15 19:19:42 +02:00
read -p "${WH}This will Clean unused Kernel images. Proceed(y/n)?${CL}" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
2022-04-15 19:19:42 +02:00
* ) echo -e "${RD}Please answer y/n${CL}";;
esac
done
clear
2022-04-15 19:19:42 +02:00
2022-03-04 16:41:56 +01:00
function check_root {
if [[ $EUID -ne 0 ]]; then
2022-04-15 19:19:42 +02:00
echo -e "${RD}Error: This script must be ran as the root user.\n${CL}"
2022-03-04 16:41:56 +01:00
exit 1
fi
}
2022-04-15 19:19:42 +02:00
2022-03-04 16:41:56 +01:00
function header_info {
2022-04-15 19:19:42 +02:00
echo -e "${RD}
2022-03-04 16:41:56 +01:00
_ __ _ _____ _
| |/ / | | / ____| |
| / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __
| < / _ \ __| _ \ / _ \ | | | | |/ _ \/ _ | _ \
| \ __/ | | | | | __/ | | |____| | __/ (_| | | | |
|_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_|
2022-04-15 19:19:42 +02:00
${CL}"
2022-03-04 16:41:56 +01:00
}
2022-04-15 19:19:42 +02:00
2022-04-15 13:43:04 +02:00
function kernel_info() {
latest_kernel=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | tac | head -n 1)
2022-04-15 19:19:42 +02:00
echo -e "${LYW}PVE Version: ${UL}${WH}$pve\n${CL}"
2022-04-15 13:43:04 +02:00
if [[ "$current_kernel" == *"pve"* ]]; then
if [[ "$latest_kernel" != *"$current_kernel"* ]]; then
2022-04-15 19:19:42 +02:00
echo -e "${GN}Latest Kernel: $latest_kernel\n${CL}"
2022-04-15 13:43:04 +02:00
fi
else
2022-04-15 19:19:42 +02:00
echo -e "\n${RD}ERROR: No PVE Kernel found\n${CL}"
2022-04-15 13:43:04 +02:00
exit 1
fi
2022-03-04 16:41:56 +01:00
}
2022-04-15 13:43:04 +02:00
function kernel_clean() {
kernels=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | sort -V)
kernels_to_remove=""
for kernel in $kernels
do
if [ "$(echo $kernel | grep $current_kernel)" ]; then
break
2022-03-04 16:41:56 +01:00
else
2022-04-15 19:19:42 +02:00
echo -e "${RD}'$kernel' ${CL}${LYW}has been added to the Kernel remove list\n${CL}"
2022-04-15 13:43:04 +02:00
kernels_to_remove+=" $kernel"
2022-03-04 16:41:56 +01:00
fi
2022-04-15 13:43:04 +02:00
done
2022-04-15 19:19:42 +02:00
echo -e "${LYW}Kernel Search Complete!\n${CL}"
2022-04-15 13:43:04 +02:00
if [[ "$kernels_to_remove" != *"pve"* ]]; then
2022-04-15 19:19:42 +02:00
echo -e "${BRT}${GN}It appears there are no old Kernels on your system. \n${CL}"
2022-04-15 13:43:04 +02:00
else
2022-04-15 19:19:42 +02:00
read -p "${LYW}Would you like to remove the${RD} $(echo $kernels_to_remove | awk '{print NF}') ${CL}${LYW}selected Kernels listed above? [y/n]: ${CL}" -n 1 -r
2022-04-15 13:43:04 +02:00
fi
if [[ $REPLY =~ ^[Yy]$ ]]; then
2022-04-15 19:19:42 +02:00
echo -e "${LYW}\nRemoving ${CL}${RD}$(echo $kernels_to_remove | awk '{print NF}') ${CL}${LYW}old Kernels...${CL}"
2022-04-15 13:43:04 +02:00
/usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1
2022-04-15 19:19:42 +02:00
echo -e "${LYW}Finished!\n${CL}"
echo -e "${LYW}Updating GRUB... \n${CL}"
2022-04-15 13:43:04 +02:00
/usr/sbin/update-grub > /dev/null 2>&1
2022-04-15 19:19:42 +02:00
echo -e "${LYW}Finished!\n${CL}"
2022-04-15 13:43:04 +02:00
else
2022-04-15 19:19:42 +02:00
echo -e "${LYW}Exiting...\n${CL}"
sleep 2
2022-04-15 13:43:04 +02:00
fi
2022-03-04 16:41:56 +01:00
}
2022-04-15 13:43:04 +02:00
function main() {
check_root
header_info
kernel_info
2022-03-04 16:41:56 +01:00
}
2022-04-15 13:43:04 +02:00
2022-03-04 16:41:56 +01:00
while true; do
2022-04-15 13:43:04 +02:00
case "$1" in
* )
main
kernel_clean
exit 1
;;
2022-03-04 16:41:56 +01:00
esac
2022-04-15 13:43:04 +02:00
shift
2022-03-04 16:41:56 +01:00
done