Choices — KVM on Ubuntu for virtualization

Teoman Haliloglu
4 min readNov 24, 2018

Now that we’ve bought a pretty decent server, I now had to choose between different host OS and virtualization alternatives. (Read the “what” and “why” here, that was the first choice). We could go Microsoft or VMWare paths, however, the licensing costs would hurt seriously, if not now then eventually.

I personally prefer no license cost and no annual renewal fees, and so does my boss!

I need to mention that I’ve been using Ubuntu at home for more than 10 years now. And I’ve played with KVM virtualization a number of times, to test open source software on virtual appliances, or to build windows VMs, for my daughter or my ex-wife, for unavoidable Microsoft-only cases.

So I knew that Linux has a very good virtualization module in the kernel, which is very stable, and the performance is very good. You can find various comparisons on the net between different hypervisors and benchmarks against bare metal. All I need to say is, KVM is no less than Hyper-V or ESX or Xen, if not better.

There are not many fancy tools for VM Management, but you can definitely survive after few minutes of googling, whenever you need help. And virt-manager is a pretty decent tool for creating and editing virtual machines. If you have a few thousand dollars to spend, you can also go packaged KVM based solutions like ProxMox, which offer built-in solutions for backup and easier management.

Anyway, OS selection was a no brainer for me, Ubuntu. And I decided to go with KVM.

For the OS part, back in the times where memory and disk were very expensive and scarce resources, a terminal based server OS would be good. But after some quick research and with my own experience at home, I decided to go for a desktop OS installation for those reasons:

  • Coming from the Microsoft world, I prefer to do things in GUI whenever possible.
  • Having a GUI on the host won’t do any harm in terms of performance for a modern server with many cores and lots of disk space. 1–2 GB of ram is well worth the time saving with a GUI.
  • There’s basically a little difference in installing a server OS and adding a GUI later, vs installing a desktop OS and add server packages whereever necessary. At least for Ubuntu.

A few notes for the setup of the host:

  • Use LVM, as it will simplify your life when creating and backing up your VMs. If you have LVM, then you can simply take a snapshot of the VM disks in a second, without taking the virtual machine down, and then backup the snapshot. And you can expand volumes dynamically, if needed. Much better than file based VMs.
  • When configuring/creating the logical volumes, a GUI will help, especially if you are not familiar with lvm commands like lvcreate, lvremove etc. I found that kvpm works great for the purpose. I created an SSD pool and a SATA pool, from which I carve out logical volumes to give to the VMs as needed.
  • nomachine is a great free tool for remote connection to your host. It uses NX protocol, which is very efficient in terms of bandwidth. Easy to install on all operating systems. Besides the host, we now have it on client machines. (we have Ubuntu and Windows clients)
  • Sound on the host can be a problem (it was for me, maybe because the server didn’t have a sound card, or nomachine media tools created problems, but I did not need sound on the server anyway), so you’d better uninstall pulseaudio and alsa packages from the host. (You can still have sound on your VMs, kvm provides a choice of virtual cards)
  • For the live backup part, the logic is as follows:
    * Pause the VM
    * Take the snapshot of the logical volume(s)
    * Resume the VM (it takes less than a second upto here, so nobody notices anything)
    * Copy the snapshot to a preferred backup location
    * There are some scripts on the net, which loops through all the virtual machines on the host and do the snapshot/copy process. But I couldn’t make them work, so I wrote a small script, which looks like this for a VM call myvm:

virsh dumpxml myvm > /backuplocation/$(date +”%Y_%m_%d”)/myvm.xml
virsh -c qemu:///system suspend myvm
lvcreate — size 1G — snapshot — name myvm_snap ‘nameofthelogicalvolumeofthevm’
virsh -c qemu:///system resume myvm
dd if=locationofthevolumegroup-myvm_snap bs=16384 | pigz -b 32624 -p 16 > /backuplocation/$(date +”%Y_%m_%d”)/myvm.gz
lvremove locationofthevolumegroup-myvm_snap -y

The first command copies the vm configuration, which will be needed to simplify things in case of a restore. I used pigz to compress and copy the snapshot, as it speeds up the process tremendously. (It is using 16 threads to compress in the above example! Adjust it to the capacity and the usage of your host at the backup time)

Another note for the speed and size of the backup is the free space. I used sdelete for windows VMs, and freezero (or cat command with proper parameters) for Linux ones, to write zeroes to the freespace, so that the backup can be compressed properly. (Maybe I should’ve filled the logical volumes with zeroes at the time of creation, and I wouldn’t need to do it one by one on the VMs.)

I add similar lines to my script whenever I create a new VM. Not very pretty or elegant, but it works and it is still automated!

Another tip would be setting the OS Type and version properly when setting up a server with virt-manager.

And last but not least, if you have a windows VM, install and use virtio drivers! Very important for disk and network performance!

Now , we have a free virtualization solution on an affordable fast server and ready for some deployments, where more choices are to be made!

--

--