Bookmark this page

Guided Exercise: Manage Virtual Machine Snapshots

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

  1. As the admin user, locate and then navigate to the Red Hat OpenShift web console. Confirm that the VM is running.

    1. 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:6443
      Login Successful
      ...output omitted...
    2. Identify the URL for the Red Hat OpenShift web console.

      [student@workstation ~]$ oc whoami --show-console
      https://console-openshift-console.apps.ocp4.example.com
    3. Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com. Click htpasswd_provider and log in as the admin user with redhatocp as the password.

    4. Navigate to VirtualizationVirtualMachines. Confirm that the vm1 VM is running.

  2. 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.

    1. Select the vm1 VM. On the Diagnostics tab, in the Status condition section, notice that the status of the AgentConnected type is True.

      That status means that the agent is installed and running inside the VM.

    2. Navigate to the Overview tab. In the Details section, locate the CPU|Memory subsection.

      Notice that the machine is assigned 1 CPU and 2 GiB of memory.

    3. Navigate to the Configuration tab. In the Disks 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.

    4. Because the QEMU guest agent is running, you can safely take a live snapshot of the VM.

      To take the snapshot, navigate to the Snapshots tab, and then click Take snapshot. Enter rhel-vm1-snap1 in the Name field. Notice the warning message about the cloudinitdisk volume, which Red Hat OpenShift Virtualization cannot include in the snapshot.

      Figure 7.3: Taking a snapshot of a VM

      Click Save. Wait for the snapshot to report the status as Succeeded.

  3. Configure the vm1 VM with 2 CPUs and 4 GiB of memory. After reverting the snapshot, notice that OpenShift Virtualization restores the initial configuration.

    1. Click ActionsStop. Wait for the machine to stop.

      Note

      The OpenShift web console displays a message that the VM is stopping and provides a Force stop button to force stop an unresponsive VM.

    2. Navigate to the Details tab. In the VirtualMachine details section, locate the CPU|Memory subsection and then click 1 CPU | 2 GiB Memory.

    3. Set the CPU cores field to 2, and set the Memory field to 4 GiB. Click Save.

      Note

      You might not see the saved changes. When you start the VM, the CPU|Memory displays updated values.

    4. Click ActionsStart. Wait for the machine to start.

    5. Navigate to the Overview tab. In the Details section, locate the CPU|Memory subsection. Notice that the machine is assigned 2 CPUs and 4 GiB of memory.

  4. 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.

    1. Navigate to the Console tab and then log in as the root user with redhat as the password.

    2. Verify that the /etc/motd file is empty.

      [root@vm1 ~]# cat /etc/motd
      [root@vm1 ~]#
    3. Add some content to the file.

      [root@vm1 ~]# echo OpenShift Virtualization VM >> /etc/motd
      [root@vm1 ~]# cat /etc/motd
      OpenShift Virtualization VM
    4. 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
    5. Log out of the VM console.

      [root@vm1 ~]# logout
  5. Restore the VM snapshot and then confirm that OpenShift Virtualization reverts the VM to its initial state.

    1. Click ActionsStop. Wait for the machine to stop.

    2. Navigate to the Snapshots tab and then locate the rhel-vm1-snap1 snapshot. Click the vertical ellipsis icon at the right of the rhel-vm1-snap1 snapshot and then click Restore. Click Restore to confirm the operation.

    3. Click ActionsStart to start the VM. Wait for the machine to start.

    4. Navigate to the Overview tab. In the Details section, locate the CPU|Memory subsection. Notice that the machine is assigned 1 CPU and 2 GiB of memory.

    5. Navigate to the Console tab and then log in as the root user with redhat as the password.

    6. 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 ~]#
    7. List the content of the /root directory. The test.txt file no longer exists.

      [root@vm1 ~]# ls /root
      [root@vm1 ~]#
    8. List the contents of the /etc/httpd/conf directory. The magic file is restored.

      [root@vm1 ~]# ls /etc/httpd/conf
      httpd.conf  magic
    9. Log out of the VM console.

      [root@vm1 ~]# logout
  6. Use a volume snapshot to copy the vm1 disk and then use that copy as a root disk for a new VM.

    Note

    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.

    1. Navigate to StorageVolumeSnapshots. Click the vertical ellipsis icon at the right of the snapshot where the source is the vm1 PersistentVolumeClaim (PVC), and then click Restore as new PVC. Complete the form by using the following information:

      FieldValue
      Name root-copy
      StorageClass ocs-external-storagecluster-ceph-rbd
      Access mode Shared access (RWX)

      Click Restore when done.

    2. 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-copy
    3. Use 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 created

      If using the OpenShift web console, navigate to VirtualizationVirtualMachines, and then click CreateWith YAML. 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 Create.

  7. Verify that the vm2 VM is using the copy of the vm1 disk as a root disk.

    1. Navigate to VirtualizationVirtualMachines and then select the vm2 VM. Navigate to the Console tab and then log in as the root user with redhat as the password.

    2. 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 ~]# hostname
      vm1
    3. Log out of the VM console.

      [root@vm1 ~]# logout
  8. Close the web browser.

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish advanced-snapshot

Revision: do316-4.14-d8a6b80