#include<linux/init.h>#include<linux/module.h>#include<linux/kernel.h>MODULE_LICENSE("GPL");/*许可证*/MODULE_AUTHOR("hbw");/*作者*/MODULE_DESCRIPTION("A simple test for kernel module write.");/*描述*/MODULE_VERSION("0.1");/*版本*/staticint __init kmw_test_init(void){/*初始化函数*/printk(KERN_INFO "Hello, my module!\n");return0;}/*此处可以定义自己的模块功能*/staticvoid __exit kmw_test_exit(void){/*移除函数*/printk(KERN_INFO "Goodbye, my module!\n");}module_init(kmw_test_init);/*将定义的初始化函数以模块形式加载到内核*/module_exit(kmw_test_exit);/*将定义的移除函数以模块形式加载到内核*/
Makefile
ifeq ($(KERNELRELEASE),)
# The linux kernel directory
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# The current directory
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.mod modules.order *.symvers
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
CONFIG_MODULE_SIG = n
obj-m := kmw_test.o
endif