1. Code
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int times=1;
module_param(times,int,S_IRUGO);
static int hello_init(void)
{
int i=0;
for(;i <times;i++)
printk(KERN_ALERT "hello,word/n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye,word/n");
}
module_init(hello_init);
module_exit(hello_exit);
2. Makefile
ifneq ($(KERNELRELEASE),)
obj-m :=hello.o
else
PWD :=$(shell pwd)
KEVER ?=$(shell uname -r)
#KDIR :=/usr/src/linux-2.6.23
KDIR :=/usr/src/kernels/2.6.23.1-42.fc8-i686
all:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions *.order *symvers
endif
3. 遇到的问题:
insmod: error inserting 'hello.ko': -1 Invalid module format
解决方法: 如下的版本号必须匹配,修改Makefile中的KDIR为系统匹配的linux内核树路径
/usr/src/linux-2.6.23/include/linux/utsrelease.h
[root@localhost ~]# modinfo hello.ko
filename: hello.ko
license: GPL
depends:
vermagic: 2.6.23 SMP mod_unload 686 4KSTACKS
parm: times:int
[root@localhost ~]# uname -r
2.6.23.1-42.fc8