1:简单代码
#include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("jiuyueguang"); MODULE_DESCRIPTION("SIMPLE MODULE DRIVER"); static int hello_init(void){ printk(KERN_INFO"hello word "); return 0; } static void hello_exit(void){ printk(KERN_INFO"hello word exit "); } module_init(hello_init); module_exit(hello_exit);
2:查看printk输出信息
dmesg | tail
3:带参数类型的模块简单代码
#include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("jiuyueguang"); MODULE_DESCRIPTION("SIMPLE MODULE DRIVER"); static char *name="no name"; static int age=0; static int hello_init(void){ printk(KERN_INFO"hello word name=%s and age=%d ",name ,age); return 0; } static void hello_exit(void){ printk(KERN_INFO"hello word exit name=%s and age=%d ",name ,age); } module_init(hello_init); module_exit(hello_exit); module_param(name,charp,S_IRUGO); module_param(age,int,S_IRUGO);
安装带参数的模块:
sudo insmod hello_param.ko
安装的时候没有加参数,采用程序中的默认值输出:
hello word name=no name and age=0
安装的时候加参数:
sudo insmod hello_param.ko name='jiuyueguang' age=25
输出:
hello word name=jiuyueguang and age=25
4:我自己的Makefile
ifeq ($(KERNELRELEASE),) KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* module* a.out .PHONY: modules modules_install clean else obj-m := hello_param.o //每次修改这里就够了 endif