Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm hacking some code in the kernel, and want to pass in a custom
parameter to the kernel (either at boot time, or after startup),
which my new code will use. This parameter is a number, ranging from
40 to 800, for example.
The existing boot command line parameters do not allow the use of a
user defined custom parameter to be passed in to the kernel. Is there
a way to do this, either during or after boot?
As far I know, any string you pass to kernel at boot time is available in /proc/cmdline pseudo file.
Check your current /proc/cmdline and you will find all strings you kernel received at boot time, even those are user space specific or are not used by anyone.
As far I know, any string you pass to kernel at boot time is available in /proc/cmdline pseudo file.
Check your current /proc/cmdline and you will find all strings you kernel received at boot time, even those are user space specific or are not used by anyone.
Thanks for the tips, Osor and Marozsas. I haven't had time to try out
either one. I will post what I find after looking into it some more.
I was looking into module_param, but it appears that it is used mainly
for modules. There seems to be no documentation on what needs to be
done if I wanted to add a parameter to the "kernel" boot command line
in grub.conf, that would be visible as a global after init is done.
There seems to be no documentation on what needs to be
done if I wanted to add a parameter to the "kernel" boot command line
in grub.conf, that would be visible as a global after init is done.
The module_param() macro is indeed for modules. If your “module” is not built as a module, but actually built-in to the kernel, you may still use the macro. In this case, you pass the parameter through the kernel command line using the modulename, followed by a dot (“.”), followed by the parameter. This is all documented here.
For example, consider the loopback block device module (loop.ko). This module takes a single optional parameter (max_loop) telling it the maximum number of loopback devices. So if I want to alter this parameter at the time of module loading, I would do something like this:
Code:
# modprobe loop max_loop=32
Suppose, however, that I have compiled the loopback block device “module” into my kernel. Then, I might pass the following line to the kernel at boot:
Code:
kernel /vmlinuz root=/dev/sda3 loop.max_loop=32
You can also assign permissions such that the file “/sys/module/loop/parameters/max_loop” is readable, is writable, is both, or is neither. In this case, the permissions are 0 (so the sysfs-interface file simply doesn’t exist).
If you want to be slightly more arcane about kernel parameters for built-in code, you can always use the __setup() macro. If you want to be slightly more sophisticated, you could create a sysctl interface.
The module_param() macro is indeed for modules. If your “module” is not built as a module, but actually built-in to the kernel, you may still use the macro. In this case, you pass the parameter through the kernel command line using the modulename, followed by a dot (“.”), followed by the parameter. This is all documented....
Thanks Osor,
Since this is a hack for a temporary test system, I decided to use the
__setup macro and get_option() in init/main.c, as follows:
I then add "my_param=<value>" to the "kernel" line in grub.conf,
and in the kernel code areas where I need to use this parameter,
I extern my_param and use it;
I'm still not clear how I would use module_param() in the case where
it is not a parameter for a single module, but rather a value that I
need to see and use in several kernel code areas, ie: mm and fs, for
example.
You don't tell us if you have tried it already, but if you start your linux with...
Thanks marozsas,
I wanted to add that in the current kernel, the command line
is also available as a global:
Code:
char *saved_command_line
Since strtok is available as a kernel function, this string can be
easily parsed in C code in the kernel, to get any user parameter that
is added to the "kernel" line of the grub configuration file. Please
see my earlier post for what I eventually used.
I didn't realize you are talking about coding in kernel space.
I had to read your post a second time to figure out that, and it is very clear at beginning of your original post - my bad ....
I then add "my_param=<value>" to the "kernel" line in grub.conf,
and in the kernel code areas where I need to use this parameter,
I extern my_param and use it;
how do i extern my_param and use it?
and is a specific point i put this function in main.c or not?
how do i extern my_param and use it?
and is a specific point i put this function in main.c or not?
Hi,
Declare the function in main.c like the other __init functions like nosmp, maxcpus, etc, and then the related __setup() function below that, for clarity. In the code area where you want to use this parameter, declare "extern my_param" up top, so you can use it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.