HOWTO Improve KVM Virtual Machine Efficiency on Fedora

The virt-manager tool is very handy. It let me quickly create a bunch of virtual machines. However, the guest machines were quite sluggish. Here are some ways to make them less sluggish.

Use the hard disk directly

Disk I/O was horrendously bad when I first created the guest machines. That’s partly because file activity in the guest results in a write to the virtual filesystem, which is backed by a file on the host filesystem, which is backed by the hard drive. So every guest I/O operation had to traverse 2 filesystems to get anything done. Better is to set up a virtual machine so that it can access disk blocks directly, without that second filesystem getting in the way. This is done by setting aside some space, then handing it to virtual machines.

Create a logical volume group to hold all of the logical volumes used for virtual machines. This is most easily done at install time, but vgcreate can be used to do it later. I named mine vg_virtual Then do the following for each virtual machine.

  1. Create a logical volume, e.g., lvcreate -n CentOS7 -L 40G vg_virtual
  2. Install the guest OS on that volume. Unfortunately, virt-manager currently doesn’t know how to do this, so we have to do it manually, e.g., virt-install -n CentOS7 -r 1024 --vcpus=2 --description "CentOS 7.0" -f /dev/vg_virtual/CentOS7 --os-type=linux --os-variant=rhel7 -c centos70.iso --cpu host

Use huge pages

Memory can be allocated more efficiently for the guest machine, and swapping avoided, if the host sets aside huge pages for the guest. I have 8 Gbytes of RAM on my host machine, so I decided to dedicate 2 GBytes to huge pages, as follows.

  1. Add this entry to /etc/fstab: hugetlbfs /dev/hugepages hugetlbfs defaults 0 0. This makes the hugetlbfs filesystem available.
  2. Add this entry to /etc/sysctl.conf: vm.nr_hugepages = 1024. That sets aside some memory for the huge pages. A reboot is needed for both this setting and the previous one to be applied. It is possible to apply them without a reboot, but memory fragmentation may interfere with the allocation of the huge pages, so a reboot is safer.
  3. For each virtual machine that you want to have take advantage of the available huge pages, do the following:
    1. virsh
    2. edit CentOS7
    3. Add this inside the <domain> tag:
      <memoryBacking>
        <hugepages/>
      </memoryBacking>