Kernel Module Programs by NwPa1VBN


									Kernel Module Program Examples

Get the file from the OS web site


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

int hello_module_init(void)
         printk(KERN_EMERG "Hello Module~! I'm in Kernel\n");
         return 0;

void hello_module_cleanup(void)
        printk("<0>Bye Module~!\n");




MY_TARGET        := hello_module.ko
obj-m            := hello_module.o

KERNEL_DIR := /lib/modules/$(shell uname -r)/build
MODULE_DIR := /lib/modules/$(shell uname -r)/kernel/hello_module
PWD        := $(shell pwd)

default :
          $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
install :
          mkdir -p $(MODULE_DIR)
          cp -f $(MY_TARGET) $(MODULE_DIR)
clean :
          $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean

   You need to use kernel module make mechanism available in your
    /lib/modules/your_kernel_version/build directory.
   The hello_module.c uses module_init() macro and module_exit() macro to execute hello_module_init
    function by insmod command and hello_module_cleanup function by rmmod command.
   Steps to create hello_module.ko kernel module object (Some commands need super user privilege).
        o In the source directory $make
        o See files created by make
        o $insmod hello_module.ko //then you can see Hello Module~ I/m in Kernel message created by
             syslogd (by $tail –f /var/log/messages )
        o $rmmod hello_module //you will see Bye Module~! From /var/log/messages
        o make install will copy the module to MODULE_DIR
        o make clean will remove some generated files

 The following link is for kernel module programming guide

Character device driver kernel module generation. Some commands need super user privilege.

 Move to the chr_driver/driver directory
 $make
 $insmod chr_test.ko //you will see a message like major NO= 253 (from /var/log/messages)
        o We have 0-255 major numbers and 0-255 minor numbers to identify a unique driver
 $mknod /dev/mydrv c 253 0 //create a special file to access character device driver module you created.
    $man mknod
 Move to the chr_driver/app directory $gcc –O –o mydrv_test mydrv_test.c //optimize the executable
 $./mydrv_test //see whether you get the correct result AAAAAAAAAAA.. ABC….Z

Block device driver kernel module generation. Some commands need super user privilege.

 Move to the blk_driver directory
 $make
 $insmod blk_test.ko //you will see major no such as 252
 $mknod /dev/mydrvb b 252 0 //create a special file to access block device driver module you created
    $man mknod
 $mke2fs /dev/mydrvb //make a new ext2/ext3 file system that is accessed by this driver $man mke2fs and
    $man mkfs for creating ext4 file system
 $mkdir test
 $mount –t ext2 –o loop /dev/mydrvb ./test //mount a new ext2 file system to ./test mount point.
        o create loopback file system without using disk partition
 $ls will show you lost+found file //this means you have a new file system created and accessed through the
    new block device driver. The new block driver functions will be invoked through the chain of command from
    application layer, via system call layer, via specific file layer, via specific file system layer, to device driver
    layer. Finally to hardware layer.

NOTE: blk_queue_hardsect_size has been renamed to blk_queue_logical_block_size (Since 2.6.30…).
Line number 131 of the blk_test.c should be updated to compile the program.

To top