当前位置: 首页>>技术教程>>正文


server – ‘apt’ 如何决定保留多少个旧内核?

, , ,

问题描述

我正在管理几台用于核心服务(NTP、DNS 等)的服务器,我突然想到其中一台服务器似乎保留了 3 个最新内核,而不是其他服务器上的 2 个:

nul@quark:~$ sudo apt-get autoremove --purge
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
nul@quark:~$ dpkg -l |grep linux-image
ii  linux-image-3.13.0-51-generic       3.13.0-51.84                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.13.0-52-generic       3.13.0-52.85                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.16.0-37-generic       3.16.0-37.49~14.04.1             amd64        Linux kernel image for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-51-generic 3.13.0-51.84                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-52-generic 3.13.0-52.85                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.16.0-37-generic 3.16.0-37.49~14.04.1             amd64        Linux kernel extra modules for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-generic                 3.13.0.52.59                     amd64        Generic Linux kernel image
ii  linux-image-generic-lts-utopic      3.16.0.37.29                     amd64        Generic Linux kernel image

...

nul@dwarf:~$ sudo apt-get autoremove --purge
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
nul@dwarf:~$ dpkg -l |grep linux-image
ii  linux-image-3.13.0-52-generic       3.13.0-52.85                     amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-3.16.0-37-generic       3.16.0-37.49~14.04.1             amd64        Linux kernel image for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-52-generic 3.13.0-52.85                     amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.16.0-37-generic 3.16.0-37.49~14.04.1             amd64        Linux kernel extra modules for version 3.16.0 on 64 bit x86 SMP
ii  linux-image-generic                 3.13.0.52.59                     amd64        Generic Linux kernel image
ii  linux-image-generic-lts-utopic      3.16.0.37.29                     amd64        Generic Linux kernel image

所有服务器的维护都是相同的,不知道我能做什么,一定是某个地方的参数,但找不到它!

请满足我的好奇心!谢谢

最佳答案

2022 年 10 月 3 日更新:

从 2.4.5 版本开始,Canonical/Debian 似乎已将这些设置直接实施到 apt 中,以保护两个内核

apt changelog apt

apt (2.4.5) unstable; urgency=medium

  * Only protect two kernels, not last installed one (LP: #1968154)
  * Fix segfault in CacheSetHelperAPTGet::tryVirtualPackage()

 -- Julian Andres Klode <[email protected]>  Fri, 08 Apr 2022 12:22:23 +0200

错误 https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1968154 的直接链接


较旧的信息:

有一个文件 auto-generated 告诉 apt-get 要自动删除哪些内核以及要保留哪些内核。 \n告诉 apt-get 它们是哪些内核的文件是 /etc/apt/apt.conf.d/01autoremove-kernels ,它是从 /etc/kernel/postinst.d/apt-auto-removal 生成的。

通常会发生的情况是,当您接收内核更新时,当内核版本更改时,例如从 3.13 更改为 3.16 ,然后更新 /etc/apt/apt.conf.d/01autoremove-kernels 以保留 3.16* 内核,然后设置为删除所有 3.13 内核,除非生成脚本指定以免被删除。

来自 apt-auto-removal 脚本:

# Author: Steve Langasek 
#
# 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 (determined using rules copied from the grub
#    package for deciding which kernel to boot)
#  - the second-latest kernel version, if the booted kernel version is
#    already the latest and this script is called for that same version,
#    to ensure a fallback remains available in the event the newly-installed
#    kernel at this ABI fails to boot
# In the common case, this results in exactly two kernels saved, but it can
# result in three kernels being saved.  It's better to err on the side of
# saving too many kernels than saving too few.
#
# We generate this list and save it to /etc/apt/apt.conf.d instead of marking
# packages in the database because this runs from a postinst script, and apt
# will overwrite the db when it exits.

但是,有时这不会将它们标记为 auto-removal,因为某些编码已随版本更改以防止这种情况发生。

如果您想要标记 autoremove 的先前内核(除了基于脚本所需的内核之外),请从终端窗口运行以下命令:

sudo apt-mark auto ^linux-image-

然后,当您运行 apt-get autoremove 命令时,只能删除旧的且不再需要的命令。我在下面举了例子:

第一个显示系统上除当前正在运行的内核之外的所有内核。


root@terrance-Linux:/home/share# dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
linux-headers-3.16.0-34
linux-headers-3.16.0-34-generic
linux-headers-3.16.0-36
linux-headers-3.16.0-36-generic
linux-headers-3.16.0-37
linux-headers-3.16.0-37-generic
linux-headers-4.0.0-040000
linux-headers-4.0.0-040000-generic
linux-image-3.16.0-34-generic
linux-image-3.16.0-36-generic
linux-image-3.16.0-37-generic
linux-image-4.0.0-040000-generic
linux-image-extra-3.16.0-34-generic
linux-image-extra-3.16.0-36-generic
linux-image-extra-3.16.0-37-generic

这显示了当前正在运行的内核。

terrance@terrance-Linux:~$ uname -r
4.0.1-040001-generic
terrance@terrance-Linux:~$ sudo apt-get autoremove
[sudo] password for terrance: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
terrance@terrance-Linux:~$ sudo apt-mark auto ^linux-image-
linux-image-extra-3.16.0-33-generic can not be marked as it is not installed.
linux-image-extra-3.13.0-27-generic can not be marked as it is not installed.
linux-image-3.13.0-44-lowlatency can not be marked as it is not installed.
linux-image-3.13.0-27-generic can not be marked as it is not installed.
linux-image-3.16.0-31-lowlatency can not be marked as it is not installed.
linux-image-3.16.0-36-generic set to automatically installed.
linux-image-lowlatency-lts-utopic can not be marked as it is not installed.
linux-image-extra-3.13.0-36-generic can not be marked as it is not installed.
linux-image-3.13.0-36-generic can not be marked as it is not installed.
linux-image-4.0.0-040000-generic set to automatically installed.
linux-image-extra-3.13.0-45-generic can not be marked as it is not installed.
linux-image-3.16.0-25-generic can not be marked as it is not installed.

注:上面的内容太长,无法列出,所以我删减了一点。

terrance@terrance-Linux:~$ sudo apt-get autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-image-3.16.0-34-generic linux-image-3.16.0-36-generic
  linux-image-4.0.0-040000-generic linux-image-extra-3.16.0-34-generic
  linux-image-extra-3.16.0-36-generic
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 613 MB disk space will be freed.
Do you want to continue? [Y/n] 

因此,运行这些命令后,您可以看到我现在可以 auto-remove 除当前内核 (4.0.1-040001-generic) 之外的所有旧内核 (4.0.1-040001-generic) 和下一个最新内核 (3.16.0-37-generic)。

希望这有帮助。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14115.html,未经允许,请勿转载。