您的位置:首页 > 运维架构 > Linux

【Linux编译内核】Redhat9(内核版本2.4.20-8)编译内核到版本(2.4.26)

2016-10-15 11:34 309 查看
文章原地址:redhat9(内核版本2.4.20-8)编译内核到版本(2.4.26)

文章的大部分都转载自网友在编译内核的所写的心路历程,另外有一些个人碰到的问题,我会在括号里标出。

前言

在我写这篇文章的时候,还是一个linux的初学者,经历了n次失败后的成功当然是兴奋的,于是很想把她写下来。

我的操作系统是Redhat9,其内核版本为2.4.20-8 ,需要升级到2.4.26,采用全新的2.4.26内核源码进行升级,主要的参考资料是《The Linux Kernel HOWTO》,另外还有一些参考资料我想就不提了,免得误导象我一样的初学者。

接下来有三个部分,“步骤索引”、“步骤说明”和“附录”,“步骤索引”真实的记录了我成功升级内核的步骤,“步骤说明”将对“步骤索引”一些注意事项进行说明,“附录”摘录自《The Linux Kernel HOWTO》的相关部分。

步骤索引

[root@localhost src]# tar vxfj linux-2.4.26.tar.bz2

[root@localhost src]# ln Cs linux2.4.26 linux (补充:ln是link的缩写,此步骤是创建一个简写的linux链接,相当于快捷方式,也可以忽略,直接往下走)

[root@localhost src]# cd /usr/src/linux

[root@localhost linux]# cp /boot/config-2.4.20-8 .config

[root@localhost linux]# make menuconfig

[root@localhost linux]# make dep

[root@localhost linux]# make clean

[root@localhost linux]# make bzImage

[root@localhost linux]# make modules

[root@localhost linux]# make modules_install

[root@localhost linux]# cp arch/i386/boot/bzImage /boot/vmlinuz-2.4.26

[root@localhost linux]# cp .config /boot/config-2.4.26

[root@localhost linux]# cp System.map /boot/System.map-2.4.26

[root@localhost linux]# mkinitrd /boot/initrd-2.4.26.img 2.4.26

[root@localhost linux]# cd /boot

[root@localhost boot]# rm -f System.map vmlinuz

[root@localhost boot]# ln -s System.map-2.4.26 System.map

[root@localhost boot]# ln -s vmlinuz-2.4.26 vmlinuz

[root@localhost boot]# cd grub

[root@localhost grub]# vi grub.conf

[root@localhost grub]# reboot

步骤说明

源码的升级,我将其归纳为源码准备、工具准备、配置内核、编译内核、安装模块、启动设置和重启7个步骤。我是以root的身份登录的,所以省略了用户切换的过程。

3.1. 源码准备

这个没有什么好说的,到https://www.kernel.org/pub/linux/kernel/v2.4去下载就是。我这里下载的是linux-2.4.26.tar.bz2,将其放到/usr/src目录下面,使用下面的方法解压缩:

[root@localhost src]# cd /usr/src

[root@localhost src]# tar vxfj linux-2.4.26.tar.bz2

[root@localhost src]# ln Cs linux2.4.26 linux(创建指向linux2.4.26新链接,这一步我没做)

解压缩完成后得到linux-2.4.26目录。喜欢图形界面的朋友可以不用这样,我在gnome下执行鼠标右键弹出菜单命令“解压缩到这里…”也能达到同样的目的,不过经常出错。

3.2. 工具准备

我所使用的两个内核版本跨度不大,所以没有特别安装什么工具,如果是升级到2.6.x版本会需要一些而外的工具。

3.3. 配置内核

这是编译内核最麻烦的地方了,我们将要面临一大堆驱动模块的配置选项,对于我这样的菜鸟就头晕了,开始时傻呼呼的按照网站上的一些资料介绍,执行make menuconfig或make xconfig,结果碰得“头破血流”。

在redhat9下执行make xconfig,会提示需要qt,在我看来make menuconfig是最好用的,不过在此之前不需要着急。 内核配置的结果是在源码的顶级目录下生成一个.config文件,其中当然是保存了各种配置项的设定值,如果不想在那么多的似懂非懂的选项中选择的话,先执行下面的操作:

[root@localhost src]# cd /usr/src/linux(我输入的是:cd /usr/src/linux-2.4.26,这个要看你解压缩后的文件名字了)

[root@localhost linux]# cp /boot/config-2.4.20-8 .config

在redhat9安装完成后,/boot/config-2.4.20-8就是我们所需要的.config文件,只要将其复制到源码目录并改名成.config就可以了,这样,在执行下面的操作就能得到当前系统的配置,如果需要增加驱动模块,只需要作少量修改就可以了。

[root@localhost linux]# make menuconfig

[root@localhost linux]# make dep

[root@localhost linux]# make clean

编译内核

[root@localhost linux]# make bzImage

当你看到类似以下信息,说明编译成功了

tools/build -b bbootsect bsetup compressed/bvmlinux.out CURRENT > bzImage

Root device is (3, 8)

Boot sector 512 bytes.

Setup is 4886 bytes.

System is 917 kB

