Snapshots let you capture the current state of a resource for later recovery or cloning. A disk snapshot saves only the data of the selected disk, while a virtual machine (VM) snapshot includes the VM parameters and the state of all its disks.

Consistent snapshots

Snapshots can be consistent or inconsistent. The requiredConsistency parameter is responsible for this, and its default value is true, which means that a consistent snapshot is required.

A consistent snapshot captures a coherent and integral state of the disk data. You can create such a snapshot when one of the following conditions is met:

  • the disk isn’t attached to any virtual machine, and then the snapshot is always consistent;
  • the virtual machine is powered off;
  • qemu-guest-agent is installed and running in the guest OS. When the snapshot is created, it temporarily pauses (“freezes”) the file system to keep the data coherent.

An inconsistent snapshot may not reflect a coherent state of the virtual machine disks and its components. Such a snapshot is created if the VM is running and qemu-guest-agent isn’t installed or isn’t running in the guest OS. If the snapshot manifest explicitly specifies requiredConsistency: false but qemu-guest-agent is running, an attempt to freeze the file system is still made so that the snapshot comes out consistent.

QEMU Guest Agent supports hook scripts that prepare applications for a snapshot without stopping services, keeping the state coherent at the application level. Configuring hook scripts is described in Guest OS agent.

When recovering from such a snapshot, file system integrity problems are possible, because the data state may be incoherent.

Creating disk snapshots

A disk snapshot saves the disk data at the moment of creation and serves as a source for new disks, for example to clone or recover information. A snapshot is described by the VirtualDiskSnapshot resource.

Data integrity is guaranteed when a snapshot is created in one of the following cases:

  • The disk isn’t attached to any virtual machine.
  • The VM is powered off.
  • The VM is running, qemu-guest-agent is installed in the guest OS, and the file system was successfully frozen (the fsfreeze operation).

If data consistency isn’t required (for example, for test scenarios), set the .spec.requiredConsistency parameter of the snapshot to false. The snapshot is then created on a running VM without freezing the file system, including when the disk is attached to it.

A disk snapshot goes through the following phases:

  • Pending: Waiting for all dependent resources required to create the snapshot to become ready.
  • InProgress: The virtual disk snapshot is being created.
  • Ready: The snapshot was created successfully and the virtual disk snapshot is available for use.
  • Failed: An error occurred while creating the virtual disk snapshot.
  • Terminating: The resource is being deleted.

The .status.conditions block shows the reason for a problem with the resource.

  • Using the CLI
  • Using the web interface

Here is an example manifest for creating a disk snapshot:

d8 k apply -f - <<EOF
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualDiskSnapshot
metadata:
  name: linux-vm-root-snapshot
spec:
  requiredConsistency: true
  virtualDiskName: linux-vm-root
EOF

To view the list of disk snapshots, run the following command:

d8 k get vdsnapshot

Example output:

NAME                   PHASE     CONSISTENT   AGE
linux-vm-root-snapshot Ready     true         3m2s

The true value in the CONSISTENT column means that the snapshot is consistent. It’s determined automatically from the snapshot creation conditions and isn’t changed manually.

  1. Go to the Projects tab and select the project you need.
  2. Go to VirtualizationDisk snapshots.
  3. Click Create.
  4. In the Create resource window that opens, enter the snapshot name in the Name field.
  5. On the Configuration tab, select the disk to take the snapshot from in the Virtual disk name field.
  6. Enable the Required consistency toggle.
  7. Click Apply.
  8. The snapshot status is shown in the Status column.

Recovering disks from snapshots

A new disk is created from a snapshot, and the original disk stays untouched.

  • Using the CLI
  • Using the web interface

To recover a disk from a previously created disk snapshot, specify the corresponding object as the dataSource:

d8 k apply -f - <<EOF
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualDisk
metadata:
  name: linux-vm-root
spec:
  # Disk storage parameters.
  persistentVolumeClaim:
    # Specify a size no smaller than the size of the source disk.
    size: 10Gi
    # Specify the name of your StorageClass.
    storageClassName: rv-thin-r2
  # The source the disk is created from.
  dataSource:
    type: ObjectRef
    objectRef:
      kind: VirtualDiskSnapshot
      name: linux-vm-root-snapshot
EOF
  1. Go to the Projects tab and select the project you need.
  2. Go to VirtualizationDisks.
  3. Click Create.
  4. In the form that opens, enter the disk name in the Disk name field.
  5. In the Source field, select the disk snapshot you want to recover from in the drop-down list.
  6. In the Size field, set a size equal to or larger than the size of the original disk.
  7. In the Storage class field, select the StorageClass of the original disk.
  8. Click Create.
  9. The disk status is shown on its page.

Creating VM snapshots

A virtual machine snapshot is the saved state of a virtual machine at a certain point in time. To create virtual machine snapshots, use the VirtualMachineSnapshot resource.

