ProxmoxVE/misc/kernel-clean.sh

121 lines
3.4 KiB
Bash
Raw Normal View History

2023-02-07 17:15:22 +00:00
#!/usr/bin/env bash
2024-01-01 17:13:05 +00:00
# Copyright (c) 2021-2024 tteck
2023-02-07 17:15:22 +00:00
# Author: tteck (tteckster)
# License: MIT
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
2023-02-07 17:15:22 +00:00
2023-01-10 16:08:36 +00:00
function header_info {
clear
cat <<"EOF"
__ __ __ ________
/ //_/__ _________ ___ / / / ____/ /__ ____ _____
2023-01-10 16:08:36 +00:00
/ ,< / _ \/ ___/ __ \/ _ \/ / / / / / _ \/ __ `/ __ \
/ /| / __/ / / / / / __/ / / /___/ / __/ /_/ / / / /
/_/ |_\___/_/ /_/ /_/\___/_/ \____/_/\___/\__,_/_/ /_/
2023-01-10 16:08:36 +00:00
EOF
}
# Color variables for output
2022-10-30 01:04:45 +00:00
YW=$(echo "\033[33m")
RD=$(echo "\033[01;31m")
GN=$(echo "\033[1;92m")
CL=$(echo "\033[m")
2022-04-23 15:17:44 +00:00
BFR="\\r\\033[K"
HOLD="-"
CM="${GN}${CL}"
# Functions for logging messages
2022-04-23 15:17:44 +00:00
function msg_info() {
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg}..."
2022-04-23 15:17:44 +00:00
}
function msg_ok() {
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
2022-04-23 15:17:44 +00:00
}
# Detect current kernel
current_kernel=$(uname -r)
# Detect all installed kernels except the current one
available_kernels=$(dpkg --list | grep 'kernel-.*-pve' | awk '{print $2}' | grep -v "$current_kernel" | sort -V)
header_info
# If no old kernels are available, exit with a message
if [ -z "$available_kernels" ]; then
whiptail --backtitle "Proxmox VE Helper Scripts" --title "No Old Kernels" \
--msgbox "It appears there are no old kernels on your system.\nCurrent kernel: $current_kernel" 10 68
echo "Exiting..."
sleep 2
clear
exit
fi
# Prepare kernel options for selection
KERNEL_MENU=()
while read -r TAG ITEM; do
OFFSET=2
MSG_MAX_LENGTH=$((MSG_MAX_LENGTH < ${#ITEM} + OFFSET ? ${#ITEM} + OFFSET : MSG_MAX_LENGTH))
KERNEL_MENU+=("$TAG" "$ITEM " "OFF")
done < <(echo "$available_kernels")
2022-04-23 15:17:44 +00:00
# Display checklist to select kernels for removal
remove_kernels=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
--title "Current Kernel: $current_kernel" \
--checklist "\nSelect kernels to remove:\n" \
16 $((MSG_MAX_LENGTH + 58)) 6 "${KERNEL_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit
# Exit if no kernel was selected
[ -z "$remove_kernels" ] && {
whiptail --backtitle "Proxmox VE Helper Scripts" --title "No Kernel Selected" \
--msgbox "It appears no kernel was selected." 10 68
echo "Exiting..."
sleep 2
clear
exit
}
# Confirm removal
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Remove Kernels" \
--yesno "Would you like to remove the $(echo $remove_kernels | awk '{print NF}') selected kernels?" 10 68 || exit
2022-09-10 01:03:20 +00:00
# Process kernel removal
msg_info "Removing ${RD}$(echo $remove_kernels | awk '{print NF}') ${YW}old kernels${CL}"
for kernel in $remove_kernels; do
if [[ $kernel == *"-signed" ]]; then
# Handle signed kernels with dependencies
touch /please-remove-proxmox-ve # Temporarily bypass Proxmox warnings
if sudo apt-get purge -y "$kernel" >/dev/null 2>&1; then
msg_ok "Removed kernel: $kernel"
else
msg_info "Failed to remove kernel: $kernel. Check dependencies or manual removal."
fi
rm -f /please-remove-proxmox-ve # Clean up bypass file
else
# Standard kernel removal
if sudo apt-get purge -y "$kernel" >/dev/null 2>&1; then
msg_ok "Removed kernel: $kernel"
else
msg_info "Failed to remove kernel: $kernel. Check dependencies or manual removal."
fi
fi
sleep 1
done
# Update GRUB configuration
msg_info "Updating GRUB"
if /usr/sbin/update-grub >/dev/null 2>&1; then
msg_ok "GRUB updated successfully"
else
msg_info "Failed to update GRUB"
fi
# Completion message
msg_info "Exiting"
sleep 2
msg_ok "Finished"