Monday, October 13, 2008

How to Insert user module into kernel

How interesting it is if your code is inserted into kernel as a kernel module. This blogg provides you with how to insert a module written by you into kernel. By doing this you can make your kernel to work according to you and at certain stage you can also write your own kernel but its tooo far.... so just dream about it...
  1. There are two main functions to insert and remove module into and from kernel. Those function are init_module() and cleanup_module()
  2. Whatever module you want to insert write code for that in init-module() and write code which do cleaning operation of your module in cleanup_module().
  3. Write make file to build your module
  4. Build you module using "make"
  5. Once your code is error free insert the module using command "insmod module.ko". This will execute code and print log messages from init_module()
  6. Remove your module from kernel using "rmmod module". This will execute code and print log messages from cleanup_module()
  7. If in case messages are not displayed on promt you can use "dmesg" command which will print all kernel messages. Lets have a look on simple example:
ex: we want to insert a module which will print "Hello World" after insertion and will display "GoodBye" after removal from kernel.
int init_module()
{
printk(KERN_INFO "Hello World\n");
return 0;
}
void cleanup_module()
{
printk(KERN_INFO "GoodBye\n");
}

save this code in file hello.c

Now write make file to compile this code:
KDIR:=/lib/modules/$(shell uname -r)/build
obj-m:=hello.o
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(RM).*.cmd*.o*.ko -r .tmp*

Save this code as Makefile in the same folder in which your hello.c is there. Now come to prompt
$make
This will create hello.ko and many other files but we will concentrate on hello.ko file
$insmod hello.ko
This will insert your module hello.c in kernel. If you want to check whether it loaded into kernel or not verify it using "lsmod | more" command. It will display your module hello on top and many oher kernel modules below it. It will also print "Hello World" on the screen. If its not then type "dmesg" command to see kernel log messages.
If you wnat to remove yor module from kernel type
$rmmod hello
Now check whether your module is removed from kernel using lsmod again. Its not there.

Other way of builiding kernel

Go to /usr/src/linux-2.x.xx/ folder and read README for building kernel.
Its the best way for building kernel.

Monday, April 9, 2007

building kernel 2.6.26

hi all
its chanda here sharing with u how to build new kernel2.x.xx . Initially i was thinking building kernel is impossible but after going through many references its not as difficult as building jvm.. :) So start it simultaneously with me....
1.Before building kernel first check for the following utilities on your machine.
you should have compiler,linker,make and binutility. These are the following commands to check them.
a.gcc --version --> check for the availability of C compiler
b.ld -v --> for binutility
c.make --version -->for make
d.fdformat --version -->for util-linux package
e.depmod -v --> for module-init tool
f.showmount --version --> for nfs utils
2.after checking these components download stable version of kernel from http://www.kernel.org/ which is in tems of Linux-kernel-2.6.26.tar.bz2 file.
3. untar the file in /usr/src directory using the command
/usr/src#tar -xjvf Linux-kernel-2.6.26.tar.bz2
It will produce Linux-kernel-2.6.26 folder in /usr/src
4. goto /boot directory for geting old configuration file and copying it to the untar folder Linux-kernel-2.6.26
#cd /boot
search for the config file using command
#ls config*
which will display config-2.x.xx-xx file
#cp config-2.x.xx-xx /usr/src/Linux-kernel-2.6.26/.config
5. Now we are going to configure kernel using one of the following commands
#make manuconfig
#make gconfig
#make xconfig
Use any one of these. These commands will ask you about configuration options..
It will popup new window containig many options to be configured. Select proper option which you require. Save the file from File menu and quit it..you will be on shell prompt showing the message "#configuration is written to file.config"
6. Now set dependencies using command
#make dep (we are in /usr/src) or(/usr/src/Linux-kernel-2.x.xx)
It just take a second
7.Then do cleaning for remove unnecessary files using
#make clean
8. create bzImage for the new kernel using command
# make bzImage
copy this bzImage to /boot/bzImage-2.x.xx
#cp /usr/src/Linux-kernel-2.x.xx/arch/i386(or x86)/boot/bzImage /boot/bzImage-2.x.xx
9. Now real steps ie compiling kernel by installing module and compiling them
#make modules
#make modules_install
#make install
Last command will install modules in /lib/modules
10. Now last step is checking for the new entry of new kernel we built in bootloader. So for this you have to check which bootloader you are using..grub or LILO by typing
#ls -F /boot grep grub
and
#ls /etc/lilo.conf
If its grub then new kernel entry is automatically added in /boot.grub.menu.lst just above old kernel. Some lines are as follows
title Redhat Enterprise Linux server 2.x.xx
root (hd0,x)
...........
.....
same entry for old kernel
....
.....
savedefault
boot
11.after checking this reboot your machine and select proper kernel from manu list
Or
11. if you are using LILO bootloader then edit /etc/lilo.conf file and add following section
image = /boot/bzImage-2.xx.xx
label = new
read-only
root=/dev/hdax
save the file and quit
12 and type #lilo on shell prompt
13.reboot machine and boot through new kernel.. and enjoy your own kernel of your own configuration..