(原创)第二章 Getting Started with the Kernel(Part1)

[color=red]声明:文章是个人阅读《Linux Kernel Development》英文影印版的学习记录,部分内容引用和个人根据理解翻译得来,无意侵犯他人权益,如果有不恰当之处,请联系本人,立即删除文章。[/color]


1、下载和使用源代码

如何下载完整的内核源代码,以及解包解压缩这类基础的东西,无需赘述。
值得一提的是,内核源码的安装位置。内核源代码通常安装在/usr/src/linux目录下,作者和很多开发人员都建议不要在这个目录下开发,一则是我们不希望一定要用root权限才能进行内核的修改;另外是因为[color=red]“The kernel version that your C library is compiled against is often linked to this tree.”[/color]。因此,还是在自己的home目录下面完成工作吧,即使是在安装新的内核时也不要触碰/usr/src/linux目录结构

2、应用补丁到内核
如何应用内核源码的patch倒是值得说下,关于使用补丁的方法在/Documentation/applying-pathes.txt 中有明确的描述,这里直接贴过来

[b]What is a patch?[/b]---
A patch is a small text document containing a delta of changes between two
different versions of a source tree. Patches are created with the `diff'
program.
To correctly apply a patch you need to know what base it was generated from
and what new version the patch will change the source tree into. These
should both be present in the patch file metadata or be possible to deduce
from the filename.
To revert a previously applied patch, use the -R argument to patch.
So, if you applied a patch like this:
        patch -p1 < ../patch-x.y.z   


You can revert (undo) it like this:
        patch -R -p1 < ../patch-x.y.z


[b]How do I feed a patch/diff file to `patch'?[/b]---
This (as usual with Linux and other UNIX like operating systems) can be
done in several different ways.
In all the examples below I feed the file (in uncompressed form) to patch
via stdin using the following syntax:
        patch -p1 < path/to/patch-x.y.z      


If you just want to be able to follow the examples below and don't want to
know of more than one way to use patch, then you can stop reading this
section here.

Patch can also get the name of the file to use via the -i argument, like
this:
        patch -p1 -i path/to/patch-x.y.z       


If your patch file is compressed with gzip or bzip2 and you don't want to
uncompress it before applying it, then you can feed it to patch like this
instead:
        zcat path/to/patch-x.y.z.gz | patch -p1                          
bzcat path/to/patch-x.y.z.bz2 | patch -p1


If you wish to uncompress the patch file by hand first before applying it
(what I assume you've done in the examples below), then you simply run
gunzip or bunzip2 on the file -- like this:
        gunzip patch-x.y.z.gz                                            
bunzip2 patch-x.y.z.bz2


Which will leave you with a plain text patch-x.y.z file that you can feed to patch via stdin or the -i argument, as you prefer.

关于给内核打补丁的详细方法可以参考这篇文章
[url]http://blog.csdn.net/hide1713/archive/2007/12/16/1941614.aspx[/url]


3、内核目录结构(直接引自本书)

[table]
|[color=darkblue]Directory Description[/color]|
| arch |Architecture-specific source|
| crypto| Crypto API|
| Documentation| Kernel source documentation|
| drivers| Device drivers
| fs| The VFS and the individual file systems|
|include| Kernel headers|
|init| Kernel boot and initialization|
|ipc| Interprocess communication code|
|kernel| Core subsystems, such as the scheduler|
|lib| Helper routines|
|mm| Memory management subsystem and the VM|
|net| Networking subsystem|
|scripts| Scripts used to build the kernel|
|security| Linux Security Module|
|sound| Sound subsystem|
|usr| Early user-space code (called initramfs)|
[/table]

4、配置内核
由于有了内核的源代码,使得我们去定植和裁剪一个符合我们需要特性和对应设备驱动的内核完全可以做到。在编译前首先要做的是内核的配置,内核的配置是通过配置项实现的。

配置项可以分为控制构建过程的和不影响该过程的两种
影响build过程的配置项有boolean型和三态型(yes 即显式的表示把代码编译进内核image而不是作为动态加载的模块、no、module),三态型选项通常在驱动中出现

kernel提供了多种工具来进行内核的配置
包括:

[table]
|make config|纯文本方式
|make menuconfig|基于ncurses的图形化工具
|make xconfig| 基于x11图形化工具
|make gconfig| 基于gtk+图形化工具
[/table]

值得注意的是:不同的linux的发行版本对图形库等的默认的支持是不同的,因此在使用后面三种工具进行配置时,可能会提示缺少了某种库,安装后即可解决,具体参见我其他的帖子内容

配置文件放在内核源代码根目录下,以.config文件形式命名,大多数内核开发者都是通过直接修改这个配置文件来进行配置的,这样可以很快速的找到和改变配置项的值。

在修改了.config内容后,或者将配置应用于一个新的内核目录树(kernel tree)时,要通过
make oldconfig
来验证和更新内核

参考资料:
1、《Linux Kernel Development Second Edition》英文影印版 By Robert Love 机械工业出版社
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值