1. Set up build environment
>aptitude install build-essential
>aptitude install linux-kernel-headers
2. Write sample code
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> int mydrv_init(void) { printk("Mydrv init ok!/n"); return 0; } void mydrv_exit(void) { printk("Mydrv exit ok!/n"); } module_init(mydrv_init); module_exit(mydrv_exit); MODULE_LICENSE("Dual BSD/GPL");
3. Makefile
obj-m := mydrv.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
4. After excute command make, the files will looks like this:
. ├── Makefile ├── modules.order ├── Module.symvers ├── mydrv.c ├── mydrv.ko ├── mydrv.mod.c ├── mydrv.mod.o └── mydrv.o
5. Then install/remove this module,you will look the message output by the driver.
>insmod mydrv.ko
>Mydrv init ok!
>rmmod mydrv.ko
>Mydrv exit ok!
If you can not see it, please use this method:
>dmesg | tail
#END