Sowndarya K
Sowndarya K

Reputation: 419

Setting kernel tunable parameter

As I wanted to introduce new kernel module parameter say new_param=1 /0 ,then after that parameter has to be checked inside kernel code as if (new_param==1) do some work..... else do other...

In this way I wanted to check by introducing new kernel module parameter.Can anyone please help me? What are the steps I need to follow to do this ?

Upvotes: 0

Views: 634

Answers (1)

buratino
buratino

Reputation: 1518

One way to use a custom kernel parameter is to add it to the kernel command line and parse it out of /proc/cmdline, i.e.:

Add the parameter to the kernel command line BOOT_IMAGE=<image> root=<root> ro quiet splash vt.handoff=7 your_parameter=<value>

When you boot, you will be able to access this parameter by parsing the contents of /proc/cmdline:

$ cat /proc/cmdline
BOOT_IMAGE=<image> root=<root> ro quiet splash vt.handoff=7 your_parameter=<value>

I believe a solution more tailored to your needs would include using __setup(), which is mentioned (but not explained well) in /Documentation/kernel-parameters.txt.

There are quite a few examples in the kernel source. One of such would be in /drivers/block/brd.c:

#ifndef MODULE
/* Legacy boot options - nonmodular */
static int __init ramdisk_size(char *str)
{
        rd_size = simple_strtol(str, NULL, 0);
        return 1;
}
__setup("ramdisk_size=", ramdisk_size);
#endif

Following this example, you could add your __init and __setup() in the relevant source file. For parsing integers from an option string in your __init function, see get_option() in /lib/cmdline.c

Update

For modules, you should use module_param(), which takes three arguments: variable name, variable type, sysfs permissions. More on the class is found at /linux/moduleparam.h.

In the module that you want to be able to pass parameters to, first declare the parameters as globals. An example usage would include the following in the module source:

int foo = 200;
module_param(foo, int, 0);

Recompile the module and you will see that you can load it via modprobe <module-name> foo=40.

Upvotes: 1

Related Questions