1.使用module_param()函数传递参数
hello.c
#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> MODULE_LICENSE("GPL"); static char *who="world"; static int times=1; //先声明变量(static型),然后调用module_param module_param(times,int,S_IRUGO); module_param(who,charp,S_IRUGO); static int hello_init(void) { int i; for(i=0;i<times;i++) { printk(KERN_ALERT "(%d) hello ,%s ",i,who); } return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye,%s! ",who); } module_init(hello_init); module_exit(hello_exit);
makefile
#General Purpose Makefile for cross compile Linux Kernel module ifneq ($(KERNELRELEASE),) obj-m := hello.o #+=是连接字符串 else ARCH := arm CROSS_COMPILE := arm-linux-gnueabihf- KERN_DIR := /home/zqh/lichee/linux-zero-4.14.y #选择内核路径 PWD :=$(shell pwd) #当前路径 all: make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(PWD) modules clean: make -C $(KERN_DIR) M=$(shell pwd) modules clean rm -rf modules.order endif
2.现象
# insmod hello.ko who="mom" times=5 # dmesg | tail -10 [ 6.057104] RTL8723BS: set pairwise key camid:4, addr:e6:02:9b:c8:c8:41, kid:0, type:AES [ 6.068026] RTL8723BS: set group key camid:5, addr:e6:02:9b:c8:c8:41, kid:2, type:AES [ 129.211272] hello: loading out-of-tree module taints kernel. [ 129.217970] (0) hello ,world [ 151.453028] Goodbye,world! [ 383.426642] (0) hello ,mom [ 383.429541] (1) hello ,mom [ 383.432419] (2) hello ,mom [ 383.435295] (3) hello ,mom [ 383.438304] (4) hello ,mom