QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3726|回复: 1

如何编译一个内核 - Fedora方式

[复制链接]
发表于 2006-11-17 01:17:18 | 显示全部楼层 |阅读模式
如何编译一个内核 - Fedora方式

版本: 1.0
作者: Falko Timme <ft [at] falkotimme [dot] com>
翻译: Ken Zhao <cn.zhaoke.com>
最后更新 11/10/2006
中文最后更新 11/16/2006

每一个Linux发行版都有自己专门的工具去构建自定义的内核. 本文主要介绍在Fedora平台上编译内核, 如何从www.kernel.org(也叫vanilla kernel)获得最新未修改的内核源代码来构建一个自定义的内核, 这样你可以不去使用发行版中已修改的内核源代码, 另外也介绍了如何给内核打补丁, 这样你可以方便增加新的功能.

下面的工作我都在Fedora Core 6上进行了测试.

首先我想说的是这不是建立自定义内核的唯一方式, 还有许多其它的方式, 这是我常用的方式. 我不能保证使用后不会出现任何问题.

1. 预备工作
这篇文章我将描述在Fedora平台上构建自定义内核的两种方法. 第一种是Fedora的方式, 最后你将能制做一个内核rpm包, 这样可以很方便地与他人分享. 第二种是传统方式, 使用于所有linux发行版, 不过最后你不会有一个rpm包.

我推荐使用root用户来做下面所有步骤. 然而, 有时候也需要使用非超级用户(比如用户tom)来运行一些命令, 所以你需要使用visudo编辑器在/etc/sudoers文件中增加下面一行:
tom  ALL=(ALL) ALL

现在无论什么时候你需要用root权限运行命令, 比如:
yum install fedora-rpmdevtools unifdef

你都可以用下面的命令来替代:
sudo yum install fedora-rpmdevtools unifdef

请记住: 如果你以root用户身份运行所有命令, 你可以完全忘记sudo. 采用什么方式, 看你自己的习惯.

2. 构建一个rpm内核包
这一小节介绍如何构建内核, 最终你可以得到一个rpm包, 这样可以方便安装或与他人分享.

2.1 创建你的rpmbuild目录
按下面创建你的rpmbuild目录:
cd ~
cp -a /usr/src/redhat/ rpmbuild
echo '%_topdir %(echo $HOME)/rpmbuild' >> .rpmmacros

然后安装构建rpm包需要的应用软件包
yum install fedora-rpmdevtools unifdef

然后运行
fedora-buildrpmtree


2.2 下载和安装一个Fedora内核src.rpm包
接下来为我们的Fedora版本下载最新的内核src.rpm包. 针对Fedora Core 6, 你可以从接下来的网址获得src.rpm包: http://download.fedora.redhat.com/pub/fedora/linux/core/6/source/SRPMS/, 针对Fedora Core 5, 你可以从接下来的网址获

得src.rpm包: http://download.fedora.redhat.com/pub/fedora/linux/core/5/source/SRPMS/, 等等.

最新的Fedora Core 6内核src.rpm包是kernel-2.6.18-1.2798.fc6.src.rpm, 所以我们现在下载和安装它:
cd /usr/src
wget http://download.fedora.redhat.com/pub/fedora/linux/core/6/source/SRPMS/kernel-2.6.18-1.2798.fc6.src.rpm
rpm -ivh kernel-2.6.18-1.2798.fc6.src.rpm

如果你看到这些警告:
warning: user brewbuilder does not exist - using root
warning: group brewbuilder does not exist - using root

你可以忽略它们.

我们已经安装了最新的2.6.18.1的内核源代码包. 所以如果我们继续构建内核源代码包的话, 我们的内核版本将是2.6.18.1.

2.3 给内核打补丁

我想安装新的内核2.6.18.2来替代2.6.18.1. 系统已经安装的src.rpm包是通过给2.6.18打过补丁的2.6.18.1. 我们现在将使用打补丁的方式来

升级内核到2.6.18.2
cd ~/rpmbuild/SOURCES/
wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.18.2.bz2

