编译和安装linux内核2.6.24,并且写一个简单的内核驱动测试之

好久没有编译过内核了,最近要弄驱动,查看了一下在/usr/src下面有两个文件夹,里面都是空的,没有任何内核源代码。我用的是CentOS 5.1,其实红帽系列在redora出现之后就一直没有附带linux内核源代码了,所以这些个头文件什么的在你安装好系统那刻起是都不会有的了。当然了,不管是什么发行版本的linux系统,下面方法都应该适用。自己从新安装一个就是,现下载:www.kernel.org下载,如图:

下载后解压到/usr/src下:bzip2 -dc linux-2.6.24.4.tar.bz2 | tar xvf -把文件夹linux-2.6.24.4改名字为:linux-kernel

随便在哪个文件夹下面,编写一个shell文件名叫a.sh,内容如下: #! /bin/bash mkdir -p /home/name/build/kernel cd /usr/src/linux-kernel make mrproper make O=/home/name/build/kernel menuconfig make O=/home/name/build/kernel sudo make O=/home/name/build/kernel modules_install install

然后打开一个终端执行bash a.shshell文件里面O=/home/name/build/kernel表示编译配置在=号后面的文件夹里面进行,中间可以自己查看一下。后面的过程除了在配置内核的时候,都可以不管了。这个过程会自动生成新的的内核启动映象,并且自动复制到/boot目录下面去,不用手动复制了。

之后修改grub.conf文件,让以后的系统用新的的内核启动:vim /etc/grub.conf内容如下:# grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to this file# NOTICE: You do not have a /boot partition. This means that# all kernel and initrd paths are relative to /, eg.# root (hd0,6)# kernel /boot/vmlinuz-version ro root=/dev/sda7# initrd /boot/initrd-version.img#boot=/dev/sdadefault=2timeout=5splashimage=(hd0,6)/boot/grub/splash.xpm.gzhiddenmenutitle CentOS (2.6.24.4) root (hd0,6) kernel /boot/vmlinuz-2.6.24.4 ro root=LABEL=/ rhgb quiet initrd /boot/initrd-2.6.24.4.imgtitle CentOS (2.6.18-53.el5) root (hd0,6) kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet initrd /boot/initrd-2.6.18-53.el5.imgtitle Windows XP rootnoverify (hd0,0) chainloader +1把这一部分注释掉:title CentOS (2.6.18-53.el5) root (hd0,6) kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet initrd /boot/initrd-2.6.18-53.el5.img修改为#title CentOS (2.6.18-53.el5)# root (hd0,6)# kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet# initrd /boot/initrd-2.6.18-53.el5.img

再修改default = 0,这里0对应第一个title,下面一次类推重启就可以了。编译内核的输出文件太大了,就是开始的/home/name/build/kernel,把这个文件夹可以删除了最后。要写驱动的话,不要删除。

写一个最简单的驱动程序:hello.c

/*======================================================================Asimplekernelmodule:"helloworld"======================================================================*/#include<linux/init.h>#include<linux/module.h>MODULE_LICENSE("DualBSD/GPL");staticinthello_init(void){printk(KERN_ALERT"HelloWorldenter ");return0;}staticvoidhello_exit(void){printk(KERN_ALERT"HelloWorldexit ");}module_init(hello_init);module_exit(hello_exit);MODULE_AUTHOR("ztz0223");MODULE_DESCRIPTION("AsimpleHelloWorldModule");MODULE_ALIAS("asimplestmodule");

然后写一个Makefile

如下:

PWD = $(shell pwd)KERNEL_SRC = /usr/src/linux-2.6.24.4/obj-m := hello.omodule-objs := hello.oall: $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules clean: rm *.ko rm *.o

打开终端进入到hello.c路径下make,2.6的内核好像不支持用gcc直接编译了,要用make,如下:

[root@BTazuoazuo]#cdhello//进入驱动程序的路径,此部分是注释[root@BTazuohello]#dirhello.cMakefile[root@BTazuohello]#make//编译make-C/lib/modules/2.6.24.4/buildM=/azuo/hellomodulesmake[1]:Enteringdirectory`/usr/src/linux-2.6.24.4’CC[M]/azuo/hello/hello.oBuildingmodules,stage2.MODPOST1modulesCC/azuo/hello/hello.mod.oLD[M]/azuo/hello/hello.komake[1]:Leavingdirectory`/usr/src/linux-2.6.24.4′[root@BTazuohello]#dir//编译成功hello.chello.kohello.mod.chello.mod.ohello.oMakefileModule.symvers[root@BTazuohello]#

加载和卸载驱动:

[root@BTazuohello]#insmod./hello.ko[root@BTazuohello]#rmmod./hello.ko

打开/var/log/messages文件可以看到,最后有内核加载和卸载的信息:

Hello World enterHello World exit表示内核加载和卸载成功!记录沿途的心情。那样的生活才是我想要的。

编译和安装linux内核2.6.24,并且写一个简单的内核驱动测试之

相关文章:

你感兴趣的文章:

标签云: