#include<linux/init.h>
#include<linux/module.h>
int hello_init(void) //模块初始化函数
{
//TODO....
printk("hello, kernel/n");
return 0;
}
void hello_exit(void) //模块退出函数
{
//TODO....
printk("exit, kernel/n");
}
module_init(hello_init); //加载模块
module_exit(hello_exit); //卸载模块
---------------------------------
/*Makefile*/
ifeq ($(KERNELRELEASE),)
KERNELDIR ?=/usr/src/kernels/2.6.18-164.el5-i686 #指向你的内核代码目录
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules #编译内核模块的指令
clean:
rm -rf modules.* *.o *~ core .depend .*.cmd *.ko
rm -rf *.mod.c .tmp_versions Module*
.PHONY: modules modules_install clean
else
obj-m := hello.o #你要编译的模块名称
endif
-------------------------------------------
用make命令生成xxxx.ko文件
装载模块:insmod xxxx.ko
查看模块:lsmod | grep xxxx
卸载模块:rmmod xxxx