Hello World : Module Parameter
In this program, you are going to learn
How to pass module parameters to Linux kernel module ?
How to use below APIs ?
Topics in this section,
To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the module_param() macro.
Define the module parameter for integer
module_param(myint, int, 0644);
MODULE_PARM_DESC(myint, "An integer");
Pass the module parameter during module load
$ sudo insmod ./prgm1.ko myint=56
See the full program below,
1#include <linux/module.h>
2#include <linux/moduleparam.h>
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/stat.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9
10static int myint = 400;
11
12module_param(myint, int, 0644);
13MODULE_PARM_DESC(myint, "An integer");
14
15static int __init hello_init(void)
16{
17 pr_info("Hello, world\n");
18 pr_info("myint is an integer: %d\n", myint);
19 return 0;
20}
21
22static void __exit hello_exit(void)
23{
24 pr_info("Goodbye, world\n");
25}
26
27module_init(hello_init);
28module_exit(hello_exit);
1obj-m += prgm1.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ make all
2
3$ sudo insmod ./prgm1.ko myint=56
4
5$ dmesg
6[495039.286549] Hello, world
7[495039.286550] myint is an integer: 56
8
9$ sudo rmmod prgm1
10
11$ dmesg
12[495039.286549] Hello, world
13[495039.286550] myint is an integer: 56
14[495049.549716] Goodbye, world
Define the module parameter for short
module_param(myshort, short, 0660);
MODULE_PARM_DESC(myshort, "A short integer");
Pass the module parameter during module load
$ sudo insmod ./prgm2.ko myshort=10
See the full program below,
1#include <linux/module.h>
2#include <linux/moduleparam.h>
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/stat.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9
10static short int myshort = 1;
11
12module_param(myshort, short, 0660);
13MODULE_PARM_DESC(myshort, "A short integer");
14
15static int __init hello_init(void)
16{
17 pr_info("Hello, world\n");
18 pr_info("myshort is a short integer: %d\n", myshort);
19
20 return 0;
21}
22
23static void __exit hello_exit(void)
24{
25 pr_info("Goodbye, world\n");
26}
27
28module_init(hello_init);
29module_exit(hello_exit);
1obj-m += prgm2.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ make all
2
3$ sudo insmod ./prgm2.ko myshort=10
4
5$ dmesg
6[495335.891744] Hello, world
7[495335.891745] myshort is a short integer: 10
8
9$ sudo rmmod prgm2
10
11$ dmesg
12[495335.891744] Hello, world
13[495335.891745] myshort is a short integer: 10
14[495343.492308] Goodbye, world
Define the module parameter for long
module_param(mylong, long, 0400);
MODULE_PARM_DESC(mylong, "A long integer");
Pass the module parameter during module load
$ sudo insmod ./prgm3.ko mylong=500
See the full program below,
1#include <linux/module.h>
2#include <linux/moduleparam.h>
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/stat.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9
10static long mylong = 100;
11
12module_param(mylong, long, 0400);
13MODULE_PARM_DESC(mylong, "A long integer");
14
15static int __init hello_init(void)
16{
17 pr_info("Hello, world\n");
18 pr_info("mylong is a long integer: %ld\n", mylong);
19 return 0;
20}
21
22static void __exit hello_exit(void)
23{
24 pr_info("Goodbye, world\n");
25}
26
27module_init(hello_init);
28module_exit(hello_exit);
1obj-m += prgm3.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ make all
2
3$ sudo insmod ./prgm3.ko mylong=500
4
5$ dmesg
6[495455.686602] Hello, world
7[495455.686604] mylong is a long integer: 500
8
9$ sudo rmmod prgm3
10
11$ dmesg
12[495455.686602] Hello, world
13[495455.686604] mylong is a long integer: 500
14[495464.038982] Goodbye, world
Define the module parameter for string
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");
Pass the module parameter during module load
$ sudo insmod ./prgm4.ko mystring="linux"
See the full program below,
1#include <linux/module.h>
2#include <linux/moduleparam.h>
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/stat.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9
10static char *mystring = "blah";
11
12module_param(mystring, charp, 0000);
13MODULE_PARM_DESC(mystring, "A character string");
14
15static int __init hello_init(void)
16{
17 pr_info("Hello, world\n");
18
19 pr_info("mystring is a string: %s\n", mystring);
20 return 0;
21}
22
23static void __exit hello_exit(void)
24{
25 pr_info("Goodbye, world\n");
26}
27
28module_init(hello_init);
29module_exit(hello_exit);
1obj-m += prgm4.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ make all
2
3$ sudo insmod ./prgm4.ko mystring="linux"
4
5$ dmesg
6[495668.254605] Hello, world
7[495668.254607] mystring is a string: linux
8
9$ sudo rmmod prgm4
10
11$ dmesg
12[495668.254605] Hello, world
13[495668.254607] mystring is a string: linux
14[495697.631035] Goodbye, world
Define the module parameter for integer array
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");
Pass the module parameter during module load
$ sudo insmod ./prgm5.ko myintArray=10,20,30
See the full program below,
1#include <linux/module.h>
2#include <linux/moduleparam.h>
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/stat.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("Linux_usr");
9
10static int myintArray[3]= {5,3,1};
11static int arr_argc = 1;
12
13module_param_array(myintArray, int, &arr_argc, 0000);
14MODULE_PARM_DESC(myintArray, "An array of integers");
15
16static int __init hello_init(void)
17{
18 int i;
19 pr_info("Hello, world\n");
20 for (i = 0; i < (sizeof(myintArray) / sizeof(int)); i++)
21 pr_info("myintArray[%d] = %d\n", i, myintArray[i]);
22 pr_info("got %d arguments for myintArray.\n", arr_argc);
23 return 0;
24}
25
26static void __exit hello_exit(void)
27{
28 pr_info("Goodbye, world\n");
29}
30
31module_init(hello_init);
32module_exit(hello_exit);
1obj-m += prgm5.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1$ make all
2
3$ sudo insmod ./prgm5.ko myintArray=10,20,30
4
5$ dmesg
6[504234.493933] Hello, world
7[504234.493935] myintArray[0] = 10
8[504234.493935] myintArray[1] = 20
9[504234.493936] myintArray[2] = 30
10[504234.493936] got 3 arguments for myintArray.
11
12
13$ sudo rmmod prgm5
14
15$ dmesg
16[504234.493933] Hello, world
17[504234.493935] myintArray[0] = 10
18[504234.493935] myintArray[1] = 20
19[504234.493936] myintArray[2] = 30
20[504234.493936] got 3 arguments for myintArray.
21[504240.118160] Goodbye, world
API |
Learning |
---|---|
module_param |
macro used to initialize the arguments |
MODULE_PARM_DESC |
used to document arguments that the module can take |
Other topic
Next Chapter
Other Chapters