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