//hellomod.c
//hello world driver for Linux 2.6
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
//模块的初始化函数
static int __init lkp_init(void)
{
printk("<1>Hello,World! from kernel space \n"); //<1> output level,print now
return 0;
}
//模块的退出和清理函数
static void __exit lkp_cleanup(void)
{
printk("<1>GoodBye,World! leaving kernel space \n");
}
//向内核注册模块所提供的新功能
module_init(lkp_init);
//注销由模块提供的所有功能
module_exit(lkp_cleanup);
MODULE_LICENSE("GPL");
//Makefile
# Makefile2.6
obj-m += hellomod.o # 产生hellomod 模块的目标文件
KERNELDIR := /usr/src/kernels/2.6.27.5-117.fc10.i686/
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions #清理
//输出
[root@colorfulgreen hellomod]# make
make -C /usr/src/kernels/2.6.27.5-117.fc10.i686/ M=/home/green/src/hellomod modules
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
CC [M] /home/green/src/hellomod/hellomod.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/green/src/hellomod/hellomod.mod.o
LD [M] /home/green/src/hellomod/hellomod.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
[root@colorfulgreen hellomod]# ls
hellomod.c hellomod.mod.c Makefile modules.order
hellomod.c~ hellomod.mod.o Makefile~ Module.symvers
hellomod.ko hellomod.o Module.markers
[root@colorfulgreen hellomod]# insmod hellomod.ko
[root@colorfulgreen hellomod]# lsmod |grep hello
hellomod 5376 0
[root@colorfulgreen hellomod]# rmmod hellomod
[root@colorfulgreen hellomod]# dmesg
Hello,World! from kernel space
GoodBye,World! leaving kernel space