16

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.

Fabby
  • 34,259
  • You have to modify the script in several places, there is no other way. If you wish, you can file a "wishlist" bug report on the apt package to allow the number of kernels to be customised. – fkraiem Dec 17 '17 at 10:09
  • 2
    The "four" in the part you quoted refers to the unusual case where "the currently booted version", "the kernel version we've been called for", "the latest kernel version", and "the second-latest kernel version" are all distinct, since all those versions are kept. In typical cases, however, this boils down to two because "the currently booted version" and "the second-latest kernel version" are the same, and likewise for the two others. – fkraiem Dec 17 '17 at 10:16

1 Answers1

14

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)"
muru
  • 197,895
  • 55
  • 485
  • 740
  • Yes, this looks like it should work. One should note that since the file is part of the apt package, it could be reverted to the default one when apt is upgraded. – fkraiem Dec 17 '17 at 10:34
  • @fkraiem the file is in /etc, so it's presumably a conffile and changes should be preserved (or, at least, the user will be asked about them) – muru Dec 17 '17 at 10:35
  • 1
    Yes, all files under /etc are conffiles, but it seems users often get confused when the "keep or replace?" question pops up... – fkraiem Dec 17 '17 at 10:52
  • 1
    I won't get confused... @fkraiem – Fabby Dec 17 '17 at 11:14
  • apt still complains: The following packages were automatically installed and are no longer required: linux-headers-4.10.0-38 linux-headers-4.10.0-38-generic linux-image-4.10.0-38-generic linux-image-extra-4.10.0-38-generic linux-signed-image-4.10.0-38-generic Use 'sudo apt autoremove' to remove them. but that's an executable and I'll stop worrying about it and have adapted the question to fit the answer. :-) – Fabby Dec 17 '17 at 11:24
  • Nice answer but above my paygrade. Here's a new question you might be able to answer: https://askubuntu.com/questions/1325387/why-is-apt-get-autoremove-failing-to-remove-my-old-kernels – WinEunuuchs2Unix Mar 23 '21 at 00:04