but only a part of ppl succeed in writing and running a proper module..
cos there are various reasons which may lead to failure ..like ur distribution,the path where you have placed your kernel source directories,kernel versions,etc,etc..
k..in this post am gonna present a basic kernel module which will print "hello " when inserted into the kernel and "bye " when you remove it from the kernel..
here we go!!
i have checked this on redhat,fedora and debian..it never ran on a suse box :(
//file name :hello.c
static int hello_init(void)
static void hello_exit(void)
does this needs an explanation?nyways..the header files asusual..the license which the source supports..here it is dual..supporting both gpl and bsd..
two user-defined functions......hello_init and hello_exit..the names can be nything cos they r user-defined..
u can define the body as ur wish..here i have attempted to print "hello" when the function hello_init is run
and "bye" when hello_exit is run..
as we have printf library function,printk is used to print in the kernel ,i mean used in lkmp..
for kernel versions 2.4.* the syntax of printk was
here <1> denotes the priority..the lower the number the higher the priority.(as we all know :) )
then we provide two entry points.
in the first one we ask the hello_init to be executed i.e., module_init(hello_init)
and the same applies for the second one too .................
and now its time to compile and run..
first write a Makefile
$ vi Makefile
obj-m := hello.o
and save the file
now you gotta compile it..
by using the make command with -C switch
here u hav to mention the path where u have compiled the kernel source(oops i forgot to tell how to compile the kernel source..k..in the next post for sure :) )
in my comp and most of the comps the path will be /usr/src/kernels/linux-*
in my comp the command is to be
$ make -C /usr/src/kernels/184.108.40.206-42.fc8-i686 M=$PWD modules
on the terminal u wil get the following:
make: Entering directory `/usr/src/kernels/220.127.116.11-42.fc8-i686'
CC [M] /home/sakthi/lkmp/hello.o
Building modules, stage 2.
MODPOST 1 modules
LD [M] /home/sakthi/lkmp/hello.ko
make: Leaving directory `/usr/src/kernels/18.104.22.168-42.fc8-i686'
if u get this then its done :)
if u dont :( pls check the path name and Makefile once..
now if u see the folder where u have ur hello.c u can find the following files:
and now its time to run (execute the .ko file) the module..
hey b4 that u need to change the run level yaar..sometimes if u in gui it wont run..
u will get:
if u check ur /proc/kallsysms file and /proc/modules file u can find ur module name man!!
$cat /proc/kallsymsgrep hello
$cat /proc/modulesgrep hello
and now its time to remove
bye will be displayed..
to come back to gui.......ctrl+alt+f7
its done..so u have written,compiled,inserted and removed back a proper kernel module successfully :)
try this out..its good..
all the best