# Install and configure KVM to your dedicated server



## wlanboy (May 21, 2013)

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.


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.
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.
Install KVM tools (I am using Ubuntu/Debian commands here)

```
sudo apt-get install qemu-kvm libvirt-bin bridge-utils
```

Add user that runs virtual servers to group (user has to re-login to get new group permissions)

```
sudo adduser [username] libvirtd
```

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.
Fix ownership

```
sudo chown root:libvirtd /dev/kvm
```
I do not know why, but for me /dev/kvm did not had the correct ownership.
Restart kernel modules - or restart the server

```
rmmod kvm
modprobe -a kvm
```

Optional: Install GUI for virtual server mangment:

```
sudo apt-get install virt-manager
```

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.

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)
Managing your virtual servers

A) start managing console


virsh --connect qemu:///system
B) List virtual servers

```
list -all
```
C) start virtual server

```
start [name of virtual server]
```
D) 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.


----------



## NodeDeals (May 22, 2013)

A nice tutorial wlanboy. More virsh commands to create, modify vm's and the usage plus examples would be much appreciated too


----------



## jcaleb (May 22, 2013)

this is nice, i can try do at home desktop for playing


----------



## wlanboy (May 22, 2013)

NodeDeals said:


> More virsh commands


Well, there are a lot of commands. Some common ones:


Start VM:start [name of vps]

Stop VM:


shutdown [name of vps]

Delete VM:


virsh destroy [name of vps]

virsh undefine [name of vps]

Export VM definition to xml


virsh dumpxml [name of vps] > ~/myvps.xml

Create VM out of xml


virsh create ~/myvps.xml

Edit VM (if you like vi)


virsh edit [name of vps]

Open serial console to vps


virsh console [name of vps]

Viewing all commands


virsh help

In my opinion altering the XML file is quite easy. Virsh is limited.


E.g. adding a second hard disk with "attach-disk [name of vps] ~/newdisk.img" is limited because some parameters are lacking.


But you can call "attach-device [name of vpy]~/myhdd.xml".


With myhdd.xml like:


<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback'/>
<source file='/home/wlanboy/images/seconddisk.img'/>
<target dev='vdb' bus='virtio'/>
</disk>

But this is only working if hotplug is working on your vps's os.


----------



## wlanboy (May 23, 2013)

And an example file of one of my small debian test vms:


```
<domain type='qemu'>
  <name>debian</name>
  <uuid>6637c913-19a4-cc63-38db-c9eb9ecc3983</uuid>
  <memory>65536</memory>
  <currentMemory>65536</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='i686' machine='pc-0.14'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/qemu</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/debian.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    </disk>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:1f:d6:79'/>
      <source network='default'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes'/>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>
```


----------



## drmike (May 23, 2013)

AWESOME!!!

Good work @wlanboy.


----------



## Enterprisevpssolutions (May 23, 2013)

And for others that want a virtual control panel installed in a few steps stop and look at proxmox. Uses kvm and openvz easy setup for new users to the virtual world who want to learn as it gives them both the command line and a gui interface.


----------



## wlanboy (May 24, 2013)

virt-manager is not that bad:













Everyone can use this. It is a build-in feature of the Linux kernel.


----------



## wlanboy (May 25, 2013)

Another cool feature of KVM are snapshots.


Get list of current snapshots

```
virsh snapshot-list
```

Create a snapshot

```
virsh snapshot-create-as [name of vps] [
```


----------



## InertiaNetworks-John (May 26, 2013)

Thanks for the great tutorial!


----------



## shahaz (May 27, 2013)

Thanks for the tutorial .


----------



## Corey (Jul 16, 2013)

Hi,

thanks for the tutorial! I'm having an issue. i have a windows 7 vm, it shows 0% cpu usage on windows, but it's showing 50% on all cores in htop, any idea how to fix this?

load average: 4.91, 5.22, 5.23


----------



## shawn_ky (Jul 16, 2013)

Thanks for the tutorial! Very nice!


----------



## Jeffrey (Jul 17, 2013)

Or you could just use VirtualBox...


----------



## elohkcalb (Jul 17, 2013)

Hmm... there's something that I always forget until I restart the node. 


```
virsh autostart [name of vps].
```


----------



## MannDude (Jul 17, 2013)

Damn wlanboy, another quality tutorial!


----------



## wlanboy (Jul 17, 2013)

MannDude said:


> Damn wlanboy, another quality tutorial!


Thank you for kind (late  - posted 21st May ) words.


----------



## Jade (Jul 20, 2013)

Nice tutorial


----------



## dzchimpo (Jun 17, 2015)

Any other GUI that doesnt require installing an application, and can control the vm with the broswer, like Solus/OverVZ web panel? I know proxmox has support for kvm.


----------



## HalfEatenPie (Jun 17, 2015)

dzchimpo said:


> Any other GUI that doesnt require installing an application, and can control the vm with the broswer, like Solus/OverVZ web panel? I know proxmox has support for kvm.


Proxmox, Virtualizor, Solus, oVirt, honestly any on this list: http://www.linux-kvm.org/page/Management_Tools

There's also WebVirtMgr. 

There are a ton available with a Web Panel.  However, some people prefer not managing their services through a browser because it's considered additional overhead and also a potential security risk.


----------



## dzchimpo (Jun 17, 2015)

After Step 3. Install KVM tools (I am using Ubuntu/Debian commands here), I get this:


```
[email protected]:~# adduser root libvirtd
adduser: The group `libvirtd' does not exist.
```


----------



## William (Jun 17, 2015)

use group "libvirt" and "libvirt-qemu":

[email protected]:~# cat /etc/group | grep lib
libvirt:x:114:
libvirt-qemu:x:116:libvirt-qemu


----------

