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