Managing Old Kernels on Ubuntu: A Custom Kernel Removal Script

Managing Old Kernels on Ubuntu: A Custom Kernel Removal Script

Post Stastics

  • This post has 775 words.
  • Estimated read time is 3.69 minute(s).

Ubuntu, like many Linux distributions, frequently updates its kernel to ensure security, stability, and compatibility with new hardware. However, over time, old kernels accumulate on the system, taking up valuable space in the /boot partition. Ubuntu tends to keep every kernel that has ever been installed, but it does not provide an easy mechanism to remove these outdated versions automatically. This can become a problem, especially for users with smaller /boot partitions.

In this article, we'll introduce a simple Bash script that provides an interactive menu to list and remove old kernels, while ensuring the current running kernel is not accidentally removed. The script also updates the GRUB configuration after each removal to reflect the changes.

Why Remove Old Kernels?

Each kernel update leaves behind the previous versions. While keeping older kernels as backups is useful in case a new kernel causes issues, allowing them to accumulate unnecessarily wastes disk space. It is important to periodically clean out old kernels to free up space, especially on systems with limited storage.

Custom Script for Kernel Management

Here’s a straightforward script that displays all installed kernels (except the current one), allows you to select one to remove, and then updates the system's GRUB configuration. The script will continue listing the kernels until you choose to exit by pressing Enter.

The Script:

#!/bin/bash
#====================================
# Remove the kernel selected from the 
# menu listing.
# Press Enter/Return to exit
#====================================

# Function to list installed kernels sorted by version in reverse order
list_kernels() {
    echo "Installed kernels (sorted by version, latest first):"
    dpkg -l | grep 'linux-image-[0-9]' | grep -v $(uname -r) | awk '{print $2}' | sort -Vr | nl
}

# Main loop
while true; do
    list_kernels

    # Ask the user for input
    echo ""
    echo "Enter the number of the kernel you wish to remove (or press Enter to exit):"
    read -p "Selection: " selection

    # If the input is empty, break the loop
    if [ -z "$selection" ]; then
        echo "Exiting..."
        break
    fi

    # Get the selected kernel package name
    kernel_package=$(dpkg -l | grep 'linux-image-[0-9]' | grep -v $(uname -r) | awk '{print $2}' | sort -Vr | sed -n "${selection}p")

    # If the selection is valid, proceed with removal
    if [ ! -z "$kernel_package" ]; then
        echo "Removing $kernel_package..."
        sudo apt-get purge -y $kernel_package
    else
        echo "Invalid selection. Please try again."
    fi

    echo ""
done

# Update GRUB configuration
echo ""
echo "Updating Grub..."
sudo update-grub
echo ""
echo "Done"

How the Script Works:

  1. Kernel Listing: The script lists all installed kernels except the currently active one, which is protected from accidental removal. The list is sorted in reverse version order, so the most recent kernels appear at the top. Each kernel is assigned a number for easy selection.
  2. Interactive Menu: The user is prompted to enter the number corresponding to the kernel they wish to remove. If no input is given (i.e., the user presses Enter), the script will exit.
  3. Kernel Removal: The script checks whether the selected kernel exists, and if valid, uses apt-get purge to remove the selected kernel and all its associated files. This method ensures that no residual files are left behind.
  4. GRUB Update: After a kernel is removed, the script automatically updates the GRUB configuration to reflect the changes. This ensures that removed kernels are no longer displayed in the GRUB boot menu during system startup.
  5. Exit Mechanism: The script continues to loop, displaying the remaining kernels until the user presses Enter without selecting a kernel.

Why Ubuntu Needs an Integrated Kernel Management Tool

While this script provides a practical solution for managing kernels, it highlights a larger issue: Ubuntu lacks an automatic kernel removal mechanism. Over time, the accumulation of kernels can consume significant disk space, especially in the /boot partition, potentially leading to space-related issues during updates.

A built-in tool that could keep only the most recent three kernels would be a welcome feature. This would strike a balance between ensuring system recovery options and maintaining disk space efficiency. Of course, users should also have the option to protect specific older kernels from automatic removal, if they wish to keep certain versions for compatibility reasons.

Conclusion

Manually removing old kernels can be a hassle, but the provided script simplifies the process by allowing users to select and remove outdated kernels interactively. By cleaning up unused kernels, you can free up space on your system and ensure that your /boot partition remains uncluttered. However, Ubuntu would benefit from an integrated tool that automatically manages kernel versions, removing older ones while retaining the latest few for recovery purposes. Until such a feature is introduced, this script offers a simple and effective solution for managing kernels.

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *