Ubuntu下轻松建立你的内核驱动开发环境

内核编译和嵌入式产品的设计与开发
回复
头像
tonychen123
帖子: 101
注册时间: 2009-04-03 20:52
来自: Guangzhou -China

Ubuntu下轻松建立你的内核驱动开发环境

#1

帖子 tonychen123 » 2009-06-04 23:18

Preface:
1.笔者刚学linux驱动开发,看到网上很多说要现建立内核树。在笔者到官网下载源码时,源码下面有如下说明: If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead. 意思是,如果你只是想为内核编译第三方的模块,那么,你不需下载此源码包。安装内核头文件包或许会更适合你。

2.如果你的ubuntu是保持更新的,你的系统是安装有内核头文件包的,不信你到/usr/src目录下查看,是不是有linux-headers-2.6.xx-xx-generic的文件夹呢,呵呵。我现在可以说,你可以在此开发你的驱动程序了。

3.笔者也是初学,有见笑的,大家多指正,emial: tonychen123@qq.com; 转载注明出处:http://sites.google.com/site/tonychen123site,作者:tony

4.环境本来就有了看来,所以我的文章,本来应该叫驱动开发最简单例子才合适……另,我要示例的是最简单,也是个最没用的驱动演示,名字叫tst,包含源码tst.c及makefile文件,编译出来的模块名叫tst.ko

Content:

1)建立你的实验目录并创建tst.c源文件:

代码: 全选

tony@ubuntu:~/mini2440$ mkdir mtst; cd mtst
tony@ubuntu:~/mini2440/mtst$ vim tst.c

======================================

/*
 * File: tst.c
 *
 * A very simple kernel module demo
 *
 * Copyright 2009, Tony, <tonychen123@qq.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation;
 *
 */

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO  */
#include <linux/init.h>     /*Needed for the macros */

static int __init tst_start(viod)
{
    printk(KERN_INFO "Loading my so simple module...\n");
    printk(KERN_INFO "I love Ubuntu\n");
    return 0;
}

static void __exit tst_end(void)
{
    printk(KERN_INFO "From tst module: Goodbye, Tony.\n");
}

module_init(tst_start);
module_exit(tst_end);

/* end */

2)创建Makefile文件:

代码: 全选

tony@ubuntu:~/mini2440/mtst$ vim Makefile
=================================================================
obj-m = tst.o

K_DIR = $(shell uname -r)
PWD = $(shell pwd)

all:
    make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean

# end #
3)编译

代码: 全选

tony@ubuntu:~/mini2440/mtst$ sudo make Note:好像要用sudo才行!
make -C /lib/modules/2.6.28-12-generic/build M=/home/tony/mini2440/mtst modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.28-12-generic'
  CC [M]  /home/tony/mini2440/mtst/tst.o
/home/tony/mini2440/mtst/tst.c:19: warning: function declaration isn’t a prototype
  Building modules, stage 2.
  MODPOST 1 modules        Note:生成一个module!
  CC      /home/tony/mini2440/mtst/tst.mod.o
  LD [M]  /home/tony/mini2440/mtst/tst.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.28-12-generic'
tony@ubuntu:~/mini2440/mtst$
4)测试

代码: 全选

