bendun.cc

Scaling libvirt Linux VMs

Written , a 2 minute read

While doing assignments and projects for university, I had to use several virtual machines at one time. Learning how to setup VPNs or testing distributed networking protocols (my Harmonia project) makes requirement for several copies of one VM quite common actually. What I didn't expect was that I spend over an hour on this, shooting myself in a foot a few times.

Why to do it and not just go trough installation several times? It's not Windows, they install quite fast. To save disk space. By scaling using shared immutable backing disk image you don't duplicate data and keep majority of disk usage in one place. Maybe immutable distros have nice interactions with that, need to investigate further.

This all could be automated with Ansible btw. Or maybe there are already existing solutions.

Actual guide

Warning: this instruction requires some libvirt knowledge and is mostly a remainder for me. I should clean it up. If you don't know virtualization with libvirt well, then you should probably go with normal setup of several installations for several machines.
  1. Get path to vm disk: virsh dumpxml vm_name
  2. Go to the directory specified in disk XML, switch to root
  3. Create n drives from 1 to n, where n is the scaling factor of your choosing: qemu-img create -f qcow2 -b ubuntu.qcow2 -F qcow2 ubuntu_1.qcow2
  4. virsh edit vm_name, switch name to vm_name_1, remove uuid, remove all mac adresses, switch to backing file:
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' discard='unmap'/>
      <source file='/var/lib/libvirt/images/ubuntu_1.qcow2' index='1'/>
      <backingStore type='file' index='2'>
        <format type='qcow2'/>
        <source file='/var/lib/libvirt/images/ubuntu.qcow2'/>
        <backingStore/>
      </backingStore>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
  5. use vm_name_1 to create next ones, by removing uuid, macs and changing name to vm_name_n and disk to the one created for this nth vm
  6. remove old vm_name
  7. inside new vms:
    • change hostnames (important if you are using avahi to connect to them like me)
    • change machine id