Update Kernel-Clean to new Version & Bugfixing (#1147)

* Update Kernel-Clean to new Version & Bugfixing

* Update kernel-clean.sh
This commit is contained in:
CanbiZ 2025-01-01 12:48:28 +01:00 committed by GitHub
parent a9df33c7cf
commit 2af4978680
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright (c) 2021-2025 community-scripts ORG
# Copyright (c) 2021-2024 tteck # Author: MickLesk
# Author: tteck (tteckster)
# License: MIT # License: MIT
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE # https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
@ -17,104 +16,65 @@ function header_info {
EOF EOF
} }
# Color variables for output # Color variables
YW=$(echo "\033[33m") YW="\033[33m"
RD=$(echo "\033[01;31m") GN="\033[1;92m"
GN=$(echo "\033[1;92m") RD="\033[01;31m"
CL=$(echo "\033[m") CL="\033[m"
BFR="\\r\\033[K"
HOLD="-"
CM="${GN}${CL}"
# Functions for logging messages
function msg_info() {
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg}..."
}
function msg_ok() {
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
}
# Detect current kernel # Detect current kernel
current_kernel=$(uname -r) 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) available_kernels=$(dpkg --list | grep 'kernel-.*-pve' | awk '{print $2}' | grep -v "$current_kernel" | sort -V)
header_info header_info
# If no old kernels are available, exit with a message
if [ -z "$available_kernels" ]; then if [ -z "$available_kernels" ]; then
whiptail --backtitle "Proxmox VE Helper Scripts" --title "No Old Kernels" \ echo -e "${GN}No old kernels detected. Current kernel: ${current_kernel}${CL}"
--msgbox "It appears there are no old kernels on your system.\nCurrent kernel: $current_kernel" 10 68 exit 0
echo "Exiting..."
sleep 2
clear
exit
fi fi
# Prepare kernel options for selection echo -e "${YW}Available kernels for removal:${CL}"
KERNEL_MENU=() echo "$available_kernels" | nl -w 2 -s '. '
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")
# Display checklist to select kernels for removal echo -e "\n${YW}Select kernels to remove (comma-separated, e.g., 1,2):${CL}"
remove_kernels=$(whiptail --backtitle "Proxmox VE Helper Scripts" \ read -r selected
--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 # Parse selection
[ -z "$remove_kernels" ] && { IFS=',' read -r -a selected_indices <<< "$selected"
whiptail --backtitle "Proxmox VE Helper Scripts" --title "No Kernel Selected" \ kernels_to_remove=()
--msgbox "It appears no kernel was selected." 10 68
echo "Exiting..."
sleep 2
clear
exit
}
# Confirm removal for index in "${selected_indices[@]}"; do
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Remove Kernels" \ kernel=$(echo "$available_kernels" | sed -n "${index}p")
--yesno "Would you like to remove the $(echo $remove_kernels | awk '{print NF}') selected kernels?" 10 68 || exit if [ -n "$kernel" ]; then
kernels_to_remove+=("$kernel")
# 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 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 done
# Update GRUB configuration if [ ${#kernels_to_remove[@]} -eq 0 ]; then
msg_info "Updating GRUB" echo -e "${RD}No valid selection made. Exiting.${CL}"
if /usr/sbin/update-grub >/dev/null 2>&1; then exit 1
msg_ok "GRUB updated successfully"
else
msg_info "Failed to update GRUB"
fi fi
# Completion message # Confirm removal
msg_info "Exiting" echo -e "${YW}Kernels to be removed:${CL}"
sleep 2 printf "%s\n" "${kernels_to_remove[@]}"
msg_ok "Finished" read -rp "Proceed with removal? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo -e "${RD}Aborted.${CL}"
exit 1
fi
# Remove kernels
for kernel in "${kernels_to_remove[@]}"; do
echo -e "${YW}Removing $kernel...${CL}"
if apt-get purge -y "$kernel" >/dev/null 2>&1; then
echo -e "${GN}Successfully removed: $kernel${CL}"
else
echo -e "${RD}Failed to remove: $kernel. Check dependencies.${CL}"
fi
done
# Clean up and update GRUB
echo -e "${YW}Cleaning up...${CL}"
apt-get autoremove -y >/dev/null 2>&1 && update-grub >/dev/null 2>&1
echo -e "${GN}Cleanup and GRUB update complete.${CL}"