第一个内核测试程序

来源:互联网 发布:linux如何查看内核日志 编辑:程序博客网 时间:2024/06/12 22:06

1.在vmware安装上centos

网络设置,参考http://blog.sina.com.cn/s/blog_3e4dd88d0100yaur.html,注意在centos 右上角的网络图标选择对应的网络设备。

2.安装gcc环境

yum install gcc-c++

3.安装内核文件

yum install kernel-devel

4.编写hello.c文件

例子参考:http://edsionte.com/techblog/archives/1336

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>//必选//模块许可声明MODULE_LICENSE("GPL");//模块加载函数static int hello_init(void){    printk(KERN_ALERT"hello,I am edsionte\n");    return 0;}//模块卸载函数static void hello_exit(void){    printk(KERN_ALERT"goodbye,kernel\n");}//模块注册module_init(hello_init);module_exit(hello_exit);//可选MODULE_AUTHOR("edsionte Wu");MODULE_DESCRIPTION("This is a simple example!\n");MODULE_ALIAS("A simplest example");


5.编写Makefile
obj-m += hello.o#generate the pathCURRENT_PATH:=$(shell pwd)#the current kernel version numberLINUX_KERNEL:=$(shell uname -r)#the absolute path#LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)#这里的内核路径注意根据实际情况修改,确保内核路径指向正确,以下是centos的路径#LINUX_KERNEL_PATH:=/usr/src/kernels/2.6.32-573.22.1.el6.i686LINUX_KERNEL_PATH:=/usr/src/kernels/2.6.18-194.el5-i686#complie objectall:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules#cleanclean:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean



#注意make前面必须有tab字符,否则编译会出现make: Nothing to be done for `all'.

6.编译
输入make命令
7.测试
运行insmod hello.ko

查看lsmod


查看消息tail /var/log/messages

卸载rmmod hello
另一种查看消息命令dmesg




0 0
原创粉丝点击