Create a VM snapshot to restore a VM to a previous state. You then use the snapshot to create a VM.
Outcomes
Take a VM snapshot.
Revert a snapshot.
Use volume snapshots as root file systems for new VMs.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster API is reachable.
The command also creates the advanced-snapshot namespace and starts the vm1 virtual machine in that namespace.
[student@workstation ~]$ lab start advanced-snapshot
Instructions
As the admin user, locate and then navigate to the Red Hat OpenShift web console.
Confirm that the VM is running.
Open a terminal window and log in to the OpenShift cluster as the admin user with redhatocp as the password.
[student@workstation ~]$oc login -u admin -p redhatocp \https://api.ocp4.example.com:6443Login Successful ...output omitted...
Identify the URL for the Red Hat OpenShift web console.
[student@workstation ~]$ oc whoami --show-console
https://console-openshift-console.apps.ocp4.example.comOpen a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com.
Click and log in as the admin user with redhatocp as the password.
Navigate to → .
Confirm that the vm1 VM is running.
Confirm that the vm1 VM runs the Quick EMUlator (QEMU) guest agent, review the machine configuration, and then take a live snapshot of the VM.
Select the vm1 VM.
On the tab, in the section, notice that the status of the type is .
That status means that the agent is installed and running inside the VM.
Navigate to the tab. In the section, locate the subsection.
Notice that the machine is assigned 1 CPU and 2 GiB of memory.
Navigate to the tab.
In the section, the VM has two disks.
The cloudinitdisk volume is a special read-only disk that the VM uses to configure the system initially.
The vm1 disk contains the root file system for the VM.
Because the QEMU guest agent is running, you can safely take a live snapshot of the VM.
To take the snapshot, navigate to the tab, and then click .
Enter rhel-vm1-snap1 in the field.
Notice the warning message about the cloudinitdisk volume, which Red Hat OpenShift Virtualization cannot include in the snapshot.
![]() |
Click .
Wait for the snapshot to report the status as Succeeded.
Configure the vm1 VM with 2 CPUs and 4 GiB of memory.
After reverting the snapshot, notice that OpenShift Virtualization restores the initial configuration.
Click → . Wait for the machine to stop.
The OpenShift web console displays a message that the VM is stopping and provides a button to force stop an unresponsive VM.
Navigate to the tab. In the section, locate the subsection and then click .
Set the field to 2, and set the field to 4 GiB. Click .
You might not see the saved changes. When you start the VM, the displays updated values.
Click → . Wait for the machine to start.
Navigate to the tab. In the section, locate the subsection. Notice that the machine is assigned 2 CPUs and 4 GiB of memory.
Use the VM console to modify the /etc/motd file, to create the /root/test.txt file and to delete the /etc/httpd/conf/magic file.
These file system updates verify that OpenShift Virtualization restores the initial state of the VM when you revert the snapshot in a later step.
Navigate to the tab and then log in as the root user with redhat as the password.
Verify that the /etc/motd file is empty.
[root@vm1 ~]# cat /etc/motd
[root@vm1 ~]#Add some content to the file.
[root@vm1 ~]#echo OpenShift Virtualization VM >> /etc/motd[root@vm1 ~]#cat /etc/motdOpenShift Virtualization VM
Create the /root/test.txt file and then delete the /etc/httpd/conf/magic file.
[root@vm1 ~]#echo Hello World > /root/test.txt[root@vm1 ~]#rm -f /etc/httpd/conf/magic
Log out of the VM console.
[root@vm1 ~]# logoutRestore the VM snapshot and then confirm that OpenShift Virtualization reverts the VM to its initial state.
Click → . Wait for the machine to stop.
Navigate to the tab and then locate the snapshot. Click the vertical ellipsis icon at the right of the snapshot and then click . Click to confirm the operation.
Click → to start the VM. Wait for the machine to start.
Navigate to the tab. In the section, locate the subsection. Notice that the machine is assigned 1 CPU and 2 GiB of memory.
Navigate to the tab and then log in as the root user with redhat as the password.
Review the content of the /etc/motd file.
The file is empty and no longer contains your content.
The state of the file is from when you took the snapshot.
[root@vm1 ~]# cat /etc/motd
[root@vm1 ~]#List the content of the /root directory.
The test.txt file no longer exists.
[root@vm1 ~]# ls /root
[root@vm1 ~]#List the contents of the /etc/httpd/conf directory.
The magic file is restored.
[root@vm1 ~]#ls /etc/httpd/confhttpd.confmagic
Log out of the VM console.
[root@vm1 ~]# logoutUse a volume snapshot to copy the vm1 disk and then use that copy as a root disk for a new VM.
The following instructions demonstrate how to use a snapshot to make a copy of a volume.
Instead of using that copy to create a VM, it is better to use the clone feature in OpenShift Virtualization to duplicate a VM. VM cloning is explained in more detail in the next section.
Navigate to → .
Click the vertical ellipsis icon at the right of the snapshot where the source is the vm1 PersistentVolumeClaim (PVC), and then click .
Complete the form by using the following information:
| Field | Value |
|---|---|
root-copy
| |
ocs-external-storagecluster-ceph-rbd
| |
Shared access (RWX)
|
Click when done.
The lab command prepared the /home/student/DO316/labs/advanced-snapshot/vm2.yaml file, which declares a VirtualMachine resource.
From the terminal on the workstation machine, edit the file to specify the PVC to use as the root disk:
...output omitted...
volumes:
- name: containerdisk
persistentVolumeClaim:
claimName: root-copyUse the oc create command to create the resource.
[student@workstation ~]$ oc create -f ~/DO316/labs/advanced-snapshot/vm2.yaml \
-n advanced-snapshot
virtualmachine.kubevirt.io/vm2 createdIf using the OpenShift web console, navigate to → , and then click → .
Copy and paste the /home/student/DO316/labs/advanced-snapshot/vm2.yaml file contents into the editor.
Set the claimName parameter to root-copy and then click .
Verify that the vm2 VM is using the copy of the vm1 disk as a root disk.
Navigate to → and then select the vm2 VM.
Navigate to the tab and then log in as the root user with redhat as the password.
Run the hostname command and notice that the machine name is vm1.
The machine has the same system configuration as the vm1 VM, because it uses an exact copy of its root disk.
[root@vm1 ~]#hostnamevm1
Log out of the VM console.
[root@vm1 ~]# logoutClose the web browser.