Detach all images (VirtualImage/ClusterVirtualImage) from a virtual machine before taking its snapshot. Disk images aren’t saved along with the VM snapshot, and their absence in the cluster during recovery can leave the virtual machine unable to start, in the Pending state, waiting for the image to become available.

Creating a virtual machine snapshot fails if at least one of the following conditions is met:

  • not all dependent devices of the virtual machine are ready;
  • one of the dependent devices is a disk that is being resized.

If the virtual machine has changes pending a restart at the moment the snapshot is taken, the updated configuration goes into the snapshot.

When a snapshot is created, the dynamic IP address of the VM is automatically converted to a static one and saved for recovery. If you don’t need the conversion and the reuse of the old address, set the .spec.keepIPAddress parameter to Never, and the address type stays as it is (Auto or Static).

The following example shows how to create a virtual machine snapshot:

  • Using the CLI
  • Using the web interface

Here is an example manifest for creating a virtual machine snapshot:

d8 k apply -f - <<EOF
apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualMachineSnapshot
metadata:
  name: linux-vm-snapshot
spec:
  virtualMachineName: linux-vm
  requiredConsistency: true
  keepIPAddress: Never
EOF

After the snapshot is created successfully, its status reflects the list of saved resources.

Example output:

status:
  ...
  resources:
  - apiVersion: virtualization.deckhouse.io/v1alpha2
    kind: VirtualMachine
    name: linux-vm
  - apiVersion: v1
    kind: Secret
    name: cloud-init
  - apiVersion: virtualization.deckhouse.io/v1alpha2
    kind: VirtualDisk
    name: linux-vm-root
  1. Go to the Projects tab and select the project you need.
  2. Go to VirtualizationVirtual machines.
  3. Select the VM you need from the list and click its name.
  4. Go to the Snapshots tab.
  5. Click Add.
  6. In the form that opens, enter linux-vm-snapshot in the Snapshot name field.
  7. Enable the Integrity guarantee toggle.
  8. Click Create.
  9. The snapshot status is shown on its page.
  10. The created snapshots are listed on the Snapshots tab of the virtual machine, with the Name, Status, Creation date, and Consistent columns.

Recovering a VM

Recovery returns a machine and its disks to the state saved in a snapshot. It’s started by a VirtualMachineOperation resource of the Restore type, which has three modes:

  • DryRun: A dry run of the recovery operation, needed to check for possible conflicts, which are shown in the resource status (status.resources).
  • Strict: The strict recovery mode, when the VM has to be recovered exactly as in the snapshot; missing external dependencies can leave the VM in Pending after recovery.
  • BestEffort: Missing external dependencies (ClusterVirtualImage, VirtualImage) are ignored and removed from the VM configuration.

Recovering a virtual machine from a snapshot is possible only when all of the following conditions are met:

  • The VM being recovered is present in the cluster (the VirtualMachine resource exists and its .metadata.uid matches the identifier used when the snapshot was created).
  • The disks being recovered (identified by name) either aren’t attached to other VMs or are absent from the cluster.
  • The IP address being recovered either isn’t taken by another VM or is absent from the cluster.
  • The MAC addresses being recovered either aren’t used by other VMs or are absent from the cluster.

The disks of the machine are recovered along with it, so the disk specification contains the dataSource parameter with a reference to the disk snapshot needed.

If some resources the VM depends on (for example, VirtualMachineClass, VirtualImage, ClusterVirtualImage) are absent from the cluster but existed at the moment the snapshot was created, the VM stays in the Pending state after recovery. In that case, edit the VM configuration manually and update or remove the missing dependencies.

Don’t cancel a recovery operation from a snapshot, that is, don’t delete the VirtualMachineOperation resource in the InProgress phase, because this can leave the virtual machine being recovered in an inconsistent state.

  • Using the CLI
  • Using the web interface

Recovery is started by a VirtualMachineOperation resource of the Restore type:

apiVersion: virtualization.deckhouse.io/v1alpha2
kind: VirtualMachineOperation
metadata:
  name: <VMOP_NAME>
spec:
  type: Restore
  virtualMachineName: <VM_NAME>
  restore:
    mode: DryRun | Strict | BestEffort
    virtualMachineSnapshotName: <VM_SNAPSHOT_NAME>

To view information about conflicts when recovering a VM from a snapshot, check the resource status:

d8 k get vmop <VMOP_NAME> -o json | jq '.status.resources'
  1. Go to the Projects tab and select the project you need.
  2. Go to VirtualizationVirtual machines.
  3. Select the virtual machine you need from the list and click the ellipsis button.
  4. In the menu that opens, select Restore.
  5. In the Machine recovery window that opens, select the snapshot in the Virtual machine snapshot name field.
  6. In the Recovery mode field, select Strict or BestEffort.
  7. Click Restore.

Additional resources