Managing KVM/QEMU Disk Formats and Snapshots with qemu-img and virsh
This guide explains how to inspect a KVM virtual machine's disk format, convert raw images to qcow2, create and list snapshots, revert to a specific snapshot, and delete unwanted snapshots using qemu-img and virsh commands on a Linux host.
This article demonstrates how to manage KVM/QEMU virtual machine disk images and snapshots on a Linux host.
Check Disk Format
cd /var/lib/libvirt/images/
qemu-img info ehs-rac-01.qcow2
image: ehs-rac-01.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 34G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 ehs-rac-01-bak 1.6G 2019-09-09 16:00:13 00:16:16.672
2 ehs-rac-01-bak02 1.9G 2019-09-09 16:04:40 00:20:27.294
Format specific information:
compat: 1.1
lazy refcounts: trueKVM uses raw format by default for best performance, but raw does not support features such as compression or encryption; qcow2 is required for those features. Since the image is already qcow2, conversion is unnecessary.
Convert Disk Format (if needed)
qemu-img convert -f raw -O qcow2 ehs-rac-01.img ehs-rac-01.qcow2-f specifies the source format, -O the target format.
Snapshot Management
Create Snapshot
virsh snapshot-create ehs-rac-01 virsh snapshot-create-as ehs-rac-01 ehs-rac-01-bakList Snapshots
virsh snapshot-list ehs-rac-01 Name Creation Time State
------------------------------------------------------------
ehs-rac-01-bak 2019-09-09 16:00:13 +0800 running
ehs-rac-01-bak02 2019-09-09 16:04:39 +0800 runningShow Current Snapshot
virsh snapshot-current ehs-rac-01 <domainsnapshot>
<name>ehs-rac-01-bak02</name>
<state>running</state>
<parent>
<name>ehs-rac-01-bak</name>
</parent>
<creationTime>1568016279</creationTime>
<memory snapshot='internal'/>
<disks>
<disk name='hda' snapshot='internal'/>
<disk name='hdb' snapshot='no'/>
</disks>
...
</domainsnapshot>Locate Snapshot Configuration Files
ls -l /var/lib/libvirt/qemu/snapshot/ehs-rac-01/ total 16
-rw------- 1 root root 5250 Sep 9 16:04 ehs-rac-01-bak02.xml
-rw------- 1 root root 5193 Sep 9 16:04 ehs-rac-01-bak.xmlRestore a Snapshot
First shut down the VM: virsh destroy ehs-rac-01 Confirm the VM is stopped, then revert to the desired snapshot (e.g., ehs-rac-01-bak02):
virsh snapshot-revert ehs-rac-01 ehs-rac-01-bak02If you prefer to revert by timestamp, replace the snapshot name with the epoch time:
virsh snapshot-revert ehs-rac-01 1500373324Delete a Snapshot
Identify existing snapshots with qemu-img info or virsh snapshot-list, then delete the unwanted one: virsh snapshot-delete ehs-rac-01 ehs-rac-01-bak Verify deletion:
virsh snapshot-list ehs-rac-01 Name Creation Time State
------------------------------------------------------------
ehs-rac-01-bak02 2019-09-09 16:04:39 +0800 runningThe snapshot ehs-rac-01-bak has been removed.
For more details, see the original article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
