2

From what I understand, apt is internally configured to only keep the current kernel plus two more (based on this answer).

However, I can see on my system that four kernels are installed:

$ dpkg -l | grep linux-image-.*-generic  | grep ^ii
ii  linux-image-5.15.0-82-generic              5.15.0-82.91                            amd64        Signed kernel image generic
ii  linux-image-5.19.0-50-generic              5.19.0-50.50                            amd64        Signed kernel image generic
ii  linux-image-6.2.0-26-generic               6.2.0-26.26~22.04.1                     amd64        Signed kernel image generic
ii  linux-image-6.2.0-31-generic               6.2.0-31.31~22.04.1                     amd64        Signed kernel image generic

I think the only reason this should happen is if I've recently upgraded my kernel and haven't rebooted yet, but that doesn't seem to be the case:

$ uname -a
Linux hostname 6.2.0-31-generic #31~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 16 13:45:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Running sudo apt autoremove doesn't uninstall any additional kernels:

$ sudo apt autoremove --purge
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
...

If I list all the linux-image packages, this is what I get:

$ dpkg -l | grep linux-image | grep ^ii
ii  linux-image-5.15.0-82-generic              5.15.0-82.91                            amd64        Signed kernel image generic
ii  linux-image-5.19.0-50-generic              5.19.0-50.50                            amd64        Signed kernel image generic
ii  linux-image-6.2.0-26-generic               6.2.0-26.26~22.04.1                     amd64        Signed kernel image generic
ii  linux-image-6.2.0-31-generic               6.2.0-31.31~22.04.1                     amd64        Signed kernel image generic
ii  linux-image-generic                        5.15.0.82.78                            amd64        Generic Linux kernel image
ii  linux-image-generic-hwe-22.04              6.2.0.31.31~22.04.8                     amd64        Generic Linux kernel image

linux-image-generic-hwe-22.04 requires the 6.2.0-31 kernel, so I'm guessing apt is preserving that kernel as well as the two previous kernels.

And linux-image-generic seems to require the 5.15.0-82 kernel as well, so I guess that explains why a fourth kernel is being kept?

But after removing linux-image-generic, sudo apt autoremove still doesn't remove the fourth kernel.

bmaupin
  • 4,930
  • "apt is internally configured to only keep the current kernel plus two more." Well, that seems bit of an oversimplification. When the apt settings and the repo metapackages all work together properly, the intended effect of three kernels occurs. Your system might fall off that path for many possible (but rare) reasons. You can safely use apt to uninstall older unused kernels as long as you carefully read your apt output before agreeing to the removal. – user535733 Aug 31 '23 at 15:53
  • Technically it can be 4: https://askubuntu.com/q/987051/158442 – muru Aug 31 '23 at 15:59
  • @muru That question and its answers refer to previous versions of Ubuntu where the logic to remove old kernels was external to apt (you'll notice /etc/kernel/postinst.d/apt-auto-removal doesn't exist in recent versions of Ubuntu). Also, the only scenario they provide for four kernels being installed is when a newer version is installed than the one being used, which doesn't apply in my case since I'm using the latest one installed. – bmaupin Aug 31 '23 at 16:07
  • Hmm, in that case you should use aptitude why to see if something else might be depending on these installed kernels – muru Aug 31 '23 at 16:14
  • @user535733 This also seems to indicate to me that no more than 3 kernels should be kept: https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1968154 But as you suggested, perhaps I am indeed oversimplifying – bmaupin Aug 31 '23 at 16:44

1 Answers1

0

My best guess at this point is that the logic in apt for automatically removing old kernels must not take into account when an HWE version of linux-image-generic is installed alongside the normal linux-image-generic package. Maybe this is intended behaviour.

Indeed, I would prefer to keep the 5.15 kernel around just in case, but unfortunately I was hit by the Ubuntu bug where my /boot partition isn't big enough (my disk is encrypted) so I don't have enough space for four kernels, even though I have a 1 TB hard drive

As a workaround, I'm uninstalling linux-image-5.15.0-82-generic manually:

$ sudo apt purge linux-image-5.15.0-82-generic linux-modules-5.15.0-82-generic
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  linux-image-5.15.0-82-generic* linux-modules-5.15.0-82-generic*
  linux-modules-extra-5.15.0-82-generic*

And I'll keep an eye on my system to see if it ends up in this situation again later.

bmaupin
  • 4,930