Ubuntu 上旧内核的移除

Ubuntu会通过自己的Repository向用户push新版本内核,安全更新和应用软件等各种更新,很方便。

早期我用Fedora Core时根本没想到过会有这种便利,不过也好,至少我自己编译过5次以上内核,对内核到底是个什么玩意儿也算是略知一二了。现在没有硬件驱动问题或者追求极致性能的情况,自己也懒得去编译内核了。

JockeyGentoo更适合懒人,我觉得 Debian / Ubuntu 或者 Arch Linux 更适合吧,或许我还不够懒,更何况我是没有耐心等编译的那种人。

系统:Ubuntu 8.04 Hardy Heron 网络升级到 8.10 Intrepid Ibex

8.04 时内核就一直有升级。在 Hardy 升级到 Intrepid 之后,只留下了一个 2.6.24-21-generic 内核,以及 Intrepid release 中带的 2.6.27-7-generic

Ubuntu 升级内核的做法是,除了最新的内核之外,再在 GRUB 启动菜单中保留一个次新版本的内核。这样做的目的就是为了避免升级内核后系统无法启动不能正常工作,但又只有这一个可用内核的情况发生。至少留一个可用的 Last Known Good 内核可以用来 boot 和诊断/修复问题。这是完全正确的,凡事都给自己留一条后路。但是 Ubuntu 不会自动清除之前安装的版本,也就是当前不再被使用的内核遗留下来的文件和目录。不明白我在说什么的,看一下你的 /lib/modules 目录。

即使再有洁癖的人,你也不是很在乎 130M 不到的硬盘空间吧?所以,别只留一个内核。

回到正题:
内核相关内容主要存在于:

/boot/vmlinuz-$(uname -r)
/boot/Systemp.map-$(uname -r)
/boot/initrd.img-$(uname -r)
/lib/modules/$(uname -r)/*

当然手工清除是可以的,但是既然有包管理,为何让它来给你服务呢?另外一个好处是,会自动找到系统中的其它 kernel 自动清除并更新 grub.conf 众懒人有福了;-)

更多关于 old kernel 的信息:
https://wiki.ubuntu.com/KernelTeam/removing-old-kernels

此处以彻底清除 2.6.24-21-generic 内核为例:

sudo apt-get remove --purge 2.6.24-21-generic*

注: apt-get purge 等价于 apt-get remove –purge 还更短。

输出:

root@linux:~# apt-get remove --purge 2.6.24-21*
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting linux-restricted-modules-2.6.24-21-generic for regex '2.6.24-21*'
Note, selecting linux-image-2.6.24-21-generic for regex '2.6.24-21*'
Note, selecting linux-ubuntu-modules-2.6.24-21-generic for regex '2.6.24-21*'
Note, selecting avm-fritz-firmware-2.6.24-21 for regex '2.6.24-21*'
Note, selecting linux-headers-2.6.24-21-generic for regex '2.6.24-21*'
Note, selecting linux-headers-2.6.24-21 for regex '2.6.24-21*'
The following packages were automatically installed and are no longer required:
nvidia-kernel-common
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
linux-image-2.6.24-21-generic* linux-restricted-modules-2.6.24-21-generic*
linux-ubuntu-modules-2.6.24-21-generic*
0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded.
After this operation, 128MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 139924 files and directories currently installed.)
Removing linux-ubuntu-modules-2.6.24-21-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.24-21-generic
Purging configuration files for linux-ubuntu-modules-2.6.24-21-generic ...
Removing linux-restricted-modules-2.6.24-21-generic ...
Purging configuration files for linux-restricted-modules-2.6.24-21-generic ...
Removing linux-image-2.6.24-21-generic ...
Examining /etc/kernel/prerm.d.
run-parts: executing /etc/kernel/prerm.d/dkms
Uninstalling: nvidia 177.80 (2.6.24-21-generic) (i686)

-------- Uninstall Beginning --------
Module: nvidia
Version: 177.80
Kernel: 2.6.24-21-generic (i686)
-------------------------------------

Status: Before uninstall, this module version was ACTIVE on this kernel.

nvidia.ko:
- Uninstallation
   - Deleting from: /lib/modules/2.6.24-21-generic/updates/dkms/
- Original module
   - Archived original module found in the DKMS tree
   - Moving it to: /lib/modules/2.6.24-21-generic/kernel/drivers/video/nvidia/
depmod....

Removing original_module from DKMS tree for kernel 2.6.24-21-generic (i686)

DKMS: uninstall Completed.
run-parts: executing /etc/kernel/prerm.d/last-good-boot
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.27-9-generic
Found kernel: /boot/vmlinuz-2.6.27-7-generic
Found kernel: /boot/memtest86+.bin
Replacing config file /var/run/grub/menu.lst with new version
Updating /boot/grub/menu.lst ... done

Purging configuration files for linux-image-2.6.24-21-generic ...
Running postrm hook script /sbin/update-grub.
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.27-9-generic
Found kernel: /boot/vmlinuz-2.6.27-7-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

rmdir: failed to remove `/lib/modules/2.6.24-21-generic': Directory not empty
dpkg - warning: while removing linux-image-2.6.24-21-generic, directory `/lib/modules/2.6.24-21-generic/kernel/drivers/video/nvidia' not empty so not removed.
dpkg - warning: while removing linux-image-2.6.24-21-generic, directory `/lib/modules/2.6.24-21-generic/kernel/drivers/video' not empty so not removed.
dpkg - warning: while removing linux-image-2.6.24-21-generic, directory `/lib/modules/2.6.24-21-generic/kernel/drivers' not empty so not removed.
dpkg - warning: while removing linux-image-2.6.24-21-generic, directory `/lib/modules/2.6.24-21-generic/kernel' not empty so not removed.
dpkg - warning: while removing linux-image-2.6.24-21-generic, directory `/lib/modules/2.6.24-21-generic' not empty so not removed.

/lib/modules/2.6.24-21-generic 目录非空(因为第一次启动 VMware Workstation for Linux 会给当前被使用的内核编译几个模块放在里面),无法删除,可以事后手工删除。搞定,收工。

在 Debian GNU/Linux 上,官方推荐使用 aptitude 前端。如果需要移除老版本的内核可以直接用

aptitude remove linux-image-2.6.24-2-686

上面这个命令的效果就相当于

apt-get remove --purge *2.6.24-2-686*

换句话说aptitude remove package命令,不但删除包,而且移除包的依赖(如果没有其它包对这些包有依赖)。
aptitude相对于apt-get系列命令还是有其优势的,另外有强大的search pattern支持,看来得慢慢适应并过渡过去了。

Red Hat Enterprise LinuxOracle Enterprise Linux (OEL):
方法类似,也是从 /lib/modules 里找到需要移除的内核版本,之后用命令找到相关的包,再用 rpm 或者 yum 命令移除掉。

例如我们需要删除 OEL 5 update 2 的老内核 2.6.18-92.el5 确实够老吧?

[root@linux ~]# rpm -qa | grep 2.6.18-92
kernel-2.6.18-92.el5
kernel-devel-2.6.18-92.el5

找到后用yum直接清除

[root@linux ~]# yum remove kernel-2.6.18-92.el5 kernel-devel-2.6.18-92.el5

# or the one liner
rpm -qa | grep 2.6.18-92 | xargs yum -y erase

注:在 Debian / Ubuntu 上可以用

apt-get clean

来清除 APT download cache 事实上就是清空了 /var/cache/apt/archives 目录而已,适合有洁癖的人。在 Fedora / EL 上则是用 yum clean all ;-D

如果你需要用 APTonCD 备份已下载的 deb 包,记得不要清 cache ;-D