How to KVM




Advantages of KVM

1. Open source solution, enough said.
2. Supports 64 bit hosts / guests as well as multiple CPU (I have used 8 virtual CPU without any issue). In general you want to use a max number of virtual CPU = to the number of physical CPU.
3. IMO, KVM is faster then VMWare and Virtual box, although this is obviously a subjective opinion and I kave no hard objective data to back that claim, your experience may vary.

Disadvantages of KVM

Perhaps the biggest disadvantage is that the the GUI interface (virt-manager) is not as polished. Personally I use KVM on the command line and write a "simple" script (with all the options) to launch a machine. It takes a little more time,

Home page : http://virt-manager.et.redhat.com/

Screen shots : http://virt-manager.et.redhat.com/screenshots.html


FAQ

1. What is KVM ? "Kernel Based Virtual Machine".2. Will my hardware support KVM ?

To test this open a terminal and enter :

Code:
egrep --color=auto '(vmx|svm)' /proc/cpuinfo
If you see vmx or svm in the output your hardware (CPU) will support KVM. If you get no output with that command you are out of luck.

You may need to enable KVM in your BIOS. I have had to do this on my hardware. Unfortunately it is not always obvious how to do this, you may need to use google, read any manual you may have, or like I did, hunt through your BIOS.


How to install KVM

Code:
sudo apt-get install kvm qemu bridge-utils uml-utilities
Alternately, you may install from source :
~ Thanks redbrain





You can then either re-boot or :

Code:
sudo modprobe kvm
Then add your user to the kvm group. You may either user the gui or :

Code:
sudo usermod -G kvm -a <user>
where "<user>" is the user you wish to add to kvm. You will need to log out and back in for the group membership to take effect.


Quick tips


Make a virtual hard drive

use qemu-img

Code:
qemu-img create -f qcow2 /home/user/Ubuntu.img 5G
/sbin/mkfs.ext3 -F /home/user/Ubuntu.img
Other options include "raw", see man qemu-img for options.


Use a physical partition

Physical partitions are very easy in KVM. Let us assume you wish to use /dev/sda2 for example :

Code:
sudo chown root.kvm /dev/sda2
Not start kvm using the physical partition:

Code:
kvm -hda /dev/sda2 ...
How to bridge your network card

First bring down your network:

Code:
sudo /etc/init.d/networking stop
Next, using any editor (gksu gedit) edit /etc/network/interfaces to add a bridge. You may use either static or dhcp :

Static :

Code:
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off
DHCP:

Code:
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off
Note:

  1. change "eth0" to your network card.
  2. This will not work with wireless.
  3. If you are using a firewall on the host, you may have problems. I find Firestarter (which is a GUI front end) will often break with complex networking.



Now bring your network back up:

Code:
sudo /etc/init.d/networking start
Wrapper script for bridged network connections on KVM

Use a wrapper script to automatically bring up a tap when you start a new virtual machine.

Save this script in /usr/bin/kvm-bridge

Code:
#!/usr/bin/env bash
 # script to manage tap interface allocation
 # for linux kernels >= 2.6.18

 # modified by bodhi.zazen from :
 # http://calamari.reverse-dns.net:980/cgi-bin/moin.cgi/FrequentlyAskedQuestions#head-2511814cb92c14dbe1480089c04f83c281117a86
 # http://ubuntuforums.org/showthread.php?t=528046
 # http://www.howtoforge.com/using-kvm-on-ubuntu-gutsy-gibbon

 # set up a tap interface for qemu
 # USERID - uid qemu is being run under.
 USERID=`whoami`

 # generate a random mac address for the qemu nic
 # shell script borrowed from user pheldens @ qemu forum

 ranmac=$(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ; \
 do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done)

 # specify which NIC to use - see qemu.org for others
 # model=r8169
 # Set model based on this how-to
 # http://www.howtoforge.com/using-kvm-on-ubuntu-gutsy-gibbon

 model=rtl8139
 iface=`sudo tunctl -b -u $USERID`

 # start kvm with our parameters
 # echo "Bringing up interface $iface with mac address $ranmac"
 # nohup added to allow kvm to run independent of the terminal
 nohup kvm -net nic,vlan=0,macaddr=$ranmac -net tap,vlan=0,ifname=$iface $@

 # kvm has stopped - no longer using tap interface
 sudo tunctl -d $iface &> /dev/null
Set ownership and permissions:

Code:
sudo chown root.kvm /usr/bn/kvm-bridge
sudo chmod 550 /usr/bin/kvm-bridge
Usage : Use kvm-bridge in place of kvm, for example ;

Code:
kvm-bridge -cdrom /dev/scd0 -m 512
KVM console

The KVM console allow you to modify your machine as it is running. To get to the console, place your mouse in the KVM screen and hit "ctrl-alt-2".

To see your options type help. You can also use help <command>

Useful commands include :

sendkey ctrl-alt-f1 => changes to the console in the virtual machine.
sendkey ctrl-alt-f7 => changes back to the normal GUI interface in the virtual machine.

ctrl-alt-1 => returns to the KVM interface.


Additional information

IMO at this time KVM is best run from the command line. Also, IMO the best soruce of inforation on KVM is "man qemu" (the qemu options all work with kvm).

Useful options include :

-snapshot = run kvm without making changes to your (virtual) hard disk.

-M = Allows you to select a machine (cup) to emulate.

-smp = sets number of CPU.

-m = sets amount or RAM for the virtual machine.

-hda , -hdb, -cdrom = sets hard drive and / or cdrom, you may use a physical partition, hard drive, cdrom, DVD, or a file (Ubuntu.img, ubuntu-desktop.iso) and (I assume) USB devices.

-boot = sets which device to boot from.

-soundhw = set sound card.

-win2k-hack = used with windows 2000 guests.

-std-vga = may increase the resolution of your guests, primarily used by windows guests.

-name = sets a name to your guest.

-nographic, -fullscreen = additional graphic options.

-vnc = start the built in VNC server rather then the standard display. Useful if running kvm without X.

-daemonize = kvm runs detached from your terminal.

-usb = enable usb driver.

-usbdevice = use for usb mouse / tablet etc (on the host).

-tftp , -smb = use built in ftp and samba servers.

-no-apci = Disable ACPI.

-g = set display resolution (see man qemu for details).



Other options : See https://help.ubuntu.com/community/KVM

virsh

GUI options. use virt-manager. IMO virt-manager has come a long way, but not all options are available on the GUI interface.

virt-manager

Peace be with you,

bodhi.zazen