Using KVM On Ubuntu 7.10 (Gutsy Gibbon)

In this HowTo I'll explain how to install and use KVM for running your services in virtual machines. KVM (Kernel-based Virtual Machine) is a Linux kernel virtualization technique that provides full virtualization by using Intel VT (Vanderpool) or AMD-V (Pacifica).

I'm working as root on the server. If you are using a normal user, you have to install everything with sudo, e.g.

sudo apt-get install kvm

Download and install KVM

You can use the package included in Ubuntu 7.10, just execute:

apt-get install kvm

You can also download the last release of KVM and compile it. But you need following packages for compiling:

apt-get install gcc gcc-3.4 gcc-4.1 libsdl1.2-dev make libz-dev uuid-dev libasound2-dev gcc libc6-dev zlib1g-dev

You also need the linux-headers packages for the Kernel you are using. E.g. linux-headers-2.6.22-14.

First of all we need the KVM package:

You can find the actual release at http://sourceforge.net/projects/kvm/. Download it to your server:

wget link/to/file /usr/src/

Now unpack the content.

cd /usr/src/
tar -xzf file.tar.gz
cd folder/to/kvm

Before compiling KVM make sure that your CPU supports virtualization and if that option is activated in the BIOS. Without this support, you won't have much performance...

So let's compile KVM.

./configure
make
make install

Now load the KVM modules:

If you have an Intel CPU load:

modprobe kvm
modprobe kvm_intel

If you have an AMD CPU load:

modprobe kvm
modprobe kvm_amd

Creating the network bridge

First, we have to install bridge-utils:

apt-get install bridge-utils

If you want to connect to your virtual machine later, you need to set up a network bridge.

Edit /etc/network/interfaces like this (make sure to insert your public IP Address):

auto lo
iface lo inet loopback
auto br0
iface br0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx
bridge_ports eth0
bridge_stp off
bridge_maxwait 5

Also edit /etc/qemu-ifup like this:

#!/bin/sh
/sbin/ifconfig $1 0.0.0.0 promisc up
/usr/sbin/brctl addif br0 $1
sleep 2

I recommand restarting your server at this point. If you are able to reconnect, your bridge is working.

Creating a virtual disk for a virtual machine & install a system into it.

Create an .img file for installation. This file acts as a virtual disk for your vm.

You have 2 possibilities for creating such a file.

Allocate the disk space needed by the .img file directly. In this case the file will have a size of 4 GBytes.

dd if=/dev/zero of=disk01.img bs=1G count=4

Create an .img file with a maximal size but the disk space will only be allocated when needed. In this case, the maximum size will be 10 GBytes, but the disk space used will be the space required by the files in the vm.

qemu-img create -f qcow2 disk01.img 10G

Next, we need an ISO file of the system you want to install. Make sure you have downloaded on onto your server.

Let's install the virtual machine. When you don't have physical access to the server, start the installation with the VNC option. So you can connect to the VNC session and you will have the GUI from the installer. In this case we will set the RAM size of the vm to 512 MBytes. At the moment the vm doesn't have access to the network interface. I prefer activating it later, after configuring IP Addresses of the vm.

kvm -hda disk01.img -cdrom os.iso -m 512 -boot d -vnc IP:1

Now you can connect to IP:1 with a VNC client. You can configure your system and network settings at this point. Don't forget to install a SSH Server. After configuring, shut down the vm.

Now we are going to start the vm with networking support. We assign a MAC-Address to the vm network interfaces. This is important if you use more than 1 vm. Every vm will get its MAC-Address.

kvm -hda disk01.img -m 512 -boot c -net nic,vlan=0,macaddr=00:16:3e:00:00:01 -net tap -nographic -daemonize

After some seconds you should be able to connect via ssh to the vm. If not, you can add the -vnc option to check if there are problems.

I had the problem that I couldn't reach my vm over the network after some time. I had to add a different network card emulation to the start command. This solved my problem.

kvm -hda disk01.img -m 512 -boot c -net nic,vlan=0,macaddr=00:16:3e:00:00:01,model=rtl8139 -net tap -nographic -daemonize

If you have the problem that your configured network interface isn't starting, delete the following file (depending on your os):

/etc/udev/rules.d/70-persistent-net.rules

or

/etc/udev/rules.d/z25-persistent-net.rules

You also have the possibility to add more virtual disks to one vm. You can add the following parameter to the start command:

-hdb disk02.img

You can add more virtual machines if you need so. You could also create start scripts for your vms.

Enjoy using KVM! :)

Share this page:

Suggested articles

5 Comment(s)

Add comment

