Ubuntu 10.04 加载/卸载 自定义 module

hello.c

01 // Defining __KERNEL__ and MODULE allows us to access kernel-level code not usually available to userspace programs.
02 #undef __KERNEL__
03 #define __KERNEL__
04   
05 #undef MODULE
06 #define MODULE
07   
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
12   
13 static int __init hello_init(void)
14 {
15     printk(KERN_INFO "Hello world!\n");
16     return 0;    // Non-zero return means that the module couldn't be loaded.
17 }
18   
19 static void __exit hello_cleanup(void)
20 {
21     printk(KERN_INFO "Cleaning up module.\n");
22 }
23   
24 module_init(hello_init);
25 module_exit(hello_cleanup);


Makefile

1 obj-m := hello.o
2 KDIR := /lib/modules/$(shell uname -r)/build
3 PWD := $(shell pwd)
4   
5 all:
6 $(MAKE) -C $(KDIR) M=$(PWD) modules
7   
8 clean:
9 $(MAKE) -C $(KDIR) M=$(PWD) clean
执行命令: make


你也许会得到如下错误:

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-
11  
12 ......


你需要做如下操作:

Install and prepare module-assistant.

1 $ sudo -i
2 $ apt-get install module-assistant
3 $ m-a prepare
4  
5  
6 $  sudo apt-get install build-essential linux-headers-$(uname -r)


然后再次输入命令:make
1 $ 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.
6   MODPOST 1 modules
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,下面进行测试:

加载module:

1 <span></span>$ sudo insmod hello.ko
验证:
1 $ tail /var/log/syslog
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信息:

1 $ lsmod
2 Module                  Size  Used by
3 hello                    596  0
4 ...                      ...  ...

卸载module:

1 $ sudo rmmod hello


1 $ tail /var/log/syslog
view source
print ?
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.
实验完毕!

你可能感兴趣的:(Ubuntu 10.04 加载/卸载 自定义 module)