Thursday, 8 December 2016

Kernel module

        

         ************Linux Kernel Module*************


what is kernel module?

kernel module is a piece of code which added when we are used upon kernel demand.
it is use for the extend the functionality of kernel.

***.#make  command is use for the compile the kernel module.***

1).#lsmod command is use for the display list of kernel module.

2).#insmod command is use for the load the kernel module.

3).printk() is used for print the message in your logfile.

4).#dmesg is used for display message which present in logfile

5).#rmmod command is use for the remove the kernel module.
our first Kernel module

*********************************************************************************


 hello.c - The simplest kernel module.


#include <linux/module.h> /* Needed by all modules */#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)

{      printk(KERN_INFO "Hello world 1.\n");

/*

* A non 0 return means init_module failed;

* module can't be loaded

*/

return 0;

}

void cleanup_module(void)

{

printk(KERN_INFO "Goodbye world 1.\n");

}

module_init(init_module);

module_exit(cleanup_module);

Note:

Now create Makefile and keep our hello.c and Makefile in a folder and that folder keep on desktop.

just copy and paste below code in Makefile



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


save file and run #make command for compile the kernel module

No comments:

Post a Comment