您的位置:首页 > 运维架构 > Linux

linux 内核模块的helloWorld

2011-05-12 23:58 218 查看
我的linux版本是Fedora14。

要进行下面步骤之前请保证你的系统安装了内核的devel包和header包,

命令是:

yum install kernel-headers-$(uname -r)

yum install kernel-devel-$(uname -r)

helloWorldLKM.c代码:

#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");

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

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

完了之后在此目录下建立Makefile文件,内容如下:

obj-m := helloWorldLKM.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules

如果你不写Makefile的话,以下命令有参考价值:

#make –C /lib/modules/’uname –r’/build M=’pwd’ modules

#make –C /lib/modules/’uname –r’/build M=’pwd’ clean

#make –C /lib/modules/’uname –r’/build M=’pwd’ modules_install

分别是编译,清除一些中间生成文件,加载模块。

执行make,输出如下:

make -C /lib/modules/2.6.35.13-91.fc14.i686/build M=/home/xiemingming modules
make[1]: Entering directory `/usr/src/kernels/2.6.35.13-91.fc14.i686'
CC [M] /home/xiemingming/helloWorldLKM.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/xiemingming/helloWorldLKM.mod.o
LD [M] /home/xiemingming/helloWorldLKM.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.35.13-91.fc14.i686'

生成了下列文件:

[root@MyFedora excalibur]# ls hello*
helloWorldLKM.c helloWorldLKM.ko helloWorldLKM.mod.c helloWorldLKM.mod.o helloWorldLKM.o

查看可加载模块信息

[root@MyFedora excalibur]# modinfo helloWorldLKM.ko
filename: helloWorldLKM.ko
license: GPL
srcversion: 471D708B0804FE26B554DB1
depends:
vermagic: 2.6.35.13-91.fc14.i686 SMP mod_unload 686

加载内核模块

[root@MyFedora excalibur]# sudo insmod helloWorldLKM.ko

查看当前内核加载模块

[root@MyFedora excalibur]# lsmod|grep hello
helloWorldLKM 664 0

移除加载的内核模块

[root@MyFedora excalibur]# rmmod helloWorldLKM

查看日志信息
[root@MyFedora excalibur]# tail -f /var/log/messages
May 12 22:39:20 MyFedora abrtd: Directory 'ccpp-1305211160-2402' creation detected
May 12 22:39:20 MyFedora abrtd: Crash is in database already (dup of /var/spool/abrt/ccpp-1304198487-2047)
May 12 22:39:20 MyFedora abrtd: Deleting crash ccpp-1305211160-2402 (dup of ccpp-1304198487-2047), sending dbus signal
May 12 22:40:11 MyFedora pulseaudio[1724]: ratelimit.c: 197 events suppressed
May 12 22:42:13 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
May 12 22:45:41 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
May 12 22:45:47 MyFedora kernel: [ 942.291550] Hello, world
May 12 22:46:12 MyFedora pulseaudio[1724]: ratelimit.c: 172 events suppressed
May 12 22:46:17 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
May 12 22:46:18 MyFedora kernel: [ 973.214438] Goodbye, cruel world
May 12 22:46:43 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
^Z
[3]+ Stopped tail -f /var/log/messages

参考:http://www.cyberciti.biz/tips/build-linux-kernel-module-against-installed-kernel-source-tree.html


http://oss.org.cn/kernel-book/ldd3/ch02s02.html

http://fedoraproject.org/wiki/Docs/CustomKernel

以及最重要的linux源码目录下的linux/Documentation/kbuild/modules.txt

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: