免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 13711 | 回复: 40
打印 上一主题 下一主题

[分享] 内核编译学习笔记之完整篇(v1.1.1) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-28 01:38 |只看该作者 |倒序浏览

**************************************************************************************
注 :该文章参考了如下内容 :

A)kernel-HOWTO

B)Module-HOWTO

C)man 手册

D)CU : RedHat Linux 2.6内核如何build kernel-sourcecode的rpm包 :

          http://linux.chinaunix.net/bbs/v ... code%B5%C4rpm%B0%FC

作者 :ailms <ailms{@}qq{dot}com>

版本 :v1.1.1

完成时间  :2007/09/29 22:48

**************************************************************************************


下面的东西都是在俺的 PC 实验得来的,可能由于环境不同会造成某些结果的不一致,欢迎各位朋友多多指正,补充 ! ^_^

目录

一、编译前的准备工作

二、编译内核的方式

三、/usr/src 目录的结构

四、2.4 内核编译过程步骤

五、2.4 下如何单独编译某个模块

六、关于 “depmod: *** Unresolved symbols ” 的问题

七、2.6 内核编译过程步骤

    a) make help

      b) 单个模块的编译

    c)如何导出当前内核的 .config (v1.1)

      d)如何生成 rpm 包 (V1.1)

八、补丁部分

    a)diff 命令的基本用法以及如何生成补丁文件

    b)补丁文件内容解读

    c)patch 命令的用法 :打补丁、测试、恢复(卸载)

    d)patchutils 工具包及 lsdiff、grepdiff 、splitdiff、combinediff 命令的使用 (V1.1)

    e)补丁的分类 :增量和差分型  [ 注 :资料上并没有明确定义,但为了便于自己记忆,所以借用备份领域的术语 ]

      f)如何给 Linux 内核打补丁:从 2.6.10 → 2.6.12.6

九、patch-kernel 脚本分析

十、patch-kernel 脚本应用实例


[ 本帖最后由 ailms 于 2007-9-29 22:48 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-09-28 01:39 |只看该作者
一、编译内核前的准备工作

1、为什么需要编译内核?

    需要手工编译内核的理由有很多,例如
   
    a)内核开发
   
    b)新版内核提供了你主机上某个硬件的驱动
   
    c)需要使用新的功能
   
    d)原来的内核过于臃肿,想裁减掉一些不必要的功能
   
    e)其他...
   
    不过编译内核之前还是需要考虑其他事情,如果只是想弄出一个 vmlinuz 来那很简单,关键是该内核日后的使用 :
   
    a)是否只能通过重编译内核来解决,有没有其他的方法
   
    b)新内核是否足够稳定?会否对那些应用产生影响?是否存在兼容性问题?
   
    c)新版本内核有那些已知的漏洞
   
    d)其他
   
    e)欢迎补充!!


2、如果是重新编译现有的内核,最好先备份整个源代码目录

一个 make mrproper 就可以把 .config 文件干掉,到时 make menuconfig 一个个重新选简直是令人抓狂 ,所以最好把该文件备份
        
3、查看 Documentation/Changes 文件

该文件告诉了要编译该版本的内核所需要的一些软件以及最低版本要求,例如
  
  
  Current Minimal Requirements
============================

Upgrade to at *least* these software revisions before thinking you've
encountered a bug!  If you're unsure what version you're currently
running, the suggested command should tell you.

Again, keep in mind that this list assumes you are already
functionally running a Linux 2.2 kernel.  Also, not all tools are
necessary on all systems; obviously, if you don't have any PCMCIA (PC
Card) hardware, for example, you probably needn't concern yourself
with pcmcia-cs.

o  Gnu C                  2.95.3                  # gcc --version
o  Gnu make               3.77                    # make --version
o  binutils               2.9.1.0.25              # ld -v
o  util-linux             2.10o                   # fdformat --version
o  modutils               2.4.2                   # insmod -V
o  e2fsprogs              1.25                    # tune2fs
o  jfsutils               1.0.12                  # fsck.jfs -V
o  reiserfsprogs          3.6.3                   # reiserfsck -V 2>&1|grep reiserfsprogs
o  pcmcia-cs              3.1.21                  # cardmgr -V
o  PPP                    2.4.0                   # pppd --version
o  isdn4k-utils           3.1pre1                 # isdnctrl 2>&1|grep version



4、使用 scripts/ver_linux 脚本进行快速检查

在源代码目录下(假设为 /usr/src/linux-2.6.20.1)有一个 scripts/ 目录,里面有很多有用的脚本,

其中一个叫做 ver_linux
  1. [root@monitor scripts]# ll ver_linux
  2. -rw-r--r--    1 root     root         2569 2002-11-29  ver_linux
  3. [root@monitor scripts]#
