如果我们需要修改内核,该怎么做呢?我们需要将代码以模块的方式添加到内核中去运行。本文就介绍最简单的模块编写和加载方法。
我们还是从最简单的hello模块介绍。
step 1:创建一个目录hello
step 2:在hello中编写hello.c如下:
#include <linux/kernel.h>
#include <linux/init.h>#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("Mchgloak<mchgloak11520@gmail.com>");
static int __init hello_init(void){printk(KERN_INFO"hello init!\n");return 0;}
static void __exit hello_exit(void){printk(KERN_INFO"hello exit!\n");}
module_init(hello_init);module_exit(hello_exit);
hello.c 解释:
(1)static int __init hello_init(void)就是模块的加载函数,一般以__init标识声明,包含了要编译的代码、初始化(包括诸如申请内存、硬件资源)等一些内容。
模块加载函数必须以“