Sunday, May 29, 2011

Loading modules for kernels with EXTRAVERSION

It's easy to write modules for kernels that are forked off main kernel tree
This is what you do roughly

Compiling Kernel
  1. make menuconfig
  2. make 
  3. make modules
  4. make modules_install
  5. copy the image to your boot partition and update the boot loader
Compiling/Loading your modules
  1. Download kernel sources and do a make oldconfig && make prepare there
  2. Create a module and its specific Makefile
  3. Compile the module pointing to this kernel directory for headers
  4. Load the compiled module via insmod 

However most of the distributions come with several variations of stock kernel. The intention there is to make sure that people choose the one that best describes their needs. For example the slackware box I'm working on has 3 versions of 2.6.21.5 kernel. To distinguish these versions from each other, the distributions use an extraversion string. This extraversion string is meant to throw a hint about the specific kernel. For example the 3 different versions on my slack box are 2.6.21.5-smp, 2.6.21.5-hugesmp and 2.6.21.5-huge.

You run into a problem while trying to load modules for  such kernels. In such cases, the module compiles ok but gives an error when we try to load the same. More verbose module load error may be found in dmesg output. It generally has something like

hello: version magic '2.6.21.5 SMP mod_unload 686 ' should be '2.6.21.5-smp SMP mod_unload 686 '

To get rid of this error, edit your .config file to have the same EXTRAVERSION string a sin the kernel that you're trying to load. Alternatively get the kernel .config file from the distribution that you're running. As a hint, these files are generally installed where the kernel is, typically in /boot directory. Then fire the following commands to use this config
  1. make oldconfig
  2. make prepare
Now try recompiling the  kernel like this


make -C /usr/src/linux-2.6.21.5 M=`pwd` modules

The Makefile looks like this

===== Makefile ==========
obj-m += hello.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

===== End Makefile ======

This will set you up compiling and loading modules with a stock kernel with an extraversion string.




Tuesday, April 19, 2011

Finally!!

You just love the posts that start like this. Right??

Anyways.. I've finally decided to create a blog that details the interesting tech questions that I'm working on. I hope to maintain a collage of sorts of my thoughts which I can peek at whenever I need inspiration.


So here we go!!