复制代码
由于没有执行权限,所以用 bash -f 来执行它。当然你也可以用 chmod a+x 来加上该权限
  1.         [root@monitor scripts]# bash -f ver_linux
  2. If some fields are empty or look unusual you may have an old version.
  3. Compare to the current minimal requirements in Documentation/Changes.

  4. Linux monitor.no7.com 2.4.20-8smp #1 SMP Thu Mar 13 17:45:54 EST 2003 i686 i686 i386 GNU/Linux

  5. Gnu C                  3.2.2
  6. Gnu make               3.79.1
  7. util-linux             2.11y
  8. mount                  2.11y
  9. modutils               2.4.22
  10. e2fsprogs              1.32
  11. jfsutils               1.0.17
  12. reiserfsprogs          3.6.4
  13. pcmcia-cs              3.1.31
  14. PPP                    2.4.1
  15. isdn4k-utils           3.1pre4
  16. Linux C Library        2.3.2
  17. Dynamic linker (ldd)   2.3.2
  18. Procps                 2.0.11
  19. Net-tools              1.60
  20. Kbd                    1.08
  21. Sh-utils               4.5.3
  22. Modules Loaded         iptable_filter ip_tables autofs e100 microcode nls_iso8859-1 nls_cp437 vfat fat loop lvm-mod keybdev mousedev hid input usb-ohci usbcore ext3 jbd aic7xxx sd_mod scsi_mod
  23. [root@monitor scripts]#
复制代码


5、最好保存每个 make 命令的输出,例如 make <target> 2>&1 |tee /tmp/make-<target>.out

6、仔细阅读 tarball 中自带的 README 文件、Documentation/HOWTO 文件以及任何与你此次编译相关的文件

7、make menuconfig 过程中如果有不懂的选项,可以选择 <help> ,如果还是看不懂,就按照建议说的做

论坛徽章:
0
3 [报告]
发表于 2007-09-28 01:40 |只看该作者
二、编译内核的方式

你可以通过 rpm (source rpm 或者 binary rpm)或者 tarball  的方式来编译。

1、安装方面 :

rpm 方式 :简单,可能有厂家专门优化的配置。但缺乏灵活性,一般都会预装很多东西,特别是一大堆的驱动模块

tarball 方式 :复杂、容易出错(例如选少了某个驱动的支持,导致新内核无法正常使用)。但提供足够的灵活性,编译什么东西都由你自己决定。
   
2、卸载方面 :

rpm 方式 :比较难。因为新内核是通过 rpm -ivh ,而不是 rpm -Uvh ,所以装完之后会有两个 kernel rpm 存在,一旦想卸载的话比较麻烦,rpm -e 会报错
        
tarball 方式 :很简单,直接把 /boot 下的相关文件和内核源代码树,/lib/modules 对应目录删除,修改 grub.conf 就可以了

论坛徽章:
0
4 [报告]
发表于 2007-09-28 01:40 |只看该作者
三、/usr/src 目录的结构

下面是 2.4 内核的 :
  1. [root@mail src]# ll
  2. total 12
  3. drwxr-xr-x    2 root     root         4096 Jan 25  2003 debug
  4. lrwxrwxrwx    1 root     root           14 Sep 17 06:20 linux-2.4 -> linux-2.4.20-8
  5. drwxr-xr-x   16 root     root         4096 Sep 17 06:20 linux-2.4.20-8
  6. drwxr-xr-x    7 root     root         4096 Sep 17 06:20 redhat
  7. [root@mail src]#
复制代码
可以看到主要分成两类 :

1、内核源代码目录,例如 linux-2.4.20-8/ 。有多个内核就有多个这样的目录

2、redhat/ 目录。该目录是用于编译 sorce rpm 用的。其结构是 :
  1. [root@monitor redhat]# find . -maxdepth 2 -type d
  2. .
  3. ./BUILD                                                                                                               
  4. ./BUILD/kernel-2.4.2031.9custom               
  5. ./RPMS                                                                                                               
  6. ./RPMS/athlon                                                                                       
  7. ./RPMS/i386
  8. ./RPMS/i486
  9. ./RPMS/i586
  10. ./RPMS/i686
  11. ./RPMS/noarch
  12. ./SOURCES                                                                                                        
  13. ./SPECS                                                                                                               
  14. ./SRPMS                                                                                                               
  15. [root@monitor redhat]#
复制代码
BUILD/ 存放编译过程中的临时内容                                                                                    

BUILD/kernel-<release>/ 就是编译新版 kernel 时产生的目录。如果是编译其他的 src rpm 也会生成相应的目录         

RPMS/ 是存放编译好的二进制 rpm 包的目录                                                                        

