源文件:
// hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void){ printk(KERN_ALERT "hello, world, init\n"); return 0; } static void hello_exit(void){ printk(KERN_ALERT "goodbye, exit\n"); } module_init(hello_init); module_exit(hello_exit);
# Makefile obj-m = hello.o KVERSION = $(shell uname -r) all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
[winlin@dev6 helloworld]$ make make -C /lib/modules/2.6.32-71.el6.x86_64/build M=/home/winlin/smartsystem/trunk/smart-server/research/kdd/helloworld modules make[1]: Entering directory `/usr/src/kernels/2.6.32-71.el6.x86_64' CC [M] /home/winlin/smartsystem/trunk/smart-server/research/kdd/helloworld/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/winlin/smartsystem/trunk/smart-server/research/kdd/helloworld/hello.mod.o LD [M] /home/winlin/smartsystem/trunk/smart-server/research/kdd/helloworld/hello.ko.unsigned NO SIGN [M] /home/winlin/smartsystem/trunk/smart-server/research/kdd/helloworld/hello.ko make[1]: Leaving directory `/usr/src/kernels/2.6.32-71.el6.x86_64'
[winlin@dev6 helloworld]$ ll total 344 -rw-rw-r-- 1 winlin winlin 312 May 28 09:55 hello.c -rw-rw-r-- 1 winlin winlin 110242 May 28 09:55 hello.ko -rw-rw-r-- 1 winlin winlin 110242 May 28 09:55 hello.ko.unsigned -rw-rw-r-- 1 winlin winlin 690 May 28 09:55 hello.mod.c -rw-rw-r-- 1 winlin winlin 60360 May 28 09:55 hello.mod.o -rw-rw-r-- 1 winlin winlin 51423 May 28 09:55 hello.o -rw-rw-r-- 1 winlin winlin 180 May 28 09:55 Makefile -rw-rw-r-- 1 winlin winlin 84 May 28 09:57 modules.order -rw-rw-r-- 1 winlin winlin 0 May 28 09:35 Module.symvers
[winlin@dev6 helloworld]$ /sbin/lsmod|grep hello [winlin@dev6 helloworld]$ sudo /sbin/insmod ./hello.ko [winlin@dev6 helloworld]$ /sbin/lsmod|grep hello hello 928 0
[winlin@dev6 helloworld]$ sudo tailf /var/log/messages May 28 09:58:11 dev6 kernel: hello, world, init
[winlin@dev6 helloworld]$ sudo /sbin/rmmod hello.ko [winlin@dev6 helloworld]$ /sbin/lsmod|grep hello [winlin@dev6 helloworld]$ sudo tailf /var/log/messages May 28 09:59:18 dev6 kernel: goodbye, exit