Howto: build Linux kernel module against installed kernel w/o full kernel source tree
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:
- How to: Compile Linux kernel modules
- Top 10 Hits and Zeitgeist of 2006 for your edification and fun
- Programming Tutorial: How To Write a FreeBSD Kernel Module ( Driver )
- How to: Compile Linux kernel 2.6
- Best Linux / UNIX Posts of 2007: Part ~ II
Discussion on This Article:
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!
Tags: c source code, kernel driver, kernel source code, kernel source tree, kernel version, linux kernel module, make_command, modprobe_command


would this work with suspend2 patching as well?
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.
on my system, /lib/modules/KERNELVERSION/build is a symlink to the corresponding full source tree.
Yes you need kernel headers but not source code from kernel.org (45+ MB tar ball). So title and post is correct.
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
probably kernel-devel*.rpm is required in addition to makefile
using /lib/modules.
You are 100% correct - you require the kernel headers
whereas the source code from kernel.org is not required.
[...] 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) [...]
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.
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
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
Daniel,
You need to see hello.c (module) example here.
HTH
I don’t know how can I built a makefile?!
:-p
where should I define the PWD??!
please help me, I’m a beginner