Wednesday, March 31, 2010

Virtualization with KVM

Introduction:
KVM is an Open-Source Virtual Machine Monitor. It leverages hardware extensions available for virtualization, to provide a FAST! virtual environment.


Installing KVM on your machine:


Does your Machine has hardware extension for virtualization? 
Check if following command produces any output, if not then your machine doesn't has the required extension:

cat /proc/cpuinfo | grep -i vmx



Installing KVM from source:
  • Install some required packages. On Ubuntu, issue following command :
    sudo apt-get install build-essential libsdl1.2-dev zlib1g-dev libasound2-dev pkg-config libgnutls-dev libpci-dev

  • Download latest qemu-kvm from http://sourceforge.net/projects/kvm/files/
  • Extract it in a directory, say ~/kvm
  • cd to above directory and issue following commands:
    • ./configure --prefix=/usr/local/kvm (Install kvm in /usr/local/kvm)
    • make
    • sudo make install
Installing and running a guest Operating System using KVM:

Installing a new Guest OS:
  • To start KVM:sudo modprobe kvm-intel (or sudo modprobe kvm-amd if on an AMD box). If this fails, look for last few lines in output of dmesg | tail - if virtual extension are disabled from boot, enable them from boot options.
  • To create a new virtual disk (on which guest OS will be installed):/usr/local/kvm/bin/qemu-img create -f qcow2 ubuntu1.img 20GThis creates a disk (which is just a file on your actual hard disk) called ubuntu1.img, that can expand to a size of 20GB.
  • To install a new OS in the virtual disk create:sudo /usr/local/kvm/bin/qemu-system-x86_64 -hda /path/to/virtual/disk -cdrom /path/to/iso/of/os/to/be/installed -boot d -m 512
    This starts the installation of guest OS.
Running a Guest OS:
  • To run a guest OS:
    sudo /usr/local/kvm/bin/qemu-system-x86_64 -drive cache=writeback,file=/path/to/virtual/disk -m 512 -soundhw es1370 

    This starts a virtual machine with 512 MB of RAM and with sound support. There are many more options available here, such as configuring guest OS's network etc.
Additional Information:
  1. Under KVM, each guest OS is just a user process running in Linux. Thus one use any command used on a process, such as killing using ctrl-c, stopping using ctrl-z etc.
  2. Around 256 MB of RAM is enough to run a Linux guest OS.
  3. There are multiple networking options available - one can assign a separate IP address to each of virtual machine and make them accessible to each other and the host. You can use such an arrangement for various experiments.

Cheers,
Nipun

Tuesday, March 30, 2010

Installing Linux Kernel from Source

Following ten steps will get you a kernel installed, from source, on your machine:

  1. Get a source from www.kernel.org
    Go for the latest stable release.
  2. Untar the source.
  3. Enter the source directory
  4. Copy an old kernel config file, from /boot, into this directory and rename the file to '.config'
    A .config file contains response (yes/no/build-module) for various configuration options such as support for a particular network card. Ideally, one shall go through all the options and select the ones needed - but there are too many options to be configured! So, we copy the config file supplied with your distribution.
  5. make silentoldconfig
    This reads the already present .config file in source directory and asks for responses of the remaining options - press enter for all the remaining options to select the default option.
  6. make -jn (where n is 2*num_cpu_cores)
    Parallel make is fast as it divides the work between multiple cores.
  7. sudo make modules_install
    This installs the modules in /lib/modules/$KERNEL_VERSION directory
  8. sudo make install
    This executes distribution dependent installation script, which for example, copies the built kernel in /boot directory.
  9. sudo update-initramfs -c -k $NEW_KERNEL'S_VERSION
    Creates ramdisk needed for booting.
  10. sudo update-grub
    Adds entry for the new kernel in your grub.
For any more information/details, drop me an e-mail.

Cheers,
Nipun

Tuesday, November 3, 2009

Using sysctl interface for Out-of-Kernel Modules

This post is for that exorbitantly frustrated Linux Kernel Dev,  who is about to start pulling out his/her hair, trying to figure out how to use sysctl interface in Out-of-Tree kernel modules, in 2.6.24+ Kernels. (Did I just described my own state? ;) ).

1. What is sysctl?
Consider following points:
  • sysclt is an interface for user-space <--> kernel communication.
  • It is used to read/write some Kernel variables, from User-Space.
  • Each files under /proc/sys directory represents a Kernel variable
  • Reading/Writing these files is equivalent to a read/write of the corresponding Kernel variable.
(I won't go further in describing sysctl interface here, you can look out for it in Understanding Linux Network Internals book)


2. What's the problem in using sysctl with 2.6.24+?

A patch was released after 2.6.24, which prohibits Out-of-Kernel modules from using sysctl interface in the normal way. This was apparently done as many broken entries were detected and thus only In-Kernel modules were allowed to use sysctl the conventional way.


3. The way out?


To use sysctl in an Out-of-Kernel module, all you have to do is to set the ctl_name field of ctl_table, of every child in subdir, to 0 (zero). Only the top level ctl_table shall have a ctl_name defined.


EDIT (30th March 2010) : In 2.6.33 onwards, ctl_name field is no longer a member of struct ctl_table. For out-of-tree kernel module, remove the ctl_name from all the concerned ctl_table structure, to get things working.


Example: 

If you want following set of files under /proc/sys/net:

/proc/sys/net
|
|----foo---|-----var1
              |-----var2



you need to have a config like: 




static struct ctl_table child_table[] = {
    {
    .ctl_name    =    0,
    .procname    =    "var1",
    .data        =    &x1,
    .maxlen        =    sizeof(int),
    .mode        =    0644,
    .child        =    NULL,
    .proc_handler    =    &proc_dointvec},


    {
        .ctl_name       =       0,
        .procname       =       "var2",
        .data           =       &x2,
        .maxlen         =       sizeof(int),
        .mode           =       0644,
        .child          =       NULL,
        .proc_handler   =       &proc_dointvec},

    {    .ctl_name    =    0,

          .procname    =    NULL
    }
};


static struct ctl_table foo_table[] = {
    {
        .ctl_name       =      0,
        .procname       =       "foo",
        .data           =       NULL,
        .maxlen         =       0,
        .mode           =       0555,
        .child          =       child_table},

    {
    .ctl_name    =    0,
    .procname    =    NULL
    }

};



static struct ctl_table root_table[] = {
    {
        .ctl_name       =       CTL_NET,
        .procname       =       "net",
        .data           =       NULL,
        .maxlen         =       0,
        .mode           =       0555,
        .child          =       foo_table},
   
    {
    .ctl_name    =    0,
    .procname    =    NULL
    }

};



Cheers!

Sunday, November 1, 2009

IPod Touch with Ubuntu

I was not able to read the content (specifically music) of my IPod touch with Ubuntu. IPod plugins for Rhythm Box etc don't seem to work with new IPods. Following command will let you mount your IPod as an external storage device:

sudo apt-get install ifuse

ifuse package shall be present for other GNU/Linux distributions too. After installing ifuse, your IPod will mount as if it was a USB stick.

Enjoy!