Ubuntu探秘之四:kernel和initrd-1

这一次探秘我们主要研究一下kernel和initrd,重点是后者。

kernel是什么以及有哪些功能相信大家都很清楚,我就不多说了。这里主要介绍ubuntu10.04上安装的相关软件包、文件、目录以及kernel的配置方法。

至于initrd文件是干什么用的呢?
大家只要做一个简单的实验就可以明白了:在开机的GRUB命令行上去掉initrd这一行,然后按CTRL+x启动,过会你会发现屏幕出现“kernel panic”的提示。
为什么会这样呢?这是因为为了减小kernle文件(vmlinuz)的大小和增强灵活性,现在的发行版如ubuntu默认将硬盘接口卡如IDE,STAT,SCSI的驱动程序以模块(modules)的形式
放在initrd文件中,GRUB中去掉initrd这一行意味着initrd中的modules没有被加载kernel就不能识别该硬盘更别提挂载上面的根文件系统了。
initrd文件的功能主要有两个:
1、提供开机必需的但kernel文件(即vmlinuz)没有提供的驱动模块(modules)
2、负责加载硬盘上的根文件系统并执行其中的/sbin/init程序进而将开机过程持续下去

GRUB将kernle加载到内存并执行,kernel在运行的后期会读取并执行initrd文件中的init脚本文件并按照其中命令逐行执行,所以要掌 握initrd的作用和硬盘上的根文件系统加载过程,全面解析init文件是很重要的。在本文的第二部分,我会将/boot/initrd文件打开,逐行 解读init文件.

一、kernel相关软件包和配置文件

1、下面我简要介绍以下ubuntu 10.04上安装的与kernle相关的软件包及文件:

geekard@geekard-laptop:~$ dpkg -l linux*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name 

          Version        Description
+++-==============-==============-============================================
ii  linux-firmware 1.34.1         Firmware for Linux kernel drivers
ii  linux-headers- 2.6.32-22.36   Header files related to Linux kernel version
ii  linux-headers- 2.6.32-22.36   Linux kernel headers for version 2.6.32 on x
ii  linux-image-2. 2.6.32-22.36   Linux kernel image for version 2.6.32 on x86
ii  linux-libc-dev 2.6.32-23.37   Linux Kernel Headers for development
。。。。。

可以看出系统安装的与kernel相关的软件包挺多的,但主要有三种类型:
linux-firmware     kernel提供的固件程序,位于/lib/firmware目录下。操作系统用驱动程序驱动硬件,而驱动程序就是和硬件上的固件(如PCI设备上的PCI BIOS)进行交互以控制对应硬件的,所以固件是驱动程序与硬件的执行机构间的桥梁。
linux-headers     与特定linux kernel版本相关的头文件,位于/usr/src/linux-headers-2.6.32-22/include和/usr/include目录下,在编译一些modules和软件如virtualbox additional tools 时使用。
linux-image      linux kernel的二进制压缩文件即/boot/vmlinu文件,此文件就是大名鼎鼎的linux内核文件!
linux-generi  主要提供相应版本linux kernel的metadata即更新文件

2、让我们重点看一下系统安装的linux-image软件包的功能和内容:
geekard@geekard-laptop:~$ dpkg -s  linux-image-2.6.32-22-generic
Package: linux-image-2.6.32-22-generic
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 94652
Maintainer: Ubuntu Kernel Team <kernel-team@lists.ubuntu.com>
Architecture: i386
Source: linux
Version: 2.6.32-22.36
Provides: fuse-module, ivtv-modules, kvm-api-4, linux-image, linux-image-2.6, ndiswrapper-modules-1.9, redhat-cluster-modules
Depends: initramfs-tools (>= 0.36ubuntu6), coreutils | fileutils (>= 4.0), module-init-tools (>= 3.3-pre11-4ubuntu3), wireless-crda
Pre-Depends: dpkg (>= 1.10.24)                    #注意这些依赖的软件包的作用是和kernel启动时需要的文件密切相关的。
Recommends: grub-pc | grub | lilo (>= 19.1)
Suggests: fdutils, linux-doc-2.6.32 | linux-source-2.6.32, linux-tools
Breaks: lvm2 (<< 2.02.54-1ubuntu3)
Conflicts: hotplug (<< 0.0.20040105-1)
Description: Linux kernel image for version 2.6.32 on x86/x86_64
This package contains the Linux kernel image for version 2.6.32 on
x86/x86_64.

