Makefile:
ifneq ($(KERNELRELEASE),)
obj-m := kernelModule.o
obj-m := kernelModulePara.o
else
KERNELDIR ?= /lib/module/${shell uname -r}/build
PWD := ${shell pwd}
default:
${MAKE} -C ${KERNELDIR} M=${PWD} modules
endif
------------------------------------------------------------
加载:sudo modprobe ./kernelModule 或 sudo insmod ./kernelModule
------------------------------------------------------------
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL"); //set kernel module copyright protocol
MODULE_AUTHOR("xiexiaozhang"); //set kernel module author
//init function
static int __init hello_init(void) //module initialize function
{
//notice:can not call lib's function
printk(KERNEL_ALERT "(init)Initializatin information..."); //print a message
return 0;
}
//exit function
static void __exit hello_exit(void) //module exit clear function
{
printk(KERNEL_ALERT "(exit)Exiting information...");
}
module_init(hello_init); //set module initialize function
module_exit(hello_exit); //set module exit clear function