RPMS/<platform>/ 是按平台区分的存放二进制 rpm 包的目录,编译后的二进制 rpm 在那个目录可以通过 $(arch) 命令获知                                                                                                               

SOURCES/ 是存放 source rpm 中的 tar.gz 的内容的。编译前会把 tar.gz 的内容解压到这里                                 

SPECS/ 目录是存放 source rpm 中的 spec 文件的                                                                     

SRPMS/ 目录是存放编译好的 source rpm 的。如果你指定不生成 source rpm ,则该目录下不会有内容                        
                                                                                                                  
        
下面是 2.6 内核的 :
  1. [root@mail src]# ll
  2. total 16
  3. drwxr-xr-x  6 root root 4096 Sep 27 17:05 kernels
  4. drwxr-xr-x  7 root root 4096 Sep 16 22:54 redhat
  5. [root@mail src]#
复制代码
  1. [root@mail src]# cd kernels/
  2. [root@mail kernels]# ll
  3. total 32
  4. drwxr-xr-x  17 root root 4096 Sep 16 22:53 2.6.9-5.EL-hugemem-i686
  5. drwxr-xr-x  17 root root 4096 Sep 26 21:10 2.6.9-5.EL-i686
  6. drwxr-xr-x  17 root root 4096 Sep 16 22:53 2.6.9-5.EL-smp-i686
  7. drwxrwxr-x  20 root root 4096 Sep 27 17:13 linux-2.6.20.1
  8. [root@mail kernels]#
复制代码
可以看到把多个内核源代码目录都集中放到 kernels/ 下,而不是直接放在 /usr/src/ 下。同时源代码目录也和 2.4 有稍微不同,具体待各位自己发现

论坛徽章:
0
5 [报告]
发表于 2007-09-28 01:41 |只看该作者
四、2.4 内核编译过程步骤

这部分内容随便一搜就很多了,我也就不多说,把大概的过程列一下。这里以编译 2.4.35 为例

1、解开 tar 包到 /usr/src/ 下

2、 如果是重新编译内核的话,记得备份旧的 .config 文件并执行 make 。不要以 .config.bak 这样的名字备份,因为 make mrproper   

         会删除 .config.* 文件 !如果是编译新内核的话,把旧的 .config 文件拷贝到新的内核源代码目录下。
              
3、执行 make clean  mrproper

4、 执行 make oldconfig ,这样可以不用经历原来的选择过程,直接使用旧的配置文件否则执行 make menuconfig  

5、make dep 检查依赖性是否存在问题

6、编辑 /usr/src/linux/Makefile 的 EXTRAVERSION ,给这个内核加上一个你自己的名称 !例如 '.custom' ,这样编译出来的内核版本就是 2.4.35.custom

          不要忘记该步,否则后面的 make modules_install 新产生的模块文件会把 /lib/modules/2.4.35/ 下的内容都覆盖掉。

7、执行 make bzImage 生成内核的二进制文件

8、执行 'make moudles' 和 'make modules_install'

9、第8步是非常久的。

10、使用 cp 把 /usr/src/linux/arch/i386/boot/bzImage 文件拷贝到 /boot 下,重命名为你想要的内核名称,一般是 vmlinuz-2.x.x.-<your_version>

            再把 /usr/src/linux-2.4.35/System.map 拷贝到 /boot 下,重命名为 System.map-2.4.35.custom
                                       
             把 /usr/src/linux-2.4.35/.config 拷贝到 /boot 下,重命名为 config-2.4.35.custom        

11、修改 grub.conf 或者 lilo.conf ,加入相应内容。

12、重启并检查新内核是否可用,并检查对应用是否有影响。

13、用 mkbootdisk 命令创建 bootdisk

14、如果有时间可以用 make rpm 创建自己的内核 rpm 包,不过目前只在 rehdat 自己提供的 kernel source rpm 包上成功过,在 kerne.org 下在的源代码

        都失败了。

15、执行 make clean 把不用的文件删除

16、把 .config 文件备份到一个安全的位置

注 1 :其实编译内核时不一定要以 root 身份进行,例如你可以把 tar 包解压到你的 HOME 目录下,并按上面一步步做,直至 make modules

但因为 make modules_install 需要写 /lib/modules/ 目录,所以只有 root 才有这个权限。