如果你想内核的名字结尾为2.6.19-rc5, 你也可以使用prepatch程序http://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.19-rc5.bz2. 请注意这项任务仅对prepaches有效. 比如. 内核补丁仅对最终发行的内核版本有效, 比如2.6.19内核. 你可以给2.6.18内核源代码打补丁, 但不能是2.6.18.1或2.6.18.2, 等. 这个规则在接下来的网页中注明: http://kernel.org/patchtypes/pre.html

prepatches等同于linux中的测试发行; 他们位于存档的测试目录中, 我们可以使用patch(1)工具对上一个完整发行版(仅三部分)打补丁(例如, 2.6.13-rc4 prepatch只可以给2.6.11内核源代码打补丁, 而不是2.6.11.10.)

现在我们必须修改kernel-2.6.spec文件, 让它知道我们使用了新的内核补丁:
cd ~/rpmbuild/SPECS/
vi kernel-2.6.spec

搜寻下面行
Patch1: patch-2.6.18.1.bz2

替换为下面的内容:
Patch1: patch-2.6.18.2.bz2

(或者其它你已下载的版本)

然后运行
rpmbuild -bp kernel-2.6.spec

(如果你想构建其它平台如i386, i586, i686或者x86_64的内核, 你可以通过运行下面的命令来实现:
rpmbuild -bp --target=i686 kernel-2.6.spec

我没有在上面例子中指定为i386, 你的系统可能是其它的平台类型, 请记得填写你的平台类型.)

现在我们进入到了关键的部分. src.rpm有很多fedora专门的补丁, 其中一些不能与我们的2.6.18.2补丁很好的工作, 所以如果你在rpmbuild输

出看到下面状况:

+ echo 'Patch #300 (linux-2.6-ppc-dac960-ipr-clash.patch):'
Patch #300 (linux-2.6-ppc-dac960-ipr-clash.patch):
+ patch -p1 -s
Reversed (or previously applied) patch detected! Assume -R? [n]
Apply anyway? [n]
1 out of 1 hunk ignored -- saving rejects to file drivers/block/DAC960.c.rej
error: Bad exit status from /var/tmp/rpm-tmp.46287 (%prep)


RPM build errors:
    Bad exit status from /var/tmp/rpm-tmp.46287 (%prep)

这时候你必须再次编辑kernel-2.6.spec文件, 注解掉patch #300一行:
vi kernel-2.6.spec

[...]
#Patch300: linux-2.6-ppc-dac960-ipr-clash.patch
[...]
#%patch300 -p1
[...]

然后再次运行rpmbuild命令, 比如:
rpmbuild -bp kernel-2.6.spec

注意: 你必须不断重复上面过程直到没有出现任何错误.


2.4 指定内核识别码
现在我们将指定一个字符串以便于以后我们能区别内核版本. 因此我们需要做:
cd ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i386
vi Makefile

在EXTRAVERSION一行, 你可以输入一个内核识别码. 我想最好是在字符串后附上内核的版本号, 这样来操作:
EXTRAVERSION = -custom-2.6.18.2

2.5 配置内核
现在我们运行
make menuconfig

然后我们看到内核的配置菜单. 移动绿色光标到 Load an Alternate Configuration File 行后选择.config文件做为配置文件:

http://www.howtoforge.com/images/kernel_compilation_fedora/6.png

http://www.howtoforge.com/images/kernel_compilation_fedora/7.png

然后浏览内核配置菜单, 选择你需要的功能. 完成配置后, 选择Exit, 回答下面的问题(Do you wish to save your new kernel

configuration? 你希望保存新的内核配置吗?), 选择Yes:

http://www.howtoforge.com/images/kernel_compilation_fedora/8.png


2.6 构建内核
现在我们简单运行下面命令来构建内核:
make rpm

然后你将在~/rpmbuild/SRPMS/目录发现一个新的src.rpm包, 比如. ~/rpmbuild/SRPMS/kernel-2.6.18custom2.6.18.2-1.src.rpm, 内核rpm包

