“Linux Kernel in a Nutshell" 读书笔记

<Linux Kernel in a Nutshell> 读书笔记

  此书专门讲解如何配置和编译 Linux 内核,我认为对于每个刚接触 Linux 内核的人,都很有必要认真的读一下此书,以夯实基础,深入理解linux内核的配置和编译技术。
我摘录了一些感兴趣的部分,作为读书笔记。 


  1.   以普通用户进行编译

用普通用户身份进行操作,这是在 Linux 上进行工作的一个良好的习惯!

This warning is the most important thing to remember while working through the steps in this book. Everything in this bookdownloading the kernel source code, uncompressing it, configuring the kernel, and building itshould be done as a normal user on the machine. Only the two or three commands it takes to install a new kernel should be done as the superuser (root).

There have been bugs in the kernel build process in the past, causing some special files in the /dev directory to be deleted if the user had superuser permissions while building the Linux kernel.There are also issues that can easily arise when uncompressing the Linux kernel with superuser rights, as some of the files in the kernel source package will not end up with the proper permissions and will cause build errors later.


2. 不要将内核源码放在 /usr/src/linux 目录下


好习惯!

The kernel source code should also never be placed in the /usr/src/linux/ directory, as that is the location of the kernel that the system libraries were built against, not your new custom kernel. Do not do any kernel development under the /usr/src/ directory tree at all, but only in a local user directory where nothing bad can happen to the system.


3. 如何对内核做部分编译

当对内核代码做了改动后,这时候最好只对改动的部分进行编译

When doing kernel development, sometimes you wish to build only a specific subdirectory or a single file within the whole kernel tree. The kernel build system allows you to easily do this. To selectively build a specific directory, specify it on the build command line. For example, to build the files in the drivers/usb/serial directory, enter:

$ make drivers/usb/serial

 

Using this syntax, however, will not build the final module images in that directory. To do that, you can use the M= argument:

$ make M=drivers/usb/serial

 

which will build all the needed files in that directory and link the final module images.

When you build a single directory in one of the ways shown, the final kernel image is not relinked together. Therefore, any changes that were made to the subdirectories will not affect the final kernel image, which is probably not what you desire. Execute a final:

$ make

 

to have the build system check all changed object files and do the final kernel image link properly.

To build only a specific file in the kernel tree, just pass it as the argument to make. For example, if you wish to build only the drivers/usb/serial/visor.ko kernel module, enter:

$ make drivers/usb/serial/visor.ko

 

The build system will build all needed files for the visor.ko kernel module, and do the final link to create the module.

 

4. 将配置和编译的结果输出到单独的目录


好习惯!

Sometimes it is easier to have the source code for the kernel tree in a read-only location (such as on a CD-ROM, or in a source code control system), and place the output of the kernel build elsewhere, so that you do not disturb the original source tree. The kernel build system handles this easily, by requiring only the single argument O= to tell it where to place the output of the build. For example, if the kernel source is located on a CD-ROM mounted on /mnt/cdrom/ and you wish to place the built files in your local directory, enter:

$ cd /mnt/cdrom/linux-2.6.17.11

$ make O=~/linux/linux-2.6.17.11

 
All of the build files will be created in the ~/linux/linux-2.6.17.11/ directory. Please note that this
0= option should also be passed to the configuration options of the build so that the configuration is correctly placed in the output directory and not in the directory containing the source code.


5. 使用 patch 和 diff

这是两个内核开发者经常使用的工具。很惭愧,到目前为止,我对它们都不是很熟悉
This section is based on an article originally published in Linux Journal.

One of the most common methods of doing kernel work is to use the patch and diff programs. To use these tools, two different directory trees: a "clean" one and a "working" one must be used. The clean tree is a released kernel version, while the working one is based on the same version but contains your modifications. Then you can use patch and diff to extract your changes and port them forward to a new kernel release.

For an example, create two directories containing the latest kernel version

$ tar -zxf linux-2.6.19.tar.gz
$ mv linux-2.6.19 linux-2.6.19-dirty
$ tar -zxf linux-2.6.19.tar.gz
$ ls
linux-2.6.19/
linux-2.6.19-dirty/

 
Now make all of the different changes you wish to do in the -dirty directory and leave the clean, original kernel directory alone. After finishing making changes, you should create a patch to send it to other people:

