测试环境: CentOS 4.1
核心版本: 2.3.13
文章日期: 2006/06/23
在安装好一个 Linux Distribution (e.g CentOS, Fedora, Debian) 后,系统已经内建一个经过发行商设定及编译好的 Linux 核心 (kernels)。一般上因为发行商希望他们的发行版适用于大部份的系统,所以会编译成较为“通用”,而自行编译则可以根据自己的需要,将不同的选项加入核心内,所以一般上效能也会较发行版内建的为高。
另外两个自行编译的理由,就是希望使用新版本核心才有的功能,以及有些硬件是现有核心未能支援,本文将会简单介绍编译核心的步骤。
准备工作
在编译前先要搞清楚自己电脑内的硬件,好让不要在编译时选漏一些必要的项目,或者选了一大堆没有用的,如果你确实清楚电脑内有什么硬件,那便可以略过以下步骤。先执行以下两个指令:
# cat /proc/cpuinfo
以上指令分别会输出类似以下内容:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# /sbin/lspci 00:00.0 Host bridge: Intel Corporation 82815 815 Chipset Host Bridge and Memory Controller Hub (rev 02) 00:01.0 PCI bridge: Intel Corporation 82815 815 Chipset AGP Bridge (rev 02) 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 02) 00:1f.0 ISA bridge: Intel Corporation 82801BA ISA Bridge (LPC) (rev 02) 00:1f.1 IDE interface: Intel Corporation 82801BA IDE U100 (rev 02) 00:1f.2 USB Controller: Intel Corporation 82801BA/BAM USB (Hub #1) (rev 02) 00:1f.3 SMBus: Intel Corporation 82801BA/BAM SMBus (rev 02) 00:1f.4 USB Controller: Intel Corporation 82801BA/BAM USB (Hub #2) (rev 02) 01:00.0 VGA compatible controller: nVidia Corporation NV5M64 [RIVA TNT2 Model 64/Model 64 Pro] (rev 15) 02:03.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ (rev 10) 02:09.0 Multimedia audio controller: C-Media Electronics Inc CM8738 (rev 10) cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 8 model name : Pentium III (Coppermine) stepping : 6 cpu MHz : 866.833 cache size : 256 KB fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 2 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse bogomips : 1735.70 |
如果想将以上内容储存成档案,只需输入:
# cat /proc/cpuinfo >> /root/hardward.conf
这样两个指令的内容就会储存到 /root/hardward.conf 内。
编译及配置核心
在写这篇文章时,最新核心版本是 2.6.13,我会以此核心作为例子,先按以下连结下载该核心:
Linux 2.6.13
P.S. 想下载最新的核心源始码,可以 http://www.kernel.org/ 下载。
先将下载回来的档案放到 /usr/src/ 目录下,然后执行以下指令:
# cd linux-2.6.13
# make mrproper //如果是第一次编译该核心,可以省略
# make menuconfig
这时会看见一个选单,是将核心内各种不同的功能分类,只要用上、下按键选择所以分类,按 Enter 就可进入,如果退出先前位置只需按 Esc。
而如果需要将功能编译到核心,就移到该项目上,使用 Space 键选择。* 号代表编译进核心;M 代表以模组方式编译;空白代表不选用。
因为这里的配置项目有很多,所以我只会找出几个较重要的介绍。
1. Code maturity level options
这时代表还在开发中的功能,除非这些功能你用得上,否则可以不选择
2. Processor type and features -> Processor family
这个较为重要,是选择 CPU 类型,选错了有机会导致不能开机,而选得适当可以增加系统的效能。
当选择好需要的项目后,便按 Esc 键,系统会询问你是否需要储存,只要选择 “Yes” 便可以储存先前的改动,并回到命令列。
然后顺序输入以下指令: (输入 make all 后可以去叹杯茶)
# make modules_install
# depmod -a
# mkinitrd /boot/initrd-2.6.13.img 2.6.13
P.S. 以上的 make all 是 make vmlinux, make modules, make bzImage 3 个指令的集合。
更换核心
当以上编译动作完成后不代表可以立即使用,还是设定开机管理程式,以下是 GRUB 修改方法:
1. GRUB
使用文书编辑软件 (例如 vi) 打开 /boot/grub/grub.conf,你应该会见到已经有目前的核心在那里,只要将以下内容加入到 title 前一行即可:
|
1 2 3 4 |
title CentOS-4 i386 (2.6.13) root (hd0,0) kernel /vmlinuz-2.6.13-mk6 ro root=/dev/VolGroup00/LogVol00 rhgb quiet initrd /initrd-2.6.13-mk6.img |
建议保留原先核心在 /boot/grub/grub.conf 内,因为如果新编译出来的核心不能开机,也可以用旧核心来补救。
P.S. 如果是使用 LILO, 只需编辑 /etc/lilo.conf,过程与 GRUB 大同小异。
最后重新开机,如果能够成功开机及测试无误后,那便可以考虑将旧核心删除。