jing kang
jing kang

Reputation: 441

How to keep(import) KVM snapshots after virt-clone a VM?

example:

virt-clone --connect=qemu:///system -o winxp_sp3_00 -n winxp_sp3_03 -f /opt/vme/winxp_sp3/winxp_sp3_03.updated.qcow2

I got a new domain winxp_sp3_03 whose qcow2 has cloned snapshots:

qemu-img info  winxp_sp3_03.updated.qcow2

image: winxp_sp3_03.updated.qcow2
file format: qcow2
virtual size: 80G (85899345920 bytes)
disk size: 8.2G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         winxpsp3_basic            0 2017-04-14 23:44:14   00:00:00.000
2         winxpsp3_pajno            0 2017-04-16 02:12:27   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: false

But virsh doesn't show snapshots:

virsh snapshot-list winxp_sp3_03
 Name                 Creation Time             State
------------------------------------------------------------

what is the safe approach to let virsh recognize those snapshots?

Thx

Upvotes: 0

Views: 4505

Answers (2)

Ehsan
Ehsan

Reputation: 3137

virt-clone removes certain meta-data from the qcow2 file. If you clone the vm with virt-clone, the meta-data for snapshot tags will be removed from the disk file, as a result you can not redefine the snapshots using xml definition from the source. Well, technically you can, but they will just point to the state of the vm at the moment of redefining the snapshot. Snapshots will appear on the machine, but you won't see the actual intended snapshots after restoring them.

After wrestling with qemu for a couple of days and going through hundreds of pages of documentation, I came up with the following method that works:

We need the xml definition for defining the destination vm. Let's use virt-clone to generate the xml definition.

virt-clone --original $Source_VM_Name --name $Destination_VM_Name --file /var/lib/libvirt/images/$Destination_VM_Name.qcow2 --print-xml > $Destination_VM_Name.xml

The above command only produces an xml definition without actually cloning the vm.

Use Linux cp command to copy the qcow2 disk. Adapt the commands to point to the right disk file on your system.

cp $Source_VM_Name.qcow2 $Destination_VM_Name.qcow2

Now you have to reset the configuration for the destination disk file:

virt-sysprep -a $Destination_VM_Name.qcow2

Note that you might need to use sudo for the above command, depending on your configurations. This command resets the UUID, MAC Addresses and other elements that need to be reset in the destination vm.

We'll use the xml file generated in step file in order to define the destination machine using a virsh command:

virsh define $Destination_VM_Name.xml

We have a functional VM now, we need to export the snapshot definitions from the source vm at this step.

virsh snapshot-list $Source_VM_Name --tree

This command will list the snapshots for the source VM.

For each snapshot listed in this tree:

virsh snapshot-dumpxml $Source_VM_Name $Snapshot_Name --security-info > Snapshot_Name.xml

We need to change the UUID and domain name in the snapshot xml definition.

nano Snapshost_Name.xml

Change

<domain type='kvm'>
  <name>$Source_VM_Name</name>
  <uuid>$Source_UUID<uuid>

to

<domain type='kvm'>
  <name>$Destination_VM_Name</name>
  <uuid>$Destination_UUID<uuid>

Also change the MAC Address field to the destination MAC Address:

<mac address='$Destination_MACADDRESS'/>

And change the backing disk file to the destination diskfile:

<source file='/var/lib/libvirt/images/$Destination_VM_Name.qcow2'/>

Now redefine each snapshot, for the cloned VM.

virsh snapshot-create $Destination_VM Snapshot_Name.xml --redefine

Note that the order of redefining snapshots matters, some snapshot might be childeren of others. Start from the root of the tree to the leaves.

If you are using virt-manager, close it, open again and you should see the snapshots.

Upvotes: 1

jing kang
jing kang

Reputation: 441

virsh snapshot-list winxp_sp3_10l_sc_99

virsh snapshot-dumpxml winxp_sp3_10l_sc_99 1497601133 > snapshot.xml

--create snapshot based on dumped snapshot.xml

virsh snapshot-create --domain winxp_sp3_10l_sc_01 snapshot.xml

Upvotes: 0

Related Questions