HOWTO Build a Linux kernel module out-of-tree

This article is incomplete and was first written in June 2006
for the BeezNest technical website (http://glasnost.beeznest.org/articles/340).
It could happen that you badly need to use a newer kernel module than what is present on your existing Linux kernel (2.6.x), but you do not want to upgrade the whole kernel or rebuild it from scratch, because:
  • you cannot or don't want to reboot afterwards
  • you don't have the original source anymore (and your kernel was patched) or you don't want to download and install them, but you do have the kernel headers handy
  • other kernel modules on the machine do not yet support the newest kernel with the desired module version
It could be tricky to rebuild a module kernel like this, but thanks to those great kernel hackers, this is not the case.
  • Download the kernel module sources and unarchive them in some directory.
  • make sure you have the kernel headers somewhere
  • go to the kernel headers directory and, from there, type the following (provided mod_src_path points to the sources of your new module):
make M=mod_src_path
  • it will build the module for your kernel
  • try it with insmod
  • finally copy the .ko file(s) in the appropriate /lib/modules/`uname -r`/kernel/... directory.
An example (cifs 1.42b module for kernel 2.6.8) [1]:
1) download and unarchive new cifs (e.g. to ~/cifs-1.42b)
2) cd to the root of your kernel headers (e.g. /usr/src/linux-headers-2.6.8)
3) make M=~/cifs-1.42b/fs/cifs
4) if the build worked as expected then "/sbin/insmod ~/cifs-1.42b/fs/cifs/cifs.ko" (you will have to be root for this)
5) if you want to make this automatic you can copy the new cifs.ko to /lib/modules/2.6.8/kernel/fs/cifs/cifs.ko after first saving your existing version of that file

[1] many thanks to Steven French from Samba for this example and his help