Update kernel-clean.sh

This commit is contained in:
tteckster 2022-04-15 07:43:04 -04:00 committed by GitHub
parent 58c1d281a0
commit b15bd4d2e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 63 deletions

View File

@ -1,28 +1,29 @@
#!/bin/bash #!/bin/bash
YW=`echo "\033[33m"` RED=$(tput setaf 1)
BL=`echo "\033[36m"` GREEN=$(tput setaf 2)
RD=`echo "\033[01;31m"` YELLOW=$(tput setaf 3)
CM='\xE2\x9C\x94\033' BLUE=$(tput setaf 4)
GN=`echo "\033[1;92m"` WHITE=$(tput setaf 7)
CL=`echo "\033[m"` NORMAL=$(tput sgr0)
while true; do while true; do
read -p "This will Clean unused Kernel images. Proceed(y/n)?" yn read -p "${YELLOW}This will Clean unused Kernel images. Proceed(y/n)?${NORMAL}" yn
case $yn in case $yn in
[Yy]* ) break;; [Yy]* ) break;;
[Nn]* ) exit;; [Nn]* ) exit;;
* ) echo "Please answer yes or no.";; * ) echo -e "${RED}Please answer y/n${NORMAL}";;
esac esac
done done
clear clear
current_kernel=$(uname -r) current_kernel=$(uname -r)
pve=$(pveversion)
function check_root { function check_root {
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
printf "[!] Error: this script must be ran as the root user.\n" echo -e "${RED}Error: This script must be ran as the root user.\n${NORMAL}"
exit 1 exit 1
fi fi
} }
function header_info { function header_info {
echo -e "${RD} echo -e "${RED}
_ __ _ _____ _ _ __ _ _____ _
| |/ / | | / ____| | | |/ / | | / ____| |
| / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __ | / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __
@ -30,57 +31,59 @@ echo -e "${RD}
| \ __/ | | | | | __/ | | |____| | __/ (_| | | | | | \ __/ | | | | | __/ | | |____| | __/ (_| | | | |
|_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_| |_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_|
${CL}" ${NORMAL}"
} }
function kernel_info { function kernel_info() {
latest_kernel=$(dpkg --list| grep 'pve-kernel-.*-pve' | awk '{print $2}' | tac | head -n 1) latest_kernel=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | tac | head -n 1)
printf "OS: $(cat /etc/os-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/["]//g' | awk '{print $0}')\n" 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}"
printf "Current Kernel: pve-kernel-$current_kernel\n" 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
printf "Latest Kernel: $latest_kernel\n" echo -e "${GREEN}Latest Kernel: $latest_kernel\n${NORMAL}"
fi fi
else else
printf "___________________________________________\n\n" echo -e "\n${RED}ERROR: No PVE Kernel found\n${NORMAL}"
printf "[!] Warning, you're not running a PVE Kernel\n"
exit 1 exit 1
fi fi
} }
function kernel_clean {
kernels=$(dpkg --list| grep 'pve-kernel-.*-pve' | awk '{print $2}' | sort -V) function kernel_clean() {
kernels=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | sort -V)
kernels_to_remove="" kernels_to_remove=""
for kernel in $kernels for kernel in $kernels
do do
if [ "$(echo $kernel | grep $current_kernel)" ]; then if [ "$(echo $kernel | grep $current_kernel)" ]; then
break break
else else
printf "\"$kernel\" has been added to the Kernel remove list\n" echo -e "${RED}'$kernel' ${NORMAL}${YELLOW}has been added to the Kernel remove list\n${NORMAL}"
kernels_to_remove+=" $kernel" kernels_to_remove+=" $kernel"
fi fi
done done
printf "Kernel Search Complete!\n" echo -e "${YELLOW}Kernel Search Complete!\n${NORMAL}"
if [[ "$kernels_to_remove" != *"pve"* ]]; then if [[ "$kernels_to_remove" != *"pve"* ]]; then
printf "It appears there are no old Kernels on your system \n" echo -e "${GREEN}It appears there are no old Kernels on your system \n${NORMAL}"
else else
read -p "[!] Would you like to remove the $(echo $kernels_to_remove | awk '{print NF}') selected Kernels listed above? [y/n]: " -n 1 -r 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
printf "\n"
fi fi
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
printf "Removing $(echo $kernels_to_remove | awk '{print NF}') old Kernels..." echo -e "${YELLOW}\nRemoving ${NORMAL}${RED}$(echo $kernels_to_remove | awk '{print NF}') ${NORMAL}${YELLOW}old Kernels...${NORMAL}"
/usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1 /usr/bin/apt purge -y $kernels_to_remove > /dev/null 2>&1
printf "Finished!\n" echo -e "${YELLOW}Finished!\n${NORMAL}"
printf "Updating GRUB... \n" echo -e "${YELLOW}Updating GRUB... \n${NORMAL}"
/usr/sbin/update-grub > /dev/null 2>&1 /usr/sbin/update-grub > /dev/null 2>&1
printf "Finished!\n" echo -e "${YELLOW}Finished!\n${NORMAL}"
else else
printf "\nExiting...\n" echo -e "${YELLOW}Exiting...\n${NORMAL}"
sleep 1
fi fi
} }
function main {
function main() {
check_root check_root
header_info header_info
kernel_info kernel_info
} }
while true; do while true; do
case "$1" in case "$1" in
* ) * )