Creating KVM VM with qcow2 disk format for supporting snapshots

From Notes_Wiki

Home > CentOS > CentOS 6.x > Virtualization tools > KVM > Creating KVM VM with qcow2 disk format for supporting snapshots

To create kvm VM with qcow2 disk format for supporting snapshots use:

  1. Create disk image for VM using something like:
    qemu-img create -f qcow2 ubuntu.qcow2 50G
  2. Create new VM with given disk image. Choose option to customize VM before install.
    • In customization or setting window for Disk 1 change "Storage format" to qcow2. Without this change KVM will treat the file as 250KB raw image file.
  3. Install Operating System
  4. Shutdown the VM
  5. Take snapshot of fresh installation using:
    virsh snapshot-create-as Ubuntu_desktop_12.04 "fresh-install" "Just installed ubuntu"
  6. Start VM again. Now it would get started from fresh-install snapshot. To verify this use:
    virsh snapshot-info Ubuntu_desktop_12.04 --current
  7. To get list of all snaphosts use:
    virsh snapshot-list Ubuntu_desktop_12.04
  8. Now change something in running VM such as menu entries and shutdown the VM.
  9. Take snapshot of new VM with modified menu entries using:
    virsh snapshot-create-as Ubuntu_desktop_12.04 "changed-menu" "Changed icons pinned to menu on left"
  10. Get list of all snaphosts using:
    virsh snapshot-list Ubuntu_desktop_12.04
  11. Restore earlier snapshot using:
    virsh snapshot-revert Ubuntu_desktop_12.04 fresh-install
  12. Verify by starting VM that the menu entries as same as in case of fresh-install and all modifications done and saved as changed-menu snapshot are not available.
  13. Verify that running VM is using fresh-install snapshot using:
    virsh snapshot-info Ubuntu_desktop_12.04 --current
  14. Note restoring "fresh-install" snapshot does not affects "changed-menu" snapshot which is still available for restoration. You can list all available snapshots using:
    virsh snapshot-list Ubuntu_desktop_12.04
  15. To see snapshots in tree fashion to know relations between them use:
    virsh snapshot-list Ubuntu_desktop_12.04 --tree
  16. To delete an unwanted snapshot use:
    virsh snapshot-delete Ubuntu_desktop_12.04 changed-menu
  17. Again verify using snapshot-list that snapshot got deleted properly.

Note that here we have created and restored all snapshots when VM was stopped. But snapshots can be created while VM is running to store complete state disk and RAM as part of snapshot. It is however recommended to create disk only snapshot for efficiency and reliability as long as possible.


Converting kvm raw image to qcow2 format for snapshots

These steps need to be tried for verification

To be able to create snapshot from virsh the VM must use qcow2 disk images. To convert a raw disk image to qcow2 format use (when VM is powered off):

qemu-img convert -f raw -O qcow2 -o preallocation=metadata windows7_sp1.img windows7_sp1.qcow2

This steps takes way too much time and disk space. It might not be worth doing it for a fresh VM or for VM which can be setup quickly.

Then modify the VM configuration to use .qcow2 file instead of original raw hard-disk using:

   virsh dumpxml <domain> > <domain>.xml
   #Edit <domain.xml> to change disk format and location
   virsh undefine <domain>
   virsh define <domain>.xml
   virsh start <domain>

Do not use 'virsh create <domain>.xml' as in that case on shutdown the guest definition and all associated snapshot information would be lost.

If the VM is working fine then delete old raw hard-disk image and shutdown the VM. Then try to take snapshot using:

   virsh snapshot-create-as <domain> <snapshot-name> <description>

Some information learned form http://redes-privadas-virtuales.blogspot.in/2011/03/taking-snapshots-on-kvm-with-libvirt.html


Home > CentOS > CentOS 6.x > Virtualization tools > KVM > Creating KVM VM with qcow2 disk format for supporting snapshots