2007年9月12日星期三

linux内核模块加载

linux内核模块加载
1,一个简单的驱动程序
//hello.c
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include
#include
#include
int hello(void)
{
printk("This is a module,hello PeiJun!\n");
return 0;
}
void bye(void)
{
printk("Bye-bye!\n");
}
module_init(hello);
module_exit(bye);
2, 编写Makefile
obj-m := hello.o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -f hello.ko hello.mod hello.mod.o hello.o
3,执行make后,生成hello.ko hello.mod hello.mod.o hello.o
4, 执行insmod hello.o
.....
insmod: erro inserting 'hello.o' : -1 Invalid module format
2.4 不会生成这些文件,只有2.6才会生成hello.ko文件,用*.ko后缀来区分内核模块与一般的目标文件,在2.4下也是在x window的终端gnome终端口加载这样的模块,printk可以正常打印,但到了2.6就不行了.文本模式下不行,主要是printk优先级的问 题.内核的优先级定义可以在linux/kernel.h和kernel/printk.c中看.
5,执行insmod hello.ko
显示 This is a module,hello PeiJun!
6,lsmod查看
Module Size Used by
hello 1536 0
7,执行rmmod hello
显示: Bye-bye!

没有评论: