Tuesday, June 25, 2013

Creating the Module

First, find the source that your current Linux kernel was compiled from. You can save the mymodule.c in any one of the folder under staging or drivers directory


mymodule.c


#include <linux/module.h>
#include <linux/config.h>
#include <linux/init.h>

static int __init mymodule_init(void)
{
 printk ("My module worked!\n");
        return 0;
}

static void __exit mymodule_exit(void)
{
 printk ("Unloading my module.\n");
        return;
}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_LICENSE("GPL");

Edit the Makefile in the same directory. Add this line
obj-m += mymodule.o
Compile your module:
 # make -C [top directory of your kernel source] SUBDIRS=$PWD modules
Load the module:
 # insmod ./mymodule.o
And check to see if your message printed out:
# dmesg
At the end of the output:
My module worked!
Now remove the kernel module:
 # rmmod mymodule
Check the output of dmesg again, you could see the output:
 Unloading my module.


1 comment:

  1. Its impressive to know something about your note on Linux Course. Please do share your articles like this your articles for our awareness. Mostly we do also provide Online Training on Cub training linux course.

    ReplyDelete