位于~/rpmbuild/RPMS/i386/ (或 ~/rpmbuild/RPMS/i586/, ~/rpmbuild/RPMS/i686/, 等. 基于你的平台类型). 比如.

~/rpmbuild/RPMS/i386/kernel-2.6.18custom2.6.18.2-1.i386.rpm. 你看到内核识别码已添加到软件包的名字中.

2.7 安装新内核
现在去新rpm内核包存在的目录(基于你的平台类型, 比如. ~/rpmbuild/RPMS/i386/), 安装rpm包:
cd ~/rpmbuild/RPMS/i386
rpm -ivh kernel-2.6.18custom2.6.18.2-1.i386.rpm

(现在你甚至能够拷贝到其它的Fedora系统, 通过上面的方式安装. 你将不需要再次编译内核.)

接下来我们为新内核创建一个ramdisk, 否则系统将不能引导新的内核:
mkinitrd /boot/initrd-2.6.18-custom-2.6.18.2.img 2.6.18-custom-2.6.18.2

然后编辑/boot/grub/menu.lst文件, 看一下你系统上已经存在(正在工作的)内核信息, 拷贝其中的一个实例, 替换为你新的内核和ramdisk:
vi /boot/grub/menu.lst

比如, 我的menu.lst, 在我添加新的内核信息前的情况:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu

title Fedora Core (2.6.18-1.2798.fc6)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-1.2798.fc6.img

修改后的情况:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu

title Fedora Core (2.6.18-custom-2.6.18.2)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-custom-2.6.18.2 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-custom-2.6.18.2.img

title Fedora Core (2.6.18-1.2798.fc6)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-1.2798.fc6.img

(你能够通过下面命令发现争取的vmlinuz和initrd文件:
ls -l /boot
)

现在重启系统:
shutdown -r now

如果一切进展顺利, 你的新内核正常工作. 你可以通过运行下面命令来检查你的新内核是否运行:
uname -r

这将会显示如:
2.6.18-custom-2.6.18.2

如果系统没有起来, 重启一下, 你会看到:

http://www.howtoforge.com/images/kernel_compilation_fedora/4.png

按任意键进入GRUB菜单:
http://www.howtoforge.com/images/kernel_compilation_fedora/5.png

选择你的过去的内核启动系统, 现在你能再次尝试编译工作内核. 不要忘记从/boot/grub/menu.1st文件中移去不需要的内核引导信息.


3. 构建内核的传统方式
这个章节讲述另外一种可用于任何其它linux系统的内核编译方式. 因为不是Fedora编译方式, 所以将不会有rpm包的内核.