.
Also includes the corresponding System.map file, the modules built by the  
packager, and scripts that try to ensure that the system is not left in an
unbootable state after an update.                  
.
Supports Generic processors.
.
Geared toward desktop systems.
.
You likely do not want to install this package directly. Instead, install
the linux-generic meta-package, which will ensure that upgrades work
correctly, and that supporting packages are also installed.
geekard@geekard-laptop:~$
geekard@geekard-laptop:~$ dpkg -L  linux-image-2.6.32-22-generic
/.
/boot                            #这就是系统启动目录,其中多是与kernel相关的文件
/boot/vmlinuz-2.6.32-22-generic
/boot/config-2.6.32-22-generic
/boot/abi-2.6.32-22-generic
/boot/System.map-2.6.32-22-generic
/boot/vmcoreinfo-2.6.32-22-generic
/lib
/lib/modules                      #这就是kernel提供的module所在的目录,在开机过程中和系统运行过程中经常使用。注意module和特定版本的kernel是对应的。
/lib/modules/2.6.32-22-generic
/lib/modules/2.6.32-22-generic/kernel
/lib/modules/2.6.32-22-generic/kernel/arch
/lib/modules/2.6.32-22-generic/kernel/arch/x86/kvm/kvm.ko
…..
/lib/firmware/2.6.32-22-generic/mts_gsm.fw   #kernel提供的固件程序
/lib/firmware/2.6.32-22-generic/yam/9600.bin
/usr
/usr/share
/usr/share/doc
/usr/share/doc/linux-image-2.6.32-22-generic
/usr/share/doc/linux-image-2.6.32-22-generic/changelog.Debian.old.gz
/usr/share/doc/linux-image-2.6.32-22-generic/copyright
/usr/share/doc/linux-image-2.6.32-22-generic/changelog.Debian.gz

3、好了,我们现在知道了/boot目录下除grub子目录外的所有文件都是由linux-image软件包提供的,那么这些文件的作用是什么呢
geekard@geekard-laptop:~$ ls /boot                #kernel相关文件
abi-2.6.32-22-generic         memtest86+.bin
config-2.6.32-22-generic      System.map-2.6.32-22-generic
grub                          vmcoreinfo-2.6.32-22-generic
initrd.img-2.6.32-22-generic  vmlinuz-2.6.32-22-generic
geekard@geekard-laptop:~$

各文件功能如下:
config-2.6.32-22-generic       内核参数配置记录文件,这个文件在重新编译配置kernel时非常有用。
System.map-2.6.32-22-generic   内核对象列表文件,内核对象是内核提供的常量及函数的符号链接
vmcoreinfo-2.6.32-22-generic   内核核心参数设置记录文件
initrd.img-2.6.32-22-generic   intrd文件,注意这个和系统的kernel版本是对应的,一般开机时必须在GRUB中指定
vmlinuz-2.6.32-22-generic      内核文件,这个和上个文件即为在GRUB命令行上指定的文件

geekard@geekard-laptop:~$ uname -a  #查看系统kernel信息命令
Linux geekard-laptop 2.6.32-22-generic #36-Ubuntu SMP Thu Jun 3 22:02:19 UTC 2010 i686 GNU/Linux
geekard@geekard-laptop:~$

显示的含义为:主机名为geekard-laptop、硬件平台为i686的笔记本电脑安装了GNU/Linux 类型的操作系统,其kernel是在SMP Thu Jun 3 22:02:19 UTC 2010第36次编译的,版本为版本为2.6.32-22-generic
另外在系统运行的过程中可以查看和编辑/proc/sys/kernel/下的相应文件实时获取和设置kernel的参数

geekard@geekard-laptop:~$ ls /etc/kernel   #更新内核过程中用到的配置脚本
header_postinst.d  postinst.d  prerm.d
geekard@geekard-laptop:~$

4、内核的安装方式:
    (1)、从配置并编译好的rpm或deb软件包安装。
    (2)、从源码包安装,需手动配置。建议高级用户使用,配置不当会造成系统不能正常启动。

从源代码安装的步骤:(以下内容摘自kernel源代码的README文件)

1、INSTALLING the kernel source:

- If you install the full sources, put the kernel tarball in a
   directory where you have permissions (eg. your home directory) and
   unpack it:

        gzip -cd linux-2.6.XX.tar.gz | tar xvf -

   or
        bzip2 -dc linux-2.6.XX.tar.bz2 | tar xvf -


   Replace "XX" with the version number of the latest kernel.
2、Make sure you have no stale .o files and dependencies lying around:

        cd linux
        make mrproper

   You should now have the sources correctly installed.
3、CONFIGURING the kernel:
        "make config"      Plain text interface.
    "make menuconfig"  Text based color menus, radiolists & dialogs.
    "make xconfig"     X windows (Qt) based configuration tool.
    "make gconfig"     X windows (Gtk) based configuration tool.
    "make oldconfig"   Default all questions based on the contents of
               your existing ./.config file and asking about
               new config symbols.

在配置kernel时最重要的是确定kernel对芯片组、总线、文件系统的支持。
4、Do a "make" to create a compressed kernel image. It is also  possible to do "make install" if you have lilo installed to suit the
   kernel makefiles, but you may want to check your particular lilo setup first.
   COMPILING the kernel: If you configured any of the parts of the kernel as `modules', you  will also have to do "make modules_install".
5、配置grub.cfg文件,在其中添加新配置的kernel及initrd文件作为启动项
6、Reboot with the new kernel and enjoy.

  原文地址 http://bbs.auxten.com/read.php?tid=684097&keyword=Ubuntu%CC%BD%EF%BF%BD%EF%BF%BD
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值