本文的参考网站:
http://kernel-handbook.alioth.debian.org/
在分析linux内核源码的过程中,要是能够修改内核源码并运行修改后的内核,我想肯定是令人高兴的事,哪怕第一次修改仅仅是在启动时打印一行"Hello, Wang Jiankun!",肯定也是令我高兴的。为了能成功编译修改后的内核,今天先编译一遍内核。
为了有一个完整的记录,今天的起点是一台裸机。
1、在裸机上安装一个最小的debian系统
为了能够尽可能清晰的显示编译一个内核所需要的组件,在安装系统时,仅仅安装最小系统,然后需要什么,就使用apt-get安装什么。
使用网络安装,在安装过程中出现Software selection界面时不要选择任何选项,这样安装的系统将是最小的系统。
为了使用ssh远程登录,最小系统安装完成后,要安装ssh服务器并且要设置静态ip地址(debian安装过程中是通过dhcp获取的ip地址)。
2、安装ssh服务器
apt-get install ssh
3、设置静态ip地址
修改文件/etc/network/interfaces,其中蓝色部分是增加的,红色部分是屏蔽掉的,黑色部分是没有变化的。
# This file describes the network interfaces available on your system# and how to activate them. For more information, see interfaces(5).
# The loopback network interfaceauto loiface lo inet loopback
# The primary network interfaceallow-hotplug eth0
# Wang Jiankun commented the following line#iface eth0 inet dhcp
# Wang Jiankun added the the following linesiface eth0 inet staticaddress 192.168.1.251netmask 255.255.255.0broadcast 192.168.1.255network 192.168.1.0gateway 192.168.1.1
重启系统后,修改将生效。
4、通过wget下载linux内核源码
administrator@wangjk:~/kernel$ wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.19.tar.bz2
5、解压文件linux-2.6.19.tar.bz2 administrator@wangjk:~/kernel$ tar jxf linux-2.6.19.tar.bz2 tar: bzip2: Cannot exec: No such file or directorytar: Error is not recoverable: exiting nowtar: Child returned status 2tar: Error exit delayed from previous errorsadministrator@wangjk:~/kernel$
看来是没有bzip2这个包,那就安装一个:
apt-get install bzip2
6、安装debian的kernel-package软件包
在安装kernel-package软件包时,最好使用命令:apt-get build-dep kernel-package,而不要使用命令:
apt-get install kernel-package,后者安装的软件包是前者的子集,使用后者安装kernel-package软件包后,执行make menuconfig命令会出现头文件找不到的错误,如下所示:
administrator@wangjk:~/kernel/linux-2.6.19$ make menuconfig HOSTCC scripts/basic/fixdepscripts/basic/fixdep.c:105:23: error: sys/types.h: No such file or directoryscripts/basic/fixdep.c:106:22: error: sys/stat.h: No such file or directoryscripts/basic/fixdep.c:107:22: error: sys/mman.h: No such file or directoryscripts/basic/fixdep.c:108:20: error: unistd.h: No such file or directoryscripts/basic/fixdep.c:109:19: error: fcntl.h: No such file or directoryscripts/basic/fixdep.c:110:20: error: string.h: No such file or directoryscripts/basic/fixdep.c:111:20: error: stdlib.h: No such file or directoryscripts/basic/fixdep.c:112:19: error: stdio.h: No such file or directory
主要是因为libc6-dev软件包没有安装。所以即使使用了apt-get install kernel-package还得使用apt-get build-dep kernel-package,不如一次使用apt-get build-dep kernel-package完成方便。
7、安装libncurses5-dev软件包来支持make menuconfig
通过apt-get build-dep kernel-package安装完成kernel-package后,执行make menuconfig仍然报错,如下所示:
administrator@wangjk:~/kernel/linux-2.6.19$ make menuconfig HOSTCC scripts/kconfig/lxdialog/checklist.oIn file included from scripts/kconfig/lxdialog/checklist.c:24:scripts/kconfig/lxdialog/dialog.h:32:20: error: curses.h: No such file or directoryIn file included from scripts/kconfig/lxdialog/checklist.c:24:scripts/kconfig/lxdialog/dialog.h:97: error: expected specifier-qualifier-list before 'chtype'scripts/kconfig/lxdialog/dialog.h:187: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:193: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:195: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:196: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:197: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:198: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/dialog.h:200: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/checklist.c:31: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/checklist.c:59: error: expected ')' before '*' tokenscripts/kconfig/lxdialog/checklist.c:95: error: expected ')' before '*' token[省略其后部分]
原来是最小系统不支持图形的原因,安装libncurses5-dev后即可。
8、将系统的config文件拷贝到内核目录下
cp /boot/config-2.6.18-6-686 ./.config
9、执行make menuconfig
虽然是将原来系统的config文件拷贝过来的,但是如果所以的配置都采用默认的配置仍然会有问题,在我的系统上在加载文件系统时会死掉,所以还是要做必要的配置,主要是将scsi和sata部分编译进内核而不要编译成模块,如下所示:
Device Drivers ---> Serial ATA (prod) and Parallel ATA (experimental) drivers --->
SCSI device support --->
将蓝色两部分级联的选项全部编译进内核(其实没有必要全部,但为了简单起见,暂时这样做)。
10、安装fakeroot软件包
11、编译内核
fakeroot make-kpkg --initrd --revision=custom.1.0 kernel_image
12、安装内核
wangjk:/home/administrator/kernel# dpkg -i linux-image-2.6.19_custom.1.0_i386.deb
Selecting previously deselected package linux-image-2.6.19.(Reading database ... 17679 files and directories currently installed.)Unpacking linux-image-2.6.19 (from linux-image-2.6.19_custom.1.0_i386.deb) ...Done.Setting up linux-image-2.6.19 (custom.1.0) ...Running depmod.Finding valid ramdisk creators.Using mkinitramfs-kpkg to build the ramdisk.Running postinst hook script /sbin/update-grub.You shouldn't call /sbin/update-grub. Please call /usr/sbin/update-grub instead!
Searching for GRUB installation directory ... found: /boot/grubSearching for default file ... found: /boot/grub/defaultTesting for an existing GRUB menu.lst file ... found: /boot/grub/menu.lstSearching for splash image ... none found, skipping ...Found kernel: /boot/vmlinuz-2.6.19Found kernel: /boot/vmlinuz-2.6.18-6-686Updating /boot/grub/menu.lst ... done13、重启系统引导新内核后查看版本号
administrator@wangjk:~$ cat /proc/version Linux version 2.6.19 (root@wangjk) (gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) #1 SMP Thu May 7 21:52:10 CST 2009administrator@wangjk:~$
可以看出已经是我编译的内核了。