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
要进行下面步骤之前请保证你的系统安装了内核的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
相关文章推荐
- Linux 下内核模块的开发方式的记录 及问题解决(入门的Helloworld)
- Linux 内核模块 helloworld
- Linux2.6.32内核笔记(1)内核模块helloworld
- 最简单的helloworld模块编译加载(linux3.5内核源码树建立)
- 在Linux下编译内核模块的Makefile的几种写法 转
- Linux 可加载内核模块剖析
- linux2.4与2.6内核模块的Makefile模版
- 在Linux 2.6内核下编译可以加载的内核模块
- Linux 可加载内核模块剖析
- Linux最小根文件系统的建立,内核模块的编译,Qemu模拟测试最小系统
- Linux内核参数和模块参数
- linux添加可加载内核模块(LKM入门)
- Linux 内核模块编程
- 使用kgdb调试linux内核及内核模块
- Linux 内核模块参数学习module_param
- Linux最简单内核模块编写
- Linux 2.6内核驱动之hello模块
- 在Linux2.6内核(CentOS)中编译内核模块的一个例子
- 基于linux2.6.30.4内核的DM9000网卡驱动编译成模块成功ping通
- systemtap 列出所有linux 内核模块与相关函数0