wlanboy

tutorials and ruby scripts

Read my tutorials

Read my gists

Tutorial

Tutorial: kvm

You have a pc that is just doing nothing? You have a dedicated server and want to play around without messing things up? Enable KVM on your linux kernel to create one/view virtual servers.

  1. Check if your cpu has Intel VT-x or AMD-V
egrep -c '(vmx|svm)' /proc/cpuinfo

1 means that your cpu does have support for hardware virtualization. 0 means you might create quite slow vps. But you are able to run virtual servers.

  1. Check if you have installed a 64bit kernel
uname -m

Something like amd64 or x86_64 indicates your are running a 64-bit kernel. You do not want to run a 32-bit kernel. That would limit your RAM usage to 2GB.

  1. Install KVM tools (I am using Ubuntu/Debian commands here)
sudo apt-get install qemu-kvm libvirt-bin virtinst libvirt-daemon-system libvirt-dev libvirt-clients bridge-utils
  1. Add user that runs virtual servers to group (user has to re-login to get new group permissions)
sudo adduser [username] libvirt
sudo adduser [username] libvirt-qemu
  1. Test your installation
virsh -c qemu:///system list
 Id Name                 State
----------------------------------

An output like “libvir: Remote error : Permission denied” or “error: failed to connect to the hypervisor” says that something went wrong.

  1. Fix ownership
sudo chown root:libvirtd /dev/kvm

I do not know why, but for me /dev/kvm did not had the correct ownership.

  1. Restart kernel modules - or restart the server
rmmod kvm
modprobe -a kvm
  1. Optional: Install GUI for virtual server mangment
sudo apt-get install virt-manager
  1. Networking

Well per default KVM has it’s own network - called usermode networking. Works like OpenVPN. You have your own subnet - e.g. 10.0.1.0/24 and every KVM gets one ip address out of it. Enough for me. I once need a connection to one of my webservers out of the KVM. I used port forwarding (iptables) to forward a connection from my host to the KVM.

  1. Create a virtual server

There are quite a view options. I prefer a python script called “virt-install”.

sudo apt-get install python-virtinst

First look to the man page of virt-install

man virt-install

And create your first vps:

sudo virt-install --connect qemu:///system -n kvm1 -r 2048 --vcpus=2 \
--disk path=/var/lib/libvirt/images/kvm1-image.img,size=20 \
-c /isos/ubuntu-min-install.iso --vnc --noautoconsole --os-type linux \
--accelerate --network=network:default --hvm --vncport=5951

Name: “kvm1”, RAM: 2 GB, CPUs: 2, HDD: 20GB and a defined vnc port you can connect to (127.0.0.1:5951)

  1. Managing your virtual servers

start managing console

virsh --connect qemu:///system

List virtual servers

list -all

start virtual server

start [name of virtual server]

stop virtual server

shutdown [name of virtual server]

That’s it. I know there are a lot of additional topics. Maybe someone wants to explain bridged networking and ip/gateway assignment. Did not need it and therefore did not play around with it.

Please feel free to add additional steps to this mini guide.