[ 本帖最后由 ailms 于 2007-9-28 01:42 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-09-28 01:44 |只看该作者
五、2.4 下如何单独编译某个模块

有时候我们编译好了内核,模块文件也生成了。但在日后的使用过程中想增加某个模块应该怎么办呢?下面就以增加 reiserfs 、jfs 、xfs 模块为例进行说明。

当前的内核版本是 2.4.35.normal ,所以新生成的模块也将放在 /lib/modules/2.4.35.normal/fs/ 下

1、执行 make dep 2>&1 |tee /tmp/make_dep.out

2、复制 Makefile 为 Makefile.onlymodule

3、修改 Makefile.onlymodule 的 EXTRAVERSION 为 .onlymodule 。之所以这么做是因为不想覆盖当前内核的模块。
         
     也就是把 Makefile.onlymodule 的 SUBDIRS 为 :SUBDIRS =fs/reiserfs fs/jfs fs/xfs
              
4、执行make -f Makefile.onlymodule modules 2>&1 |tee /tmp/make_only_modules.out 生成上面的模块

    记得要加上 -f 参数,否则使用的还是 Makefile ,而不是 Makefile.onlymodule 文件
                                 
5、执行make -f Makefile.onlymodule modules_install 2>&1 |tee /tmp/make_only_modules_install.out 安装模块

6、下面把生成好的模块文件拷贝到当前内核的模块目录下
  1. [root@mail fs]# cd /lib/modules/2.4.35.onlymodule/kernel/fs
  2. [root@mail fs]#
  3. [root@mail fs]# ll
  4. total 12
  5. drwxr-xr-x    2 root     root         4096 Sep 19 01:15 jfs
  6. drwxr-xr-x    2 root     root         4096 Sep 19 01:15 reiserfs
  7. drwxr-xr-x    2 root     root         4096 Sep 19 01:15 xfs
  8. [root@mail fs]#        
  9. [root@mail fs]# uname -a
  10. Linux mail.bob.com 2.4.35.normal #1 SMP Wed Sep 19 00:46:00 CST 2007 i686 i686 i386 GNU/Linux
  11. [root@mail fs]#
  12. [root@mail fs]# cp -a * /lib/modules/2.4.35.normal/kernel/fs/
  13. [root@mail fs]#
复制代码
可以看到上面的 /lib/modules/2.4.35.onlymodule/kernel/fs/ 下只有 jfs/、reiserfs/、xfs/ 三个目录,这就是因为 SUBDIRS 指定只生成这3个目录而已。

所以如果我们上面不修改 EXTRAVERSION 的话,则会把当前内核的模块目录(/lib/modules/2.4.35.normal/) 下的所有其他内容都删除,

再拷贝这3个目录 !!

而在 Kernel-HOWTO 中则没有修改 EXTRAVERSION 一步,直接修改 SUBDIRS ,结果就如同上面所说

7、这样我们就有了 reiserfs、jfs、xfs 文件系统的模块了。下面执行 depmod -a 生成新的 modules.dep 文件

注 :最好先备份原来的 /lib/modules/2.4.35.normal/modules.dep 文件,再执行 depmod

  1. [root@mail modules]# depmod -a
  2. [root@mail modules]# ll /lib/modules/2.4.35.normal/modules.dep
  3. -rw-r--r--    1 root     root         8363 Sep 19 01:18 /lib/modules/2.4.35.normal/modules.dep
  4. [root@mail modules]#
复制代码


8、在新的 modules.dep 中查找相应的行 :
  1. [root@mail modules]# grep -i 'reiserfs\|xfs\|jfs' /lib/modules/2.4.35.normal/modules.dep
  2. /lib/modules/2.4.35.normal/kernel/fs/jfs/jfs.o:
  3. /lib/modules/2.4.35.normal/kernel/fs/reiserfs/reiserfs.o:
  4. /lib/modules/2.4.35.normal/kernel/fs/xfs/xfs.o:
  5. [root@mail modules]#
复制代码
可以看到已经有这些模块的行了

9、尝试加载这些模块
  1. [root@mail modules]# modprobe jfs
  2. [root@mail modules]# modprobe reiserfs
  3. [root@mail modules]# modprobe xfs
  4. [root@mail modules]# lsmod |grep fs
  5. xfs                   594884   0  (unused)
  6. reiserfs              298800   0  (unused)
  7. jfs                   192248   0  (unused)
  8. [root@mail modules]#
复制代码
可以看到成功加载了

10、下面把 /lib/modules/2.4.35.onlymodule 删除,因为已经用不到了。
  1. [root@mail modules]# rm -fr 2.4.35.onlymodule/
  2. [root@mail modules]#
复制代码

论坛徽章:
0
7 [报告]
发表于 2007-09-28 01:44 |只看该作者
六、关于 “depmod: *** Unresolved symbols ” 的问题

不知道你是否注意到,前面有一个 System.map 文件,这个文件的具体作用在 Kernel-HOWOTO 中有讲,不过一般不用关心它

因为它是自动生成的。但是该文件的正确与否却会影响到模块的正常加载。下面以1个例子来说明

例如此前我们没有选择 ACPIP 支持,而现在我们想把该功能加到内核中,下面是 .config 文件中对应的部分

# ACPI Support
#
CONFIG_ACPI=y
CONFIG_ACPI_BOOT=y
CONFIG_ACPI_BUS=y
CONFIG_ACPI_INTERPRETER=y
CONFIG_ACPI_EC=y
CONFIG_ACPI_POWER=y
CONFIG_ACPI_PCI=y
CONFIG_ACPI_MMCONFIG=y
CONFIG_ACPI_SLEEP=y
CONFIG_ACPI_SYSTEM=y
CONFIG_ACPI_AC=m
CONFIG_ACPI_BATTERY=m
CONFIG_ACPI_BUTTON=m
CONFIG_ACPI_FAN=m
CONFIG_ACPI_PROCESSOR=m
CONFIG_ACPI_THERMAL=m
CONFIG_ACPI_ASUS=m
CONFIG_ACPI_TOSHIBA=m


如果直接按照上面的方法编译模块(不执行 make bzImage )的再执行 depmod -a ,会出现下面的错误
  1. [root@mail 2.4.34.4]# depmod -a -n -F /boot/System.map-2.4.34.4 2.4.34.4  |more
  2. depmod: /lib/modules/2.4.34.4/modules.dep.bak is not an ELF file
  3. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/ac.o
  4. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/asus_acpi.o
  5. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/battery.o
  6. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/button.o
  7. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/fan.o
  8. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/processor.o
  9. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/thermal.o
  10. depmod: *** Unresolved symbols in /lib/modules/2.4.34.4/kernel/drivers/acpi/toshiba_acpi.o
复制代码
1、执行 make clean  bzImage

2、把新的 bzImage、.config、System.map 都拷贝到 /boot 下

3、现在重新执行 make -f Makefile.onlymodules modules

4、同样把我们需要的模块文件拷贝过去就可以。

5、再次执行 depmod ,这次不再出错了 !!!

6、这说明是由于此前没有执行 make bzImage 所致,导致 System.map 文件的内容不正确,

  1. [root@mail 2.4.34.4]# depmod -a -F /boot/System.map-2.4.34.4 2.4.34.4 -n 2>/dev/null |grep -i 'unresolved'
  2. [root@mail 2.4.34.4]#
复制代码
7、去掉 -n 再次执行,可以看到 modules.dep 生成了 :
  1. [root@mail 2.4.34.4]# ll modules.dep
  2. -rw-r--r--    1 root     root         7513 Sep 20 00:22 modules.dep
  3. [root@mail 2.4.34.4]#
复制代码
8、检查 modules.dep 中是否含有 acpi 相关信息 :
  1. [root@mail 2.4.34.4]# grep -i acpi modules.dep
  2. /lib/modules/2.4.34.4/kernel/drivers/acpi/ac.o:
  3. /lib/modules/2.4.34.4/kernel/drivers/acpi/asus_acpi.o:
  4. /lib/modules/2.4.34.4/kernel/drivers/acpi/battery.o:
  5. /lib/modules/2.4.34.4/kernel/drivers/acpi/button.o:
  6. /lib/modules/2.4.34.4/kernel/drivers/acpi/fan.o:
  7. /lib/modules/2.4.34.4/kernel/drivers/acpi/processor.o:
  8. /lib/modules/2.4.34.4/kernel/drivers/acpi/thermal.o:    /lib/modules/2.4.34.4/kernel/drivers/acpi/processor.o
  9. /lib/modules/2.4.34.4/kernel/drivers/acpi/toshiba_acpi.o:
  10. [root@mail 2.4.34.4]#
复制代码

所以当出现 'Unresolved symbols' 的问题时,有可能和 System.map 文件版本有关

论坛徽章:
0
8 [报告]
发表于 2007-09-28 01:45 |只看该作者
七、2.6 内核的编译


1、RPM 方式编译

这部分见 http://linux.chinaunix.net/bbs/v ... code%B5%C4rpm%B0%FC

2、tarball 方式编译

个人感觉 2.6 的编译过程比 2.4 简单很多,有很多东西 make 都替你作了,就连修改 grub.conf 这样的东西也算在内。

而且 2.6 的 Makefile 中还有一个名为 help 的 target ,执行 make help 可以看到该 Makeilfe 中可用的 target

而在 2.4 的 Makefile 中并没有该 target
  1. [root@mail linux-2.6.20.16]# grep help Makefile
  2. # To see a list of typical targets execute "make help"
  3. # Basic helpers built in scripts/
  4.                          cscope TAGS tags help %docs check% \
  5. # Additional helpers built in scripts/
  6. help:
  7.         @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
  8.         @$(MAKE) $(build)=$(package-dir) help
  9.         @$(MAKE) -f $(srctree)/Documentation/DocBook/Makefile dochelp
  10.         @$(if $(archhelp),$(archhelp),\
  11.                 echo '  No architecture specific help defined for $(ARCH)')
  12. help:
  13. [root@mail linux-2.6.20.16]#
复制代码
下面是执行 make help 的输出
  1. [root@mail linux-2.6.9]# make help
  2. Cleaning targets:
  3.   clean           - remove most generated files but keep the config
  4.   mrproper        - remove all generated files + config + various backup files

  5. Configuration targets:
  6.   oldconfig       - Update current config utilising a line-oriented program
  7.   menuconfig      - Update current config utilising a menu based program
  8.   xconfig         - Update current config utilising a QT based front-end
  9.   gconfig         - Update current config utilising a GTK based front-end
  10.   defconfig       - New config with default answer to all options
  11.   allmodconfig    - New config selecting modules when possible
  12.   allyesconfig    - New config where all options are accepted with yes
  13.   allnoconfig     - New minimal config

  14. Other generic targets:
  15.   all             - Build all targets marked with [*]
  16. * vmlinux         - Build the bare kernel
  17. * modules         - Build all modules
  18.   modules_install - Install all modules
  19.   dir/            - Build all files in dir and below
  20.   dir/file.[ois]  - Build specified target only
  21.   rpm             - Build a kernel as an RPM package
  22.   tags/TAGS       - Generate tags file for editors
  23.   cscope          - Generate cscope index

  24. Static analysers
  25.   buildcheck      - List dangling references to vmlinux discarded sections
  26.                     and init sections from non-init sections
  27.   checkstack      - Generate a list of stack hogs
  28.   namespacecheck  - Name space analysis on compiled kernel

  29. Kernel packaging:
  30.   rpm-pkg         - Build the kernel as an RPM package
  31.   binrpm-pkg      - Build an rpm package containing the compiled kernel & modules
  32.   deb-pkg         - Build the kernel as an deb package

  33. Documentation targets:
  34.   Linux kernel internal documentation in different formats:
  35.   sgmldocs (SGML), psdocs (Postscript), pdfdocs (PDF)
  36.   htmldocs (HTML), mandocs (man pages, use installmandocs to install)

  37. Architecture specific targets (i386):
  38. * bzImage       - Compressed kernel image (arch/i386/boot/bzImage)  
  39. install       - Install kernel using
  40.                    (your) ~/bin/installkernel or
  41.                    (distribution) /sbin/installkernel or
  42.                    install to $(INSTALL_PATH) and run lilo

  43.   bzdisk       - Create a boot floppy in /dev/fd0
  44.   fdimage      - Create a boot floppy image

  45.   make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build
  46.   make O=dir [targets] Locate all output files in "dir", including .config
  47.   make C=1   [targets] Check all c source with $CHECK (sparse)
  48.   make C=2   [targets] Force check of all c source with $CHECK (sparse)

  49. Execute "make" or "make all" to build all targets marked with [*]
  50. For further info see the ./README file
  51. [root@mail linux-2.6.9]#
复制代码
例如 V 变量是用于控制是否启用 verbose 模式,默认是 0 (不启用)

O 变量控制 make 生成的文件放到那里,默认是当前目录

比较有用的有 clean 、mrproper、oldconfig、menuconfig、modules_install、<dir/> 、<dir/file.ko>、rpm 等几个

3、2.6 内核的编译过程

2.6 内核的比较简单,只有几个步骤而已

        a) make mrproper  clean ,同样需要先备份 .config 文件

        b) make oldconfig 或者 make menuconfig

        c) make

        d) make modules_install

        e) make install

