编译Linux内核(kernel)


学习Linux就从内核编译开始,现在就让我们迈出第一步,下面记录编译过程(以下操作均在root下完成)。

1、准备工作

我安装的是Ubuntu10.04,为完成内核编译,还需要安装一下包:

# apt-get install gcc
# apt-get install make

# apt-get install bzip2
# apt-get install libc6-dev
# apt-get install ncurse-dev
# apt-get install initrd-tools ####为了使用mkinitrd命令生成initrd.img-XXX

2、下载源码

可以在官网www.kernel.org下载响应的源码(下载完整版F),也可以wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.tar.bz2命令获取。

#wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.tar.bz2

#tar xjf linux-2.6.39.tar.bz2 -C /usr/src

3、配置文件

根据自己机器的硬件情况,选择内核编译配置选项,在这里我使用的是默认配置(make menuconfig后直接保存退出,生成.config文件);具体配置可以参考金步国先生翻译的资料:点击这里下载Linux 2.6.19.x 内核编译配置选项。

#cd /usr/src/linux-2.6.39

#make menuconfig

4、编译内核

   #make clean          ####第一次编译可以省略

   #make mrproper    ####第一次编译可以省略

   #make

5、编译和安装模块

#make modules

 # make modules_install

6、生成内核镜像文件

 #makinitramfs -o /boot/initrd-2.6.39.img /lib/modules/2.6.39   ##启动、加载模块
  # cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.39   ##可引导的、压缩的内核
  # cp System.map /boot/System.map-2.6.39            ##kernel 索引
  # cp .config /boot/config-2.6.39                             #内核配置文件


7、设置启动项
启动项是/boot/grub/grub.cfg文件,我们需要修改该文件,具体如下:

首先,复制下面内容。
menuentry 'Ubuntu, with Linux 2.6.32-32-generic' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,4)'
search --no-floppy --fs-uuid --set 8450af8f-090b-4eae-a123-6f1e3604fa9e
linux /boot/vmlinuz-2.6.32-32-generic root=UUID=8450af8f-090b-4eae-a123-6f1e3604fa9e ro quiet splash
initrd /boot/initrd.img-2.6.32-32-generic
}

  然后,对上面内容进行响应的修改,红色部分。
menuentry 'Ubuntu_Test, with Linux 2.6.39' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,4)'
search --no-floppy --fs-uuid --set 8450af8f-090b-4eae-a123-6f1e3604fa9e
linux /boot/vmlinuz-2.6.39 root=UUID=8450af8f-090b-4eae-a123-6f1e3604fa9e ro quiet splash
initrd /boot/initrd.img-2.6.39
}

8、测试
重启,选择Ubuntu_Test, with Linux 2.6.39进入ubuntu系统,最后通过命令uname -a查看系统的内核信息。

相关内容