Chinaunix首页 | 论坛 | 博客
  • 博客访问: 314559
  • 博文数量: 28
  • 博客积分: 2156
  • 博客等级: 大尉
  • 技术积分: 232
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 15:31
文章分类

全部博文(28)

文章存档

2011年(7)

2010年(21)

分类: LINUX

2010-12-02 11:31:08

开始接触内核开发,模块当然是第一步,可是无奈,一个小小的Hello World模块,竟然试了几次,都没有成功,情何以堪啊~~

今天终于一个不起眼的小小改动,成功搞定,特此纪念,哈哈

第一步,当然是编写Hello World模块的代码。


[root@node81 xudonglee]# vi hello.c

/*************** hello.c ***************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("xudonglee");

static int hello_init(void)
{
  printk(KERN_ALERT "Hello, world!\n");
  return 0;
}

static void hello_exit(void)
{
  printk(KERN_ALERT "Goodbye, My Dear World!\n");
}

module_init(hello_init);
module_exit(hello_exit);


第二步,当然是编写Makefile文件了。


obj-m = hello.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
~


第三步,执行make命令!


[root@node81 xudonglee]# make
make -C /lib/modules/2.6.18.9-xen/build M=/root/xudonglee modules
make[1]: Entering directory `/root/linux-2.6.18-xen-3.3.0'
  CC [M] /root/xudonglee/hello.o
  Building modules, stage 2.
  MODPOST
  LD [M] /root/xudonglee/hello.ko
make[1]: Leaving directory `/root/linux-2.6.18-xen-3.3.0'


第四步,通过ls或者ll命令,查看生成的模块文件。

[root@node81 xudonglee]# ll hello.*
-rw-r--r-- 1 root root 435 Dec 2 11:12 hello.c
-rw-r--r-- 1 root root 91569 Dec 2 11:15 hello.ko
-rw-r--r-- 1 root root 429 Dec 2 10:53 hello.mod.c
-rw-r--r-- 1 root root 46800 Dec 2 10:53 hello.mod.o
-rw-r--r-- 1 root root 46240 Dec 2 11:15 hello.o


第五步, 利用insmod加载helloworld模块。


[root@node81 xudonglee]# insmod hello.ko
[root@node81 xudonglee]#


第六步,上面没有在我的xshell上面显示出要打印的信息,下面查看log日志文件,确认模块已经执行。


[root@node81 xudonglee]# tail /var/log/messages


信息显示如下:

Dec  2 11:17:54 node81 kernel: Hello, world!


第七步,通过rmmod命令,将helloworld模块从内核中移走。

[root@node81 xudonglee]# rmmod hello.ko


第八步,同第六步,查看log日志文件。

[root@node81 xudonglee]# tail /var/log/messages


信息显示如下:

Dec  2 11:21:35 node81 kernel: Goodbye, My Dear World!


OK,从编写到顺利执行,已经全部完成!
-----------
下面总结一下曾经遇到过的问题:
(1) 在VMware虚拟机中进行make的时候,提示没有ncurse库的支持,无法编译内核模块。
(2) 内核代码中, 在hello_init前加了_init, 在hello_exit前加了_exit后,在make的时候就出现了如下的错误信息:

[root@node81 xudonglee]# make
make -C /lib/modules/2.6.18.9-xen/build M=/root/xudonglee modules
make[1]: Entering directory `/root/linux-2.6.18-xen-3.3.0'
  CC [M] /root/xudonglee/hello.o
/root/xudonglee/hello.c:8: error: expected ?.?. ?.?. ?.?. ?.sm?.or ?_attribute__?.before ?.ello_init?
/root/xudonglee/hello.c:14: error: expected ?.?. ?.?. ?.?. ?.sm?.or ?._attribute__?.before ?.ello_exit?
/root/xudonglee/hello.c: In function ?._inittest?.
/root/xudonglee/hello.c:19: error: ?.ello_init?.undeclared (first use in this function)
/root/xudonglee/hello.c:19: error: (Each undeclared identifier is reported only once
/root/xudonglee/hello.c:19: error: for each function it appears in.)
/root/xudonglee/hello.c: In function ?._exittest?.
/root/xudonglee/hello.c:20: error: ?.ello_exit?.undeclared (first use in this function)
make[2]: *** [/root/xudonglee/hello.o] Error 1
make[1]: *** [_module_/root/xudonglee] Error 2
make[1]: Leaving directory `/root/linux-2.6.18-xen-3.3.0'

make: *** [all] Error 2


原因还不清楚,暂且记录在案,搞清楚了再补充吧!
阅读(2398) | 评论(0) | 转发(0) |
0

上一篇:intel english

下一篇:话说sizeof的对齐

给主人留下些什么吧!~~