Wednesday, December 23, 2015

Writing the first kernel module

Steps to create user defined kernel module......

1) Before writing any kernel module we have to check whether require header files such as kernel.h, init.h and module.h are available with our kernel source code or not. If it is not available in our kernel module then we have to download by using...
 i) In Ubuntu:-
                       sudo apt-get install build-essential linux-headers-$(uname -r).
 ii) In Linux:-
                       main menu-->system setting-->administration-->add/remove application/software.

2) Create new folder in desktop and create two new file in that folder. One file having extention .c which contains the source code of our kernel module and other file is Makefile which is build our kernel module. Both this file is reside in our same folder.

3) Write the source code of kernel module in .c file.

4) Write a command to build our kernel module in our Makefile.

5) Run "make" utility which is use to run Makefile. If there is error in Makefile, we have to solve that error by editing the Makefile.

6) After building our kernel module successfully. We get  .ko (kernel object) and .o (object) file in our current directory.

7) We can check information of our kernel object by using the command.
                  "modinfo filename.ko"

8) After getting .ko file we have to insert that kernel object insert inside kernel image by using the command.
                    "sudo insmod filename.ko "

9) By using "insmod" command our kernel module gets inserted in kernel image whether it is successfully inserted or not is to be check by looking "/proc/module" file or "lsmod" command.

10) After inserting module inside kernel image. init_module() function gets called automatically. If that function contains printk() function call which is use to write the contents inside kernel log file. To get the contents of kernel file by using "dmesg" command or we can open the file which contains the kernel log named as "/var/log/syslog". Inside that file we get the data which is written in printk() function.

11) Now remove that module from the kernel image by using command.
                  "sudo rmmod filename.ko"

12) To check whether our module gets removed from kernel image again we have to call "lsmod" command.
--------------------------------------------------------------------------------------------------------------------------
Steps to write kernel module program.

1) Create .c file having any name.

2) Include require header files for our kernel module. For basic kernel module we have to include kernel.h, module.h and init.h header file.

3) Write a user defined function named as init_module( ) which accept nothing and it should return integer value

4) A code which is written inside that function gets executed automatically when our kernel module gets inserted into kernel by using insmod command and intial stage instead write some other logic we can write prink() function (printk() function is like a normal printf() function) which is use to write data in kernel log file.
        printk() function require two parameter :-
 i) First parameter is macro it indicate priority of our function there are 8 different macros are our arguments. If our message is high priority we can write KERN_ALERT and if priority is normal priority then we can write KERN_INFO macro.

ii) Second parameter of printk() function is like printf() function which is the string that we want to print with the desired format specifier like %d , %s etc.

5) This init_module() is return 0 to indicate the success and other value indicate the failure.

6) We have to write other user defined function named as cleanup_module() function which accept nothing and return nothing.
         Generally this function is use to write for resource deallocation which are allocated by init_module() function.
         This function gets automatically called when our kernel module is removed by rmmod command.

7) The init_module() function is similar as constructor  and cleanup_module() function is similar as destructor in C++  and JAVA language.

-------------------------------------------------------------------------------------------------------------------------
/* First  Kernel Module Program */
/* Program Name:- hello-1.c   */

// This header file is required for KERN_INFO, KERN_ALERT etc macros.
#include<linux/kernel.h>

// This header file is required for all kernel module.
#include<linux/module.h>

// This function gets called automatically when module gets loaded by insmod command
// This function returns 0 if it successfully executed otherwise it returns non zero value

int init_module(void)
{
            printk(KERN_INFO "Hello World \n");
            return 0;
}

// This function gets called automatically when module gets removed from memory by rmmod
// command.

void cleanup_module(void)
{
                  printk(KERN_INFO "Good Bye \n");
}

--------------------------------------------------------------------------------------------------------------------------

/*  Makefile */
// Understand that in makefile name,  the first alphabet M should be capital otherwise compiler
// generate error.
--------------------------------------------------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------------------------------------------------

Steps to run the kernel module

1) Open the terminal and change directory up to your folder we have to save our kernel module program.

2) Type "make" command and enter to execute the kernel module.
     "make"

3) After that we have to give any error solve it. Only changing the Makefile.

4) Type the "ls" command to see the all files. We have to check whether .ko file is created or not.

5) If .ko file is created then we have to insert it into kernel image by using the command:-
     "sudo insmod hello-1.ko"

6) To get information about our kernel module. Type "modinfo" command.
     "modinfo hello-1.ko "

7) To check whether the our kernel module is successfully insert or not. Type "lsmod" command.
    " lsmod | grep hello-1.ko"   or  "lsmod"

8)  After this to see the printk() function message in "var/log/syslog" kernel log file  or simply type "dmesg" command.
   Open that syslog file using "cat syslog" command. or "dmesg" command

9)  After the completing the kernel module task. We have to remove from kernel image by using "rmmod" command.
   
    "sudo rmmod hello-1.ko"
-------------------------------------------------------------------------------------------------------------------------
thank you,

If you have regarding any query contact us:
Mob. No. :- +91 9767623508.
Email ID:- sunilbhagwat24@gmail.com.



No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...