测试环境CPU架构:x86_64
操作系统版本:openEuler 22.03 LTS
内核版本:5.10
测试代码
// module_param_test.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_AUTHOR("HYQ");
MODULE_DESCRIPTION("module param test!");
MODULE_LICENSE("GPL");
static bool bool_param = true;
module_param(bool_param, bool, 0644);
MODULE_PARM_DESC(bool_param, "this is bool test!");
static u_char byte_param = 'a';
module_param(byte_param, byte, 0644);
MODULE_PARM_DESC(byte_param, "this is byte test!");
static uint hexint_param = 0xffffffff;
module_param(hexint_param, hexint, 0644);
MODULE_PARM_DESC(hexint_param, "this is hexint test!");
static short short_param = -15;
module_param(short_param, short, 0644);
MODULE_PARM_DESC(short_param, "this is short test!");
static unsigned short ushort_param = 15;
module_param(ushort_param, ushort, 0644);
MODULE_PARM_DESC(ushort_param, "this is ushort test!");
static int int_param = -150;
module_param(int_param, int, 0644);
MODULE_PARM_DESC(int_param, "this is int test!");
static unsigned int uint_param = 150;
module_param(uint_param, uint, 0644);
MODULE_PARM_DESC(uint_param, "this is uint test!");
static long long_param = -1500;
module_param(long_param, long, 0644);
MODULE_PARM_DESC(long_param, "this is long test!");
static unsigned long ulong_param = 1500;
module_param(ulong_param, ulong, 0644);
MODULE_PARM_DESC(ulong_param, "this is ulong test!");
static int __init module_param_test_init(void)
{
printk(KERN_INFO "bool_param = %d\n", bool_param);
printk(KERN_INFO "byte_param = %d\n", byte_param);
printk(KERN_INFO "hexint_param = %d\n", hexint_param);
printk(KERN_INFO "short_param = %d\n", short_param);
printk(KERN_INFO "ushort_param = %d\n", ushort_param);
printk(KERN_INFO "int_param = %d\n", int_param);
printk(KERN_INFO "uint_param = %d\n", uint_param);
printk(KERN_INFO "long_param = %ld\n", long_param);
printk(KERN_INFO "ulong_param = %ld\n", ulong_param);
return 0;
}
static void __exit module_param_test_exit(void)
{
printk(KERN_INFO "module exit!");
}
module_init(module_param_test_init);
module_exit(module_param_test_exit);
Makefile:
obj-m+=module_param_test.o
CONFIG_MODULE_SIG=n
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
加载模块
insmod module_param_test.ko
内核日志输出:
查看内核模块信息
modinfo module_param_test.ko
卸载模块
rmmod module_param_test.ko
加载内核模块时传入参数int_param
从内核日志输出可以看到该参数的值已经被设置为1000,而不是原来默认的-150。如下图所示:
传递多个参数只要在加载模块后用"参数名=参数值"的方式添加即可,如下图所示: