hello.c linux
01 |
// Defining __KERNEL__ and MODULE allows us to access kernel-level code not usually available to userspace programs. |
08 |
// Linux Kernel/LKM headers: module.h is needed by all modules and kernel.h is needed for KERN_INFO. |
09 |
#include <linux/module.h> // included for all kernel modules |
10 |
#include <linux/kernel.h> // included for KERN_INFO |
11 |
#include <linux/init.h> // included for __init and __exit macros |
13 |
static int __init hello_init(void) |
15 |
printk(KERN_INFO "Hello world!\n"); |
16 |
return 0; // Non-zero return means that the module couldn't be loaded. |
19 |
static void __exit hello_cleanup(void) |
21 |
printk(KERN_INFO "Cleaning up module.\n"); |
24 |
module_init(hello_init); |
25 |
module_exit(hello_cleanup); |
Makefile shell
2 |
KDIR := /lib/modules/$(shell uname -r)/build |
6 |
$(MAKE) -C $(KDIR) M=$(PWD) modules |
9 |
$(MAKE) -C $(KDIR) M=$(PWD) clean |
執行命令: make
你也許會獲得以下錯誤: ubuntu
01 |
In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/list.h:6, |
02 |
from /usr/src/linux-headers-2.6.32-38-generic/include/linux/module.h:9, |
03 |
from hello_module.c:1: |
04 |
/usr/src/linux-headers-2.6.32-38-generic/include/linux/prefetch.h:14:27: error: asm/processor.h: No such file or directory |
05 |
/usr/src/linux-headers-2.6.32-38-generic/include/linux/prefetch.h:15:23: error: asm/cache.h: No such file or directory |
06 |
In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/module.h:9, |
07 |
from hello_module.c:1: |
08 |
/usr/src/linux-headers-2.6.32-38-generic/include/linux/list.h:7:24: error: asm/system.h: No such file or directory |
09 |
In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/kernel.h:11, |
10 |
from /usr/src/linux-headers-2.6.32-38- |
你須要作以下操做: 測試
Install and prepare module-assistant. fetch
2 |
$ apt-get install module-assistant |
6 |
$ sudo apt-get install build-essential linux-headers-$(uname -r) |
而後再次輸入命令:make
2 |
make -C /lib/modules/2.6.32-38-generic/build M=/home/jerikc/studydir/linux/module modules |
3 |
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-38-generic' |
4 |
CC [M] /home/jerikc/studydir/linux/module/hello.o |
5 |
Building modules, stage 2. |
7 |
CC /home/jerikc/studydir/linux/module/hello.mod.o |
8 |
LD [M] /home/jerikc/studydir/linux/module/hello.ko |
9 |
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-38-generic' |
OK,成功獲得hello.ko,下面進行測試: ui
加載module: spa
1 |
<span></span>$ sudo insmod hello.ko |
驗證:
1 |
Aug 25 12:29:07 ubuntu dhclient: DHCPREQUEST of 192.168.10.129 on eth0 to 192.168.10.254 port 67 |
2 |
Aug 25 12:29:07 ubuntu dhclient: DHCPACK of 192.168.10.129 from 192.168.10.254 |
3 |
Aug 25 12:29:07 ubuntu dhclient: bound to 192.168.10.129 -- renewal in 865 seconds. |
4 |
Aug 25 12:33:32 ubuntu kernel: [ 1886.549673] Hello world! |
或者使用lsmod查看module信息: .net
卸載module: code
1 |
Aug 25 12:43:32 ubuntu dhclient: DHCPREQUEST of 192.168.10.129 on eth0 to 192.168.10.254 port 67 |
2 |
Aug 25 12:43:32 ubuntu dhclient: DHCPACK of 192.168.10.129 from 192.168.10.254 |
3 |
Aug 25 12:43:32 ubuntu dhclient: bound to 192.168.10.129 -- renewal in 702 seconds. |
4 |
Aug 25 12:51:07 ubuntu kernel: [ 2939.379275] Cleaning up module. |
實驗完畢!