文章目录
1.1什么是Kernel Module?
内核模块是可以按需加载或卸载的内核代码,可以不重启系统就扩充内核的功能。例如,一种内核模块叫做设备驱动程序(Device driver),它允许内核访问连接到系统的硬件。如果没有内核模块,我们将必须构建整体内核,并将新功能直接添加到内核映像中。除了具有更大的内核之外,这还有一个缺点,即每次我们想要新功能时都要求我们重建并重新启动内核。
1.2关于Kernel的基本操作
- 显示当前装入的内核模块:
$ lsmod
- 显示模块信息:
$ modinfo module_name
- 显示所有模块的配置信息
$ modprobe -c | less
- 显示某个模块的配置信息:
$ modprobe -c | grep module_name
2.1 Hello World (part1): 最简单的Module
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
- 头文件中的
#include <linux/module.h> 和 #include <linux/kernel.h>
是所有module都需要添加的头文件。 - Kernel modules必须包含至少两个函数:初始化函数init_module() 和 结束函数cleanup_module()。
- 初始化函数init_module()作用是将module写入kernel。
- 结束函数cleanup_module()作用是清理内存结束module。
- printk()函数:用来记录信息或给出warning,该函数有8个优先级,其中第一个就是KERN_ALERT,如果你不写优先级会被默认为DEFAULT_MESSAGE_LOGLEVEL (在后文中会具体讲解printk函数)
2.2 编译Kernel Modules
- 编写makefile文件 (下文中讲解如何编写makefile文件)
- 在相应的文件目录下使用
make
命令来编译module,如果编译成功,则resulting module code 包含在 <module_name>.ko 文件中 - 通过输入
su -
变成superuser,因此你必须输入密码来操作下一步 - 通过输入
insmod <module_name>.ko
命令,将module插入到Kernel中,任何通过printk()函数将会显示在 /var/log/messages 文件中。查看输出的最好方法是通过输入tail -f /var/log/messages
命令。 - 你可以删除掉已存在的module,通过输入
rmmod <module_name>
命令。
2.2.1如何编写makefile文件
一个基础的makefile文件:
obj-m += hello-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
- 第一行
obj-m += hello-1.o
: 指定编译模块名称,会自动寻找hello-1.c make -C /lib/modules/$(shell uname -r)/build M=$(PWD)
表示C进入内核目录读取Makefile,M表明后回到当前目录读取Makefile
2.3 Hello World (part2) : 宏定义1⃣️
module_init()
和 module_exit()
The only caveat is that your init and cleanup functions must be defined before calling the macros, otherwise you’ll get compilation errors.
/*
* hello-2.c - Demonstrating the module_init() and module_exit() macros.
* This is preferred over using init_module() and cleanup_module().
*/
#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_2_init(void)
{
printk(KERN_INFO "Hello, world 2\n");
return 0;
}
static void __exit hello_2_exit(void)
{
printk(KERN_INFO "Goodbye, world 2\n");
}
module_init(hello_2_init); //宏定义
module_exit(hello_2_exit); //宏定义
2.4 Hello World (part3) : 宏定义2⃣️
还有一个__initdata与__init相似,但用于init变量而不是函数。
/*
* hello-3.c - Illustrating the __init, __initdata and __exit macros.
*/
#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 hello3_data __initdata = 3;
static int __init hello_3_init(void)
{
printk(KERN_INFO "Hello, world %d\n", hello3_data);
return 0;
}
static void __exit hello_3_exit(void)
{
printk(KERN_INFO "Goodbye, world 3\n");
}
module_init(hello_3_init);
module_exit(hello_3_exit);
2.5 传递参数到Module中
- step1: 声明头文件
#include<linux/moduleparam.h>
- step2: 创建参数
- step3:
module_param(name_var, type, permissions);
permissions有五种:
S_IRUSR 其中第一个R表示Reading,USR表示user
S_IWUSR 其中W表示Writing,USR表示user
S_IXUSR 其中X表示Execution,USR表示user
S_IWGRP 其中W表示Writing,GRP表示group
S_IRGRP 其中R表示Reading,GRP表示group
/*
* hello-5.c - Demonstrates command line argument passing to a module.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { -1, -1 };
static int arr_argc = 0;
/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non-zero) at a later stage.
*/
module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
module_param(mylong, long, S_IRUSR);
module_param(mystring, charp, 0000);
/*
* module_param_array(name, type, num, perm);
* The first param is the parameter's (in this case the array's) name
* The second param is the data type of the elements of the array
* The third argument is a pointer to the variable that will store the number
* of elements of the array initialized by the user at module loading time
* The fourth argument is the permission bits
*/
module_param_array(myintArray, int, &arr_argc, 0000);
static int __init hello_5_init(void)
{
int i;
printk(KERN_INFO "Hello, world 5\n=============\n");
printk(KERN_INFO "myshort is a short integer: %hd\n", myshort);
printk(KERN_INFO "myint is an integer: %d\n", myint);
printk(KERN_INFO "mylong is a long integer: %ld\n", mylong);
printk(KERN_INFO "mystring is a string: %s\n", mystring);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
{
printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]);
}
printk(KERN_INFO "got %d arguments for myintArray.\n", arr_argc);
return 0;
}
static void __exit hello_5_exit(void)
{
printk(KERN_INFO "Goodbye, world 5\n");
}
module_init(hello_5_init);
module_exit(hello_5_exit);