Update kernel-clean.sh

This commit is contained in:
tteckster 2022-04-15 13:19:42 -04:00 committed by GitHub
parent 39ef2b7d10
commit e754ffc82e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 27 deletions

View File

@ -1,29 +1,33 @@
#!/bin/bash #!/bin/bash
RED=$(tput setaf 1) RD=$(tput setaf 1)
GREEN=$(tput setaf 2) GN=$(tput setaf 2)
YELLOW=$(tput setaf 3) LYW=$(tput setaf 190)
BLUE=$(tput setaf 4) WH=$(tput setaf 7)
WHITE=$(tput setaf 7) BRT=$(tput bold)
NORMAL=$(tput sgr0) CL=$(tput sgr0)
UL=$(tput smul)
current_kernel=$(uname -r)
pve=$(pveversion)
while true; do while true; do
read -p "${YELLOW}This will Clean unused Kernel images. Proceed(y/n)?${NORMAL}" yn read -p "${WH}This will Clean unused Kernel images. Proceed(y/n)?${CL}" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) exit;; [Nn]* ) exit;;
* ) echo -e "${RED}Please answer y/n${NORMAL}";; * ) echo -e "${RD}Please answer y/n${CL}";;
esac esac
done done
clear clear
current_kernel=$(uname -r)
pve=$(pveversion)
function check_root { function check_root {
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "${RED}Error: This script must be ran as the root user.\n${NORMAL}" echo -e "${RD}Error: This script must be ran as the root user.\n${CL}"
exit 1 exit 1
fi fi
} }
function header_info { function header_info {
echo -e "${RED} echo -e "${RD}
_ __ _ _____ _ _ __ _ _____ _
| |/ / | | / ____| | | |/ / | | / ____| |
| / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __ | / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __
@ -31,18 +35,18 @@ echo -e "${RED}
| \ __/ | | | | | __/ | | |____| | __/ (_| | | | | | \ __/ | | | | | __/ | | |____| | __/ (_| | | | |
|_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_| |_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_|
${NORMAL}" ${CL}"
} }
function kernel_info() { function kernel_info() {
latest_kernel=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | tac | head -n 1) latest_kernel=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | tac | head -n 1)
echo -e "${YELLOW}OS: ${GREEN}$(cat /etc/os-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/["]//g' | awk '{print $0}')\r${NORMAL}" echo -e "${LYW}PVE Version: ${UL}${WH}$pve\n${CL}"
echo -e "${YELLOW}PVE Version: ${GREEN}$pve\n${NORMAL}"
if [[ "$current_kernel" == *"pve"* ]]; then if [[ "$current_kernel" == *"pve"* ]]; then
if [[ "$latest_kernel" != *"$current_kernel"* ]]; then if [[ "$latest_kernel" != *"$current_kernel"* ]]; then
echo -e "${GREEN}Latest Kernel: $latest_kernel\n${NORMAL}" echo -e "${GN}Latest Kernel: $latest_kernel\n${CL}"
fi fi
else else
echo -e "\n${RED}ERROR: No PVE Kernel found\n${NORMAL}" echo -e "\n${RD}ERROR: No PVE Kernel found\n${CL}"
exit 1 exit 1
fi fi
} }
@ -55,26 +59,26 @@ function kernel_clean() {
if [ "$(echo $kernel | grep $current_kernel)" ]; then if [ "$(echo $kernel | grep $current_kernel)" ]; then
break break
else else
echo -e "${RED}'$kernel' ${NORMAL}${YELLOW}has been added to the Kernel remove list\n${NORMAL}" echo -e "${RD}'$kernel' ${CL}${LYW}has been added to the Kernel remove list\n${CL}"
kernels_to_remove+=" $kernel" kernels_to_remove+=" $kernel"
fi fi
done done
echo -e "${YELLOW}Kernel Search Complete!\n${NORMAL}" echo -e "${LYW}Kernel Search Complete!\n${CL}"
if [[ "$kernels_to_remove" != *"pve"* ]]; then if [[ "$kernels_to_remove" != *"pve"* ]]; then
echo -e "${GREEN}It appears there are no old Kernels on your system \n${NORMAL}" echo -e "${BRT}${GN}It appears there are no old Kernels on your system. \n${CL}"
else else
read -p "${YELLOW}Would you like to remove the${RED} $(echo $kernels_to_remove | awk '{print NF}') ${NORMAL}${YELLOW}selected Kernels listed above? [y/n]: ${NORMAL}" -n 1 -r 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
fi fi
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}\nRemoving ${NORMAL}${RED}$(echo $kernels_to_remove | awk '{print NF}') ${NORMAL}${YELLOW}old Kernels...${NORMAL}" echo -e "${LYW}\nRemoving ${CL}${RD}$(echo $kernels_to_remove | awk '{print NF}') ${CL}${LYW}old Kernels...${CL}"
/usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1 /usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1
echo -e "${YELLOW}Finished!\n${NORMAL}" echo -e "${LYW}Finished!\n${CL}"
echo -e "${YELLOW}Updating GRUB... \n${NORMAL}" echo -e "${LYW}Updating GRUB... \n${CL}"
/usr/sbin/update-grub > /dev/null 2>&1 /usr/sbin/update-grub > /dev/null 2>&1
echo -e "${YELLOW}Finished!\n${NORMAL}" echo -e "${LYW}Finished!\n${CL}"
else else
echo -e "${YELLOW}Exiting...\n${NORMAL}" echo -e "${LYW}Exiting...\n${CL}"
sleep 1 sleep 2
fi fi
} }