Abstract
| Goal |
Create a snapshot, clone, and live migrate a virtual machine, and initiate node maintenance. |
| Objectives |
|
| Sections |
|
| Lab |
|
A snapshot is a view of a Virtual Machine (VM) that includes the operating system and applications on all the attached disks at a given point in time. A developer might take a snapshot of a VM before changing it. The snapshot can protect against errors that might have unintended consequences. In the event of a problem, the developer can revert to the state of the VM that is recorded in the snapshot.
With Red Hat OpenShift Virtualization, you can take live snapshots of running VMs, as well as snapshots of stopped VMs.
When the VM is running, OpenShift Virtualization communicates with the Quick EMUlator (QEMU) guest agent that is running inside the VM to quiesce the file systems before taking the snapshot. This mechanism ensures that the file systems are in a consistent state. If you do not install the QEMU guest agent, then you can still take live snapshots, but the file systems might not be consistent. The default Red Hat templates come with the QEMU guest agent.
Disk snapshots rely on the back-end storage. For example, Red Hat OpenShift Data Foundation (ODF) provides a storage solution that supports snapshots. Other storage technologies might not provide that feature. The Container Storage Interface (CSI) driver that Kubernetes uses to pilot the back-end storage must also support the snapshot feature. The references section provides a link to the list of drivers and their snapshot support.
If one VM disk relies on back-end storage that does not support snapshots, then OpenShift Virtualization displays a warning message when you take the snapshot. When you revert the snapshot, OpenShift Virtualization does not restore the data for that specific disk, which stays unchanged after reverting.
Red Hat supports taking a live snapshot of a VM with hot-plugged disks. However, the snapshot includes only the hot-plugged disks in the VM specification. Hot-plugged disks are disks that you attach to a running VM.
A VM snapshot also records the VM configuration, such as the number of CPUs or the amount of memory. If you change the VM configuration after taking the snapshot, then OpenShift Virtualization restores the original configuration when you revert the snapshot.
Use the OpenShift web console to create a VM snapshot. Navigate to → and then select the VM to snapshot.
If you did not install the QEMU guest agent in the VM, then Red Hat recommends stopping the VM before taking the snapshot. Click → and wait for the VM to stop.
Navigate to the tab and then click to initiate the snapshot. Complete the form and then click . Consider providing a meaningful name and description to help with snapshot identification.
A warning message is displayed if the VM includes disks that do not support snapshots, as shown in the following screen capture.
The nfs1 disk uses the nfs-storage storage class, which does not support snapshots.
![]() |
If you stopped the VM before taking the snapshot, then restart it.
You create a snapshot from the command line by declaring a VirtualMachineSnapshot resource in a file.
You trigger the snapshot from the file by running the oc apply -f command.
The following resource file prepares a snapshot named resourcefile.yamlmariadb-server-2024-3-11 for the mariadb-server VM.
apiVersion: snapshot.kubevirt.io/v1alpha1 kind: VirtualMachineSnapshot metadata: name:mariadb-server-2024-3-11spec: source: apiGroup: kubevirt.io kind: VirtualMachine name:mariadb-server
Before you can restore a snapshot, you must stop the VM.
After you stop the machine, navigate to the tab, and then locate the snapshot to restore. You can click a snapshot name to access the description that you provided at creation time.
To initiate the restoration process from the OpenShift web console, navigate to the tab, and then locate the snapshot to restore. Click the vertical ellipsis icon at the right of the snapshot, and then click . You can restart the VM after the restoration completes.
From the command line, use the oc get virtualmachinesnapshot command to list the available snapshots in your project:
[user@host ~]$ oc get virtualmachinesnapshot
NAME SOURCEKIND SOURCENAME PHASE READYTOUSE ...
db-before-schema VirtualMachine mariadb-server Succeeded true ...
mariadb-server-2024-3-11 VirtualMachine mariadb-server Succeeded true ...To restore a snapshot, create a VirtualMachineRestore resource in a file. and then use the oc apply -f command to initiate the process.
The following resource file restores the resourcefile.yamlmariadb-server-2024-3-11 snapshot for the mariadb-server VM.
apiVersion: snapshot.kubevirt.io/v1alpha1
kind: VirtualMachineRestore
metadata:
name: restore-db
spec:
target:
apiGroup: kubevirt.io
kind: VirtualMachine
name: mariadb-server
virtualMachineSnapshotName: mariadb-server-2024-3-11Because snapshots consume disk space on back-end storage, delete the snapshots that you are no longer using.
To delete a snapshot, navigate to the tab, click the vertical ellipsis icon at the right of the snapshot to delete, and then click . You do not need to stop the VM to delete its snapshots.
From the command line, use the oc delete command.
The following example deletes the mariadb-server-2024-3-11 snapshot:
[user@host ~]$ oc delete virtualmachinesnapshot mariadb-server-2024-3-11
virtualmachinesnapshot.snapshot.kubevirt.io "mariadb-server-2024-3-11" deletedA VM snapshot includes the VM configuration. If you change the VM disk configuration after taking a snapshot, then review the following considerations before restoring the snapshot:
If you removed a disk, then OpenShift Virtualization re-creates the disk. If the initial disk does not support snapshots, then OpenShift Virtualization attaches a new blank disk with the same characteristics as the original disk. With this type of disk, OpenShift Virtualization does not restore the data from the time that you took the snapshot. That data is lost.
If you add a disk, then OpenShift Virtualization removes the disk when you restore the snapshot.
In addition to restoring a complete VM from a snapshot, you can also duplicate individual VM disks from their snapshots.
VM snapshots rely on Kubernetes volume snapshots. When you take a snapshot of a VM, OpenShift Virtualization creates a Kubernetes volume snapshot for each VM disk. For example, if the VM has four disks that use a storage class that supports snapshots, then OpenShift Virtualization creates four Kubernetes volume snapshots, one for each disk.
To list the volume snapshots, use the OpenShift web console, and navigate to → .
You can also run the oc get volumesnapshots command:
[user@host ~]$ oc get volumesnapshots
NAME SOURCEPVC
vmsnapshot-0575...ce32-volume-dbdata ... mariadb-server-dbdata-dv01d
vmsnapshot-0575...ce32-volume-dbredo ... mariadb-server-dbredo-6zt45
vmsnapshot-0575...ce32-volume-dbundo ... mariadb-server-dbundo-etpi5
vmsnapshot-0575...ce32-volume-mariadb-server ... mariadb-serverThe preceding output lists the Kubernetes volume snapshots and their associated PVC. With this information, you can identify the snapshots that correspond to a VM disk.
You can restore a Kubernetes volume snapshot as a new PVC. You can then attach the new PVC as a VM disk and access its content.
Duplicating a VM disk snapshot and attaching it to a VM is useful for inspecting the disk's content or for using the same initial data with several VMs.
To restore a Kubernetes volume snapshot and attach it to a VM, use the following steps:
Navigate to → .
Identify the Kubernetes volume snapshot to restore from the associated source PVC, which corresponds to the VM disk to copy.
Click the vertical ellipsis icon at the right of the snapshot, and then click . Complete the form and then click .
![]() |
Navigate to → , select the VM you want to attach the PVC, and then click → to stop the VM.
Navigate to the tab and then click .
Select Use an existing PVC for the field, and set the field to the name of the restored PVC.
Click to attach the disk.
Click → to start the VM.
For more information about installing the VirtIO driver, refer to the Installing the QEMU Guest Agent and VirtIO Drivers section in the Red Hat OpenShift Container Platform 4.14 documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/virtualization/index#virt-installing-qemu-guest-agent
For more information about managing virtual machine snapshots, refer to the Backup and Restore by Using VM Snapshots section in the Red Hat OpenShift Container Platform 4.14 documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/virtualization/index#backup-and-restore
For more information about volume snapshots, refer to the CSI Volume Snapshots section in the Red Hat OpenShift Container Platform Storage guide at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/storage/index#persistent-storage-csi-snapshots