0

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 ?

Sowndarya K
  • 399
  • 5
  • 17
  • Your question is not clear to me. Are you just looking to add another variable to some kernel file that you can access within that particular file, or do you want to be able to access it from multiple files? If it is the former, you can simply add the variable how you normally would -- in a header or in the proper scope within the source itself. After you've modified the kernel code, just compile the kernel via `make`, any modules if necessary (`make modules_install`), and finally `make install`. Boot into your modified kernel and have fun. – buratino Jun 01 '16 at 09:06
  • I have to introduce a kernel tunable for an application for eg once that application has been installed set a kernel tunnable parameter say ex_param=1 manually.After setting,I wanted to check that parameter in one kernel source as,if that parameter say if( ex_param==1) do some action else do some action...How can achieve this? – Sowndarya K Jun 01 '16 at 09:21
  • Oh, I believe I misunderstood. You want to add a parameter to be used from the kernel command line, correct? – buratino Jun 01 '16 at 09:24
  • Ya,and I want to use that parameter inside one kernel source code to check if that is set properly or not ?How do I do it? – Sowndarya K Jun 01 '16 at 09:25

1 Answers1

1

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.

buratino
  • 1,408
  • 2
  • 17
  • 40
  • Thank you for the respose. After doing this, when I set my parameter through modprobe module parameter= ,will this directly loads the value in the source code which we have defined in __init ? – Sowndarya K Jun 02 '16 at 05:33
  • 1
    I updated my answer to include information about module parameters. You should update your question if module parameters are what you originally intended to ask about. – buratino Jun 02 '16 at 09:27
  • Ok,Can this be changed in runtime?and How can I add new kernel parameter in proc/sys/kernel/ ? – Sowndarya K Jun 02 '16 at 09:48
  • 1
    If you write your module to be able to change at runtime, then it will be able to change at runtime. Adding anything to /proc/sys is done via [sysctl](http://linux.die.net/man/8/sysctl). An example usage of the important function here, `register_sysctl_table()`, can be found [here](http://stackoverflow.com/questions/20164041/dynamically-adding-entries-to-sysctl). All you have to do is create a parent table with `.procname = "kernel"` and `.child=""`. I think this topic alone could represent a new question here on SO. – buratino Jun 02 '16 at 10:07
  • Actually I want to introduce new kernel parameter inside audit.c source code and that parameter should be updated first in /proc/sys/kernel/ and then could able to change parameter in runtime.Can you help me How can I implement this? – Sowndarya K Jun 02 '16 at 10:17
  • I think it might be best for you to either reword your original post to include exactly what you wanted, or to ask a new question related specifically to /proc/sys/kernel entries and how they can interact with kernel modules. – buratino Jun 02 '16 at 11:42