Debian 7.6 64bit 驱动开发 内核源码树搭建

来源:互联网 发布:deepin 添加ubuntu 源 编辑:程序博客网 时间:2024/06/07 19:15

1,搜索源码包

apt-get update

搜索源码包
root@demo-2:~/dm# apt-cache search linux-source
linux-source-3.2 - Linux kernel source for version 3.2 with Debian patches
linux-source - Linux kernel source (meta-package)
linux-source-2.6 - Linux kernel source (dummy package)
linux-source-3.16 - Linux kernel source for version 3.16 with Debian patches

2、构造源码树

tar -xvjf linux-source-2.6.26.tar.bz2 apt-get install ncurses-dev

# make
# make bzImage
# make modules // 编译模块
# make modules_install // 安装模块
OK 完成!
在/lib/modules下生成新的目录/lib/modules/2.6.26-2-generic/。 在编译驱动模块时,要用到这个路径下的build目录。

前在Ubuntu里写个kernel module 的 hello world. make出错.后来知道是因为Ubuntu没有KERNEL 原码树, 以为Ubuntu主打桌面,而且Ubuntu桌面是比较慢.
所以就换用Debian了. 但是Debian 装好后还是没有原码树…
所以在这里记录一下安装的方法:
  1. 查看自己系统的内核版本:

  hengl@lhdebian:~$ uname -r

  2.6.26-1-686

  2. 安装相应版本的内核原码包:

  hengl@lhdebian:~$ sudo apt-get install linux-source-2.6.26

  然后在/usr/src/ 下就会有内核原码包,是一个压缩包.

  hengl@lhdebian:~$ ls /usr/src/

  linux-source-2.6.26.tar.bz2

  3. 解压该包:

  hengl@lhdebian:/usr/src$ sudo tar -xvf linux-source-2.6.26

  然后会生成内核原码目录: linux-source-2.6.26

  4. 配置内核:

  hengl@lhdebian:/usr/src/linux-source-2.6.26$ sudo make oldconfig

  也可以使用别的配置方式 : menuconfig , xconfig. 看自己的需求了.

  5. 编译内核:

  hengl@lhdebian:/usr/src/linux-source-2.6.26$ sudo make bzImage

  这个操作要花一些时间,然后会在当前目录下www.linuxidc.com生成一个新的文件: vmlinux

  6. 编译 模块:

  hengl@lhdebian:/usr/src/linux-source-2.6.26$ sudo make modules

  这里要花更长一些时间… …

  7.安装 模块:

  hengl@lhdebian:/usr/src/linux-source-2.6.26$ sudo make modules_install

  然后会在 /lib/modules/ 目录下生成 目录 : 2.6.26. make 模块的时候用到的就是它了.

  至此, 就搞了, 重启一下系统, 写个网上到处可以找到的hello world试试:

//hello.c  #include  #include  MODULE_LICENSE("Dual BSD/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);

Debian 6.0 驱动开发环境搭建以及Hello模块测试 linux 2.6.32内核

本帖最后由 brauceunix 于 2011-07-23 22:18 编辑

前几天看到论坛里有开发环境搭建的文章,不过是基于Redhat 版本的, 今天发个Deiban 6.0版本。就 这么点过程,自己可能走了有一两年之久,现在明白一点了,写下来,扫盲。
1。 安装需求
A. 需要一个当前系统的内核源码树
B. 需要内核的头文件
C. 需要一些日常用到的gcc, make, gdb等应用程序开发工具链. (PS: 这个Debian 6.0默认已经支持)
D. 写个Helloworld模块测试,环境是否正确
2. 确认这些需求
A. 需求一个当前系统的源码树
a.确定内核版本brauce@debian:~$ uname -r
2.6.32-5-686
b. 从www.kernel.org 上下载 2.6.32内核, 这个,应该都Ok的,这里的源码,根据uname -r下,2.6.32 版本就是我们要的。至于 后面的,一般变化不会太大,原因是模块化的编程,在一个次版本号里变化应该不会太大
B. 需要内核的头文件
a.搜索相关的内核头文件,aptitude 是apt-get 的一个前端,笔者有点习惯这个

