Following is a very good startup program for Linux enthusiasts and beginners at linux kernel driver level. Any one can try this in a Linux x86 PC. Herewith given Code and Makefile for compilation.

————————————————————————————————-

module1.c

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

int init_module(void) {
printk(KERN_INFO “My first Linux driver\n”);
return 0;
}

void cleanup_module(void) {
printk(KERN_INFO “Exiting My first Linux driver\n”);
}

————————————————————————————————-

Makefile

KVERSION = $(shell uname -r)
obj-m = module1.o

all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

 

————————————————————————————————-

Steps to compile and execute

  1. First create a new directory and put this c file and Makefile in that.
  2. Take the linux terminal and go to this directory
  3. Type make all
  4. You can see following files created in the directory
    module1.ko
    module1.mod.c
    module1.mod.o
    module1.o
    modules.order
    Module.symvers
  5. Out of this module1.ko is the newly created Linux kernel module.
  6. Type insmod module1.ko. This command will insert the module to the Linux kernel area and execute the program. Function init_module will be the first function which get executed.
  7. Type dmesg. You can see the print comes at the tail end of log as “My first Linux driver”.
  8. Type lsmod. It will list all the modules already running in your PC and you can see your module also.
  9. For stopping or removing your module type rmmod module1.
  10. Type lsmod. It won’t list your module in the list.
  11. Type dmesg. You can see the print comes as “Exiting My first Linux driver”at the tail end of log.

Try this and All the very best for all the beginners.