3.1 下载内核源代码
我们下载需要的内核到/usr/src目录(去www.kernel.org网站下载你需要的内核版本, 比如. linux-2.6.18.2.tar.bz2(你可以从这里下载所有

的2.6内核: http://www.kernel.org/pub/linux/kernel/v2.6/). 然后下载到/usr/src目录:
cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.2.tar.bz2

然后解压内核源代码, 创建一个指向内核源代码目录的linux字符链接:
tar xjf linux-2.6.18.2.tar.bz2
ln -s linux-2.6.18.2 linux
cd /usr/src/linux

3.2 给内核源代码打补丁(可选)
有时你的内核缺省不支持新买的设备, 你需要安装新的驱动. 或者你需要使用虚拟技术或其它高级的技术, 而这些技术现在的内核不支持. 在所有的这些情况下你需要给给内核源代码打补丁(当然补丁必须能够获得..)

现在我们假设你已经下载需要的补丁(下面的例子我叫它patch.bz2)到/usr/src. 下面的命令能帮助你给内核源代码直接打上补丁(你的用户必

须处于/usr/src/linux目录):
bzip2 -dc /usr/src/patch.bz2 | patch -p1 --dry-run
bzip2 -dc /usr/src/patch.bz2 | patch -p1

第一个命令用于测试, 不会对内核有任何影响. 如果没有西安市错误, 你可以运行第二个命令给内核打上补丁. 如果第一个命令有误, 请不要继续的操作!

你也能够通过kernel prepatches方式给内核打补丁. 比如, 如果你需要一个功能, 而这个功能仅存在于2.6.19-rc5中, 但是正式完整的内核版本仍没有发布, 不过patch-2.6.19-rc5.biz2已经发布. 你可以把这个补丁打到2.6.18的内核源代码中, 但请不要达到2.6.18.1或2.6.18.2, 等. 这个规则在接下来的网页中有所注明: http://kernel.org/patchtypes/pre.html

预先补丁等同于linux中的测试发行; 他们位于归档的测试目录中, 我们可以使用patch工具对上一个第三位完整发行版打补丁(例如, 2.6.13-

rc4 prepatch只可以给2.6.11内核源代码打补丁, 而不是2.6.11.10.)

所以如果你想编译2.6.19-rc5内核, 你必须在步骤3.1下载2.6.18内核源代码(http://www.kernel.org/pub/linux/kernel/v2.6/linux-

2.6.18.tar.bz2)来替代2.6.18.2!

下面是如何给2.6.18打上 2.6.19-rc5补丁:
cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.19-rc5.bz2
cd /usr/src/linux
bzip2 -dc /usr/src/patch-2.6.19-rc5.bz2 | patch -p1 --dry-run
bzip2 -dc /usr/src/patch-2.6.19-rc5.bz2 | patch -p1

3.3. 配置内核
使用当前工作内核的配置文件做为新内核配置文件的基础是一个很好的主意. 因此我们拷贝已存的配置文件到/usr/src/linux:
make mrproper
cp /boot/config-`uname -r` ./.config

然后运行
make menuconfig

然后我们看到内核的配置菜单. 移动绿色光标到 Load an Alternate Configuration File 行后选择.config文件做为配置文件:

http://www.howtoforge.com/images/kernel_compilation_fedora/1.png

http://www.howtoforge.com/images/kernel_compilation_fedora/2.png

然后浏览内核配置菜单, 选择你需要的功能. 完成配置后, 选择Exit, 回答下面的问题(Do you wish to save your new kernel configuration? 你希望保存新的内核配置吗?), 选择Yes:

http://www.howtoforge.com/images/kernel_compilation_fedora/3.png

3.4 构建和安装新的内核

构建和安装新的内核, 执行下面的3个命令:
make all
make modules_install
make install

保持耐心, 内核编译需要一定时间, 主要看你的内核配置和处理器速度. 最后一个命令将自动创建一个ramdisk, 你将在
/boot/grub/menu.lst用到.

现在编辑/boot/grub/menu.lst. 把你的新的内核信息放到列表的顶端, 确认新的内核已经替代老的内核, default值需要设为0.

vi /boot/grub/menu.lst

我的menu.lst是这样的:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu

title Fedora Core (2.6.18.2)
        root (hd0,0)
        kernel /vmlinuz-2.6.18.2 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18.2.img

title Fedora Core (2.6.18-1.2798.fc6)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-1.2798.fc6.img

现在重新启动系统:
shutdown -r now

如果一切进展顺利, 你的新内核正常工作. 你可以通过运行下面命令来检查你的新内核是否运行:
uname -r

这将会显示如:
2.6.18.2

如果系统没有起来, 重启一下, 你会看到:

http://www.howtoforge.com/images/kernel_compilation_fedora/4.png

按任意键进入GRUB菜单:
http://www.howtoforge.com/images/kernel_compilation_fedora/5.png

选择你的过去的内核启动系统, 现在你能再次尝试编译工作内核. 不要忘记从/boot/grub/menu.1st文件中移去不需要的内核信息.


参考的3个链接:

Fedora: http://fedora.redhat.com
Fedora Wiki: http://fedoraproject.org/wiki
The Linux Kernel Archives: http://www.kernel.org
发表于 2006-12-1 19:14:53 | 显示全部楼层
看完了。文中的2.6.11应为2.6.12把
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-3-29 17:28 , Processed in 0.144270 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表