Sample Header Ad - 728x90

Delete snapshots only in libvirt, but retain them in qcow2

1 vote
0 answers
734 views
I executed the following commands
-- 12:18:40 sudo virsh snapshot-delete Windows-10-128GB 2022-04-12-Pre-Activation
Domain snapshot 2022-04-12-Pre-Activation deleted
                                                                                                                                                                                                                                                  66ms

-- 12:28:20 sudo virsh snapshot-list Windows-10-128GB
 Name                         Creation Time               State
-------------------------------------------------------------------
 2022-04-12-Post-Activation   2022-04-12 21:54:19 +0200   shutoff
                                                                                                                                                                                                                                                  58ms


-- 12:28:24 qemu-img snapshot -l ~/kvm-images/Windows-10-128GB.qcow2
Snapshot list:
ID        TAG               VM SIZE                DATE     VM CLOCK     ICOUNT
1         2022-04-12-Pre-Activation      0 B 2022-04-12 21:19:39 00:00:00.000
2         2022-04-12-Post-Activation      0 B 2022-04-12 21:54:19 00:00:00.000
which lead me to believe that virsh snapshot-delete only removes the snapshots from libvirt's internal datastore without affecting the qcow2 file. So I deleted both this way and removed the VM from libvirt via sudo virsh undefine Windows-10-128GB. Before doing all this I exported the xml definitions of the snapshots and the VM:
sudo virsh dumpxml Windows-10-128GB > 2023-09-10---Windows-10-128GB.xml
sudo virsh snapshot-dumpxml Windows-10-128GB 2022-04-12-Pre-Activation > 2023-09-10---Windows-10--128GB---2022-04-12-Pre-Activation.xml
sudo virsh snapshot-dumpxml Windows-10-128GB 2022-04-12-Post-Activation > 2023-09-10---Windows-10-128GB---2022-04-12-Post-Activation.xml
But now when I re-issue qemu-img snapshot -l ~/kvm-images/Windows-10-128GB.qcow2, it no longer shows snapshots in that file. Also, the size decreased from
48G -rw-------  1 user user 156G Sep 10 12:52 Windows-10-128GB.qcow2
to
31G -rw-------  1 user user 156G Sep 10 12:52 Windows-10-128GB.qcow2
which seems to indicate that the snapshots are lost. Why didn't virsh snapshot-delete initially not delete the data from the qcow2 file, and, for future reference, how do I just remove a snapshot from libvirt but retain it in the qcow2 file, so that I can virsh undefine the VM and move the unused qcow2 file to some cold storage? virsh undefine Windows-10-128GB throwing the following was what moved me to remove the snapshots in the first place.
error: Failed to undefine domain 'Windows-10-128GB'
error: Requested operation is not valid: cannot delete inactive domain with 2 snapshots
I should add that the source paths to the qcow2 file in the snapshot-dumpxml were pointing to a non-existing file, because I once moved the file and adapted the xml of the VM to point to the new location. Could this be the reason why the snapshot didn't get removed in the first place? Because after that, I corrected the exported snapshot-dumpxml to point to the correct location and the used sudo virsh snapshot-create --redefine Windows-10-128GB 2023-09-10---Windows-10-128GB---2022-04-12-Pre-Activation---corrected.xml to re-integrate the snapshot into libvirt. Maybe these corrections then enabled virsh to remove it from the qcow2 file?
Asked by Daniel F (937 rep)
Sep 10, 2023, 11:49 AM