9

I would like to use libvirt to run multiple Domains (VMs) based on the same image at once. The image itself should not be modified. The image should be considered as a starting point or template.

An obvious possibility would be to create a (temporary) copy for every domain. Since the image might take multiple GB, I don't want to create a full copy of it every time. It would like to store differences only. As I understand the documentation, external snapshots are using such technics. But it seems that snapshots are bound to a domain and I cannot use them as template.

According to documentation of qemu, I could use qemu directly while passing option -snapshot. As far as I'm not committing changes manually, it should work.

qemu-system-x86_64 -snapshot -hda <image>

Is there a way to achieve something similar in libvirt?

JojOatXGME
  • 3,023
  • 2
  • 25
  • 41

1 Answers1

13

All you need is to use qcow2 backing files. In the next steps I'll assume that you already have your base image as a qcow2.

Create a disk image backed by your base image:

qemu-img create -f qcow2 \
                -o backing_file=/path/to/base/image.qcow2 \
                /path/to/guest/image.qcow2

Then in your guest, use /path/to/guest/image.qcow2 as disk. This file will only get the difference with the base image.

Check qemu-img's man page for more details. qemu-img also has commands to commit the overlay file changes into the base image, rebase on another base, etc.

  • Thanks! I will check it out. :) – JojOatXGME Nov 18 '16 at 14:29
  • It works this way. I would like to avoid calling `qemu-img` directly but to create such images with *libvirt*, I have to define *storage-pools*. I thing I will use `qemu-img` directly since using libvirt seems to be much overhead in this case. – JojOatXGME Nov 22 '16 at 22:31
  • This command needed a `-F qcow2` to work for me. – Botje May 16 '23 at 22:07