Wednesday 31 August 2011

Configuring UML kernel For Linux Kernel Development.

It is always a good idea to create a simulated environment while doing kernel programming, because playing with kernel may lead to data corruption, system hang-ups and all such nasty things. This post may help you to configure User Mode Linux . User Mode Linux is just like an operating system which is running in user space. So you can do all your experiments on this user space kernel with a less probability of serious damages.


Installing/downloading The appropriate Packages.
You should download / install the following packages.
1)Linux kernel Source 2.6.xx
2)UML Utilities
You can download a latest kernel from www.kernel.org
Extract the kernel to a directory and change your current working directory to that directory.
In my case I have installed the kernel from the Debian's package manager
host# apt-get install kernel-source
host# apt-get install uml-utilities
host# cd /usr/src/
host# bunzip2 linux-source-2.6.32.tar.bz
host# tar -xvf linux-source-2.6.32.tar
host# cd linux-source-2.6.32/

Compiling The UML Kernel
Now you have to compile your linux kernel source for uml architecture . The compilation is similar to the usual compilation.
host# make mrproper
host# make mrproper ARCH=um
host# make defconfig ARCH=um
if you have to compile with a customized config file you can use make menuconfig or make xconfig and customize the config file.
host# make Linux ARCH=um
The above command will create an executable kernel file called Linux.
host# make modules ARCH=um
The above command will create the loadable modules

Creating the Filesystem
Now lets create a root filesystem for our uml kernel.
host# dd if=/dev/zero of=root_fs bs=4k count=1M
this will create a file named root_fs of size around 4G, after this we can write a filesystem onto this root_fs file
host# mkfs.ext3 root_fs
now you have an ext3 file system in the file root_fs. so you can mount that file to a directory .
host# mkdir rootdir/
host# mount -o loop root_fs rootdir/
now you need all the root directory tree should be place on to this directory in our case say rootdir.For this we can use debootstrap command.
host# debootstrap squeeze rootdir/ file:/media/cdrom0/
I created a root file system for the squeeze release from the installation dvd of Debian Squeeze.if you don't have a installation DVD you retrieve the files form the Debian squeeze remote repository. for this you can read the man page of debootstrap.After completion of debootstraping if you go to that directory<rootdir> you can see the complete root directory structure in that directory. now you have a complete directory structure so you can now install the modules to the existing directory structure.
host# make modules_install INSTALL_MOD_PATH=rootdir/ ARCH=um
now your compiled loadable module files will the copied to rootdir/lib/modules/ . now you have to set the password for the root in the filesystem.
host# cd rootdir
host# chroot .
by executing the above command your root of the filesystem tree of the host will be changed to the current working directory . now by executing the passwd command you can update the passward .
host# passwd
set the passward .
host# exit
now you have to create a swap filesystem.
host# dd if=/dev/zero of=swap_file bs=4k count=50k
host# mkswap swap_file

now we should edit some of the configuration files of our newly created filesytem .
1.etc/inittab
2.etc/fstab
3.etc/udev/links.conf

edit file etc/inittab keep the line given below and  delete all such similar lines.

1:2345:respawn:/sbin/getty 38400 tty1


edit file etc/fstab as

/dev/ubd0    /    ext3    defaults 0    0
/dev/ubd1    none    swap    sw     0    0
proc        /proc     proc    defaults 0     0


edit file etc/udev/links.conf

M ubd0    b 98 0
M ubd1    b 98 16
M ubd2    b 98 32
M ubd3    b 98 48
M ubd4    b 98 64
M ubd5    b 98 80
M ubd6    b 98 96

#host umount rootdir/
Networking
To configure networking in uml machine we will use the tuntap methord.
<to--do > write what is tuntap</to--do>
you can create a tap0 interface using the tunctl coammdn
#host tunctl -t tap0
#host ifconfig tap0 192.168.2.33 up
now you are redy to run the code uml kernel
#host ./linux ubd0=root_fs ubd1=swap_file mem=256M eth0=tuntap,tap0
after executing the above command you will get a console on which you can login with the password that you have set using the passwd command
now in the virtual machine you have to set up the the ip of eth0
#host ifconfig eth0 192.168.2.2
#host route add default gw 192.168.2.33

now you have to write some rules for iptable if we want to connect to some public network.
host# echo 1 > /proc/sys/net/ipv4/ip_forward
host# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
host# iptables -A FORWARD -i eth1 -o tap0 -m state --state RELATED,ESTABLISHED -j ACCEPT
host# iptables -A FORWARD -i tap0 -o eth1 -j ACCEPT
now to start the virtual machine
host# ifup tap0
host# ./linux ubd0=root_fs ubd1=swap_file mem=256M eth0=tuntap,tap0
now you will get a login window if all the above things works fine.To check the whether the connection is ok you can ping to the host machine
virtual-machine# ping 192.168.2.33
virtual-machine# ping 192.168.1.7
virtual-machine# ping google.com