Ubuntu 10.04 加載/卸載 自定義 module

hello.c linux

// Defining __KERNEL__ and MODULE allows us to access kernel-level code not usually available to userspace programs.
#undef __KERNEL__
#define __KERNEL__
 
#undef MODULE
#define MODULE
 
// Linux Kernel/LKM headers: module.h is needed by all modules and kernel.h is needed for KERN_INFO.
#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>        // included for __init and __exit macros
 
static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;    // Non-zero return means that the module couldn't be loaded.
}
 
static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
}
 
module_init(hello_init);
module_exit(hello_cleanup);


Makefile shell

obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
 
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
 
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
執行命令: make


你也許會獲得以下錯誤: ubuntu

In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/list.h:6,
                 from /usr/src/linux-headers-2.6.32-38-generic/include/linux/module.h:9,
                 from hello_module.c:1:
/usr/src/linux-headers-2.6.32-38-generic/include/linux/prefetch.h:14:27: error: asm/processor.h: No such file or directory
/usr/src/linux-headers-2.6.32-38-generic/include/linux/prefetch.h:15:23: error: asm/cache.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/module.h:9,
                 from hello_module.c:1:
/usr/src/linux-headers-2.6.32-38-generic/include/linux/list.h:7:24: error: asm/system.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.32-38-generic/include/linux/kernel.h:11,
                 from /usr/src/linux-headers-2.6.32-38-

......


你須要作以下操做: 測試

Install and prepare module-assistant. fetch

$ sudo -i
$ apt-get install module-assistant
$ m-a prepare


$  sudo apt-get install build-essential linux-headers-$(uname -r)


而後再次輸入命令:make
$ make
make -C /lib/modules/2.6.32-38-generic/build M=/home/jerikc/studydir/linux/module modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-38-generic'
  CC [M]  /home/jerikc/studydir/linux/module/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/jerikc/studydir/linux/module/hello.mod.o
  LD [M]  /home/jerikc/studydir/linux/module/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-38-generic'


OK,成功獲得hello.ko,下面進行測試: ui

加載module: spa

$ sudo insmod hello.ko
驗證:
$ tail /var/log/syslog
Aug 25 12:29:07 ubuntu dhclient: DHCPREQUEST of 192.168.10.129 on eth0 to 192.168.10.254 port 67
Aug 25 12:29:07 ubuntu dhclient: DHCPACK of 192.168.10.129 from 192.168.10.254
Aug 25 12:29:07 ubuntu dhclient: bound to 192.168.10.129 -- renewal in 865 seconds.
Aug 25 12:33:32 ubuntu kernel: [ 1886.549673] Hello world!

或者使用lsmod查看module信息: code

$ lsmod
Module                  Size  Used by
hello                    596  0 
...                      ...  ...

卸載module: get

$ sudo rmmod hello


$ tail /var/log/syslog
Aug 25 12:43:32 ubuntu dhclient: DHCPREQUEST of 192.168.10.129 on eth0 to 192.168.10.254 port 67
Aug 25 12:43:32 ubuntu dhclient: DHCPACK of 192.168.10.129 from 192.168.10.254
Aug 25 12:43:32 ubuntu dhclient: bound to 192.168.10.129 -- renewal in 702 seconds.
Aug 25 12:51:07 ubuntu kernel: [ 2939.379275] Cleaning up module.
實驗完畢!
相關文章
相關標籤/搜索