root@debian:/home/brauce# aptitude search linux-headers-2.6.32*p   linux-headers-2.6.32-5-486      - Header files for Linux 2.6.32-5-486       i A linux-headers-2.6.32-5-686      - Header files for Linux 2.6.32-5-686       p   linux-headers-2.6.32-5-686-bigm - Header files for Linux 2.6.32-5-686-bigmemp   linux-headers-2.6.32-5-all      - All header files for Linux 2.6.32 (meta-pap   linux-headers-2.6.32-5-all-i386 - All header files for Linux 2.6.32 (meta-pap   linux-headers-2.6.32-5-amd64    - Header files for Linux 2.6.32-5-amd64     i A linux-headers-2.6.32-5-common   - Common header files for Linux 2.6.32-5    p   linux-headers-2.6.32-5-common-o - Common header files for Linux 2.6.32-5-opep   linux-headers-2.6.32-5-common-v - Common header files for Linux 2.6.32-5-vsep   linux-headers-2.6.32-5-common-x - Common header files for Linux 2.6.32-5-xenp   linux-headers-2.6.32-5-openvz-6 - Header files for Linux 2.6.32-5-openvz-686p   linux-headers-2.6.32-5-vserver- - Header files for Linux 2.6.32-5-vserver-68p   linux-headers-2.6.32-5-vserver- - Header files for Linux 2.6.32-5-vserver-68p   linux-headers-2.6.32-5-xen-686  - Header files for Linux 2.6.32-5-xen-686   

b. 安装 这些头文件,我的机器是Pentium M 是686系列,根据uname -r的结果来选择root@debian:/home/brauce# aptitude install linux-headers-2.6.32-5-686
C. 基本的工具链就不说了,相信来编译内核的都知道基本的工具链怎么用

  1. 写个Hello模块测试
    A. Makefileobj-m := hello.o

KERNELDIR := /home/brauce/linux-2.6-2.6.32 #我把内核放在了/home/brauce目录。内核源码在哪个目录就写哪个目录

PWD :=$(shell pwd)all:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules#/home/brauce/linux-2.6.32B. hello.c/** $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $*/#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);

C. 运行
插入模块root@debian:/home/brauce/hello# insmod ./hello.ko
insmod: error inserting ‘./hello.ko’: -1 Invalid module format
很不幸的是,这个程序出了问题,不过前几天还运行的好好的,不管这些原因了,既然出问题了,就要解决掉它们,没得选
正在解决中。。
问题找到了是Makefile 写得不对,上面的Makefile 有问题,用下面的这个Makefile 替换掉就OK了,可以进行模块的插入和删除

ifeq ($(KERNELRELEASE),)    KERNELDIR ?= /lib/modules/$(shell uname -r)/build    PWD := $(shell pwd)modules:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install:        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions.PHONY: modules modules_install cleanelse    obj-m := hello.o endif

这个时候的Makefile 写对了,运行 下试试root@debian:/home/brauce/hello# dmesg | tail
[ 50.424186] ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[ 60.476039] wlan0: no IPv6 routers present
[ 113.269053] res is in use!
[ 181.494943] Hello, world
[ 187.344542] Goodbye, cruel world
[ 196.763165] lo: Disabled Privacy Extensions
[ 196.852809] lo: Disabled Privacy Extensions
[ 197.909771] lo: Disabled Privacy Extensions
[ 224.600522] lo: Disabled Privacy Extensions
[ 454.078709] Hello, world
root@debian:/home/brauce/hello#
PS: 最后一行是重点
这个时候我们再试试 删除模块root@debian:/home/brauce/hello# rmmod hello
root@debian:/home/brauce/hello# dmesg | tail
[ 60.476039] wlan0: no IPv6 routers present
[ 113.269053] res is in use!
[ 181.494943] Hello, world
[ 187.344542] Goodbye, cruel world
[ 196.763165] lo: Disabled Privacy Extensions
[ 196.852809] lo: Disabled Privacy Extensions
[ 197.909771] lo: Disabled Privacy Extensions
[ 224.600522] lo: Disabled Privacy Extensions
[ 454.078709] Hello, world
[ 522.206318] Goodbye, cruel world
root@debian:/home/brauce/hello#
到这个地方,环境搭建成功了!

0 0
原创粉丝点击