nixCraft Poll

Topics

Howto: build Linux kernel module against installed kernel w/o full kernel source tree

Posted by Vivek Gite [Last updated: October 26, 2007]

Recently I received a question via email:

How do I build Linux kernel module against installed or running Linux kernel? Do I need to install new kernel source tree from kernel.org?

To be frank you do not need a new full source tree in order to just compile or build module against the running kernel i.e an exploded source tree is not required to build kernel driver or module. The instruction outlined below will benefit immensely to a developers/power users.

This is essential because if you just want to compile and install driver for new hardware such as Wireless card or SCSI device etc. With following method, you will save the time, as you are not going to compile entire Linux kernel.

Please note that to work with this hack you just need the Linux kernel headers and not the full kernel source tree. Install the linux-kernel-headers package which provides headers from the Linux kernel. These headers are used by the installed headers for GNU glibc and other system libraries as well as compiling modules. Use following command to install kernel headers:
# apt-get install kernel-headers-2.6.xx.xx.xx

Replace xx.xx with your actual running kernel version (e.g. 2.6.8.-2) and architecture name (e.g. 686/em64t/amd64). Use uname -r command to get actual kernel version name. Please note that above command will only install kernel headers and not the entire kernel source-code tree.

All you need to do is change Makefile to use current kernel build directory. You can obtain this directory name by typing following command:
$ ls -d /lib/modules/$(uname -r)/buildOutput:

/lib/modules/2.6.15.4/build

Let, say you have .c source code file called hello.c. Now create a Makefile as follows in the directory containing hello.c program / file:
$ vi Makefile
Append following text:

obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

Save and close the file. Type the following command to build the hello.ko module:
$ make

To load Linux kernel module type the command:
# modprobe hello

See a detailed and complete example with C source code - how to Compiling Linux kernel module for more information.

Updated for accuracy.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. chesss Says:

    would this work with suspend2 patching as well?

  2. Anonymous Says:

    This is untrue, /lib/modules/$(shell uname -r)/build is a symlink to the source tree. Title is misleading, you always need the at least the headers to build kernel modules.

  3. zhangmaike Says:

    on my system, /lib/modules/KERNELVERSION/build is a symlink to the corresponding full source tree.

  4. nixcraft Says:

    Yes you need kernel headers but not source code from kernel.org (45+ MB tar ball). So title and post is correct.

  5. nixcraft Says:

    You should have mentioned your distro name. I have tested this on Red Hat, Cent OS, FC and Debian and it works perfectly.

    On some distro it makes a soft link to kernel header to /usr/src/version but you don\’t need source code. Just delete rest of linux kernel source code (except for headers). And try out this hack

  6. s.kartikeyan Says:

    probably kernel-devel*.rpm is required in addition to makefile
    using /lib/modules.

  7. Jon Eastwood Says:

    You are 100% correct - you require the kernel headers
    whereas the source code from kernel.org is not required.

  8. links for 2007-02-15 « Where Is All This Leading To? Says:

    [...] Howto: build Linux kernel module against installed kernel w/o full kernel source tree | nixCraft (tags: linux kernel howto Reference programming module tutorial opensource compile) [...]

  9. Frank Says:

    Here’s one for you… I have a network interface that I need to re-compile every time the kernel is updated. This is for the realtek 8169 NIC.
    Any way to automate this task on a yum update of a kernel?

    If you are running the target kernel, then you should be able to do :

    # make clean modules (as root or with sudo)
    # make install
    # depmod -a
    # insmod ./src/r8169.ko

    You can check whether the driver is loaded by using following commands.

    # lsmod | grep r8169
    # ifconfig -a

    If there is a device name, ethX, shown on the monitor, the linux
    driver is loaded. Then, you can use the following command to activate
    the ethX.

    # ifconfig ethX

    ,where X=0,1,2,…

    If I go though this process on the local box after a kernel upgrade, I’m good to go, but it sucks that I have to do this..

    Thanks,
    Frank.

  10. Alexander Mestiashvili Says:

    this Makefile did not work for me .
    i used this one


    obj-m += hello-1.o

    all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

    clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

  11. Daniel Says:

    This is not clear enough for linux beginners. I am so sad that google placed this page so high in rankings.

    Maybe you should try to make it a bit easier to understand (give more details or explain why you do that ‘hello.c’ thing).

    Sincerely,
    Dan

  12. vivek Says:

    Daniel,

    You need to see hello.c (module) example here.

    HTH

  13. almassian Says:

    I don’t know how can I built a makefile?!
    where should I define the PWD??!
    please help me, I’m a beginner :D :-p

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.