Inserting a module in the Linux Kernel

The following article contains source code and steps for inserting a LOADABLE-KERNEL-MODULE in the linux kernel. P.S. : It is recommended that you download the kernel source code first from here, compile the new kernel and insert the module in the newly compiled kernel. Steps for Linux Kernel Compilation are available here.

Module Name: Hello World. <hello.c>


/*
Hello.c : Simplest Kernel Module
*/
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#define DRIVER_AUTHOR “Ashwin Tumma”
#define DRIVER_DESC “A Sample Driver”
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
int init_module(void)
{
printk(KERN_INFO “Hello World\n”);
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO “Goodbye World\n”);
}

Makefile:

#insmod hello.ko

Check in /var/log/messages : Your message init_module() gets displayed there.

#lsmod | grep hello
Will give the module number if the module is loaded in the Kernel.

#rmmod hello
Will remove the module from the Kernel.
Again check /var/log/messages : Your message exit_module() gets displayed there.

Steps for writing a character device driver : Steps Character_Device_Driver_Steps.pdf

Here’s a screenshot:

Creative Commons License
Inserting a module in the Linux Kernel by Ashwin Tumma is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 India License.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: