Tags:
create new tag
, view all tags

Virtualization with Qemu, Kqemu and KVM

This page presents: 

    1. Introduction
    2. Installation
    3. Disk images 
      1. New image 
      2. Resize created images
    4. Network configuration
    5. Client configuration
    6. Every day use

Introduction

Qemu is a virtualization tool that started, and is maintained by  Fabrice Bellard, the main pages are QEMU, KQEMU and KVM. There are many, really many, information sources about these tools. I think these tools became even MORE popular after the linux kernel introduce support for them (kvm) by default.  

What we will discuss here is related to Debian based distributions, what does include Ubuntu. Attention that KVM will work ONLY if you have a newer processor with support to virtualization (e.g. Intel Core duo or AMD Athlon or Turion), other thing is that KVM is based in QEMU, but is an independent project. The basic difference, for the user point of view, between KVM, Kqemu and Qemu is the perceived speed but the procedures are basically the same. 

Other good tutorials and information sources can be found at:

 

Conventions: 

  • host #>: denotes commands executed in the real machine, the host machine
  • virtual #>: denotes commands executed in the virtual machine, the exemplified one

Installation

  • At first add the repository to /etc/apt/sources.list for edgy:

       host #> cat >> /etc/apt/sources.list
        deb http://ubuntu.tolero.org/ edgy main
        deb-src http://ubuntu.tolero.org/ edgy main

    Or that lines if you are running dapper:

        host #> cat >> /etc/apt/sources.list
        deb http://ubuntu.tolero.org/ dapper main
        deb-src http://ubuntu.tolero.org/ dapper main

    I, personally, prefer to do the installation from the console package manager, rather than with the graphical one (synaptic for instance). Ensure that you have also ubuntu universal repository is included to the sources.list. If so, cross your fingers and pass one by one to the console the next commands:

            host #> sudo aptitude update
            host #> sudo aptitude install kqemu-common kqemu-source
            host #> sudo aptitude install module-assistant
           host #> sudo m-a prepare
            host #>  sudo m-a build kqemu
            host #> sudo m-a install kqemu
            host #> sudo aptitude install qemu

           host #> depmod -a
           host #> modprobe kqemu

        To see if every thing it is OK
           host #> /sbin/lsmod|grep kqemu    ##Comment## The kqemu should appear here.

Disk Images

  • To run Qemu you need first to have an image of the system you want to run. You probably can find many different images already made in the internet, or you can create your own.
  • To create a new image
    host #> qemu-img create -f raw rtmk_linux-2.6.16.49.img 3G
  • Let me say you do have an iso of the desired Linux distribution, let us call here of "ubuntu-7.04-desktop-i386.iso", if you do not have you can download from the distributor site or buy a CD.
      host #> qemu -boot d -cdrom ubuntu-7.04-desktop-i386.iso rtmk_linux-2.6.16.49.img -m 400
  • If you have problems with the designed memory size
       host #> umount /dev/shm
       host #> mount -t tmpfs -o size=416m none /dev/shm
    Well.... in truth, as much memory you have the better, but remember that you need to keep your own machine running wink
  • To resize the image IF needed
  • Ok you already have an image but it is start to get really small, to resize it:
    • The image people let at the QEMU website is small, only 20MB, if I recall well, let me say we need to resize it
      host #> dd if=/dev/zero of=rt_linux-2.6.16.49.img seek=300 obs=1MB count=0
        where 300 is new size to resize hdd.img in MB

      host #> e2fsck rt_linux-2.6.16.49.img
      host #> qemu-img info rt_linux-2.6.16.49.img 
         to see the new virtual size, in my case it was 286M
      host #> resize2fs /root/rt_linux-2.6.16.49.img 286M
  • To Use with the image

host #> modprobe kqemu host #> rm -rf /dev/kqemu host #> mknod /dev/kqemu c 250 0 host #> /bin/chmod 666 /dev/kqemu   ##Comment## make it accessible to all users host #> qemu -m 200 rtmk_linux-2.6.16.49.img&

Network Configuration

  • Ok I don't think you will want to use the virtual machine without network, so we need to setup it. There are many different ways to do this we will use tap interfaces using a bridge.

host #> tunctl -d tap0   Set 'tap0' persistent and owned by uid 1000

 

  • To configure the nat

host #> iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE host #> echo 1 > /proc/sys/net/ipv4/ip_forward

 

  • Create a file, for example qemu-ifup.sh

host #>  cat > qemu-ifup.sh

#!/bin/sh echo "Executing /etc/qemu-ifup" echo "Bringing up $1 for bridged mode..." sudo /sbin/ifconfig $1 0.0.0.0 promisc up echo "Adding $1 to br0..." sudo /usr/sbin/brctl addif br0 $1 #sleep 2

  • To Start the Bridge 
  • # insert the tuntap module
    host #> modprobe tun
  • insert the bridge module
    host #> modprobe bridge
  • insert the kqemu module
    host #> modprobe kqemu major=0
  •  To configure the bridge :
    host #> ifconfig eth0 down
    host #> brctl addbr br0
    host #> ifconfig eth0 up
    host #> brctl addif br0 eth0
    host #> ifconfig br0 192.168.12.102 up   ##comment## Change for your bridge address
    host #> ifconfig eth0 down
    host #> ifconfig eth0 0.0.0.0 up
    host #> route add default gw 192.168.12.100  ##comment## Change for your gateway
  • To start QEMU using the network
    host #> qemu -m 200 -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=qemu-ifup.sh YourImage.img&

Configuring the client 

  • Everything now happens INSIDE the virtual machine
  • Put insite /etc/network/interfaces the following lines:
    auto eth0
    iface eth0 inet static
    address 192.168.12.102  ##comment## Change for your bridge address
    netmask 255.255.255.0
    gateway 192.168.12.100  ##comment## Change for your gateway
  • To restart configurations
    virtual #> /etc/init.d/networking restart 
  • To solve the names put in the /etc/resolv.conf file
    virtual #> cat > /etc/resolv.conf
    nameserver 192.168.12.100   ##comment## Change for your real name server

For every day usage

If you are following this as a step by step tutorial, up to here it is one time only, you will need to do every thing that is below only once, here we will discuss the every day use, what you need to do daily make the thing work.

  • To start the virtual machine using the network
    host #> qemu -m 200 -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=qemu-ifup.sh YourImage.img&
  • If the kqemu doesn't work try
    host #> modprobe kqemu
    host #> rm -rf /dev/kqemu
    host #> mknod /dev/kqemu c 250 0
    host #> /bin/chmod 666 /dev/kqemu   ##comment## to make it accessible to all users

 -- DanielCamara - 21 Dec 2007

Topic revision: r1 - 21 Dec 2007 - DanielCamara
 
Copyright 2004-2010 Institut Eurecom & all contributing authors.