其中 make 等同于 make dep bzImage modules ,而 make install 会做如下事情 :

        a) 把 bzImage、System.map 都拷贝到 /boot 下并自动改名为 vmlinz-<releas> 和 System.map-<release>

        b) 自动生成 initrd ,而且是 cpio 格式的,不再是原来的 image 格式

        c) 自动修改 grub.conf ,加上新的内核,但不会修改默认引导的内核。这点和 rpm 方式安装新内核不同。



4、单个模块的编译

下面以 ntfs 为例进行说明

        a)直接生成模块文件
  1. [root@mail linux-2.6.20.1]# make M='fs/ntfs'
  2.   LD      fs/ntfs/built-in.o
  3.   CC [M]  fs/ntfs/aops.o
  4.   CC [M]  fs/ntfs/attrib.o
  5.   CC [M]  fs/ntfs/collate.o
  6.   CC [M]  fs/ntfs/compress.o
  7.   CC [M]  fs/ntfs/debug.o
  8.   CC [M]  fs/ntfs/dir.o
  9.   CC [M]  fs/ntfs/file.o
  10.   CC [M]  fs/ntfs/index.o
  11.   CC [M]  fs/ntfs/inode.o
  12.   CC [M]  fs/ntfs/mft.o
  13.   CC [M]  fs/ntfs/mst.o
  14.   CC [M]  fs/ntfs/namei.o
  15.   CC [M]  fs/ntfs/runlist.o
  16.   CC [M]  fs/ntfs/super.o
  17.   CC [M]  fs/ntfs/sysctl.o
  18.   CC [M]  fs/ntfs/unistr.o
  19.   CC [M]  fs/ntfs/upcase.o
  20.   CC [M]  fs/ntfs/bitmap.o
  21.   CC [M]  fs/ntfs/lcnalloc.o
  22.   CC [M]  fs/ntfs/logfile.o
  23.   CC [M]  fs/ntfs/quota.o
  24.   CC [M]  fs/ntfs/usnjrnl.o
  25.   LD [M]  fs/ntfs/ntfs.o
  26.   Building modules, stage 2.
  27.   MODPOST 1 modules
  28.   CC      fs/ntfs/ntfs.mod.o
  29.   LD [M]  fs/ntfs/ntfs.ko               
  30. [root@mail linux-2.6.20.1]#
