<<

In this report I will mention the steps to simple kernel module in environment

Structure of the module :­ init_module() { // code that initializes the kernel module data structures //and may call some helping functions //entry function of the module

//This functions is called when the module is loaded into kernel using “insmod” //insmod stands for kernel module } cleanup_module() { //code that frees all the module data structures etc // function of the module

//This functions is called when the module is unloaded from kernel using “rmmod”

// rmmod stands for remove module i.e unload the loadable module

} some other helping functions()

Structure of makefile :­ Say the module is written in named as hello. obj­m+=hello.o all: ­c /lib/modules/$(shell ­)/build M=$() modules clean: make ­c /lib/module/$(shell uname ­r)/build M=$(pwd) clean

Note:­ please make a note of tab before make command

Command to build an external module:­ make ­c M=’pwd’

Command to build for running kernel use:­ make ­c /lib/modules/’uname ­r’/build M=’pwd’ uname command gives the underlined machine kernel version when passed with ­r option

M is used to tell kbulid that an external module is being built

To install modules that were just built:­ make ­c M=’pwd’ modules­install

Steps:­ 1) write the module with exit and entry functions of the module

2) create a makefile as shown in the above example

3) execute makefile

4)use insmod to load the module into kernel insmod ./hello.ko

5) we can see the loaded module in /proc/modules file

6)we can view the logs created by module in /var/log/messages

a) If syslod is not installed we can't view the messages use command sudo apt­get install inetuils­syslogd to install it

7) we can remove the module by using command sudo rmmod ./hello.ko

The entry and exit functions is supposed to have standard name as shown in the above example before kernel 2.6 version.

Above 2.6 version we can have our own customized name for both the functions and they are recognised by kernel by using module_init() and module_exit() macros

Note:­ The macros have to be written after the declaration/definition of the entry and exit functions

Init macro causes the init function to be discarded and its memory freed once the init function finishes for built­in­driver, and it does not work for loadable modules exit macro caused the omission of the function when the module is built into the kernel Command line arguments can also be given to the modules using module_param() macro

If module is spanned across multiple files, we can create a module by following lines in makefile files:­ start.c stop.c obj­m+=startstop.o startstop­objs:= start.o stop.o

References :­

The Linux Kernel Module Programming Guide by Peter Jay Salzman chapter­2