/* This is my first driver module program */
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
static int __init hello_init(void)
{
printk(KERN_ALERT "Hello my first module \n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "Good Bye \n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello world Module");
MODULE_AUTHOR("SURENDRA PATIL");
2) Make file for the module
ifneq ($(KERNELRELEASE),)
obj-m := hello_driver.o
else
KDIR ?= /lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD)
endif
Note:Both file should be in the same directory.
3) when this make file is run hello_driver.ko will be produced.
4) insmod ./hello_driver.ko -> will insert the module into kernel modules. To see the message print the kernel dmesg logs.
you can see "Hello my first module" displayed in the dmesg log.
5) lsmod -> displays all the kernel modules
see that "hello_driver" module is displayed in kernel modules.
6) rmmod hello_driver.ko -> will remove the module from kernel
Note that "Good Bye" message is printed in dmesg kernel logs
Hurray you wrote a Linux kernel module now :-) keep it up :-)
Comments