单独编译某个内核模块

对开发内核驱动和文件系统的人来说,经常需要编译某个内核模块。 当然从编译角度,有built-in kernel module和external kernel module之分。 怎么编译一个外部内核模块,google一下“hello world kernel module Makefile”吧。 这里我说下编译内核自带模块的坑... 我用的是SUSE Linux

  1. 安装源代码包

    #zypper in kernel-source

  2.       重要参考文件

#ls -l /usr/src/linux/README.SUSE
lrwxrwxrwx 1 root root 61 Dec 31 10:41 /usr/src/linux/README.SUSE -> ../../share/doc/packages/kernel-source-3.12.49-11/README.SUSE

这个文档介绍编译内核以及模块所需的软件包,概念,方法和步骤。 基本上也适用于其他Linux发型版。

贴出相关的一段:

The second method involves the following steps:

  (1)  Install the kernel-devel package.

  (2)  Install the kernel-$FLAVOR-devel package. This is necessary for
       symbol version information (CONFIG_MODVERSIONS).

  (3)  Compile the module(s) by changing into the module source directory
       and typing ``make -C /usr/src/linux-obj/$ARCH/$FLAVOR M=$(pwd)''.
       Substitute $ARCH and $FLAVOR with the architecture and flavor
       for which to build the module(s).

       If the installed kernel sources match the running kernel, you
       can build modules for the running kernel by using the path
       /lib/modules/$(uname -r)/build as the -C option in the above
       command. (build is a symlink to /usr/src/linux-obj/$ARCH/$FLAVOR).

  (4)  Install the module(s) with
       ``make -C /usr/src/linux-obj/$ARCH/$FLAVOR M=$(pwd) modules_install''.

   3.坑

但是按照上面的步骤总是编译不出来.ko内核模块:

eric1211:/usr/src/linux/fs/ocfs2 # make -C /lib/modules/3.12.49-11-default/build M=`pwd` modules
make: Entering directory '/usr/src/linux-3.12.49-11-obj/x86_64/default'
make[1]: Entering directory `/usr/src/linux-3.12.49-11-obj/x86_64/default'
  Building modules, stage 2.
  MODPOST 0 modules
make: Leaving directory '/usr/src/linux-3.12.49-11-obj/x86_64/default'

原因是配置文件默认没有选择OCFS2,所以要手动make menuconfig,把ocfs2选上, 再试就可以了。具体步骤:

  1. cd /usr/src/linux-obj/x86_64/default

  2. make menuconfig 选择ocfs2

  3. make scripts

  4. make -C /lib/modules/3.12.49-11-default/build M=`pwd` modules

你可能感兴趣的:(linux,SuSE,内核模块,ocfs2)