To keep additional kernels I think you would have to essentially duplicate the apt-auto-removal
script concept. Have a script that runs and generates an apt configuration specifying what to keep in an APT::NeverAutoRemove
block. You might even be able to simply download the old apt-auto-removal
script into /etc/kernel/postinst.d/
and patch it to keep the number of kernels you want, but I have not tested that.
The apt-auto-removal
script was removed from the apt
package recently, and the logic for protecting kernels from autoremove was added into apt
itself. There were some edge cases that the script did not handle properly because it was only run when a kernel was installed. From the apt
changelog.
apt (2.1.16) unstable; urgency=medium
...
* Rewrite of the kernel autoremoval code:
- Determine autoremovable kernels at run-time (LP: #1615381), this fixes the
issue where apt could consider a running kernel autoremovable
- Automatically remove unused kernels on apt {full,dist}-upgrade.
This helps ensuring that we don't run out of /boot space.
- Only keep up to 3 (not 4) kernels.
Ubuntu boot partitions were sized for 3 kernels, not 4.
...
-- Julian Andres Klode <jak@debian.org> Fri, 08 Jan 2021 21:49:15 +0100
Based on the apt
source code it looks like using the "run-time" logic apt
will not autoremove
- the booted kernel
- the latest kernel
- the previous kernel
I do not see any obvious way to configure the behavior to be different. These are some apt
configuration options that might be useful.
APT
{
...
NeverAutoRemove "<LIST>"; // list of package name regexes
LastInstalledKernel "<STRING>"; // last installed kernel version
VersionedKernelPackages "<LIST>"; // regular expressions to be protected from autoremoval (kernel uname will be appended)
Protect-Kernels "<BOOL>"; // whether to protect installed kernels against autoremoval (default: true)
links