复制代码
上面最后显示的 fs/ntfs/ntfs.ko 就是我们想要的。

        b)手工拷贝带模块文件到
  1. [root@mail fs]# pwd
  2. /lib/modules/2.6.20.1/kernel/fs
  3. [root@mail fs]# mkdir ntfs
  4. [root@mail fs]#
  5. [root@mail fs]# cd ntfs
  6. [root@mail ntfs]# ll
  7. total 0
  8. [root@mail ntfs]# cp -a /usr/src/linux-2.6.20.1/fs/ntfs/ntfs.ko .
  9. [root@mail ntfs]#                        
复制代码
c)执行 depmod -a 生成新的 modules.dep 文件
  1. [root@mail ntfs]# depmod -a
  2. [root@mail ntfs]#
  3. [root@mail 2.6.20.1]# ll modules.dep
  4. -rw-r--r--  1 root root 92323 Sep 27 01:00 modules.dep               
  5. [root@mail 2.6.20.1]#
复制代码
d)尝试手工加载
  1. 手工尝试加载
  2. [root@mail ntfs]# modprobe ntfs
  3. [root@mail ntfs]#
  4. [root@mail ntfs]# lsmod |grep ntfs
  5. ntfs                  206740  0
  6. [root@mail ntfs]#
复制代码
可以看到 ntfs 模块已经加载成功了 !!!

