Proxmox/misc/kernel-clean.sh

130 lines
3.5 KiB
Bash
Raw Normal View History

2022-04-23 15:17:44 +00:00
#!/usr/bin/env bash -ex
set -euo pipefail
shopt -s inherit_errexit nullglob
YW=`echo "\033[33m"`
BL=`echo "\033[36m"`
RD=`echo "\033[01;31m"`
BGN=`echo "\033[4;92m"`
GN=`echo "\033[1;92m"`
DGN=`echo "\033[32m"`
CL=`echo "\033[m"`
BFR="\\r\\033[K"
HOLD="-"
CM="${GN}${CL}"
CROSS="${RD}${CL}"
PARTY="🎉"
current_kernel=$(uname -r)
while true; do
2022-07-13 15:45:24 +00:00
read -p "This will Clean Unused Kernel Images, USE AT YOUR OWN RISK. Proceed(y/n)?" yn
2022-04-23 15:17:44 +00:00
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo -e "${RD}Please answer y/n${CL}";;
esac
done
clear
function header_info {
echo -e "${RD}
_ __ _ _____ _
| |/ / | | / ____| |
| / ___ _ __ _ __ ___| | | | | | ___ __ _ _ __
| < / _ \ __| _ \ / _ \ | | | | |/ _ \/ _ | _ \
| \ __/ | | | | | __/ | | |____| | __/ (_| | | | |
|_|\_\___|_| |_| |_|\___|_| \_____|_|\___|\__,_|_| |_|
2022-04-23 15:17:44 +00:00
${CL}"
}
function msg_info() {
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg}..."
}
function msg_ok() {
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
}
function check_root() {
if [[ $EUID -ne 0 ]]; then
echo -e "${CROSS}${RD}Error: This script must be ran as the root user.\n${CL}"
exit 1
else
header_info
2022-07-13 16:56:23 +00:00
edge_kernel
2022-04-23 15:17:44 +00:00
kernel_info
kernel_clean
fi
}
2022-07-13 16:56:23 +00:00
function edge_kernel() {
if [[ "$current_kernel" == *"edge"* ]]; then
echo -e "\n${CROSS} ${RD}ERROR:${CL} Proxmox ${BL}${current_kernel}${CL} Kernel Active"
2022-07-13 21:04:10 +00:00
echo -e "\nAn Active PVE Kernel is required to use Kernel Clean\n"
2022-07-13 16:56:23 +00:00
exit 1
fi
}
2022-04-23 15:17:44 +00:00
function kernel_info() {
2022-09-10 01:03:20 +00:00
if [[ "$MODE" != "PBS" ]]; then
echo -e "${YW}PVE Version: ${BL}$(pveversion)\n${CL}"
fi
2022-04-23 15:17:44 +00:00
if [[ "$current_kernel" == *"pve"* ]]; then
2022-10-13 13:24:33 +00:00
echo -e "${YW}Current Kernel: ${BL}$current_kernel\n${CL}"
2022-04-23 15:17:44 +00:00
else
2022-07-13 16:56:23 +00:00
echo -e "\n${CROSS} ${RD}ERROR: No PVE Kernel Found\n${CL}"
2022-04-23 15:17:44 +00:00
exit 1
fi
}
function kernel_clean() {
kernels=$(dpkg --list| grep 'kernel-.*-pve' | awk '{print $2}' | sort -V)
remove_kernels=""
for kernel in $kernels
do
if [ "$(echo $kernel | grep $current_kernel)" ]; then
break
else
echo -e "${BL}'$kernel' ${CL}${YW}has been added to the remove Kernel list\n${CL}"
remove_kernels+=" $kernel"
fi
done
msg_ok "Kernel Search Completed\n"
if [[ "$remove_kernels" != *"pve"* ]]; then
echo -e "${PARTY} ${GN}It appears there are no old Kernels on your system. \n${CL}"
msg_info "Exiting"
sleep 2
msg_ok "Done"
else
2022-05-04 09:56:10 +00:00
read -p "Would you like to remove the $(echo $remove_kernels | awk '{print NF}') selected Kernels listed above? [y/n]: " -n 1 -r
2022-04-23 15:17:44 +00:00
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
2022-05-04 10:08:21 +00:00
msg_info "Removing ${CL}${RD}$(echo $remove_kernels | awk '{print NF}') ${CL}${YW}old Kernels${CL}"
2022-04-23 15:17:44 +00:00
/usr/bin/apt purge -y $remove_kernels > /dev/null 2>&1
msg_ok "Successfully Removed Kernels"
msg_info "Updating GRUB"
/usr/sbin/update-grub > /dev/null 2>&1
msg_ok "Successfully Updated GRUB"
msg_info "Exiting"
sleep 2
msg_ok "Done"
else
msg_info "Exiting"
sleep 2
msg_ok "Done"
fi
fi
}
2022-09-10 01:03:20 +00:00
if ! command -v pveversion >/dev/null 2>&1; then
echo -e " Switching to PBS mode"
MODE="PBS"
sleep 2
else
MODE="PVE"
fi
2022-04-23 15:17:44 +00:00
check_root