Proxmox/misc/kernel-clean.sh

101 lines
2.8 KiB
Bash
Raw Normal View History

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