$ diff -Naur -X linux-2.6.19/Documentation/dontdiff linux-2.6.19/ /
   
   
linux-2.6.19-dirty/ > my_patch

 

This will create a file called my_patch that contains the difference between your work and a clean 2.6.19 kernel tree. This patch then can be sent to other people via email.

 

6. 内核的开发与发布周期 

2.6 内核开始,所有的内核版本都被认为是稳定的。

2.6.17, 2.6.18 这样的稳定版本,又叫做 base version

2.6.17.1, 2.6.17.2 这样的,表示在 2.6.17 基础上做了一些小的改动,但也是稳定版本。

2.6.18-rc1 这样的,称为 release candidate,“发布候选”,这是在为从 2.6.17 升级到 2.6.18 进行准备。这些版本也应该被认为是稳定的。

 

7. 如何升级内核

  7.1.                    首先,备份你的配置文件

及时对关键的数据进行备份,好习惯!

First off, please back up the .config file in the kernel source directory. You have spent some time and effort into creating it, and it should be saved in case something goes wrong when trying to upgrade.

$ cd ~/linux/linux-2.6.17.11
$ cp .config ../good_config

 

7.2.                    下载需要的 patch 文件

 

www.kernel.org ,下载需要的 patch 文件

那么如何知道应该下载哪些 patch 文件了?

  Which Patch Applies to Which Release?

A kernel patch file will upgrade the source code from only one specific release to another specific release. Here is how the different patch files can be applied:

·         Stable kernel patches apply to the base kernel version. This means that the 2.6.17.10 patch will only apply to the 2.6.17 kernel release. The 2.6.17.10 kernel patch will not apply to the 2.6.17.9 kernel or any other release.

·         Base kernel release patches only apply to the previous base kernel version. This means that the 2.6.18 patch will only apply to the 2.6.17 kernel release. It will not apply to the last 2.6.17.y kernel release, or any other release.

·         Incremental patches upgrade from a specific release to the next release. This allows developers to not have to downgrade their kernel and then upgrade it, just to switch from the latest stable release to the next stable release (remember that the stable release patches are only against the base kernel, not the previous stable release). Whenever possible, it is recommended that you use the incremental patches to make your life easier.

 

有三种 patch

1、  stable kernel patch:只能打在 base kernel version 上,例如  2.6.17.10 patch 只能打在 2.6.17 这个 release

2、  base kernel patch:只能打在前一个 base kernel version 上,例如 2.6.18 patch 只能打在 2.6.17

3、  incremental patch:这种 patch 通常打在一个 stable kernel release 上,以升级到 next release,例如patch-2.6.17.10-11,表示打在 2.6.17.10 上,打完以后,升级到 2.6.17.11

 

最重要的一点: patch 要一步一步打

如果希望从一个版本,经过一个较大的跨度,升级到另一个版本,中间可能需要经过多次 patch才能达到目标。

 

  7.3.                    patch

 

$ cd linux-2.6.17.9

 
Now run the patch program to apply the first patch moving the source tree from the 2.6.17.9 to the 2.6.17.10 release:

$ patch -p1 < ../patch-2.6.17.9-10

patching file Makefile

patching file block/elevator.c

patching file fs/udf/super.c

patching file fs/udf/truncate.c

patching file include/net/sctp/sctp.h

patching file include/net/sctp/sm.h

patching file net/sctp/sm_make_chunk.c

patching file net/sctp/sm_statefuns.c

patching file net/sctp/socket.c

 

Verify that the patch really did work properly and that there are no errors or warnings in the output of the patch program. It is also a good idea to look at the Makefile of the kernel to see the kernel version:

$ head -n 5 Makefile

VERSION = 2

PATCHLEVEL = 6

SUBLEVEL = 17

EXTRAVERSION = .10

NAME=Crazed Snow-Weasel

 
打完 patch 之后,记住把内核的目录也做相应的修改,例如这里应该把  linux-2.6.17.9 改为 linux-2.6.17.10

 

7.4.                    重新配置内核
7.5.                    重新编译和安装
7.6.                    使用 ketchup 工具简化工作
7.7.                    问题:如何从一个较高版本 down grow 到一个较低的版本上?
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值