Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 4 Kernel Module Operating System Lab.

Similar presentations


Presentation on theme: "Lab 4 Kernel Module Operating System Lab."— Presentation transcript:

1 Lab 4 Kernel Module Operating System Lab

2 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

3 Files relate to modules
/etc/modprobe.conf Kernel will mount modules according to this file when boot on. /lib/modules/ ***/modules.dep If other modules must be loaded before the requested module may be loaded. /lib/modules/ ***/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

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

5 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/ ***/modules.dep. ]#modprobe [-lcfr] modules_name NCHU System & Network Lab

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

7 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/ ***/kernel rmmod remove modules ]#insmod module_name.ko ]#rmmod module_name NCHU System & Network Lab

8 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

9 Makefile for a Basic Kernel Module (1/2)
obj-m +=testfunction.o all: make –C /lib/modules/linux /build M=$(PWD) modules clean: make –C /lib/modules/linux /build M=$(PWD) clean NCHU System & Network Lab

10 Makefile for a Basic Kernel Module (2/2)
]#make make −C /lib/modules/ /build M=/root/Desktop/ make[1]: Entering directory `/usr/src/ 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/ ' NCHU System & Network Lab

11 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

12 NCHU System & Network Lab
Reference The Linux Kernel Module Programming Guide Google Keyword “kernel module” Linux kernel module and TCP/IP program design NCHU System & Network Lab

13 NCHU System & Network Lab

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

15 NCHU System & Network Lab
Exercise(2/8) ]#vim /usr/src/linux ***/net/core/dev.c NCHU System & Network Lab

16 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

17 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

18 NCHU System & Network Lab
Exercise(5/8) ]#vim /usr/src/linux ***/net/core/dev.c dev_queue_xmit() int (*testfunction)(struct sk_buff *)=0; if(testfunction){ testfunction(skb); } NCHU System & Network Lab

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

20 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

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

22 NCHU System & Network Lab
Reference The Linux Kernel Module Programming Guide Google Keyword “kernel module” Linux kernel module and TCP/IP program design NCHU System & Network Lab


Download ppt "Lab 4 Kernel Module Operating System Lab."

Similar presentations


Ads by Google