Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Qcow2 Conversion and Snapshotting

If you use Virtual machines, such as within KVM, then you may be running raw disk images. Converting them to Qcow2 will give you some advantages, such as the ability to snapshot your VM. Here we will show you how to:

  • Check your disk file's format to see if it is raw or qcow2
  • Convert your disk file from raw to qcow2
  • Take a snapshot of your qcow2 disk.

Check Your Format

You can test your disk file with the following command:

qemu-img info [disk filename]  

If it is a raw image, you should get output similar to below:

file format: raw  
virtual size: 30G (32217432064 bytes)  
disk size: 30G  

If it is a qcow2 image format, then it will output something similar to:

file format: qcow2  
virtual size: 30G (32217432064 bytes)  
disk size: 4.8G  
cluster_size: 65536  
Format specific information:  
    compat: 1.1
    lazy refcounts: false

Convert Raw To Qcow2

Simply enter the following command:

qemu-img convert -f raw -O qcow2 [input filename] [output filename]  

After the operation completes, you may wish to delete the original input file. You will need to update any configs that utilize the new image, such as through sudo virsh edit [VM ID] for KVM. E.g. change the type from raw to qcow2 and update the path.

Take A Snapshot

If your KVM guests are now running with Qcow2 disk images, you can take an internal snapshot of the guest with the following command:

virsh snapshot-create [VM ID]  

Alternatively, you can create a file with the following contents

    <name>Name for the snapshot</name>
    <description>Description for the snapshot</description>

Then pass it to the snapshotting command.

virsh snapshot-create [VM ID] [filepath]  

You can take snapshots of guests whilst they are running. Whilst the snapshot is being taken, the guest will be "paused". The "state" of the guest is also saved.

List Snapshots

One can list a virtual machine's snapshots with

sudo virsh snapshot-list [VM ID]  

Which will output something like:

 Name                 Creation Time             State
 1422179589           2015-01-25 09:53:09 +0000 running

Restore Snapshot

If you want to "revert", "go to", or "restore" a previous snapshot, then execute the following command:

virsh snapshot-revert [VM ID] [Snapshot Name]  

Delete Snapshot

virsh snapshot-delete [VM ID] [Snapshot Name]  


Now you should be able to convert your raw KVM guests to qcow2 ones to enable and utilize snapshotting. I have only covered the basics, but if you are interested in learning about internal and external snapshots, then I recommend you read Kashyap Chamarthy's blog post.