Comments

By:

KVM is really a great technology. I made some benchmarks with early versions which revealed that it is able to run applications close to native speed. I also compared it's performance with VMware, and they were comparable. Unfortunately, VMware prohibits publishing benchmarks of it's product, that's another good reason to go with KVM!

By:

I recommand restarting your server at this point. If you are able to reconnect, your bridge is working.

This is a little ambigous (which server?). I recommend rewritting this in the form:

I recommand restarting your foobar server at this point, by typing:

/etc/init.d/foobard restart 

If you are able to reconnect, your bridge is working.

Again I find "(make sure to insert your public IP Address)" ambiguous for people behind a NAT. Would the following be better?

To find your Address and Netmask type type

ifconfig eth0

To find your gateway type

route

You say "Also edit /etc/qemu-ifup like this:". However some machines do not have this file. Perhaps we should have a line

(create this file if it does not exist)

or, perhaps instead:

if this file does not exist, type:

apt-get install qemu 

By:

After I rebooted I was unable to access the internet, 'route' reported that it thought that br0 was my access to the internet. Changing /etc/network/interfaces back to the original and rebooting fixed this problem. Take note of this before you try these instructions. Not much point trying to look this up after your net access has stopped working ;).

By: Cyrille

Hello,

Thks for your work.

I want to know if it is possible to set a Virtual Machine with a specific/limit frequency processor.

Hope to read you

Best Regards

By: Tariq Daradkeh

hello there,

  In fact i'm a new user to kvm i had installed it and try it and everything was good .   Now I'm working to get the source code and compile it and install it I found these two links for source code  http://sourceforge.net/projects/kvm/files/qemu-kvm/1.2.0/   http://sourceforge.net/projects/kvm/files/kvm-kmod/3.10.21/   I don't know exactly what the difference between them but the code from the first link i can configure (./configure) it and compile it (make) but once I start install it (make install) i get this message   sudo make install [sudo] password for tariq: install -d -m 0755 "/usr/local/share/qemu" install -d -m 0755 "/usr/local/etc/qemu" install -c -m 0644 /usr/src/qemu-kvm-1.2.0/sysconfigs/target/target-x86_64.conf "/usr/local/etc/qemu" install -c -m 0644 /usr/src/qemu-kvm-1.2.0/sysconfigs/target/cpus-x86_64.conf "/usr/local/share/qemu" install -d -m 0755 "/usr/local/bin" install -c -m 0755  qemu-ga qemu-nbd qemu-img qemu-io  "/usr/local/bin" install -d -m 0755 "/usr/local/libexec" install -c -m 0755  qemu-bridge-helper "/usr/local/libexec" set -e; for x in bios.bin sgabios.bin vgabios.bin vgabios-cirrus.bin vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.bin ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom pxe-pcnet.rom pxe-rtl8139.rom pxe-virtio.rom qemu-icon.bmp bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb multiboot.bin linuxboot.bin kvmvapic.bin s390-zipl.rom spapr-rtas.bin slof.bin palcode-clipper; do \                 install -c -m 0644 /usr/src/qemu-kvm-1.2.0/pc-bios/$x "/usr/local/share/qemu"; \         done install -d -m 0755 "/usr/local/share/qemu/keymaps" set -e; for x in da     en-gb  et  fr     fr-ch  is  lt  modifiers  no  pt-br  sv ar      de     en-us  fi  fr-be  hr     it  lv  nl         pl  ru     th common  de-ch  es     fo  fr-ca  hu     ja  mk  nl-be      pt  sl     tr bepo; do \                 install -c -m 0644 /usr/src/qemu-kvm-1.2.0/pc-bios/keymaps/$x "/usr/local/share/qemu/keymaps"; \         done for d in x86_64-softmmu; do \         make -C $d install || exit 1 ; \         done make[1]: Entering directory `/usr/src/qemu-kvm-1.2.0/x86_64-softmmu' install -m 755 qemu-system-x86_64 "/usr/local/bin" strip "/usr/local/bin/qemu-system-x86_64" make[1]: Leaving directory `/usr/src/qemu-kvm-1.2.0/x86_64-softmmu' then I tried the second code but in the second code i can't even configure it   sudo ./configure   Error: kernel is more recent than KVM modules delivered with this release. You probably want to use the KVM support that is already part of your kernel.   I have ubuntu 12.04 installed on dell machine  $ uname -rm 3.13.0-32-generic x86_64 please any help that i can start to compile the kvm and install it then i want to modify some feature of KVM, if you advice me how I can get documentation about the code of kvm and how the code is structured    thank you very much