Home > Coding > Trouble shooting for writing your first Linux module on Ubuntu

Trouble shooting for writing your first Linux module on Ubuntu

April 15th, 2009

As I was told, since Linux Kernel 2.6, if you want to write kernel modules (like device drivers) on Linux, you should have a built kernel tree. To do this, you need to download the kernel source code and compile it. For this part, you can refer to this very well written “how-to”. But during and after this, there are some problems. I want to keep a note of it, so here they are:

By the way, this note is for Ubuntu, but it should apply to Debian as well.

1. Install C header files
C headers are not installed by default on Ubuntu. So you may see errors like “stdio.h cannot be found”. The following command can solve it for you.

sudo apt-get install libc6-dev



2. If you cannot run “make menuconfig”
menuconfig can save you a lot of time if you want to keep just the default configurations. But it depends on Ncurses. To install ncurses libraries, you can use this command:

sudo apt-get install libncurses5-dev



3. How to compile your first module?
If you have a module named test.c. You need to write a Makefile like this:

obj-m  :=  test.o

Yes, just one line. Note, it’s “test.o”, not “test.c”. Save it under the same directory as your source code. Now you can compile it, using a command like this:

make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

After this is done, you will see a bunch of files generated under the name of “test” but different suffix. Load the module with

sudo insmod ./test.ko

unload the module with

sudo rmmod test



4. Check module output
If you have used printk in the init and exit functions, you expect to see output in the console. But you won’t, because all such output on Ubuntu has been redirected to file “/var/log/kern.log”. You can use “less” to see it.

less /var/log/kern.log

The latest output is at the end of this very long file.



5. See if your module is loaded or not
In this case, your module is named “test”. You can use the “proc” interface to see if this module has been loaded into the kernel:

cat /proc/modules | grep test

The result would look something like this:

test 2304 0 – live 0xe0959000

I don’t the meaning of each field, but if the module is not loaded, you will see nothing.

Coding

  1. No comments yet.
  1. No trackbacks yet.