Chinaunix首页 | 论坛 | 博客
  • 博客访问: 649825
  • 博文数量: 160
  • 博客积分: 2384
  • 博客等级: 大尉
  • 技术积分: 1366
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 11:35
文章分类
文章存档

2015年(45)

2014年(36)

2012年(28)

2011年(37)

2010年(2)

2009年(10)

2008年(2)

分类: LINUX

2012-11-12 12:20:38

首先是要重新编译内核,构建linux内核树。我完全是小白一个,网上搜了很多方案,尝试了好些次,终于成功。步骤如下:

1.安装编译内核所需要的软件

有:build-essential、autoconf、automake、cvs、subversion


$apt-get install build-essential kernel-package libncurses5-dev
libncurses5这个软件包在使用menuconfig配置内核的时候会用到。

2.下载内核源码

先查看linux内核版本:$uname -r

然后用apt-cache search linux-source命令, 会列出一些可选源码包,对准你的内核版本号,选择“with patche”的那个

最后用apt-get install linux-source-2.6.32下载之。解压缩源码包,进入解压后的源码目录。

在编译之前我们需要原来内核的一个配置文件,这是我/usr/src目录下的文件预览:ls -al

drwxr-xr-x  4 root root     4096 2010-09-04 21:31 fglrx-8.723.1
drwxr-xr-x 24 root root     4096 2010-09-04 20:35 linux-headers-2.6.32-25
drwxr-xr-x  7 root root     4096 2010-09-04 20:35 linux-headers-2.6.32-25-generic
drwxr-xr-x 25 root root     4096 2010-09-16 21:39 linux-source-2.6.32
-rw-r--r--  1 root root 65846876 2010-09-01 22:41 linux-source-2.6.32.tar.bz2

现在我们需要linux-headers-2.6.32-25-generic目录下的.config文件,我们把它拷贝到我们刚下好解压的目录,也就是linux-source-2.6.32

sudo cp /usr/src/linux-headers-2.6.32-25-generic/.config /usr/src/linux-2.6.32

接下来切换到root用户
sudo -i

cd /usr/src/linux-2.6.32

make menuconfig

终端会弹出一个配置界面

最后有两项:load a alternative kernel configuration...
save a alternative configuration...
选择load a kernel configuration保存,然后在选择save akernel configuration再保存退出,并退出配置环境。

接下来我们就要开始编译了。

#cd /usr/src/linux-2.6.32
#make
记住一定要是管理员帐号运行,这个过程很久,如果你的cpu是双核的可以在make后面加个参数,make -j4.

#make bzImage 执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。
#make modules /* 编译 模块 */

#make modules_install  这条命令能在/lib/modules目录下产生一个目录

如果一切顺利,编译过程中不出现什么错误的话,接下来我们就可以开始linux模块的helloworld了。

我在 /home/xxxx/linux_modules/ 目录下创建2个文本文件 hello.c Makefile

 //hello.c
#include
#include
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.32-21-generic/build
PWD := $(shell pwd)
modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)

需要注意的是makefile的格式$(MAKE)前面要加个tab.
make 编译,不出现错误的话,用ls -al查看linux_modules目录下产生了如下文件:
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers

其中hello.ko就是可加载的模块文件

现在我们就可以将编译好的模块helloworld加载到内核中去了

#insmod ./hello.ko   //这个命令把hello.ko加载到内核

#lsmod|grep hello   //lsmod 这个命令可以查看当前所有的驱动模块,结果应该显示hello 680 0

#rmmod hello         //这个命令是把hello这个模块移除掉

程序的输出结果可以在
/var/log/syslog文件中查看

Hello,World
Goodbye,cruel world

这是程序的输出。


阅读(2447) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~