论坛徽章:
0
9 [报告]
发表于 2007-09-28 01:46 |只看该作者
5、 如何获取当前内核的配置(.config 文件)

在 2.4 的时候,我经常碰到一个问题就是不小心把 .config 文件弄丢了,除非事先有备份,否则只能 make menuconfig 一个个重新选。

但在 2.6 就不同了,在 Gerneal Setup 部分有一个功能叫做 "Kernel .config support" ,意思如下 :





一旦你选中它,还有另外一个功能出现,就是 "Enabel access to .config through /proc/config.gz“



建议把这个选上。有了这个,上面的问题就迎刃而解了,方法有二 :

        a)通过 scripts/extract_ikconfig 脚本从一个 bzImage 文件中提出出来。不过目前试了很多次,总是报下面的错误
       

  1.         [root@mail scripts]# ./extract-ikconfig  /boot/vmlinuz-2.6.20.1  
  2.         cc: ./scripts/binoffset.c: No such file or directory
  3.         cc: no input files
  4.         [root@mail scripts]#
  5.        
复制代码

               
        b)通过 /proc/config.gz 文件获取,这是最方便的方法之一。步骤如下 :
       

  1.         [root@mail proc]# ll /proc/config.gz
  2.         -r--r--r--  1 root root 15192 Sep 27 01:12 /proc/config.gz
  3.         [root@mail proc]# file /proc/config.gz
  4.         /proc/config.gz: gzip compressed data, from Unix, max compression
  5.         [root@mail proc]#
  6.         [root@mail proc]# cat /proc/config.gz |gzip -d > /tmp/config
  7.         [root@mail proc]# ll /tmp/config
  8.         -rw-r--r--  1 root root 58949 Sep 27 01:13 /tmp/config
  9.         [root@mail proc]#
  10.         [root@mail proc]# cd /tmp
  11.         [root@mail tmp]# more config
  12.         #
  13.         # Automatically generated make config: don't edit
  14.         # Linux kernel version: 2.6.20.1
  15.         # Thu Sep 27 00:07:58 2007
  16.         #
  17.         CONFIG_X86_32=y
  18.         CONFIG_GENERIC_TIME=y
  19.         CONFIG_LOCKDEP_SUPPORT=y
  20.         CONFIG_STACKTRACE_SUPPORT=y
  21.         CONFIG_SEMAPHORE_SLEEPERS=y
  22.         CONFIG_X86=y
  23.         CONFIG_MMU=y
  24.         CONFIG_GENERIC_ISA_DMA=y
  25.         CONFIG_GENERIC_IOMAP=y
  26. (省略)
  27.        
