Monday 28 July 2014

Linux Kernel Programming- Step2 (First Program) Writing & compiling a simple hellokernel module

Now that we have setup our playground (read ready to start programming), we can move on to writing a small program for kernel. There are two ways to add your program to the kernel.
  1. Write a source code and add it to kernel source tree and recompile the kernel, kernel configuration is used to choose which files to be included in kernel to compile.
  2. Write a loadable kernel module- which can be loaded and unloaded into kernel dynamically without the need to recompile the kernel.
These loadable kernel modules can do a lot of things, mainly they are in one of these three forms 
1) device drivers 2) filesystem drivers  3) system calls.

following the tradition of writing "helloworld" program  whenever we start learning any new language, we are going to write a simple hellokernel.c program, and compile this program to generate a .ko (kernel object) file which can be added to the kernel by certain commands.

  • Create hellokernel.c file 

make a directory for saving your kernel programs, and create a file named hellokernel.c using gedit, or any of your favourite editor,

gedit hellokernel.c 

printk(KERN_INFO,"message")
KERN_INFO is one of the many log levels present, this macro provides kernel with information about the severity of messages. depending on loglevels it might print on console as well, you can try the KERN_WARNING macro for that.
Available Loglevels
Loglevel
Description
KERN_EMERG
An emergency condition; the system is probably dead
KERN_ALERT
A problem that requires immediate attention
KERN_CRIT
A critical condition
KERN_ERR
An error
KERN_WARNING
A warning
KERN_NOTICE
A normal, but perhaps noteworthy, condition
KERN_INFO
An informational message
KERN_DEBUG
A debug message typically superfluous

in absence of any loglevel, kernel will take it as  DEFAULT_MESSAGE_LOGLEVELwhich is currently KERN_WARNING.

  •  Create Makefile


make command checks for makefile in the directory, Makefile is the file through which you tell compiler about which files are to be included for build, it contains detail about target, dependency & rules for generating the output file.

gedit Makefile

obj -m as target specifies that we need a kernel object module from hellokernel.o file, KDIR macro contains the path to the library files required for compilation.

here is an detailed tutorial for make & makefile Makefiles-in-Linux-An-Overview

  • run make command

make

 If the make command is successful in compiling the hellokernel.c file, then it will generate various files, 


one of the files will be "hellokernel.ko" file. this is the kernel object file, which can be inserted into the kernel without the need to recompile the whole kernel source.


  • Inserting module into Kernel


The kernel object file generated in previous step can be loaded into kernel using insmod command, one will need to have root password to insert the module( using sudo)

sudo insmod hellokernel.ko

  • Removing Module from Kernel


The kernel object added can be removed from the kernel as well, for this we need to use-

sudo rmmod hellokernel.ko


  • Verifying module insertion & removal

After the module is successfully inserted or removed from kernel we can check the messages  by dmesg command, remember in c file we wrote "printk" function, this function does not print on console but on the kernel log file, to check the kernel log we have to use dmesg command.

dmesg



this indicates successful insertion and execution of module from kernel.

1 comment: