Linux 内核模块载入参数

| 2022-06-18 11:51:03

linux 内核模块可以使用 module_param API声明一些参数,在载入时由用户填写

module_param

module_param(name, type, prem)

name 变量名,需要事先定义 type 类型的同名变量

type 类型 int、char 等

prem 类似 linux 的文件权限

示例

+ // 定义变量
+ static int debug = 0;

+ static int __init module_init(void) {
+     if (debug == 1) {
+         printk("fried_chicken module init with debug");
+     } 
+     return 0;
+ }

+ // 注册 int 类型参数 debug 
+ module_param(debug, int, S_IRUGO);

使用

insmod fried_chicken.ko debug=1