Rebuilding a single kernel module

Posted by gnurbs on Wed 22 Jul 2009 at 23:27

This article shows how to rebuild only a single module that comes with the main kernel tree for folks that neither need nor want to rebuild the whole kernel. If you want to build an out-of-tree module, than that module's documentation is probably the best starting point.

To make the intention of this howto somewhat clearer, this is the Problem I had: I have an old box i use for server-purposes and want to extend its functionality to an access-point. It has an ath5k-driven card, and for my running kernel (2.6.29-2-686 debian sid build) ath5k needs to be patched to enable master mode. Since the box is somewhat slow, I did not want to rebuild the kernel, plus that would force me to restart and losing uptime ;)

OK here we go:

apt-get install linux-headers-`uname-r` linux-source-$YourKernelsVersion
cd /usr/src
tar xjf linux-source-$Version.tar.bz2

kernels Makefile has a target M=$DIR you can use to build single modules. However, running this in /usr/src/linux-source-$Version fails, at least on my box, since I had not used it to actually build a kernel. Header files of a complete Build are missing, I understand. For this reason, we use linux-headers-`uname -r`-directory. However, this directory does not contain everything we need, most of the header files reside in linux-headers-$Version-common dir. I simply copied its contents into linux-headers-`uname -r`. There are propably more elegant ways...

