Bookmark this page

Guided Exercise: Clone a Virtual Machine

Clone an existing VM's PVC into new data volumes, and then use the data volumes to create additional VMs.

Outcomes

  • Clone a VM.

  • Use DataVolume resources to clone VM disks.

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. It also creates the advanced-clone namespace and starts two virtual machines in that namespace: golden-rhel and vm1.

[student@workstation ~]$ lab start advanced-clone

Instructions

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

    1. From a command line, log in to your Red Hat OpenShift cluster as the admin user.

      [student@workstation ~]$ oc login -u admin -p redhatocp \
        https://api.ocp4.example.com:6443
      Login Successful
      ...output omitted...
    2. Identify the URL for the 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. Select htpasswd_provider and log in as the admin user with redhatocp as the password.

    4. Navigate to VirtualizationVirtualMachines and select the advanced-clone project from the Project list. Confirm that the golden-rhel and vm1 VMs are running.

  2. Clone the golden-rhel VM to a new VM named web1.

    1. The cloning process requires that you stop the source machine.

      Select the golden-rhel VM and then click ActionsStop. Wait for the machine to stop.

    2. Click ActionsClone. Set the VM name to web1, select Start virtual machine on clone, and then click Clone.

    3. Navigate to VirtualizationVirtualMachines and confirm that the new web1 VM is running.

  3. List the data volumes that are associated with the web1 VM. Delete the VM and then confirm that Red Hat OpenShift Virtualization also deletes the data volumes and the underlying PVCs.

    1. From the command line on the workstation machine, use the oc command to select the advanced-clone project.

      [student@workstation ~]$ oc project advanced-clone
      Now using project "advanced-clone" on server "https://api.ocp4.example.com:6443".
    2. List the data volumes in the advanced-clone project.

      [student@workstation ~]$ oc get datavolume
      NAME                PHASE       PROGRESS   RESTARTS   AGE
      golden-rhel         Succeeded   100.0%     1          16m
      golden-rhel-9uo0y   Succeeded   100.0%                6m17s
      vm1                 Succeeded   100.0%     1          12m
      webroot             Succeeded   100.0%                16m
      webroot-9uo0y       Succeeded   100.0%                6m17s

      The cloning process appends the name of the source data volumes with the random characters, in the preceding output.

    3. List the PVCs in the project. PVCs have the same name as the associated data volumes.

      [student@workstation ~]$ oc get pvc
      NAME                STATUS   VOLUME            CAPACITY      ACCESS MODES   ...
      golden-rhel         Bound    pvc-ccb7...3667   10Gi          RWX            ...
      golden-rhel-9uo0y   Bound    pvc-0bd3...684f   11362347344   RWX            ...
      vm1                 Bound    pvc-9ae1...459a   10Gi          RWX            ...
      webroot             Bound    pvc-c1de...2358   1Gi           RWX            ...
      webroot-9uo0y       Bound    pvc-8690...95d6   1136234735    RWX            ...
    4. Use the OpenShift web console to delete the web1 VM. Navigate to VirtualizationVirtualMachines, select the web1 VM, and then click ActionsDelete. Select Delete Disks (2x) and then click Delete.

    5. From the command line, use the oc command to list the data volume and the PVC resources. Notice that the data volumes and PVC resources for the web1 VM no longer exist.

      [student@workstation ~]$ oc get datavolume
      NAME          PHASE       PROGRESS   RESTARTS   AGE
      golden-rhel   Succeeded   100.0%     1          22m
      vm1           Succeeded   100.0%     1          18m
      webroot       Succeeded   100.0%                22m
      [student@workstation ~]$ oc get pvc
      NAME          STATUS   VOLUME            CAPACITY   ACCESS MODES ...
      golden-rhel   Bound    pvc-ccb7...3667   10Gi       RWX          ...
      vm1           Bound    pvc-9ae1...459a   10Gi       RWX          ...
      webroot       Bound    pvc-c1de...2358   1Gi        RWX          ...
  4. Clone the webroot PVC. The golden-rhel VM uses that PVC as an additional disk to store contents for the web server that is running inside the VM.

    1. From the command line, edit the /home/student/DO316/labs/advanced-clone/dv.yaml file that the lab command prepares. The file defines a DataVolume resource named documentroot-clone1. In the .spec.source.pvc section, update the name parameter to specify the source PVC.

      apiVersion: cdi.kubevirt.io/v1beta1
      kind: DataVolume
      metadata:
        name: documentroot-clone1
        namespace: advanced-clone
      spec:
        storage:
          resources:
            requests:
              storage: 1Gi
          storageClassName: ocs-external-storagecluster-ceph-rbd-virtualization
        source:
          pvc:
            name: webroot
            namespace: advanced-clone
    2. Use the oc apply command to create the resource.

      [student@workstation ~]$ oc apply -f ~/DO316/labs/advanced-clone/dv.yaml
      datavolume.cdi.kubevirt.io/documentroot-clone1 created
    3. Use the oc command to confirm that OpenShift Virtualization creates the data volume and the PVC resources.

      [student@workstation ~]$ oc get datavolume
      NAME                  PHASE       PROGRESS   RESTARTS   AGE
      documentroot-clone1   Succeeded                         30s
      ...output omitted...
      [student@workstation ~]$ oc get pvc
      NAME                  STATUS   VOLUME            CAPACITY   ACCESS MODES ...
      documentroot-clone1   Bound    pvc-7bc3...f58d   1Gi        RWX          ...
      ...output omitted...
  5. Attach the documentroot-clone1 PVC to the vm1 VM.

    1. From the OpenShift web console, navigate to VirtualizationVirtualMachines. Select the vm1 VM, and then click ActionsStop. Wait for the machine to stop.

    2. Navigate to ConfigurationDisks and then click Add disk. Complete the form by using the following information:

      Note

      If the Interface field is set to scsi and virtio is not available, then click Cancel and start over. The web interface takes a few seconds to detect that the VM is stopped.

      FieldValue
      Name docroot
      Source Use an existing PVC
      PVC project advanced-clone
      PVC name documentroot-clone1
      Type Disk
      Interface VirtIO

      Click Save to attach the disk to the VM.

    3. Click ActionsStart to start the VM.

    4. Log in to the VM console and confirm that a new 1 GiB device is available. Navigate to the Console tab and then log in as the root user with redhat as the password. Run the lsblk command to list the block devices. Notice the 1 GiB vdb block device.

      [root@vm1 ~]# lsblk
      NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      vda    252:0    0   10G  0 disk
      ├─vda1 252:1    0    1M  0 part
      ├─vda2 252:2    0  100M  0 part /boot/efi
      └─vda3 252:3    0  9.9G  0 part /
      vdb    252:16   0    1G  0 disk
    5. Log out of the VM console.

      [root@vm1 ~]# logout
  6. Delete the vm1 VM to verify that OpenShift Virtualization does not delete DataVolume resources that you create independently of a VM. Confirm that the documentroot-clone1 data volume and the underlying PVC still exist.

    1. Click ActionsDelete. Select Delete Disks (2x) and then click Delete.

    2. From the command line, use the oc command to list the data volume and PVC resources. Notice that the documentroot-clone1 data volume and the PVC still exist.

      [student@workstation ~]$ oc get datavolume
      NAME                  PHASE       PROGRESS   RESTARTS   AGE
      documentroot-clone1   Succeeded                         59s
      ...output omitted...
      [student@workstation ~]$ oc get pvc
      NAME                  STATUS   VOLUME            CAPACITY   ACCESS MODES ...
      documentroot-clone1   Bound    pvc-7bc3...f58d   1Gi        RWX          ...
      ...output omitted...
    3. Delete the documentroot-clone1 data volume and then confirm that OpenShift Virtualization automatically deletes the underlying PVC.

      [student@workstation ~]$ oc delete datavolume documentroot-clone1
      datavolume.cdi.kubevirt.io "documentroot-clone1" deleted
      [student@workstation ~]$ oc get pvc
      NAME                     STATUS   VOLUME            CAPACITY   ACCESS MODES ...
      golden-rhel              Bound    pvc-f874...367d   10Gi       RWX          ...
      webroot                  Bound    pvc-2588...9f7d   1Gi        RWX          ...

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-clone

Revision: do316-4.14-d8a6b80