[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module

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

Source

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);

About module_param (3)

Do something like module_param (param1, int, S_IRUGO);.

About the types that can be used

Array parameters (module_param_array (4))

There is also a module_param_array (4) that can handle array parameters. TODO: Sample creation

Execution example

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

Relation

[Linux] [kernel module] Build and load a simple loadable kernel module-Qiita

reference

"Linux Device Driver 3rd Edition" 2.8 Module Parameters p.35

Recommended Posts

[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module
How to manage arguments when implementing a Python script as a command line tool
[Linux] [kernel module] Build and load a simple loadable kernel module
How to import NoteBook as a module in Jupyter (IPython)
How to import NoteBook as a module in Jupyter (IPython)
How to pass arguments to a Python script in SPSS Modeler Batch
How to set Jupytext nicely when managing code as a team
How to put the string JSON passed as a parameter into the dictionary when writing Ansible module
When running a Python shell from Electron, pass multiple arguments to run Python.
[Python / Tkinter] How to pass arguments to command
How to disguise a ZIP file as a PNG file
Let's understand how to pass arguments (Python version)
How to remember when you forget a word
How to uninstall a module installed using setup.py
How to pass arguments when invoking python script from blender on the command line
How to execute sleep command, command that takes time as a test [bash, linux, mac]
How to install Linux on a 32bit UEFI PC
How to display DataFrame as a table in Markdown
How to create a local repository for Linux OS
Execute Python function from Powershell (how to pass arguments)
How to install git on Linux such as EC2
[Tips] How to use iPhone as webcam on Linux
How to build a Python environment on amazon linux 2
[Linux] How to put your IP in a variable
About Linux kernel parameters
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
A memo on how to easily prepare a Linux exercise environment
[Ansible] How to call variables when creating your own module
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
How to print characters as a table with Python's print function
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit edition-
(Note) How to pass the path of your own module
How to use cuML SVC as a Gridsearch CV classifier
How to get Instruction Pointer (= program counter) in Linux kernel
[Linux] How to deal with garbled characters when viewing files
Be careful when assigning Series as a column to pandas.DataFrame
How to register a package on PyPI (as of September 2017)
How to specify command line arguments when debugging in PyCharm
How to use a file other than .fabricrc as a configuration file
How to output additional information when logging with python's logging module