什么是内核树?刚开始我也没弄明白,通过这几天的学习,有所感悟,就说说我的理解吧!从形式上看,内核树与内核源码的目录结构形式是相同的,都是由各个层次的文件目录结构组成,但是其中的具体内容肯定是不同的。从功能上看,内核树中含有编写或编译Kernel程序或驱动时所需要的一些内核函数库以及定义好的一些数据结构,数据类型等,说白了,当你在进行kernel编程或者是编写linux设备驱动时都需要用到这里面的一些资源,并且我们写好的驱动程序时,也需要用到内核树来编译,因此从这个角度上来看,内核树也可以看作是一个编译器,很显然,内核相关程序的编译与应用层的编译过程是不同的。

 解释了内核树后,下面就来看看如何建立自己的内核树吧!

1确认你当前使用ubuntu系统的内核版本

  当模块代码要链接至不同版本的内核时,必须要做的就是对这个模块代码重新进行编译。这是因为模块对某一版本内核中定义的一些数据结构以及函数原型等都具有很强的信赖性,而模块所访问的内核接口因内核版本的不同而不同。所以在建立自己当前的内核树前一定要查看当前PC上使用的的linux内核版本,然后再建立针对此版本的内核树。如果版本不对会出现一些错误,如当前我的系统内核版本为:

2.6.32-21-generic

但是我用其它版本的内核源码来建立内核树,那么会导致在我当前内核版本(即2.6.32-21-generic下)执行insmod *.ko操作时会发生如下错误:

Error inserting './hello.ko': -1 Invalid module format

切记!~~~

2如在系统的Terminal中输入:

zhm@o_o:/lib/modules$ uname -r 

响应:

2.6.32-21-generic   //这是显示当前系统所使用的内核版本

3下载针对此版本的内核源码,在终端中输入:

zhm@o_o:/lib/modules$  apt-cache search linux-source (注意,此时不能以root身份操作)

响应:

linux-source - Linux kernel source with Ubuntu patches

linux-source-2.6.32 - Linux kernel source for version 2.6.32 with Ubuntu patches

紧接着下载此源码包即可:

zhm@o_o:/lib/modules$ sudo apt-get install linux-source-2.6.32

于是我们得到: linux-source-2.6.32.tar.bz2

4解压

将linux-source-2.6.32.tar.bz2 内核源码在/usr/src中解压:

zhm@o_o: /usr/src/$ sudo tar jxvf linux-source-2.6.32.tar.bz2

然后进入其目录

zhm@o_o:/usr/src$   cd  linux-source-2.6.32

zhm@o_o:/usr/src/linux-source-2.6.32$

5配置编译内核

我采用的是最快速的配置(即默认)的方式:

zhm@o_o:/usr/src/linux-source-2.6.32$sudo make oldconfig

上面步骤完成后,开始进行make, 此步要花很长时间,你可以去干其它事情了,一般要1-2小时吧

zhm@o_o:/usr/src/linux-source-2.6.32$sudo make

执行结束后会在当前文件夹下生成一个vmlinux的文件, 其属性为-rwxr-xr-x

然后编译模块:

zhm@o_o:/usr/src/linux-source-2.6.32$sudo make modules

最后一步,安装模块:

zhm@o_o:/usr/src/linux-source-2.6.32$sudo make modules_install

执行成功后,紧接着会在/lib/modules下生成一个2.6.32-21-generic文件夹,文件夹中含有build及source文件,这两个文件就是我们在编译模块时在Makefile中需要用到的,它们实际上是链接文件,指向我们在建立内核树时的那个目录:/usr/src//linux-source-2.6.32,实际上这个目录也就是我们要建立的内核树了,如下所示:

zhm@o_o:/lib/modules/2.6.32-21-generic$ ls -la

总用量 3712

drwxr-xr-x 3 root root   4096 2010-09-29 14:12 .

drwxr-xr-x 5 root root   4096 2010-09-29 14:09 ..

lrwxrwxrwx 1 root root     24 2010-09-29 14:09 b uild -> /usr/src/linux-source-2.6.32 

drwxr-xr-x 9 root root   4096 2010-09-29 14:11 kernel

-rw-r--r-- 1 root root 599140 2010-09-29 14:12 modules.alias

-rw-r--r-- 1 root root 576280 2010-09-29 14:12 modules.alias.bin

-rw-r--r-- 1 root root     69 2010-09-29 14:12 modules.ccwmap

-rw-r--r-- 1 root root 269377 2010-09-29 14:12 modules.dep

-rw-r--r-- 1 root root 396085 2010-09-29 14:12 modules.dep.bin

