Friday, September 24, 2010

Sample makefile to build a Loadable Kernel Module (LKM)

Step 1: Open a file with file name called qc (quick compile)
        
          vi qc
          #Write the below contents to the file
          #!/bin/bash
           KDIR=/lib/modules/`uname -r`/source
           echo "obj-m := $1.o" > Makefile
           make -C $KDIR M=`pwd` modules
          rm -f .$1*
          rm -rf .tmp_versions
          rm -f $1.o
          rm -f $1.mod*
         rm -f modules.order
         rm -f Module.symvers
         rm -f Makefile

Save the Makefile and close the same.
Now build your KLM by paasing your programe name as follows
 ./qc <programname>
Ex: ./qc driver

The output will be a driver.ko (kernel object file)

This can be loaded and removed into the kernel using the follwing commands
insmod - insert file into the kernel
rmmod - remove the module from kernel

example: insmod driver.ko  #Now you can see the driver in the kernel by running "lsmod" command
              rmmod driver  # Now this will not be seen in "lsmod"

        

0 comments:

Post a Comment