分类: LINUX
2014-12-12 10:39:23
第一步:创建实验目录#mkdir dir
#vi param
#vi Makefile
在当前目录下编写一个整形,一个字符串型模块参数
#include
#include
MODULE_LICENSE("GPL");
static char *name = "DAVID";
static int age=30;
module_param(age, int ,S_IRUGO);
module_param(name, charp ,S_IRUGO);
static int __init hello_init(void)
{
printk(KERN_EMERG "Name:%s\n",name);
printk(KERN_EMERG "Age:%d\n",age);
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_EMERG"Module exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
ifneq ($(KERNELRELEASE),)
obj-m :=param.o
else
KDIR:= /lib/modules/2.6.32-279.el6.i686/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c .symvers
Endif
第二步:编译内核模块
#make生成param.ko
第三步:加载内核模块
#insmod param.ko
[root@localhost prama]#
Message from syslogd@localhost at Dec 12 09:43:39 ...
kernel:Name:DAVID
Message from syslogd@localhost at Dec 12 09:43:39 ...
kernel:Age:30
查看是否被加载
root@localhost prama]# lsmod |grep param
param 818 0
第四步:使用内核模块参数
[root@localhost prama]# rmmod param //若要重新加载已经被加载过的模块,需先卸载,否在再次加载会报该内核模块已经存在的错误。
Message from syslogd@localhost at Dec 12 09:44:32 ...
kernel:Module exit!
[root@localhost prama]# insmod param.ko name="Tom" age=10
[root@localhost prama]#
Message from syslogd@localhost at Dec 12 09:45:10 ...
kernel:Name:Tom
Message from syslogd@localhost at Dec 12 09:45:10 ...
kernel:Age:10
第五步:卸载内核模块
[root@localhost prama]# rmmod param
Message from syslogd@localhost at Dec 12 09:45:58 ...
kernel:Module exit!
[root@localhost prama]# lsmod |grep param查看是否卸载成功