复制代码

       
        这样是不是很方便呢 ^_^ !

[ 本帖最后由 ailms 于 2007-9-28 15:58 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-09-28 01:47 |只看该作者
6、生成 rpm 包


首先它会调用 make spec 在 /usr/src/redhat/SPECS/ 下生成一个 kernel.spec 文件

然后在 rpmbuild -ba 该 spec 文件
  1. [root@mail SPECS]# ps -ef |grep rpm
  2. root     19536 19501  0 23:06 pts/0    00:00:00 make rpm
  3. root     19761 19536  0 23:06 pts/0    00:00:00 make -f scripts/Makefile.build obj=/usr/src/kernels/linux-2.6.20.16/scripts/package rpm
  4. root     20478 19761  0 23:08 pts/0    00:00:00 rpmbuild --target i386 -ta ../kernel-2.6.20.16.tar.gz
  5. root     20503 20478  0 23:08 pts/0    00:00:00 /bin/sh -e /var/tmp/rpm-tmp.64176
  6. root     32099 27815  0 23:16 pts/1    00:00:00 grep rpm
  7. [root@mail SPECS]#
复制代码
这个过程很久,和整个编译流程差不多久

当你看到类似下面的输出

  INSTALL sound/synth/snd-util-mem.ko
  INSTALL sound/usb/snd-usb-audio.ko
  INSTALL sound/usb/snd-usb-lib.ko
  INSTALL sound/usb/usx2y/snd-usb-usx2y.ko
if [ -r System.map -a -x /sbin/depmod ]; then /sbin/depmod -ae -F System.map -b /var/tmp/kernel-2.6.20.16-root -r 2.6.20.16; fi
+ cp arch/i386/boot/bzImage /var/tmp/kernel-2.6.20.16-root/boot/vmlinuz-2.6.20.16
+ cp System.map /var/tmp/kernel-2.6.20.16-root/boot/System.map-2.6.20.16
+ cp .config /var/tmp/kernel-2.6.20.16-root/boot/config-2.6.20.16
+ /usr/lib/rpm/brp-compress
Processing files: kernel-2.6.20.16-4
Provides: kernel-2.6.20.16 kernel-drm
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /var/tmp/kernel-2.6.20.16-root

Wrote: /usr/src/redhat/SRPMS/kernel-2.6.20.16-4.src.rpm               
Wrote: /usr/src/redhat/RPMS/i386/kernel-2.6.20.16-4.i386.rpm        
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.30844
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd kernel-2.6.20.16
+ exit 0
rm ../kernel-2.6.20.16.tar.gz
[root@mail linux-2.6.20.16]#


下面查看生成的 rpm 包
  1. [root@mail SRPMS]# ll |grep '2.6.20' --color
  2. -rw-r--r--  1 root root 57218857 Sep 27 23:39 kernel-2.6.20.16-4.src.rpm
  3. [root@mail SRPMS]#
  4. [root@mail redhat]# cd RPMS
  5. [root@mail RPMS]# cd i386/
  6. [root@mail i386]# ll
  7. total 72160
  8. -rw-r--r--  1 root root 73808854 Sep 27 23:43 kernel-2.6.20.16-4.i386.rpm
  9. [root@mail i386]#
复制代码
下面是 binary rpm 包的内容
  1. [root@mail i386]# rpm -qlp kernel-2.6.20.16-4.i386.rpm |more
  2. /boot/System.map-2.6.20.16
  3. /boot/config-2.6.20.16
  4. /boot/vmlinuz-2.6.20.16
  5. /lib/modules
  6. /lib/modules/2.6.20.16
  7. /lib/modules/2.6.20.16/build
  8. /lib/modules/2.6.20.16/kernel
  9. /lib/modules/2.6.20.16/kernel/arch
  10. /lib/modules/2.6.20.16/kernel/arch/i386
  11. /lib/modules/2.6.20.16/kernel/arch/i386/crypto
  12. /lib/modules/2.6.20.16/kernel/arch/i386/crypto/aes-i586.ko
  13. (省略)
复制代码
日后你可以使用该 rpm 来安装或者升级内核,不需要重新编译和安装模块了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP