Comment 2 for bug 1990624

Revision history for this message
Jeffrey Walton (noloader) wrote :

> Are you invoking kernel-install to remove kernels? Or what action did you perform exactly?

Good question, Nick.

I am not using kernel-install. Instead, I use a command like:

   # For the case of 5.15.0-41-generic kernel
   $ sudo apt-get remove --purge '*5.15.0-41*'

In fact, I ran the command manually when I noticed the accumulation of old kernel parts in /lib/modules. Apt said there was nothing to do.

I knew about the 2016630 bug from Red Hat because I first experienced the issue on Fedora 35. Now I am seeing it on Ubuntu, too.

----------

Here is the actual script I am running. It looks at the current kernel, and makes a list of all kernels. It removes the current kernel from the list of all kernels, and then removes all the old kernels.

    # Get a list mostly removed kernels. The kernel and package have been
    # removed, but the configuration files and directory remains.
    old_kernels=($(dpkg -l | grep linux-image | grep '^rc' | tr -s " " | \
        cut -f 2 -d ' ' | sort | uniq))

    if [ "${#old_kernels[@]}" -ne 0 ]; then
        apt-get remove -y --purge "${old_kernels[@]}" 1>/dev/null
    fi

    # Get the current kernel
    current_kernel=$(uname -r)
    version_only=$(get_version ${current_kernel})

    echo "Current kernel:"
    echo " ${current_kernel} (${version_only})"

    # Get a list of installed kernels. The `grep -v` removes the
    # current kernel from the list, which should be the latest kernel.
    old_kernels=($(dpkg -l | grep linux-image | grep '^ii' | tr -s " " | \
        cut -f 2 -d ' ' | sort -V | uniq | grep -v "${current_kernel}"))

    temp_list=()
    for k in "${old_kernels[@]}"
    do
        # Skip the metapackage linux-image-generic
        if [[ "${k}" == linux-image-generic* ]]; then
            continue
        fi
        temp_list+=("${k}")
    done

    # Swap-in the new list
    old_kernels=("${temp_list[@]}")

    if [ "${#old_kernels[@]}" -eq 0 ]; then
        echo "No old kernels found"
        exit 0
    fi

    echo "Old kernels to remove:"
    for k in "${old_kernels[@]}"
    do
        version_only=$(get_version ${k})
        echo " ${k} (${version_only})"
    done

    for k in "${old_kernels[@]}"
    do
        version_only=$(get_version ${k})
        if ! apt-get remove -y --purge "*${version_only}*"; then
            echo "Failed to remove ${k}"
            some_error=1
        fi

    done

    apt-get install -y --reinstall linux-image-generic linux-headers-generic

Fedora is a lot easier to remove old kernels. On Fedora I use:

    old_kernels=($(dnf repoquery --installonly --latest-limit=-1 -q))
    if [ "${#old_kernels[@]}" -eq 0 ]; then
        echo "No old kernels found"
        exit 0
    fi

    if ! dnf -y remove "${old_kernels[@]}"; then
        echo "Failed to remove old kernels"
        exit 1
    fi