ProxmoxVE-HelperScripts/misc/kernel-clean.sh

101 lines
2.8 KiB
Bash
Raw Normal View History

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