本文的参考网站:
在分析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).
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
allow-hotplug eth0
# Wang Jiankun commented the following line
#iface eth0 inet dhcp
#iface eth0 inet dhcp
# Wang Jiankun added the the following lines
iface eth0 inet static
address 192.168.1.251
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
iface eth0 inet static
address 192.168.1.251
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
重启系统后,修改将生效。
4、通过wget下载linux内核源码
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 directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
administrator@wangjk:~/kernel$
administrator@wangjk:~/kernel$ tar jxf linux-2.6.19.tar.bz2
tar: bzip2: Cannot exec: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
administrator@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/fixdep
scripts/basic/fixdep.c:105:23: error: sys/types.h: No such file or directory
scripts/basic/fixdep.c:106:22: error: sys/stat.h: No such file or directory
scripts/basic/fixdep.c:107:22: error: sys/mman.h: No such file or directory
scripts/basic/fixdep.c:108:20: error: unistd.h: No such file or directory
scripts/basic/fixdep.c:109:19: error: fcntl.h: No such file or directory
scripts/basic/fixdep.c:110:20: error: string.h: No such file or directory
scripts/basic/fixdep.c:111:20: error: stdlib.h: No such file or directory
scripts/basic/fixdep.c:112:19: error: stdio.h: No such file or directory
HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c:105:23: error: sys/types.h: No such file or directory
scripts/basic/fixdep.c:106:22: error: sys/stat.h: No such file or directory
scripts/basic/fixdep.c:107:22: error: sys/mman.h: No such file or directory
scripts/basic/fixdep.c:108:20: error: unistd.h: No such file or directory
scripts/basic/fixdep.c:109:19: error: fcntl.h: No such file or directory
scripts/basic/fixdep.c:110:20: error: string.h: No such file or directory
scripts/basic/fixdep.c:111:20: error: stdlib.h: No such file or directory
scripts/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.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:32:20: error: curses.h: No such file or directory
In 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 '*' token
scripts/kconfig/lxdialog/dialog.h:193: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:195: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:196: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:197: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:198: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:200: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:31: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:59: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:95: error: expected ')' before '*' token
[省略其后部分]
HOSTCC scripts/kconfig/lxdialog/checklist.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:32:20: error: curses.h: No such file or directory
In 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 '*' token
scripts/kconfig/lxdialog/dialog.h:193: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:195: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:196: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:197: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:198: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:200: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:31: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:59: error: expected ')' before '*' token
scripts/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 --->
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!
(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/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.19
Found kernel: /boot/vmlinuz-2.6.18-6-686
Updating /boot/grub/menu.lst ... done
13、重启系统引导新内核后查看版本号
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.19
Found kernel: /boot/vmlinuz-2.6.18-6-686
Updating /boot/grub/menu.lst ... done
13、重启系统引导新内核后查看版本号
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 2009
administrator@wangjk:~$
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 2009
administrator@wangjk:~$
可以看出已经是我编译的内核了。
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/jiankun_wang/archive/2009/05/04/4147806.aspx
------------------------------------
ubuntu不带linux内核源码,需要自己下载安装。
1,查看自己的内核版本
uname -r
2,查看源内的内核源码类表
apt-cache search linux-source
3,下载安装内核源代码
sudo apt-get install linux-source-2.6.27 //我选的是这一个,自己看着办吧
4,等待........
下载完成后,在/usr/src下会有一个文件名为linux-source-2.6.xx.tar.bz2的压缩包
5,解压缩包
tar jxvf linux-source-2.6.27.tar.bz2 //看清自己的版本
解压后会生成一个源代码目录/usr/src/linux-source-2.6.27
6,进入源码目录后,配置文件
make oldconfig
7,生成内核
make
8,疯狂等待,大约1个多小时
9,编译模块
make modules
10,安装模块
make modules_install
大功告成!^_^
下面说一下Makefile文件
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install #PWD当前工作目录的变量
obj-m := hello.o
obj-m := hello.o
#hello.o是你要生成的驱动,以后可以自己改
KERNELDIR:=/lib/modules/2.6.27-7-generic/build
KERNELDIR:=/lib/modules/2.6.27-7-generic/build
#这里别抄,写成你自己的版本,这个目录执行了内核源码目录
PWD:=$(shell pwd) #将当前工作目录赋值该PWD
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
PWD:=$(shell pwd) #将当前工作目录赋值该PWD
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
下面是一个经典的Hello,world!例子自己make一下就行了。
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world!\n");
return 0;
}
{
printk(KERN_ALERT "Hello, world!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world!\n");
}
{
printk(KERN_ALERT"Goodbye, cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
module_exit(hello_exit);
-----------------------------------------------------------------------------
修改的这篇文章,自己加了几个注意点
http://blog.theosoft.net/article.asp?id=57
第一次的Linux kernel上机内容是编译一个内核。我用的是Ubuntu,有很多地方和其它 Linux不一样,所以就来把我的过程写下来,以后也好有个参照
首先当然是下载内核源代码,如果你要最新的内核,可以去 ftp.kernel.org。当然,国内速度可能会很慢。如果你是教育网用户,可以去上海交大的镜像站下载,地址是 http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/,缺点就是没有最新的内核(更新需要一定的时间)。
==================================================备注
如果是就是编译ubuntu本省自带的内核,只需要
我下载的是linux-2.6.19.2.tar.gz可以下到的最新版本了。下完之后当然是解压缩。不过还不能直接编译,因为Ubuntu的默认安装里缺少必要的组建。打开终端,输入一下命令:
sudo -i
apt-get install build-essential kernel-package libncurses5-dev
然后到新立得里把所有以ncurses开头的软件包全部装上,这样就可以开始编译内核了。
先运行以下命令,复制当前内核的配置文件。
cp /boot/config-`uname -r` ./.config
然后
make menuconfig
http://blog.theosoft.net/article.asp?id=57
第一次的Linux kernel上机内容是编译一个内核。我用的是Ubuntu,有很多地方和其它 Linux不一样,所以就来把我的过程写下来,以后也好有个参照
![](http://blog.theosoft.net/fckeditor/editor/images/smiley/tt/face1.gif)
首先当然是下载内核源代码,如果你要最新的内核,可以去 ftp.kernel.org。当然,国内速度可能会很慢。如果你是教育网用户,可以去上海交大的镜像站下载,地址是 http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/,缺点就是没有最新的内核(更新需要一定的时间)。
==================================================备注
如果是就是编译ubuntu本省自带的内核,只需要
新立得搜索linux souce,下载带ubuntupatch的kernel的source code。
sudo apt-get source linux-source-2.6.27
==================================================备注
我下载的是linux-2.6.19.2.tar.gz可以下到的最新版本了。下完之后当然是解压缩。不过还不能直接编译,因为Ubuntu的默认安装里缺少必要的组建。打开终端,输入一下命令:
sudo -i
apt-get install build-essential kernel-package libncurses5-dev
然后到新立得里把所有以ncurses开头的软件包全部装上,这样就可以开始编译内核了。
先运行以下命令,复制当前内核的配置文件。
cp /boot/config-`uname -r` ./.config
然后
make menuconfig
选择 "Load an Alternate Configuration File",再选择你刚才拷贝过来的.config文件作为配置文件。然后确定。当结束后,你选择退出时,会提示问你 "Do you wish to save your new kernel configuration?"选择yes即可。
接下来就要编译了。输入命令:
make
你也可以将编译任务分成2个线程来执行,只要输入:
make -j2
编译一般需要1~1.5小时左右,就看cpu的性能如何
make
你也可以将编译任务分成2个线程来执行,只要输入:
make -j2
编译一般需要1~1.5小时左右,就看cpu的性能如何
![](http://blog.theosoft.net/fckeditor/editor/images/smiley/tt/face2.gif)
编译完成后,开始安装:
make module_install
make install
然后添加引导信息,不过之前还是要装一个组件initramfs-tools,装完以后输入:
make module_install
make install
然后添加引导信息,不过之前还是要装一个组件initramfs-tools,装完以后输入:
mkinitramfs -o /boot/initrd.img-2.X.XX /lib/modules/2.X.XX
==================================================备注
后面的参数不要忘记了,否则启动新内核会出现错误:
WARNING: Couldn’t open directory /lib/modules/2.6.15.7-ubuntu1: No such file or directory
FATAL: Could not open /lib/modules/2.6.15.7-ubuntu1/modules.dep.temp for writing: No such file or directory
FATAL: Could not open /lib/modules/2.6.15.7-ubuntu1/modules.dep.temp for writing: No such file or directory
==================================================备注
最后打开 /boot/grub/menu.lst
在 ## ## End Default Options ## 下面添加类似下面的两段
title Ubuntu, kernel 2.6.19.2
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6
initrd /initrd.img-2.6.19.2
savedefault
boot
title Ubuntu, kernel 2.6.19.2 (recovery mode)
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6 ro single
initrd /initrd.img-2.6.19.2
boot
注意 root和kernel字段要模仿menu.lst下面已有的内容写。下面是 (hd0,4),那么你也写(hd0,4),下面写root=/dev/hdd6,你也写root=/dev/hdd6,只是内核的版本号改为现在编译的版本号。然后重新启动计算机,在GRUB中选择新内核启动。如果启动失败,你可以重启选择老内核。
在 ## ## End Default Options ## 下面添加类似下面的两段
title Ubuntu, kernel 2.6.19.2
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6
initrd /initrd.img-2.6.19.2
savedefault
boot
title Ubuntu, kernel 2.6.19.2 (recovery mode)
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6 ro single
initrd /initrd.img-2.6.19.2
boot
注意 root和kernel字段要模仿menu.lst下面已有的内容写。下面是 (hd0,4),那么你也写(hd0,4),下面写root=/dev/hdd6,你也写root=/dev/hdd6,只是内核的版本号改为现在编译的版本号。然后重新启动计算机,在GRUB中选择新内核启动。如果启动失败,你可以重启选择老内核。
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/unikingest/archive/2009/03/10/3977747.aspx