-rw-r--r-- 1 root root   1405 2010-09-29 14:12 modules.ieee1394map

-rw-r--r-- 1 root root    218 2010-09-29 14:12 modules.inputmap

-rw-r--r-- 1 root root  24732 2010-09-29 14:12 modules.isapnpmap

-rw-r--r-- 1 root root     74 2010-09-29 14:12 modules.ofmap

-rw-r--r-- 1 root root 103687 2010-09-29 14:09 modules.order

-rw-r--r-- 1 root root 405322 2010-09-29 14:12 modules.pcimap

-rw-r--r-- 1 root root   1597 2010-09-29 14:12 modules.seriomap

-rw-r--r-- 1 root root 218944 2010-09-29 14:12 modules.symbols

-rw-r--r-- 1 root root 284287 2010-09-29 14:12 modules.symbols.bin

-rw-r--r-- 1 root root 870799 2010-09-29 14:12 modules.usbmap

l r wxrwxrwx 1 root root     24 2010-09-29 14:09 source -> /usr/src/linux-source-2.6.3 2

后面的操作就简单了,在Makefile文件中指定编译时内核树路径即可,下面为一段简单的Makefile源码:

# If KERNELRELEASE is defined, we've been invoked from the

# kernel build system and can use its language.



ifneq ($(KERNELRELEASE),)



    obj-m := hello.o 



# Otherwise we were called directly from the command

# line; invoke the kernel build system.



else

    KERNELDIR ?= /lib/modules/$(shell uname -r)/build

    PWD  := $(shell pwd)



default:

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



endif

后面的事就简单了,写好模块代码,写好Makefile,直接在Terminal输make就可以编译成*.ko文件了。

写一个 最简单 最没用的驱动吧 

我在 /home/shana/linux_q/ 目录下创建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.20/build 

PWD := $(shell pwd) 



modules: 

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



modules_install: 

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



如果以上你都完成了在 make 时出现这样的错误 



shana@shana:~/linux_驱动开发$ make 

make: 没有什么可以做的为 `modules'。 



原因很简单 你肯定是从我这直接复制的吧~~~呵呵,Makefile格式错误啦~ 

解决办法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 移动到行首 然后按Tab 键自动对齐 

这时里边的变量都变成绿色了~然后在 make 吧 



shana@shana:~/linux_驱动开发$ make 

make -C /lib/modules/2.6.22-14-generic/build M=/home/shana/linux_驱动开发 modules 

make[1]: Entering directory `/usr/src/linux-headers-2.6.22-14-generic' 

CC [M] /home/shana/linux_驱动开发/hello.o 

Building modules, stage 2. 

MODPOST 1 modules 

CC /home/shana/linux_驱动开发/hello.mod.o 

LD [M] /home/shana/linux_驱动开发/hello.ko 

