How to add a new Linux Kernel Module (LKM)
Open any editor like vi or gedit in a shell prompt and the type the following
/* Name of the file is hello.c */
printk(KERN_INFO “Hello world.n”);
printk(KERN_INFO “Goodbye worldn”);
The above is a simple C program that displays “Hello world” when the module is inserted and displays “Goodbye world” when the module is removed.
The program can be compiled using gcc compiler in the shell prompt itself, but we need to write the commands twice or more. So to compile shortly let us write a makefile to compile the above program
The make file should be named as Makefile (See the First letter M is uppercase and there should not be any space between Make and file)
Copy and paste the following lines in an editor (vi or gedit)
obj-m += hello.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
after this, execute the following command
You will get the following output which indicates there is no error
make -C /lib/modules/188.8.131.52-117.fc10.i686/build M=/home/pradeepkumar/lsp modules
make: Entering directory `/usr/src/kernels/184.108.40.206-117.fc10.i686′
Building modules, stage 2.
MODPOST 1 modules
make: Leaving directory `/usr/src/kernels/220.127.116.11-117.fc10.i686′
The make command creates new set of files like hello.ko, hello.mod.o, modules.order, etc
The .ko indicates it is a kernel object, but ordinary C compiler generates only an object file..
go to the shell prompt (open the terminal) execute the command one by one
The above command waits for the root password, please provide the password
inserting the module
listing all the modules running under the kernel, you can see the first module will be listed as hello
you can see thelast line says hello world
removing the module
you can see the last line says goodbye world
Like this we can write many modules and can be inserted to the kernel..