1. hello.clinux
#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello World enter\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Hello World exit\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Song YanNa"); MODULE_DESCRIPTION("A Sample Hello World Module"); MODULE_ALIAS("A Sample module");
2. Makefileshell
# #Makefile for the helloworld.c # obj-m := hello.o CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r) LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL) all: $(MAKE) -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules clean: rm *.ko rm *.o
3.在shell終端輸入makebash
出現以下信息,說明編譯成功。ui
NaNa:/home/nana/driver # make make -C /home/nana/driver/linux-3.1.10 M=/home/nana/driver modules make[1]: Entering directory `/home/nana/driver/linux-3.1.10' CC [M] /home/nana/driver/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/nana/driver/hello.mod.o LD [M] /home/nana/driver/hello.ko make[1]: Leaving directory `/home/nana/driver/linux-3.1.10'
4. 將模塊載入內核日誌
sudo insmod ./hello.ko
5. 刪除內核中的模塊 blog
rmmod hello.ko
在終端輸入dmesg,能夠查看內核的日誌信息it
查詢安裝的模塊編譯
lsmod