Creating VMs by cloning PVCs

You can create virtual machines (VMs) by cloning existing persistent volume claims (PVCs) with custom images.

You clone a PVC by creating a data volume that references a source PVC.

You must install the QEMU guest agent on VMs created from operating system images that are not provided by Red Hat.

Creating a VM from a PVC by using the web console

You can create a virtual machine (VM) by cloning a persistent volume claim (PVC) by using the OKD web console.

Prerequisites

  • You must have access to the namespace that contains the source PVC.

Procedure

  1. Navigate to VirtualizationCatalog in the web console.

  2. Click a template tile without an available boot source.

  3. Click Customize VirtualMachine.

  4. On the Customize template parameters page, expand Storage and select PVC (clone PVC) from the Disk source list.

  5. Select the PVC project and the PVC name.

  6. Set the disk size.

  7. Click Customize VirtualMachine.

  8. Click Create VirtualMachine.

Creating a VM from a PVC by using the command line

You can create a virtual machine (VM) by cloning the persistent volume claim (PVC) of an existing VM by using the command line.

You can clone a PVC by using one of the following options:

  • Cloning a PVC to a new data volume.

    This method creates a data volume whose lifecycle is independent of the original VM. Deleting the original VM does not affect the new data volume or its associated PVC.

  • Cloning a PVC by creating a VirtualMachine manifest with a dataVolumeTemplates stanza.

    This method creates a data volume whose lifecycle is dependent on the original VM. Deleting the original VM deletes the cloned data volume and its associated PVC.

Cloning a PVC to a data volume

You can clone the persistent volume claim (PVC) of an existing virtual machine (VM) disk to a data volume by using the command line.

You create a data volume that references the original source PVC. The lifecycle of the new data volume is independent of the original VM. Deleting the original VM does not affect the new data volume or its associated PVC.

Cloning between different volume modes is supported for host-assisted cloning, such as cloning from a block persistent volume (PV) to a file system PV, as long as the source and target PVs belong to the kubevirt content type.

Smart-cloning is faster and more efficient than host-assisted cloning because it uses snapshots to clone PVCs. Smart-cloning is supported by storage providers that support snapshots, such as Red Hat OpenShift Data Foundation.

Cloning between different volume modes is not supported for smart-cloning.

Prerequisites

  • The VM with the source PVC must be powered down.

  • If you clone a PVC to a different namespace, you must have permissions to create resources in the target namespace.

  • Additional prerequisites for smart-cloning:

    • Your storage provider must support snapshots.

    • The source and target PVCs must have the same storage provider and volume mode.

    • The value of the driver key of the VolumeSnapshotClass object must match the value of the provisioner key of the StorageClass object as shown in the following example:

      Example VolumeSnapshotClass object

      1. kind: VolumeSnapshotClass
      2. apiVersion: snapshot.storage.k8s.io/v1
      3. driver: openshift-storage.rbd.csi.ceph.com
      4. # ...

      Example StorageClass object

      1. kind: StorageClass
      2. apiVersion: storage.k8s.io/v1
      3. # ...
      4. provisioner: openshift-storage.rbd.csi.ceph.com

Procedure

  1. Create a DataVolume manifest as shown in the following example:

    1. apiVersion: cdi.kubevirt.io/v1beta1
    2. kind: DataVolume
    3. metadata:
    4. name: <datavolume> (1)
    5. spec:
    6. source:
    7. pvc:
    8. namespace: "<source_namespace>" (2)
    9. name: "<my_vm_disk>" (3)
    10. storage: {}
    1Specify the name of the new data volume.
    2Specify the namespace of the source PVC.
    3Specify the name of the source PVC.
  2. Create the data volume by running the following command:

    1. $ oc create -f <datavolume>.yaml

    Data volumes prevent a VM from starting before the PVC is prepared. You can create a VM that references the new data volume while the PVC is being cloned.

Creating a VM from a cloned PVC by using a data volume template

You can create a virtual machine (VM) that clones the persistent volume claim (PVC) of an existing VM by using a data volume template.

This method creates a data volume whose lifecycle is dependent on the original VM. Deleting the original VM deletes the cloned data volume and its associated PVC.

Prerequisites

  • The VM with the source PVC must be powered down.

Procedure

  1. Create a VirtualMachine manifest as shown in the following example:

    1. apiVersion: kubevirt.io/v1
    2. kind: VirtualMachine
    3. metadata:
    4. labels:
    5. kubevirt.io/vm: vm-dv-clone
    6. name: vm-dv-clone (1)
    7. spec:
    8. running: false
    9. template:
    10. metadata:
    11. labels:
    12. kubevirt.io/vm: vm-dv-clone
    13. spec:
    14. domain:
    15. devices:
    16. disks:
    17. - disk:
    18. bus: virtio
    19. name: root-disk
    20. resources:
    21. requests:
    22. memory: 64M
    23. volumes:
    24. - dataVolume:
    25. name: favorite-clone
    26. name: root-disk
    27. dataVolumeTemplates:
    28. - metadata:
    29. name: favorite-clone
    30. spec:
    31. storage:
    32. accessModes:
    33. - ReadWriteOnce
    34. resources:
    35. requests:
    36. storage: 2Gi
    37. source:
    38. pvc:
    39. namespace: <source_namespace> (2)
    40. name: "<source_pvc>" (3)
    1Specify the name of the VM.
    2Specify the namespace of the source PVC.
    3Specify the name of the source PVC.
  2. Create the virtual machine with the PVC-cloned data volume:

    1. $ oc create -f <vm-clone-datavolumetemplate>.yaml