Kernel Module Load Unload

Compile, Load and Unload Linux Kernel Module:

1. List Kernel Module:

lsmod | head -10

2. Create hello.c file

vi hello.c

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
printk(KERN_INFO “Welcome to OST Today…..\n”);
return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO “Bye, Take care….\n”);
}
3. Create Makefile

vi 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

4. Compile Module/Program
make

5. Module Information:
modinfo hello.ko

6. Insert Module:
insmod hello.ko

7. Remove Module:
rmmod –all hello.ko