Table of Contents
KVM Tutorial
Installing KVM
Check if your CPU supports hardware virtualization via command:
egrep '(vmx|svm)' --color=always /proc/cpuinfo
If nothing is displayed CPU doesn't support virtualization.
Instal packages ubuntu-virt-server, python-vm-builder and kvm-pxe via command (but just in case, first update the package repository on the node):
apt-get update apt-get install ubuntu-virt-server python-vm-builder kvm-pxe
Add the current user to the group:
adduser `id -un` libvirtd adduser `id -un` kvm
Logout and login again:
logout ssh root@node1-1
Check users group by comamand:
groups
Output should be similar to:
root kvm libvirtd
To list all running KVMs use:
virsh -c qemu:///system list
Setup network bridge on the host
Install the package bridge-utils:
apt-get install bridge-utils
Create the bridge interface named br0:
brctl addbr br0
Edit the network interfaces configuration with editor (for example nano):
nano /etc/network/interfaces
and add persistent configuration for br0 interface:
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth1 iface eth1 inet dhcp # The secondary network interface auto eth0 iface eth0 inet manual #Bridge 0 interface auto br0 iface br0 inet static address 10.28.1.1 netmask 255.255.0.0 broadcast 10.28.255.255 network 10.28.0.0 bridge_ports eth0 bridge_fd 9 bridge_hello 2 bridge_maxage 12 bridge_stp off
Restart networking with command:
/etc/init.d/networking restart
For bridge verification open the new ssh window and run:
tcpdump -i br0
and then, in the other window, try to ping some adress in same subnet, for example:
ping 10.28.0.1
In the second windows you should see logging ofrequest/reply messages.
It is recommended to reboot the machine once the bridge is configured.
Create and run KVM image using vmbuilder
Folder /var/lib/libvirt/images/ will be created for storing images. Each image will have separate folder for example /var/lib/libvirt/images/virtual-machine1
Create necessary folders:
mkdir -p /var/lib/libvirt/images/vm1/mytemplates/libvirt
'-p' option of mkdir command means that all parent folders will be created if they do not exist.
vmbuilder tool uses templates for creating of images so copy the template:
cp /etc/vmbuilder/libvirt/* /var/lib/libvirt/images/vm1/mytemplates/libvirt/
Open partition configuration file with command:
nano /var/lib/libvirt/images/vm1/vmbuilder.partition
and add the following lines:
root 8000 swap 4000 --- /var 20000
Copied text defines three partitions - root, swap and var and their sizes. "—-" means that var partition will be separate image file.
Go to folder vm1:
cd /var/lib/libvirt/images/vm1/
Execute vmbuilder command:
vmbuilder kvm ubuntu --suite=oneiric --flavour=virtual --arch=amd64 --mirror=http://de.archive.ubuntu.com/ubuntu -o --libvirt=qemu:///system --ip=10.28.4.4 --gw=10.28.0.1 --mask=255.255.0.0 --bcast=10.28.255.255 --net=10.28.0.0 --dns=10.0.0.9 --part=vmbuilder.partition --templates=mytemplates --user=guest --name=Guest --pass=guest --addpkg=vim-nox --addpkg=unattended-upgrades --addpkg=acpid --mem=256 --hostname=vm1 --bridge=br0 --addpkg openssh-server --addpkg nano
Create and run KVM image using KVM directly
Download ubuntu server ISO file:
wget -O ubuntu-server.iso http://www.ubuntu.com/start-download?distro=server&bits=64&release=latest
Create Disk:
qemu-img create -f qcow2 vm-oneric-amd64-serial 10G
Setup network bridge (see part III).
Install tool for Tun:
apt-get install uml-utilities
Create the tap interface:
tunctl
Output of the name is (tap0).
Add tap interface to the bridge as a port:
brctl addif br0 tap0
Bring up tap0 interface:
ifconfig tap0 up
Boot and Install Ubuntu using VNC:
kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -cdrom ubuntu-server.iso -boot d -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0
To run already created image you can enter:
kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0
Use VNC client to connect to virtual machine. Kill Virtual machine (KVM process):
pkill -9 kvm
Remove tap0 in as a bridge interface:
brctl delif br0 tap0
Delete tap interface:
tunctl -d tap0
kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0
kvm -daemonize -m 1024 -hda vm-oneric-amd64-serial -cdrom ubuntu-11.10-server-amd64.iso -boot d -smp 2 -cpu qemu64 -net nic,model=virtio -net tap,ifname=tap0,script=no -vnc :0