1、代码
[root@proEnv29 test2]# cat hello.c
[root@proEnv29 test2]#
2、Makefile
[root@proEnv29 test2]# cat Makefile
[root@proEnv29 test2]# 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);
[root@proEnv29 test2]#
2、Makefile
[root@proEnv29 test2]# cat Makefile
CONFIG_MODULE_SIG=n
#如果已经定义KERNELRELEASE,则说明是从内核构造系统调用的,
#因此可利用其内建语句。
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
#否则,是直接从命令行调用,
#这时要调用内核构造系统。
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -f *.o *.ko *.mod.c .hello*
[root@proEnv29 test2]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symvers
3、编译
[root@proEnv29 test2]# make
make -C /lib/modules/3.10.0-327.36.2.el7.x86_64/build M=/home/maokx/deviceDrivers/test2 modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'
CC [M] /home/maokx/deviceDrivers/test2/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/maokx/deviceDrivers/test2/hello.mod.o
LD [M] /home/maokx/deviceDrivers/test2/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'
4、加载与卸载
3、编译
[root@proEnv29 test2]# make
make -C /lib/modules/3.10.0-327.36.2.el7.x86_64/build M=/home/maokx/deviceDrivers/test2 modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'
CC [M] /home/maokx/deviceDrivers/test2/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/maokx/deviceDrivers/test2/hello.mod.o
LD [M] /home/maokx/deviceDrivers/test2/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'
4、加载与卸载
[root@proEnv29 test2]# insmod hello.ko
[root@proEnv29 test2]# modinfo hello
modinfo: ERROR: Module hello not found.
[root@proEnv29 test2]# lsmod|grep hello
hello 12496 0
[root@proEnv29 test2]# modinfo hello.ko
filename: /home/maokx/deviceDrivers/test2/hello.ko
license: Dual BSD/GPL
rhelversion: 7.2
srcversion: 08163D92C9B5B3FB43ECECA
depends:
vermagic: 3.10.0 SMP mod_unload modversions
[root@proEnv29 test2]# rmmod hello
[root@proEnv29 test2]#
[root@proEnv29 ~]# > /var/log/messages
[root@proEnv29 ~]# tail -f /var/log/messages
Jul 4 15:10:26 proEnv29 kernel: Hello,world
Jul 4 15:11:58 proEnv29 kernel: Goodbye,cruel world
^C
[root@proEnv29 ~]#