Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275907
  • 博文数量: 59
  • 博客积分: 235
  • 博客等级: 二等列兵
  • 技术积分: 409
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-12 15:11
文章分类
文章存档

2013年(4)

2012年(55)

分类: LINUX

2012-01-17 15:09:04

  参考 :
 
  编译内核模块需要源码,详细设置网上搜索,以下内容对上面网址内容的部分翻译。
  在 VMware ubuntu10.10 下验证通过。
 
1 vim 编写代码并保存 。 
  hello.c
 
  1.   1 #include <linux/init.h>
  2.   2 #include <linux/module.h>
  3.   3 MODULE_LICENSE("Dual BSD/GPL");
  4.   4
  5.   5 static int hello_init(void)
  6.   6 {
  7.   7 printk(KERN_ALERT "Hello,world\n");
  8.   8 return 0;
  9.   9 }
  10.  10
  11.  11 static void hello_exit(void)
  12.  12 {
  13.  13 printk(KERN_ALERT "Goodbye,Cruel world \n");
  14.  14 }
  15.  15
  16.  16 module_init(hello_init);
  17.  17 module_exit(hello_exit);
Makefile
 
     
  1.   1 obj-m = hello.o
  2.   2 KVERSION = $(shell uname -r)
  3.   3 all:
  4.   4 make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
  5.   5 clean:
  6.   6 make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

2 编译模块 make

3 加载模块 (需要root 权限)

# insmod hello.ko

4 检查模块是否加载

# lsmod | less

5 查看 /var/log/message 里面的信息

# tail -f /var/log/message

6 卸载模块

# rmmod hello

7 当 Linux 系统启动的时加载模块。 文件 /etc/modules 设置加载的内核,这个文件里包含了系统

启动的时候要加载的模块,每一个模块一行。 首先 把你的模块 拷贝到 /lib/modules/$(uname -r)/kernel/drivers. 下面是建议的步骤:

(a)为 hello 模块建立一个目录

#mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello

(b)拷贝模块

# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/

(c)编辑 /etc/modules 文件

#vim /etc/modules

(d)加上下面一行

hello

(e)重启查看是否加载。

#cat /proc/modules 

或者

#lsmod | less

 

 

 

 

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