make[1]: Leaving directory `/usr/src/linux-headers-2.6.22-14-generic' 

shana@shana:~/linux_驱动开发$ 





shana@shana:~/linux_驱动开发$ ls -l 

总用量 124 

-rw-r--r-- 1 shana shana 303 2008-03-16 10:43 hello.c 

-rw-r--r-- 1 shana shana 49039 2008-03-16 12:11 hello.ko 

-rw-r--r-- 1 shana shana 687 2008-03-16 12:11 hello.mod.c 

-rw-r--r-- 1 shana shana 25840 2008-03-16 12:11 hello.mod.o 

-rw-r--r-- 1 shana shana 24360 2008-03-16 12:11 hello.o 

-rw-r--r-- 1 shana shana 8344 2008-03-16 09:17 linux_qudong_qu.txt 

-rw-r--r-- 1 shana shana 266 2008-03-16 12:09 Makefile 

-rw-r--r-- 1 shana shana 0 2008-03-16 12:11 Module.symvers 

shana@shana:~/linux_驱动开发$ 





然后加载模块 (超级用户) 



root@shana:/home/shana/linux_驱动开发# insmod ./hello.ko 



按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现 (原因不解) 



root@shana:/home/shana/linux_驱动开发# insmod ./hello.ko 

root@shana:/home/shana/linux_驱动开发# 



查看加载模块 



root@shana:/home/shana/linux_驱动开发# lsmod 

Module Size Used by 

hello 2560 0 



已经加载上咯~~ 



删除模块 



root@shana:/home/shana/linux_驱动开发# rmmod hello 

root@shana:/home/shana/linux_驱动开发# 



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



shana@shana:~/linux_驱动开发$ cat /var/log/syslog |grep world 

Mar 16 12:14:53 shana kernel: [ 5937.529297] Hello, world 

Mar 16 12:16:05 shana kernel: [ 6009.439036] Goodbye, cruel world 

shana@shana:~/linux_驱动开发$ 



至此 全部工作都完成了。是否对你有用呢?

From:

http://forum.ubuntu.org.cn/viewtopic.php?t=109762

http://blog.csdn.net/ab198604/article/details/5914203

Ubuntu编译内核树的更多相关文章

  1. linux 解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题

    解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题 http://www.lin ...

  2. Ubuntu 编译安装 Linux 4.0.5 内核,并修复 vmware 网络内核模块编译错误

    . . . . . 今天把 Ubuntu 14.04 升级到了最新的 4.0.5 的内核版本,本来不打算记录下来的,但是升级的过程中确实遇到了一些问题,所以还是记录下来,分享给遇到同样问题的猿友. 先 ...

  3. 【基于mini2440开发板的交叉编译环境及内核树配置.

    在学习linux驱动开发过程中,交叉编译环境的配置及内核树的生成无疑是对linux不是十分了解的新人面前的一堵墙.高高大大的墙...笔者在初探这一方向时,就在这2个问题上苦恼了很久.查阅无数资料,大多 ...

  4. Linux内核树的建立-基于ubuntu系统

    刚看 O'REILLY 写的<LINUX 设备驱动程序>时.作者一再强调在编写驱动程序时必须 建立内核树.先前的内核只需要有一套内核头文件就够了,但因为2.6的内核模块吆喝内核源码树中的目 ...

  5. Ubuntu下编译内核

    一.下载源代码和编译软件的准备 下载内核源代码:http://www.kernel.org/ 注意,点击2.6.25内核的F版,即完整版. 如果你懒得去网站点联接,运行下列命令:  代码: $cd ~ ...

  6. ubuntu 下编译内核

    目的: 1. 练习.网上有很多类似的文章可供参考. 2. 为写qemu的watchdog驱动练手. 有朋友问make的 watchdog驱动 需要什么准备,所以写这个blog. 环境: ubuntu ...

  7. 使用Ubuntu编译Linux内核

    1.下载内核并解压到 /usr/src 目录下 在终端执行以下命令即可下载 4.16.14版本(目前最新的稳定版)的内核到当前shell打开的目录下 wget https://cdn.kernel.o ...

  8. ubuntu下内核源码树的建立

    参考的博文: http://www.360doc.com/content/12/0604/12/8890849_215794364.shtml http://www.cnblogs.com/pd520 ...

  9. Linux下编译内核配置选项简介

    Code maturity level options代码成熟度选项 Prompt for development and/or incomplete code/drivers 显示尚在开发中或尚未完 ...

随机推荐

  1. 数组循环左移 i 位

    数组左移 i 位 3 种方法 1.临时数组存储 先将前 i 个元素用数组存起来 再将后 n - i 个元素左移 i 位 最后将存起来的数组添加到后面去即可 2.通过多次调用左移 1 位的函数 3.翻转 ...

  2. 15-oauth2+oidc实现Server部分

    1-我们使用之前项目的mvcCookieAuthSampe2进行改造 1.1  增加IdentityServer4 2-增加Config.cs文件,对IdentityServer提供相关的配置数据 u ...

  3. 分布式存储系统Kudu与HBase的简要分析与对比

    本文来自网易云社区 作者:闽涛 背景 Cloudera在2016年发布了新型的分布式存储系统——kudu,kudu目前也是apache下面的开源项目.Hadoop生态圈中的技术繁多,HDFS作为底层数 ...

  4. Git中分支merge和rebase的适用场景及区别

    Git merge是用来合并两个分支的. git merge b      # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 原理 如下: 假设你现在基于远程分 ...

  5. 北京Uber优步司机奖励政策(3月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 优步北京B组奖励政策

    用户组:优步北京B组(2015年7月20日前激活的部分司机) 更新日期:2015年8月4日 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最 ...

  7. dvs-panotracking编译运行

    编译运行dvs-panotracking > 编译dvs-panotracking之前首先需要安装imageutilities . 源码下载 https://github.com/VLOGrou ...

  8. Python 获取windows管理员权限办法

    from __future__ import print_function import ctypes, sys, os def is_admin(): try: return ctypes.wind ...

  9. convert-Csharp-DateTime-Ticks-to-js

    <!DOCTYPE html> <html> <head> <script> function myFunction() { var b = forma ...

  10. 初学Direct X(10)—— D3D基础预备知识

    初学Direct X(10) -- D3D基础预备知识 1. 像素格式 D3DFMT_X8R8G8B8(F) X:未加使用 8:8位用于显示 B:用于显示蓝色 F:浮点像素类型 以下三个较为常用,使用 ...