红联Linux门户
Linux帮助

centos6.5下编译hello.ko驱动程序

发布时间:2017-04-23 10:04:25来源:linux网站作者:小治
说明:
该驱动编译过程是在虚拟机中,安装完centos 6.5系统后:
 
A:默认make为用的自带的内核版本,我的centos 6.5系统 版本为(命令uname -r):2.6.32-431.el6.x86_64时:
 
A1、cd /usr/src/kernel/下;
 
A2、将2.6.32-431.el6.x86_64的kernel文件夹复制到/usr/src/下;
 
A3、在/usr/src/2.6.32-431.el6.x86_64/drivers/下创建存放hello测试程序的目录:mkdir test;
 
A4、进入到test下,创建hello.c和Makefile
 
A5、其中hello.c如下:
/*****************************/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT " Hello world enter\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT " Hello world exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("zzz");
MODULE_DESCRIPTION("A simple Hello world module");
MODULE_ALIAS("a simplest module");
/*****************************************/
而Makefile中只有一句:obj-m := hello.o
 
A6、在当前test目录下,执行:
make -C /usr/src/2.6.32-431.el6.x86_64 M=$(pwd) modules
完成后,则在当前目录下能够看到hello.ko驱动文件。
执行modinfo hello.ko将看到该模块的信息如下:
/****************************/
filename: hello.ko
alias: a simplest module
description: A simple Hello world module
author: zzz
license: Dual BSD/GPL
srcversion: 5AB83CD37662439404D5EE3
depends:
vermagic: 2.6.32-431.el6.x86_64 SMP mod_unload modversions
/*****************************/
 
A7、执行insmod hello.ko,将驱动模块加载到内核中
 
A8、执行cat /var/log/messages | tail
将在屏幕上看到系统打印的信息:Apr 2 18:02:51 localhost kernel: Hello world enter。
 
A9、执行rmmod hello.ko 从内核中移除hello驱动模块和 cat /var/log/messages |tail
将在屏幕上看到系统打印信息:Apr 2 18:04:33 localhost kernel: Hello world exit。
 
A10、至此,在自带的centos 6.5下的hello.ko驱动编译结束。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/30234.html