1、下载源码
参考:https://wiki.centos.org/zh/HowTos/I_need_the_Kernel_Source
yum install kernel-devel
2、编写内核模块代码
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye,hello world\n");
}
module_init(hello_start);
module_exit(hello_end);
3、编写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