测试的hello.c内核模块
novell@sles11sp3:~/tmp> ls hello.c Makefile
hello.c Makefilenovell@sles11sp3:~/tmp> cat hello.c Makefile
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
obj-m := hello.o
novell@sles11sp3:~/tmp> cat 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\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
准备build目录
novell@sles11sp3:~> mkdir kernel
novell@sles11sp3:~> cd kernel
novell@sles11sp3:~kernel>make -C /usr/src/linux O=$(pwd) cloneconfig
编译这个模块所用的Makefile
novell@sles11sp3:~/tmp> cat Makefile
obj-m := hello.o编译命令
novell@sles11sp3:~/tmp> make -C ~/kernel/ M=$(pwd) modules
make: Entering directory `/home/novell/kernel'
make -C /usr/src/linux-3.0.76-0.11 O=/home/novell/kernel/. modules
Building modules, stage 2.
MODPOST 1 modules
make: Leaving directory `/home/novell/kernel'
novell@sles11sp3:~/tmp> ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symvers
模块加载及卸载
novell@sles11sp3:~/tmp> sudo /sbin/insmod hello.ko
novell@sles11sp3:~/tmp> sudo tail -n5 /var/log/messages
Sep 16 15:34:54 sles11sp3 kernel: [21336.059008] Hello, world
Sep 16 15:35:15 sles11sp3 sudo: novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/usr/bin/tail -n5 /var/log/messages
novell@sles11sp3:~/tmp> sudo /sbin/rmmod hello.ko
novell@sles11sp3:~/tmp> sudo tail -n3 /var/log/messagesSep 16 15:35:28 sles11sp3 sudo: novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/sbin/rmmod hello.ko
Sep 16 15:35:28 sles11sp3 kernel: [21369.558324] Goodbye, cruel world
Sep 16 15:35:32 sles11sp3 sudo: novell : TTY=pts/1 ; PWD=/home/novell/tmp ; USER=root ; COMMAND=/usr/bin/tail -n3 /var/log/messages
novell@sles11sp3:~/tmp>