make[1]: Leaving directory `/usr/src/linux-2.4.26/arch/i386/boot’

安装模块

[root@localhost linux]# make modules

[root@localhost linux]# make modules_install

在make modules_install执行完成后,如果成功了,你将会在/lib/modules目录下看到2.4.26。

启动设置

[root@localhost linux]# cp arch/i386/boot/bzImage /boot/vmlinuz-2.4.26

[root@localhost linux]# cp .config /boot/config-2.4.26

[root@localhost linux]# cp System.map /boot/System.map-2.4.26

[root@localhost linux]# mkinitrd /boot/initrd-2.4.26.img 2.4.26

很多资料上介绍了手工生成ramdisk的过程,mkinitrd可以帮我们完成这个稍微复杂的过程,如果这个操作执行成功,mkinitrd会在/boot目录下生成initrd-2.4.26.img文件。

[root@localhost linux]# cd /boot

[root@localhost boot]# rm -f System.map vmlinuz

[root@localhost boot]# ln -s System.map-2.4.26 System.map

[root@localhost boot]# ln -s vmlinuz-2.4.26 vmlinuz

关于bzImage和System.map请参考附录。

我安装redhat9时选择的是grub,所以需要改变grub.config中的设置。

[root@localhost boot]# cd grub

[root@localhost grub]# vi grub.conf

用vi打开/boot/grub/grub.conf文件,找到下面的两行,将其注释,并增加下面的内容。

#       kernel /boot/vmlinuz-2.4.20-8 ro root=LABEL=/
#       initrd /boot/initrd-2.4.20-8.img
kernel /boot/vmlinuz ro root=/dev/hda8
initrd /boot/initrd-2.4.26.img


root指定/挂载点的位置,如果不知道,可以使用df命令查看。(我朋友给我的建议是进入编辑器后不要将那两行注释掉,而是复制最后四行,粘贴,然后将复制后的四行中的2.4.20-8改成2.4.26。这样就会有两个版本供选择,万一编译失败了还可以进旧版本中。)

这里有一点需要注意,整个启动设置可以使用一个命令make install来完成,不过会有很多问题,对grub.conf文件的设置就是其中之一,make install修改的grub.conf文件仍然使用”LABEL”符号,而这个符号在2.4.26的内核下已经不起作用了。

重启

[root@localhost grub]# reboot

但愿你也成功!

总结

网上的资料虽然丰富,但多数不系统,也没有随着环境的升级而得到及时的更正,很多时候找下来的资料都不知道实用于那个版本的操作系统或内核。举个小例子,在

www.kernel.org

官方网站上有一个readme文件,其中说明如何编译内核,其中有这么几步操作:

cd include

rm Crf asm linux scsi

ln Cs asm-i386 asm

ln Cs linux linux

ln Cs scsi scsi

如果执行了这些操作,在编译2.4.26的内核时会出现错误。

在没有人指导的情况下,最好能找比较系统点的资料,或者到书店买本书。我就是这样,多亏找到了《The Linux Kernel HOWTO》才使得我成功编译内核,否则还不知道要失败多少次呢。

附录

.config

Everytime you compile and install the kernel image in /boot, you should also copy the corresponding config

file to /boot area, for documentation and future reference. Do NOT touch or edit these files!!

bzImage

The bzImage is the compressed kernel image created with command ‘make bzImage’ during kernel compile. It

important to note that bzImage is not compressed with bzip2 !! The name bz in bzImage is misleading!! It

stands for “Big Zimage”. The “b” in bzImage is “big”. Both zImage and bzImage are compressed with gzip.

The kernel includes a mini−gunzip to uncompress the kernel and boot into it. The difference is that the old

zImage uncompresses the kernel into low memory (the first 640k), and bzImage uncompresses the kernel into

high memory (over 1M). The only problem is that there are a very few machines where bzImage is known to

have problems (because the machines are buggy). The bzImage actually boots faster, but other than that,

there’s no difference in the way the system runs. The rule is that if all drivers cannot fit into the zImage,

then you need to modularize more.

If the kernel is small, it will work as both a zImage and a bzImage, and the booted system runs the same way.

A big kernel will work as a bzImage, but not as a zImage. Both bootimages are gzipped, (bzImage is not

bzipped as the name would suggest), but are put together and loaded differently, that allows the kernel to load

in higher address space, that does not limit it to lower memory in the pathetic intel architecture. So why have

both? Backward compatability. Some older lilos and loadlins don’t handle the bzImage format. Note, they

boot differently, but run the same. There is a lot of misinformation given out about what a bzImage file is

(mostly about it being bzip2ed).

System.map

System.map is a “phone directory” list of function in a particular build of a kernel. It is typically a symlink to

the System.map of the currently running kernel. If you use the wrong (or no) System.map, debugging crashes

is harder, but has no other effects. Without System.map, you may face minor annoyance messages.

Do NOT touch the System.map files.

写在后面的话:

基本上我是比着葫芦画瓢,再加上随时有指点,而且我自己又没加入什么模块,所以一遍就编译成功了。收获是熟悉了流程,对以前一些书本上概念性的东西加深了理解,对一些Linux命令掌握更熟练。虽然所得很少,但是仍然很开心。以后的路还很长,我会尝试自己去发现问题解决问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: