User Tools

Site Tools


linux:kvm

QEMU / KVM


This is just a little summary for an own personal information overview on this topic.
Feel free to read the Official CentOS 6 Manual on Virtualisation.
And heres the RHEL one.

Preparation

Check if your CPU supports virualisation.

egrep '^flags.*(vmx|svm)' /proc/cpuinfo

Install the kvm kernel mod: For Intel CPUs:

modprobe kvm-intel

For AMD CPUs:

modprobe kvm-amd


Reboot your system and check if dbus loads the module automatically.

/sbin/lsmod | grep kvm

The output should look similar to this:

(~) root@dc:$ /sbin/lsmod | grep kvm
kvm_amd                37434  0
kvm                   292815  1 kvm_amd

Installation on CentOS 6

yum install kvm qemu-kvm qemu virt-manager libvirt libvirt-python libvirt-python python-virtinstvirt-manager libvirt libvirt-python libvirt-python python-virtinst

I have no idea why, but the qemu-kvm binary isn't located in a regular path. Its located in /usr/libexec/qemu-kvm.
To make it more convenient just add /usr/libexec to your $PATH or symlink qemu-kvm in /usr/bin.

If you want to use your vms with normal users (not root) you have to add the specific users into the kvm group:

usermod -G kvm -a skull

Step one: Creating an Image

qemu-img create -f qcow2 ldap.lan.darktemple.ch.img 5G
switch description
-f Filesystem: gcow2 (standard noways)

Step two: Start your new VM

qemu-kvm -hda mail.lan.darktemple.ch.img -cdrom /data/daten/isos/CentOS-6.0-x86_64-netinstall.iso -m 1024 -boot d -vnc :3 -usbdevice tablet &
switch description
-hda loads the specified image file into the first virtual harddrive
-cdrom use a specified iso file as your virtual cd drive
-m Memory
-boot Boot your virtual machine from a specified drive (Windows drive letters!)
-net specifies the network interface which we are gonna use
-vnc Sets the vnc display, accessible in this example over hostname:5903
-usbdevice Setting usbdevice to “tablet” fixes the mousebug in the vnc

After that your virtual machine is started and accessable over vnc through 127.0.0.1.
That sucks because we have a CLI only server so we cannot access the vnc session.

Found a fairly simple solution here at linux-kvm.com
Just start your vm with this command

qemu-kvm ldap.lan.darktemple.ch.img -cdrom /data/daten/isos/CentOS-6.0-x86_64-netinstall.iso -boot d -vnc :1

After that you can connect to your Host System via any VNCviewer in your network. Use the port according to your -vnc :N option.
In Example: 192.168.2.253:5901

And here is the Result :)

Network Bridging

start_bridge.sh

To build a network bridge we have to install bridge-utils and tunctl (rpmforge repo).

yum install bridge-utils
yum install runctl

Here is a script that sets up a network bridge (br0) and creates a new network interface tap0 to use with our vm.

bridge_eth0_tap0.sh
#!/bin/bash
#===============================================================================
#
#          FILE:  bridge20.sh
# 
#         USAGE:  ./bridge20.sh 
# 
#   DESCRIPTION:  
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  SKull (), skull@darktemple.ch
#       COMPANY:  
#       VERSION:  1.0
#       CREATED:  08/21/2011 12:57:16 AM CEST
#      REVISION:  ---
#===============================================================================
 
PATH=$PATH:/usr/sbin:/sbin
sudo chown root:kvm /dev/net/tun
sudo chmod 0660 /dev/net/tun
 
# Add a bridge called br0
sudo brctl addbr br0
 
# reset eth0
sudo ifconfig eth0 0.0.0.0
 
# add eth0 to the bridge
sudo brctl addif br0 eth0
 
# give br0 your old ip
sudo ifconfig br0 192.168.2.253 netmask 255.255.255.0 up
 
# add a route to the 
sudo route add -net 192.168.2.0 netmask 255.255.255.0 br0
 
# set default gateway
sudo route add default gw 192.168.2.1 br0
 
# create tap0 and permit acces for the user "skull"
sudo tunctl -b -u skull
 
#use this line if you want to use a fix ip adress
#sudo ifconfig tap0 192.168.2.120 netmask 255.255.255.0 up
 
#enable tap0 and recieve a dhcp adress
sudo ifconfig tap0 up
 
#add tap0 to the bridge
sudo brctl addif br0 tap0
 
export SDL_VIDEO_X11_DGAMOUSE=0
 
# optional set an iptables rule:
# sudo iptables -I RH-Firewall-1-INPUT -i br0 -j ACCEP

To Start your VM with the new interface use:

qemu-kvm /vms/auth.img -m 1024 -k de-ch -vnc :2 -usbdevice tablet -net nic -net tap,ifname=tap0,script=no &

Tap interfaces

To create an additional tap interface use the following command:

(~)skull@dc: tunctl -b -u skull

To use your VM with your new created interface start it with the according -net switch:

qemu-kvm /vms/auth.img -m 1024 -k de-ch -vnc :2 -usbdevice tablet -net nic -net tap,ifname=tap1,script=no &

To use your VM with two network interfaces use the following command:

qemu-kvm /vms/auth.img -m 1024 -k de-ch -vnc :2 -usbdevice tablet -net nic -net tap,ifname=tap1,script=no -net nic -net tap,ifname=tap1,script=no &

MAC Addresses

For some reason qemu-kvm always uses the default mac adress.
To change that connect to your guest with VNC and edit the according network scripts:

(~) skull@auth:$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
HWADDR="52:54:00:12:34:56"
NM_CONTROLLED="yes"
ONBOOT="yes"

To change the address:

ifconfig eth0 down
ifconfig eth0 hw ether 52:54:00:12:30:56
ifconfig eth0 up

The changes get lost if you restart the machine.
Just put the commands in your rc.local to solve this issue.

Troubleshooting

Warning! Using dnsmasq as a DNS causes a problem! If you want to start your vms you get an error saying that:

Virtual network default has not been started

You have to add the following lines to your dnsmasq.conf

      1 bind-interfaces
      2 listen-address=192.168.2.253

Then start your default Network by typing:

virsh net-start default
linux/kvm.txt · Last modified: 2013/10/25 21:55 by skull@darktemple.ch