红联Linux门户
Linux帮助

A simple tourial for Linux 2.6.24 kernel module

发布时间:2008-10-12 00:22:21来源:红联作者:Offera
Author: Charles Yang <chio.yang@gmail.com>

Linux Dist: Ubuntu 8.04 Hardy

1. Preparation

1.1 Download source code with apporiate version

Usually, the linux release distribution have no full kernel source code tree. So you should get a copy of kernel source from www.kernel.org. For convience, you had better download the same version as you running linux kernel.

You can type uname -r in you X-term, for example:

charles@charles-laptop ~> uname -r
2.6.24-16-generic

1.2 Learn the document for Linux Kernel Module (LKM)

Once you plan to do modules development, you should refer to: $SRCDIR/Document/kbuild/modules.txt

In my ubuntu 8.04, I can find it in /usr/src/linux-headers-2.6.24-16/Documentation/kbuild

Now let read the document for module development..

1). How to build external module

From modules.txt, we know the simplest make command:

make -C M=`pwd`
the means the kernel source path.

For the running kernel use:

make -C /lib/modules/`uname -r`/build M=`pwd`

The -C option: change to the directory before execute the make command.

Actually, that will result in search the Makefile in /lib/modules/2.6.24-16-generic/build in my OS.

Notice: Most of files in /lib/modules/2.6.24-16-generic/build actually link to /usr/src/linux-headers-2.6.24-16/

The M=`pwd`: define a MACRO required by Makefile in /lib/modules/2.6.24-16-generic/build, which can tell the directory of current exernal module code.

Therefore you needn't write your own Makefile for build your modules, that's completed by kbuild - a good machism for kernel modules.

2. make target

make -C $KDIR M=`pwd`
make -C $KDIR M=`pwd` all
make -C $KDIR M=`pwd` modules
make -C $KDIR M=`pwd` modules_install
make -C $KDIR M=`pwd` clean
make -C $KDIR M=`pwd` help
the first 3 commands are absolutely same. For more details, you can refer to modules.txt
文章评论

共有 0 条评论