Linux KVM Virtualization Manual Low-Level Guest Export/Import Procedure: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(33 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Overview=
=Overview=


The goal of a ''snapshotting'' 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, later, possibly after the guest was removed for a while from the virtualization host. This is the procedure to [[Linux_Virtualization_Concepts#KVM_Virtual_Machine_Snapshot|snapshot a KVM virtual machine]].
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 [[Linux_Virtualization_Concepts#KVM_Virtual_Machine_Snapshot|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.


This is one way to do it, and consists in manually saving the guest XML configuration and the state of the associated filesystems. The guest must be down to take the snapshot. This procedure does not produce a formal snapshot, recorded by the virtualization host. An alternative is to use
The export procedure presented below has been coded in bash as:


{{External|https://github.com/NovaOrdis/libvirt-functions/blob/master/export-guest}}


It consists in taking snapshots of its XML definition and performing manual snapshots of all its storage devices.
Another way to snapshot a guest is:


 
{{Internal|Linux KVM Virtualization Guest Snapshot Using virsh snapshot Functionality|Guest Snapshot Using virsh snapshot Functionality}}
{{Error|Investigate virsh snapshot and factor it in.}}


=Take a Virtual Machine Snapshot=
=Take a Virtual Machine Snapshot=


{{Warn|If the virtual machine has more than one storage device attached, they all must be snapshot at the same time to produce a consistent set of storage snapshots that can be reverted to.}}
{{Warn|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==
==Shutdown the Target Guest==
Line 35: Line 35:
  </disk>
  </disk>


==Snapshot Each Storage Device==
==Preserve the Content of all Storage Devices==


qemu-img snapshot -c <snapshot-tag> <volume-name>
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 more details about snapshot mechanics, see
* For each storage device, [[Qemu-img#Take_a_Snapshot|snapshot the underlying storage volume with qemu-img]].
* For each storage device, [[KVM_Virtualization_Storage_Volume_Removal,_Off-Line_Archival_and_Restoration#Storage_Volume_Removal_and_Content_Archival|remove the corresponding storage volume and archive it off-line]].


[[Qemu-img#Take_a_Snapshot|qemu-img snapshot -c]]
==Save the Guest XML Definition==


==Save the 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:


This is only necessary if we plan to modify the hardware configuration.
[[virsh dumpxml]] ''<vm-name>'' > ./vm-definitions/template.xml


=Restore from Snapshot=
Optionally, if the guest is temporarily removed from the system, the guest definition can be removed:


Perform the steps described in the "[[Linux_Virtualization_Snapshot_a_KVM_Guest_Virtual_Machine#Take_a_Virtual_Machine_Snapshot|Take a Virtual Machine Snapshot]]" section in reverse order:
{{Internal|Linux_KVM_Virtualization_Guest_Operations#Remove_a_Guest|Remove a Guest}}


==Restore the XML Definition==
=Restore from Export Artifacts=


This is only necessary if the hardware configuration changed between snapshots.
Perform the steps described in the "[[#Take_a_Virtual_Machine_Snapshot|Take a Virtual Machine Snapshot]]" section in reverse order:


==Restore the Content of Storage Device from Snapshot==
==Restore the Content of Storage Device from Snapshot==


qemu-img snapshot -a <snapshot-tag> <volume-name>
Depending on how the content of the storage devices was saved, for each storage device:
* [[Qemu-img#Revert_the_Disk_to_a_Saved_State|Revert a disk to a previously saved state with qemu-img]].
* [[KVM_Virtualization_Storage_Volume_Removal,_Off-Line_Archival_and_Restoration#Storage_Volume_Restoration|Restore the content of an externally archived volume]].


For more details about restoration mechanics, see
==Restore the Guest XML Definition==


[[Qemu-img#Revert_the_Disk_to_a_Saved_State|qemu-img snapshot -a]]
This is only necessary if the hardware configuration changed between snapshots.
 
=Alternative=


Alternative suing [[Qemu-img#create|qemu-img create]]. See {{External|http://dustymabe.com/2015/01/11/qemu-img-backing-files-a-poor-mans-snapshotrollback/}}
Define the guest from the previously saved XML definition:
[[virsh define]]

Latest revision as of 01:53, 4 November 2017

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:

https://github.com/NovaOrdis/libvirt-functions/blob/master/export-guest

Another way to snapshot a guest is:

Guest Snapshot Using virsh snapshot Functionality

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:

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:

Remove a Guest

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:

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