cd linux-headers-`uname -r`
cp -rf ../linux-headers-$Version-common/* .

Figure out what directory your wanted module resides in. For me this is drivers/net/wireless/ath5k

cd linux-headers-`uname -r`
mkdir -p Path/To/Module
cp -rf ../linux-source-$Version/Path/To/Module/* Path/To/Module
make -M=Path/To/Module

You propably know how to go on yourself, but for completeness sake:
Now you have compiled the module, check it runs properly. You may have to rmmod earlier versions of the module.
insmod Path/To/Module/Module.ko

If it works properly, its time to install it to your system:

cd /lib/modules/`uname -r`
mkdir -p Path/To/Module

if there is another build of the module you just compiled, you may want to do a backup:

mv Path/To/Module/Module.ko Path/To/Module/Module.ko.bak
cp /usr/src/linux-headers-`uname -r`/Path/To/Module/Module.ko Path/To/Module
depmod -a

Congratulations! If you made it this way, you finished. For me, it's now time to setup hostap...



Posted by Anonymous (195.98.xx.xx) on Thu 23 Jul 2009 at 07:21
green text is very bad to read :-(

[ Parent | Reply to this comment ]

Posted by Steve (80.68.xx.xx) on Thu 23 Jul 2009 at 15:13
[ Send Message | View Steve's Scratchpad | View Weblogs ]

One thing that has changed in recent kernels is that /lib/modules/$(name -r)/updates is checked for modules first.

So you don't need to backup and overwrite the existing module, just place your new one in the updates directory and it will be loaded in preference to the existing one.


[ Parent | Reply to this comment ]

Posted by Anonymous (87.177.xx.xx) on Thu 23 Jul 2009 at 20:53
I never imagined that the most difficult part when it comes to building kernel modules is reading the text...
Green text on white background is a really poor choice.

[ Parent | Reply to this comment ]

Posted by gnurbs (84.137.xx.xx) on Fri 24 Jul 2009 at 09:40
[ Send Message ]
I'd usually agree, but on my machine it's displayed in light green on black background. I used the [code]-bb-tag, so its propably the site admins who chose this design. maybe your browser has difficulties with the css?

[ Parent | Reply to this comment ]

Posted by Anonymous (87.177.xx.xx) on Sat 25 Jul 2009 at 00:47
No it doesn't have difficulties with CSS. But there was an unclosed <span> tag which colored half of the text green.

[ Parent | Reply to this comment ]

Posted by Anonymous (210.215.xx.xx) on Fri 24 Jul 2009 at 04:30
    mkdir -p /Path/To/Module

I assume the leading '/' in the directory chain is incorrect.

Also, I noticed that you copied the headers into your linux source directory, but then proceeded to copy the module source to the headers directory and compile it from there. Any reason why you didn't just compile it in the source directory, after copying headers?

    mv Path/To/Module/Module.ko Path/To/Module/Module.ko.bak
    cp Path/To/Module/Module.ko /lib/modules/`uname -r`/kernel/Path/To/Module

So you've renamed Module.ko to Module.ko.bak. Then you try to copy the Module.ko that you've just renamed?

Lastly, and this is just being picky, I noticed that the current directory in your root prompt is always the home dir. You may want to put in something a little more informative, to indicate what dir you want to be in.

[ Parent | Reply to this comment ]

Posted by gnurbs (84.137.xx.xx) on Fri 24 Jul 2009 at 10:45
[ Send Message ]
thx for you advice

actually, these root-prompts where automatically inserted by the [code]-bb-tag I used. obviously not the best choice, with this and the readability problem...
anyway I removed them now.

@copy to source: actually, I left out one step. sorry for this :/ I did not copy the headers into the source dir, but the linux-headers-common into the linux-headers-dir - did not use the source dir, propably because I thought it would fail, just because it failed before (when I tried before realising that I needed the headers...)

that part you quoted is mostly trash, youre right... should be correct now

[ Parent | Reply to this comment ]

Posted by Anonymous (212.2.xx.xx) on Tue 18 Aug 2009 at 23:53
I think you had to copy stuff around manually because you didn't run `make modules_prepare` etc. I've detailed the process here for rpm distros, but it should be easy enough to translate:

[ Parent | Reply to this comment ]

Posted by undefined (68.93.xx.xx) on Sat 12 Sep 2009 at 22:48
[ Send Message ]
your "make -M=Path/To/Module" is wrong.

it should be "make M=Path/To/Module", as the "M" is a variable (processed by the makefile), not an option (recognized by make).


[ Parent | Reply to this comment ]

Posted by stuart02 (2a01:0xx:0xx:0xxx:0xxx:0xxx:xx) on Mon 8 Mar 2010 at 22:52
[ Send Message ]
this is how I compiled the modules in drivers/usb/misc using Debian unstable.
sudo apt-get install linux-source-2.6.32 kernel-package linux-headers-`uname -r`
cd /usr/src
tar jxf linux-source-2.6.32.tar.bz2
cd linux-source-2.6.32
ln -s /boot/config-2.6.32-2-amd64 .config
ln -s /usr/src/linux-headers-2.6.32-2-amd64/Module.symvers Module.symvers
make modules_prepare
make M=drivers/usb/misc/
sudo insmod drivers/usb/misc/usbsevseg.ko
The above is my history slightly edited so please excuse any errors.


[ Parent | Reply to this comment ]

Posted by stuart02 (2a01:0xx:0xx:0xxx:0xxx:0xxx:xx) on Mon 8 Mar 2010 at 23:17
[ Send Message ]
as started above, /lib/modules/$(uname -r)/update is read before the standard modules
: root; mkdir /lib/modules/$(uname -r)/update
: root; cp drivers/usb/misc/usbsevseg.ko /lib/modules/$(uname -r)/update/.
: root; depmod -a
: root; rmmod usbsevseg
: root; modprobe -v usbsevseg
insmod /lib/modules/2.6.32-2-amd64/update/usbsevseg.ko 

[ Parent | Reply to this comment ]

Posted by Anonymous (80.200.xx.xx) on Sat 24 Sep 2011 at 07:38
Thanks, it works, I like this method better.

[ Parent | Reply to this comment ]

Posted by checkoff (188.102.xx.xx) on Thu 9 Aug 2012 at 17:14
[ Send Message ]
I've tried to follow the instructions to compile the module for xfs filesystem on debian (2.6.32-5-kirkwood) but make M=fs/xfs results in the following output:

/usr/src/linux-source-2.6.32# make M=fs/xfs
LD fs/xfs/built-in.o
Building modules, stage 2.
MODPOST 0 modules

and no xfs.ko-File was created. Any suggestions?

Hopefully anyone out there, who could help me!

[ Parent | Reply to this comment ]

Posted by Anonymous (37.107.xx.xx) on Mon 17 Dec 2012 at 20:25
I am having the exact same thing. Trying to compile fs/cifs. I tried with both

make M=fs/cifs 
make M=/absolute/path/to/fs/cifs

[ Parent | Reply to this comment ]

Posted by Anonymous (194.114.xx.xx) on Tue 23 Jul 2013 at 17:00
Thanks a lot for this post, helped me save at least a couple of hours!

[ Parent | Reply to this comment ]

Sign In







Current Poll

Which init system are you using in Debian?

( 1057 votes ~ 6 comments )