How to allow parameters to be passed during insmod of loadable kernel module notes Basically you can use module_param (3).
Please refer to the following for how to build the kernel module.
[Linux] [kernel module] Build and load a simple loadable kernel module-Qiita
testmod.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
MODULE_LICENSE("MIT");
static int param1 = 1;
static int param2 = 2;
module_param(param1, int, S_IRUGO);
module_param(param2, int, S_IRUGO);
static int testmod_init(void)
{
printk(KERN_INFO "driver loaded\n");
printk(KERN_INFO "param1 = %d\n", param1);
printk(KERN_INFO "param2 = %d\n", param2);
return 0;
}
static void testmod_exit(void)
{
printk(KERN_INFO "driver unloaded\n");
}
module_init(testmod_init);
module_exit(testmod_exit);
Do something like module_param (param1, int, S_IRUGO);
.
S_IRUGO | S_IWUSR
.There is also a module_param_array (4)
that can handle array parameters.
TODO: Sample creation
Execution example (no parameters)
$ sudo insmod testmod.ko
$ sudo rmmod testmod.ko
$ dmesg | tail
[109428.165469] driver loaded
[109428.165472] param1 = 1
[109428.165474] param2 = 2
[109428.173077] driver unloaded
Execution example (with parameters)
$ sudo insmod testmod.ko param1=10 param2=20
$ sudo rmmod testmod.ko
$ dmesg | tail
[109428.165469] driver loaded
[109428.165472] param1 = 10
[109428.165474] param2 = 20
[109428.173077] driver unloaded
[Linux] [kernel module] Build and load a simple loadable kernel module-Qiita
"Linux Device Driver 3rd Edition" 2.8 Module Parameters p.35
Recommended Posts