How to KVM (Kernal virtual Machine)
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 :
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:
- change "eth0" to your network card.
- This will not work with wireless.
- 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,
http://img381.imageshack.us/img381/4...o2small0lj.png bodhi.zazen
Re: How to KVM (Kernal virtual Machine)
From jdong : For servers, without X, use libvirt/virsh as outlined on the Ubuntu wiki :
https://help.ubuntu.com/community/KVM
Re: How to KVM (Kernal virtual Machine)
Do you know how to create the config files needed for seeing the VM in virt-manager when using pre-existing QEMU images?
I created my image 2 years ago when KVM first made it into the kernel, and I cannot figure out how to get virt-manager to see my guest. It runs from the command line just fine.
Re: How to KVM (Kernal virtual Machine)
How stable is KVM for most of you, and where should I go to get support?
I've been trying to get it working for a couple months now. And it "works" pretty well, in that I can install and start up an ubuntu guest and use it for a while, but after a few hours or at random times, it freezes up, and the kvm process in the host goes to 100% cpu busy.
I'm not sure if this is some configuration issue, or just some bug in KVM, but I've been playing with configuration options night and day and am starting to run out of options.
I tried posting a message on the gmane kvm developer mailing list but didn't get much response.
What's the right way to get some support?
Below is the message I posted to the KVM development mailer.
Thanks,
Chris
----------------------
I've have setup a couple virtual machines and they work great... for anywhere between 2-24 hours. But then, for no reason I can determine, they just go 100% busy and stop responding.
My basic setup is:
Ubuntu 8.10 server running on both host and guests.
kvm version is the one from the Ubuntu distribution (kvm-72)
Kernel is Ubuntu 8.10 kernel (2.6.27-7-server)
The two VMs are both run like:
kvm -daemonize \
-hda Imgs/ndev_root.img \
-m 1024 -cdrom ISOs/ubuntu-8.10-server-amd64.iso \
-vnc :1 -net nic,macaddr=DE:AD:BE:EF:04:04,model=e1000 \
-net tap,ifname=tap1,script=/home/chris/kvm/qemu-ifup.sh
(With different disk imgs, vnc addresses and macaddrs)
The disk images are raw format:
qemu-img create -f raw 80G Imgs/ndev_root.img
I've tried running with these options, and in different combos:
-no-kvm-pit
-no-kvm-irqchip
-no-acpi
They don't seem to help much. If anything, -no-kvm-irqchip seems to cause more troubles.
I tried running with -no-kvm and it won't boot at all.
I also built my own 2.6.27.4 kernel, from kernel.org, and built my own version of kvm-78, but saw the same behavior.
Anyone have any advice how I can resolve these hangs? When the setup works, it's a beautiful setup and I love kvm. But with the 100% busy hangs I can't really continue with it.
Thanks!
Chris
Re: How to KVM (Kernal virtual Machine)
To be honest KVM has been rock solid for me.
Re: How to KVM (Kernal virtual Machine)
hey bodhi,
very nice guide! :D
May i suggest adding a part for allowing opengl modes from the host, to linux guests via 'vmgl' Im sure many users will like this handy feature ;)
AJ
Re: How to KVM (Kernal virtual Machine)
I am trying to enable a VNC password for my guest machines, however it seems to be ignoring the password parameter.
From http://libvirt.org/formatdomain.html:
Quote:
The password attribute provides a VNC password in clear text.
Can someone provide the proper syntax? The above page doesn't provide the syntax or a single example, so I'm kind of in a bind... :/
Re: How to KVM (Kernal virtual Machine)
I've recently switched from Xen to KVM for my Virtualization platform. Currently I have a few Win2k3-64bit guests installed on LVM partitions. No problems so far.
Honestly, I'm sad that most linux distributions have dropped Xen, but KVM looks promising.
Re: How to KVM (Kernal virtual Machine)
This doesn't work for me:
qemu-img create -f qcow2 /home/user/Ubuntu.img 5G
/sbin/mkfs.ext3 -F /home/user/Ubuntu.img
Since the image created will be 32k big, and mkfs can't make a fs on such a small area. Substitute "raw" for "qcow2" and it will work.
Anyone actually made it work as written?
Thanks for a very helpful HOWTO otherwise!!
Re: How to KVM (Kernal virtual Machine)
finite9 : Just start virt-manager, create a dummy machine (just launch a live cd with a 1k disk...) and go have a look at /etc/libvirt/ ... you'll find interesting stuff, including the xml files describing your machines.
The format is there : http://libvirt.org/format.html , an interesting section is "capabilities"
----EDIT----
Sorry, the page looks empty. If you look at the left, under "XML Format" you have sections that you can click on to get the info
--END EDIT--
Be aware that you have to restart quite a few services for virt-manager to notice the changes you made in there. I don't remember which, but I would have a look at /etc/init.d/kvm, /etc/init.d/libvirt* /etc/init.d/virt* ...
Have fun,
François