amuck-landowner

Install and configure KVM to your dedicated server

wlanboy

Content Contributer
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
    Code:
    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)
    Code:
    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)
    Code:
    sudo adduser [username] libvirtd
  • Test your installation
    Code:
    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
    Code:
    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
    Code:
    rmmod kvm
    modprobe -a kvm
  • Optional: Install GUI for virtual server mangment:
    Code:
    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
    Code:
    man virt-install
    And create your first vps:
    Code:
    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
    Code:
    list -all
    C) start virtual server
    Code:
    start [name of virtual server]
    D) stop virtual server
    Code:
    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.
 
Last edited by a moderator:

NodeDeals

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

wlanboy

Content Contributer
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.
 
Last edited by a moderator:

wlanboy

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

Code:
<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>
 

Enterprisevpssolutions

Article Submitter
Verified Provider
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

Content Contributer
virt-manager is not that bad:

virt_manager_1.jpg

virt_manager_2.jpg

virt_manager_3.jpg

virt_manager_4.jpg

virt_manager_5.jpg

virt_manager_6.jpg

Everyone can use this. It is a build-in feature of the Linux kernel.
 
Last edited by a moderator:

wlanboy

Content Contributer
Another cool feature of KVM are snapshots.

  • Get list of current snapshots
    Code:
    virsh snapshot-list
  • Create a snapshot
    Code:
    virsh snapshot-create-as [name of vps] [
 

Corey

New Member
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
 
 
Last edited by a moderator:

elohkcalb

New Member
Hmm... there's something that I always forget until I restart the node. :)

Code:
virsh autostart [name of vps].
 

dzchimpo

New Member
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

The Irrational One
Retired Staff
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.  
 
Top
amuck-landowner