对开发内核驱动和文件系统的人来讲,常常须要编译某个内核模块。 固然从编译角度,有built-in kernel module和external kernel module之分。 怎么编译一个外部内核模块,google一下“hello world kernel module Makefile”吧。 这里我说下编译内核自带模块的坑... 我用的是SUSE Linuxlinux
安装源代码包和开发包ui
#zypper in kernel-source kernel-devel
开发包里面放的是头文件和Makefile文件,用RPM命令来查看下:google
# rpm -qa|grep kernel-devel kernel-devel-4.1.27-27.1.noarch # rpm -ql kernel-devel-4.1.27-24.1.noarch|more /usr/share/doc/packages/kernel-source-4.1.27-24 /usr/share/doc/packages/kernel-source-4.1.27-24/README.SUSE /usr/share/doc/packages/kernel-source-4.1.27-24/config-options.changes.txt /usr/src/linux /usr/src/linux-4.1.27-24 /usr/src/linux-4.1.27-24/Documentation /usr/src/linux-4.1.27-24/Documentation/DocBook /usr/src/linux-4.1.27-24/Documentation/DocBook/Makefile ....
重要参考文件code
#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.SUSEorm
这个文档介绍编译内核以及模块所需的软件包,概念,方法和步骤。 基本上也适用于其余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.坑it
可是按照上面的步骤老是编译不出来.ko内核模块, 缘由是当前的发行版默认没有打开ocfs2模块编译选项:io
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选上, 再试就能够了。具体步骤:编译
cd /usr/src/linux
make menuconfig 选择ocfs2
cd /usr/src/linux/fs/ocfs2
make -C /lib/modules/`uname -r`/build M=`pwd` modules