tony@ubuntu:~/mini2440/mtst$ su     Note:加载模块要有root特权                                           
Password:                                                                                      
root@ubuntu:/home/tony/mini2440/mtst# insmod tst.ko  Note:加载模块要有root特权
root@ubuntu:/home/tony/mini2440/mtst# dmesg | tail   
[   25.464531] r8169: eth0: link up              
[   25.464534] r8169: eth0: link up              
[   54.931357] UDF-fs: No VRS found              
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root           
[ 2932.746291] module init                                 
[ 3138.097884] Cleanup module                              
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...   Note:注意这两行,就是我们的模块加载信息  
[ 7444.575121] I love Ubuntu                                           
root@ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
tst                     9344  0
root@ubuntu:/home/tony/mini2440/mtst# rmmod tst.ko
root@ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
root@ubuntu:/home/tony/mini2440/mtst# dmesg | tail
[   25.464534] r8169: eth0: link up
[   54.931357] UDF-fs: No VRS found
[   54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[   55.013712] ISOFS: changing to secondary root
[ 2932.746291] module init
[ 3138.097884] Cleanup module
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...
[ 7444.575121] I love Ubuntu
[ 7559.570808] From tst module: Goodbye, Tony.    Note:呵呵,模块正常卸载
root@ubuntu:/home/tony/mini2440/mtst#  /* end Bye ~ */
上次由 tonychen123 在 2009-06-28 22:29,总共编辑 1 次。
There should be one-- and preferably only one --obvious way to do it.
dysunset
帖子: 7
注册时间: 2009-03-17 19:01

Re: Ubuntu下轻松建立你的内核驱动开发环境

#2

帖子 dysunset » 2009-06-14 10:11

不错不错,我是新手,刚学习中。。。。。。
受益匪浅!
bigship
帖子: 1
注册时间: 2009-06-24 14:13

Re: Ubuntu下轻松建立你的内核驱动开发环境

#3

帖子 bigship » 2009-06-24 14:40

很不错,特别是这一句话:If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead. 省的我去再编译内核了。
surefire
帖子: 19
注册时间: 2007-11-10 8:54

Re: Ubuntu下轻松建立你的内核驱动开发环境

#4

帖子 surefire » 2009-06-26 10:45

请问在Server版上要如何做?好像没有build目录,我无法编译驱动模块,在桌面版上编译的又不能在Server版上安装,提示disagrees about version of symbol struct_module错误
fin_mai
帖子: 9
注册时间: 2009-06-28 14:16

Re: Ubuntu下轻松建立你的内核驱动开发环境

#5

帖子 fin_mai » 2009-06-28 14:48

If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead.

这句话折腾不少菜鸟,包括我!
qincheng
帖子: 41
注册时间: 2009-07-26 12:09

Re: Ubuntu下轻松建立你的内核驱动开发环境

#6

帖子 qincheng » 2009-07-31 1:59

我是菜鸟哦
请教下 tony@ubuntu:~/mini2440/mtst$ vim tst.c
tony@ubuntu:~/mini2440/mtst$ vim Makefile
上面两行命令具体什么意义呢 Vim貌似是个什么软件吗?
还有
Makefile 这个是自己写的还是自动生成的?

驱动后具体效果是怎么东西 不明白
我刚起步的 别见笑那么愚蠢的问题 :em06
头像
tonychen123
帖子: 101
注册时间: 2009-04-03 20:52
来自: Guangzhou -China

Re: Ubuntu下轻松建立你的内核驱动开发环境

#7

帖子 tonychen123 » 2009-07-31 11:21

qincheng 写了:我是菜鸟哦
请教下 tony@ubuntu:~/mini2440/mtst$ vim tst.c
tony@ubuntu:~/mini2440/mtst$ vim Makefile
上面两行命令具体什么意义呢 Vim貌似是个什么软件吗?
还有
Makefile 这个是自己写的还是自动生成的?

驱动后具体效果是怎么东西 不明白
我刚起步的 别见笑那么愚蠢的问题 :em06
不急,我也是刚过来的,呵呵
vim是个命令行编辑软件,google一下吧,慢慢来。至于makefile,这里当是自己编写的,你需要去了解下linux下的编译环境吧,祝学习愉快
There should be one-- and preferably only one --obvious way to do it.
qincheng
帖子: 41
注册时间: 2009-07-26 12:09

Re: Ubuntu下轻松建立你的内核驱动开发环境

#8

帖子 qincheng » 2009-07-31 12:53

qincheng@qincheng-desktop:~/Desktop/qincheng$ tony@ubuntu:~/mini2440/mtst$ sudo make Note:好像要用sudo才行!
bash: tony@ubuntu:~/mini2440/mtst$: 没有该文件或目录
qincheng@qincheng-desktop:~/Desktop/qincheng$ sudo make Note:好像要用sudo才行!
[sudo] password for qincheng:
sudo: make Note:好像要用sudo才行!: command not found
qincheng@qincheng-desktop:~/Desktop/qincheng$ sudo make note
make: *** 没有规则可以创建目标“note”。 停止。
qincheng@qincheng-desktop:~/Desktop/qincheng$ sudo make Note
sudo: make Note: command not found
qincheng@qincheng-desktop:~/Desktop/qincheng$ sudo su
root@qincheng-desktop:/home/qincheng/Desktop/qincheng# sudo make Note
sudo: make Note: command not found
root@qincheng-desktop:/home/qincheng/Desktop/qincheng#



我编译出现这个问题
还有我打开着文件夹来 Vim tst.c 发现文件夹里面根本没建立这个文件 是怎么回事呢???
麻烦回复
这你这个主题有点麻烦 呵呵 谢谢了
头像
demonyangyue
帖子: 1
注册时间: 2009-09-16 11:46

Re: Ubuntu下轻松建立你的内核驱动开发环境

#9

帖子 demonyangyue » 2009-09-16 11:49

还是这个问题:
make -C /lib/modules/2.6.28-15-generic/build M=/home/yy modules
make[1]: 正在进入目录 `/usr/src/linux-headers-2.6.28-15-generic'
Building modules, stage 2.
MODPOST 0 modules
make[1]:正在离开目录 `/usr/src/linux-headers-2.6.28-15-generic'

怨念的内核树。。。 :em20
月光林地的猫
帖子: 37
注册时间: 2009-06-27 23:33

Re: Ubuntu下轻松建立你的内核驱动开发环境

#10

帖子 月光林地的猫 » 2009-09-26 23:28

我也搞定了。
If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead.
这个帮了大忙了,省了至少1个半小时的时间。
就是出了点小问题。
卸载的时候,printk的输出能用 dmesg|tail 看到。
insmod的时候却看不到,控制台上也没有显示。
我用lsmod|grep hello 的时候,确实看到了自己模块在列表里。
迷惑。
源码是LLD3里第2章开始的那段。
直接在控制台打的命令:

代码: 全选

make -C /usr/src/linux-headers-2.6.28-15-generic SUBDIRS=$PWD modules
回复