Linux KVM Virtualization Manual Low-Level Guest Export/Import Procedure
Internal
Overview
The goal of an export operation is to preserve the state of the guest - its definition and the state of its storage - at a certain moment in time, so it can be reverted to (import), later, possibly after the guest was removed for a while from the virtualization host. This is one of the possible procedures to snapshot a KVM virtual machine. It involves manually saving the XML definition of the guest and the state of its files systems. The guest must be down to be exported. This procedure does not produce a formal snapshot, recorded by the virtualization host.
The export procedure presented below has been coded in bash as:
Another way to snapshot a guest is:
Take a Virtual Machine Snapshot
If the virtual machine has more than one storage device attached, they all must be exported at the same time to produce a consistent set of storage snapshots that can be reverted to.
Shutdown the Target Guest
virsh shutdown <vm-name>
Identify all Storage Devices
virsh dumpxml <vm-name>
Look for <disk> definitions. The disk definition specifies the source and the format of the virtual storage device. Example:
<disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/main-storage-pool/appproxy.qcow2'/> ... </disk>
Preserve the Content of all Storage Devices
The storage volumes can be snapshot in-place, but in this case the virtualization host's storage pool will continue to store the content, or they can be removed from the storage pool and archived off-line. Both procedures are available below:
- For each storage device, snapshot the underlying storage volume with qemu-img.
- For each storage device, remove the corresponding storage volume and archive it off-line.
Save the Guest XML Definition
This is only necessary if we plan to modify the hardware configuration, or we want to temporarily remove any reference to the guest from the virtualization host:
virsh dumpxml <vm-name> > ./vm-definitions/template.xml
Optionally, if the guest is temporarily removed from the system, the guest definition can be removed:
Restore from Export Artifacts
Perform the steps described in the "Take a Virtual Machine Snapshot" section in reverse order:
Restore the Content of Storage Device from Snapshot
Depending on how the content of the storage devices was saved, for each storage device:
- Revert a disk to a previously saved state with qemu-img.
- Restore the content of an externally archived volume.
Restore the Guest XML Definition
This is only necessary if the hardware configuration changed between snapshots.
Define the guest from the previously saved XML definition:
virsh define