Linux Kernel Module Parameters
Reference :
1. http://tldp.org/LDP/lkmpg/2.6/html/x323.html
2. http://makelinux.com/ldd3/chp-2-sect-8
前言:
這個文章是我最近在寫Linux Kernel Device Driver遇到的問題,所以想說寫下來才不會忘記,也順便分享出來讓有需要的人更快可以上手,如果對於我寫的文章有問題或是有更好的想法,也請大家一起交流。
相關參數介紹:
在寫Linux Kernel Module的時候,我們用insmod或 modprobe安裝的時候,可以對module填入我們想要的參數,例如:
$> sudo insmod mymod.ko number=10
這樣的好處讓你的Kernel modules在設計的時候會比較有彈性,然後這些參數在Kernel module是經過module_param這個Macro來處理。以下是對module_param的介紹:
module_param( param name, data type, permissions bits);
1. param name : 參數名稱
2. data type : 資料型態
-
- bool
-
- invbool
- A boolean (true or false) value (the associated variable should be of type int). The invbool type inverts the value, so that true values become false and vice versa.
-
- charp
- A char pointer value. Memory is allocated for user-provided strings, and the pointer is set accordingly.
-
- int
-
- long
-
- short
-
- uint
-
- ulong
-
- ushort
(有關參數的權限,但我還不太懂之後比較瞭解再補充。)
再另外補充一下macro就是MODULE_PARM_DESC,這個marco主要就是讓你可以用modinfo這個工具去讀取kernel module的資訊時顯示這個module參數的資訊。以下是例子:
static short int myshort = 1;
module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "A short integer");
完整的範例:
# Makefile
obj-m += hello-5.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
# hello-5.c (這是由Reference 1的網頁中直接拿來做範例)
/*
* hello-5.c - Demonstrates command line argument passing to a module.
*/
#include
|
$> make
$> sudo insmod hello_5.ko
用dmesg去看一下,Kernel Module秀出什麼資訊 。
$> dmesg
[ 6532.204976] Hello, world 5
[ 6532.204979] =============
[ 6532.204983] myshort is a short integer: 1
[ 6532.204986] myint is an integer: 420
[ 6532.204989] mylong is a long integer: 9999
[ 6532.204991] mystring is a string: blah
[ 6532.204994] myintArray[0] = -1
[ 6532.204996] myintArray[1] = -1
[ 6532.204999] got 0 arguments for myintArray.
$> sudo rmmod hello_5.ko
$> sudo insmod hello_5.ko mystring="abcde" myintArray=1,12
[ 6915.780582] Hello, world 5
[ 6915.780585] =============
[ 6915.780592] myshort is a short integer: 1
[ 6915.780597] myint is an integer: 420
[ 6915.780601] mylong is a long integer: 9999
[ 6915.780605] mystring is a string: abcde
[ 6915.780609] myintArray[0] = 1
[ 6915.780613] myintArray[1] = 12
[ 6915.780617] got 1 arguments for myintArray.
$> modinfo hello_5.ko
filename: hello_5.ko
author: Peter Jay Salzman
license: GPL
srcversion: A704327C32F7F311666C13C
depends:
vermagic: 3.0.0-12-generic SMP mod_unload modversions
parm: myshort:A short integer (short)
parm: myint:An integer (int)
parm: mylong:A long integer (long)
parm: mystring:A character string (charp)
parm: myintArray:An array of integers (array of int)
留言