`
脸同学
  • 浏览: 38615 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

ubuntu 2.6.31 内核树建立

阅读更多

刚看 O'REILLY 写的《LINUX 设备驱动程序》时。作者一再强调在编写驱动程序时必须 建立内核树。所谓内核树,我的理解和网上资料说的一致就是内核源码的一种逻辑形式。那怎么建立呢?为此上网“翻云覆雨”起来而结果却是“惨败而归“。
为此托了一天又4个小时(当然包括吃饭睡觉的时间),连个简单的 hello wrold 都没实现。(书中p22页最简单也最没用的驱动事列)
不过功夫不负有心人。在今天终于弄明白了怎么回事。下面就请让我慢慢道来吧。

先查看自己OS使用的内核版本
czh@czh-desktop:~$ uname -r
2.6.31-20-generic /* 这是我显示的结果 */

如果安装系统时,自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。
czh@czh-desktop:/usr/src$ ls
linux-headers-2.6.31-20        
linux-source-2.6.31          /*这个就是解压后的源码目录 */
linux-headers-2.6.31-20-generic
linux-source-2.6.31.tar.bz2 /* 这是我下的源码 包 */
nvidia-185.18.36
如果没有源码。(一般ubuntu 都没有吧)
查看一下下载的源码包(切记不要使用超级用户使用此命令否则……会提示没有此命令)

czh@czh-desktop:/usr/src$ apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
linux-source-2.6.31 - Linux kernel source for version 2.6.31 with Ubuntu patches
czh@czh-desktop:/usr/src$

选择inux-source-2.6.31 - Linux kernel source for version 2.6.31 with Ubuntu patches了这个
然后 install:
czh@czh-desktop:/usr/src$ sudo apt-get install linux-source-2.6.31

下载完成后,在/usr/src下,文件名为:linux-source-2.6.31.tar.bz2,是一个压缩包,解压缩既可以得到整个内核的源代码:

czh@czh-desktop:/usr/src$ sudo tar jxvf linux-source-2.6.31.tar.bz2

解压后生成一个新的目录/usr/src/linux-source-2.6.31,所有的源代码都在该目录下。

进入该目录

开始配置内核 选择最快的原版的配置(默认)方式

czh@czh-desktop:/usr/src/linux-source-2.6.31$ sudo make oldconfig

完成后,开始make 吧 这儿比较久 一般有1一个小时吧。要保证空间足够 2 G左右吧

czh@czh-desktop:/usr/src/linux-source-2.6.31$ sudo make
执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。
然后 :

czh@czh-desktop:/usr/src/linux-source-2.6.31$ sudo make modules /* 编译 模块 */
czh@czh-desktop:/usr/src/linux-source-2.6.31$ sudo make modules_install /* 安装 模块 */

执行结束之后,会在/lib/modules下生成新的目录/lib/modules/2.6.31-20-generic/
。 在随后的编译模块文件时,要用到这个路径下的build目录。至此,内核编译完成。可以重启一下系统。至此 内核树就建立啦。

写一个 最简单 最没用的驱动吧 .
在 /home/linux_driver_codes/目录下创建2个文本文件 hello.c Makefile

//hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

程序我就不解释了……

//Makefile 文件
obj-m:=hello.o
KERNELDIR:=/lib/modules/2.6.31-20-generic/build
PWD:=$(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

czh@czh-desktop:~/linux_driver_codes$ sudo make
make -C /lib/modules/2.6.31-20-generic/build M=/home/linux_driver_codes modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.31-20-generic'
CC [M] /home/linux_driver_codes/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/linux_driver_codes/hello.mod.o
LD [M] /home/linux_driver_codes/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-20-generic'
czh@czh-desktop:~/linux_driver_codes$

czh@czh-desktop:~/linux_driver_codes$ ls -l
total 44
-rw-r--r-- 1 czh czh 285 2010-04-02 10:57 hello.c
-rw-r--r-- 1 czh czh 274 2010-04-02 10:49 hello.c~
-rw-r--r-- 1 czh czh 2303 2010-04-02 12:01 hello.ko
-rw-r--r-- 1 czh czh 663 2010-04-02 12:01 hello.mod.c
-rw-r--r-- 1 czh czh 1756 2010-04-02 12:01 hello.mod.o
-rw-r--r-- 1 czh czh 1144 2010-04-02 12:01 hello.o
drwxr-xr-x 2 czh czh 4096 2010-04-02 00:09 kernel
-rw-r--r-- 1 czh czh 204 2010-04-02 12:01 Makefile
-rw-r--r-- 1 czh czh 193 2010-04-02 12:01 Makefile~
-rw-r--r-- 1 czh czh 207 2010-04-01 23:43 mode.c~
-rw-r--r-- 1 czh czh    0 2010-04-02 12:01 Module.markers
-rw-r--r-- 1 czh czh   45 2010-04-02 12:40 modules.order
-rw-r--r-- 1 czh czh    0 2010-04-02 11:56 Module.symvers
czh@czh-desktop:~/linux_driver_codes$
然后加载模块 (一定要转到超级用户,不然会出现错误)
root@czh-desktop:~/linux_driver_codes$ insmod ./hello.ko

按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现(原因不解)
root@czh-desktop:~/linux_driver_codes$insmod ./hello.ko
root@czh-desktop:~/linux_driver_codes$

查看加载模块
root@czh-desktop:~/linux_driver_codes$ lsmod
Module           Size          Used by
.....
....
hello        1052          0
...
...
已经加载上咯~~

删除模块
root@czh-desktop:/home/czh/linux_driver_codes# rmmod hello
root@czh-desktop:/home/czh/linux_driver_codes#

那程序的输出在那呢?书中说明 如果不出现在终端 则会写进 syslog 文件中

czh@czh-desktop:~/linux_driver_codes$ cat /var/log/syslog |grep world
Apr 2 12:08:49 czh-desktop kernel: [ 5258.665347] Goodbye cruel world
Apr 2 12:49:14 czh-desktop kernel: [ 7683.945341] Hello,world
czh@czh-desktop:~/linux_driver_codes$
至此 全部工作都完成了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics