How to keep 4 Linux kernels in /boot by default before they are removed automatically

I did some digging around and found that the automatic clean-up for kernels is in /etc/kernel/postinst.d/apt-auto-removal and the file states:

# In the common case this results in two kernels saved (booted into the
# second-latest kernel, we install the latest kernel in an upgrade), but
# can save up to four. Kernel refers here to a distinct release, which can
# potentially be installed in multiple flavours counting as one kernel.

but that script is way above my head and I cannot easily find how autoclean anything but the last 4 kernels.


Solution 1:

What it means when it says "up to four" is that the currently running, current installed, latest, and previous versions can all be different, resulting in four versions being automatically kept (with a minimum of two). See this part of the code:

debkernels="$(echo "$latest_version
$installed_version
$running_version
$previous_version" | sort -u | sed -e '/^$/ d')"

I think the simplest way to get it to save older kernels would be to extend the $previous_version to a list. Instead of:

previous_version="$(echo "$debverlist" | sed -n 2p)"

Do:

previous_version="$(echo "$debverlist" | sed -n 2,4p)"