Kernel Module

Document Sample
Kernel Module Powered By Docstoc
					    Lab 4
Kernel Module
Operating System Lab




    NCHU System & Network Lab
        What is a Kernel Module?
• Modules are pieces of code that can be loaded and unloaded
  into the kernel upon demand.

• They extend the functionality of the kernel without the need to
  recompile and reboot the system.

• Example
   – One type of module is the device driver, which allows the kernel to
     access hardware connected to the system.

   – Without modules, we would have to build monolithic kernels and add
     new functionality directly into the kernel image.


                            NCHU System & Network Lab
             Files relate to modules
• /etc/modprobe.conf
   – Kernel will mount modules according to this file when boot on.
• /lib/modules/2.6.22.***/modules.dep
   – If other modules must be loaded before the requested module may be
     loaded.
• /lib/modules/2.6.22.***/kernel
   – All modules of a kernel are in it.
• /proc/modules
   – what modules are already loaded into the kernel by reading this file




                             NCHU System & Network Lab
    How Do Modules Get Into The
           Kernel? (1/4)
• lsmod
   – what modules are already loaded into the kernel by reading
     the file /proc/modules.
[root@localhost~ ]#lsmod




                           NCHU System & Network Lab
  How Do Modules Get Into The
         Kernel? (2/4)
• modprobe
  – This instruction can load the designated specific
    module , or a group of interdependent module .
     • according to /lib/modules/2.6.22.***/modules.dep.
 [root@localhost~ ]#modprobe [-lcfr] modules_name




                         NCHU System & Network Lab
   How Do Modules Get Into The
          Kernel? (3/4)
• depmod [-aens]
   – the file /lib/modules/version/modules.dep is created by depmod −a.
   [root@localhost~ ]#depmod -a
• modinfo
   – show the information of a module.
   [root@localhost~ ]#modinfo module_name




                           NCHU System & Network Lab
How Do Modules Get Into The Kernel?
             (4/4)
• insmod
  – similar to modprobe, but it can load the modules that aren’t in
     /lib/modules/2.6.22.***/kernel
  [root@localhost~ ]#insmod module_name.ko
• rmmod
  – remove modules
  [root@localhost~ ]#rmmod module_name




                           NCHU System & Network Lab
    Hello, World: The Simplest
             Module
/*
 * hello.c − The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
    printk("Hello world 1.\n");
          return 0;
}
void cleanup_module(void)
{
    printk("Goodbye world 1.\n");
}
MODULE_LICENSE(“GPL")

                       NCHU System & Network Lab
  Makefile for a Basic Kernel Module
                 (1/2)
obj-m +=testfunction.o

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




                         NCHU System & Network Lab
  Makefile for a Basic Kernel Module
                 (2/2)
[root@localhost~ ]#make
make −C /lib/modules/2.6.22.6/build M=/root/Desktop/
make[1]: Entering directory `/usr/src/2.6.22.6
   CC [M] /root/Desktop/hello.o
Building modules, stage 2.
MODPOS
   CC /root/Desktop/hello.mod.o
   LD [M] /root/Desktop/hello.ko
make[1]: Leaving directory `/usr/src/2.6.22.6'




                           NCHU System & Network Lab
                          Exercise
• We want to `spy' on a certain user, and to printk() a message
  whenever that user opens a file.

• Towards this end, we replace the system call to open a file
  with our own function, called our_sys_open.

• This function checks the uid (user's id) of the current process,
  and if it's equal to the uid we spy on, it calls printk() to display
  the name of the file to be opened.

• Then, either way, it calls the original open() function with the
  same parameters, to actually open the file.

                          NCHU System & Network Lab
                     Reference
• The Linux Kernel Module Programming Guide
  – http://www.faqs.org/docs/kernel/
• Google
  – Keyword “kernel module”
• Linux kernel module and TCP/IP program design




                      NCHU System & Network Lab
NCHU System & Network Lab
Exercise (1/8)




  NCHU System & Network Lab
                       Exercise(2/8)
• [root@localhost~ ]#vim /usr/src/linux2.6.22.***/net/core/dev.c




                            NCHU System & Network Lab
                Exercise (3/8)
• From previous two slides, we can use
  command “ping” to show some message.

• If we want to change the message showed, we
  should re-edit the kernel, re-compile it, and
  reboot each times.
  – There is no efficiency.




                    NCHU System & Network Lab
                   Exercise(4/8)
• Use kernel module to avoid recompiling kernel when
  we edit the kernel each times.

• So, we define a back door procedure in the kernel.
   – In other words, we make a hole in the kernel, and we can
     put the kernel module in it.


• Write a kernel module that can make the “ping”
  instruction show “hello world”

                       NCHU System & Network Lab
                               Exercise(5/8)
•   [root@localhost~ ]#vim /usr/src/linux2.6.22.***/net/core/dev.c
      – dev_queue_xmit()
          int (*testfunction)(struct sk_buff *)=0;

          if(testfunction){
              testfunction(skb);
          }




                                     NCHU System & Network Lab
             Exercise(6/8)
EXPORT_SYMBOL(testfunction);




                NCHU System & Network Lab
                   Exercise(7/8)
• Include file
   –   <linux/module.h>
   –   <linux/kernel.h>
   –   <linux/skbuff.h>
   –   <linux/ip.h>




                          NCHU System & Network Lab
Exercise(8/8)




  NCHU System & Network Lab
                     Reference
• The Linux Kernel Module Programming Guide
  – http://www.faqs.org/docs/kernel/
• Google
  – Keyword “kernel module”
• Linux kernel module and TCP/IP program design




                      NCHU System & Network Lab

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:12/2/2011
language:English
pages:22