2

As of 18.04, Ubuntu removes old kernels when upgrading to a new one. As far as I can tell, this procedure is part of the install process. I currently have 3 different kernel versions installed on my machine.

After a kernel update which seems to have broken my system, requiring me to manually boot an old kernel, I am now wondering what logic Ubuntu uses to decide which kernels to keep around and which ones to remove. Specifically, I want to know whether the working kernel will eventually be rotated out of the kernels to keep around.

  • Am I right that all kernel cleanup tasks are executed at install time, with nothing deferred until the system is rebooted? (As in the latter case, these deferred tasks could theoretically remove any kernel other then the newly installed one.)
  • How does Ubuntu determine which kernels to remove? Does it simply keep the n most recent kernels, or is the logic more complex than that?
  • Is the kernel running at the time of the update guaranteed to be kept around until the next update?
  • Does Ubuntu have any tracking mechanism to detect which kernels have booted cleanly, and ensure not all of them are erased?

1 Answer 1

3

Kernel installation-related tasks are handled by scripts in /etc/kernel, and you are free to browse the logic yourself directly.

How does Ubuntu determine which kernels to remove? Does it simply keep the n most recent kernels, or is the logic more complex than that?

Is the kernel running at the time of the update guaranteed to be kept around until the next update?

Kernels are selected by the script /etc/kernel/postinst.d/apt-auto-removal. The comments at the top of that script are particularly enlightening:

# Mark as not-for-autoremoval those kernel packages that are:
#  - the currently booted version
#  - the kernel version we've been called for
#  - the latest kernel version (as determined by debian version number)
#  - the second-latest kernel version
#
# 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.

Am I right that all kernel cleanup tasks are executed at install time, with nothing deferred until the system is rebooted? (As in the latter case, these deferred tasks could theoretically remove any kernel other then the newly installed one.)

Not quite. Older kernel(s) are made eligible for autoremoval immediately by /etc/kernel/postinst.d/apt-auto-removal. The older packages not actually removed until the next time apt runs autoremove. In systems with unattended-upgrades installed, that usually means the next day.


Does Ubuntu have any tracking mechanism to detect which kernels have booted cleanly, and ensure not all of them are erased?

Not sure what you mean by "booted cleanly". Ubuntu assumes that the currently-booted kernel works, and won't make the currently-booted kernel eligible for autoremoval...but you knew that already.

2
  • Thanks for your answer! I should probably clarify about kernel cleanup tasks—what I really meant is: does the decision of kernels to remove depend exclusively on the system state at the time the new kernel is installed? I infer from your answer that this is the case.
    – user149408
    Jan 31, 2019 at 14:56
  • You don't need to infer. The answer is explicitly 'yes' according to the script.
    – user535733
    Jan 31, 2019 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .