Cloning a virtual machine by using a data volume template

You can create a new virtual machine by cloning the persistent volume claim (PVC) of an existing VM. By including a dataVolumeTemplate in your virtual machine configuration file, you create a new data volume from the original PVC.

Cloning operations between different volume modes are supported, such as cloning from a persistent volume (PV) with volumeMode: Block to a PV with volumeMode: Filesystem.

However, you can only clone between different volume modes if they are of the contentType: kubevirt.

When you enable preallocation globally, or for a single data volume, the Containerized Data Importer (CDI) preallocates disk space during cloning. Preallocation enhances write performance. For more information, see Using preallocation for data volumes.

Prerequisites

About data volumes

DataVolume objects are custom resources that are provided by the Containerized Data Importer (CDI) project. Data volumes orchestrate import, clone, and upload operations that are associated with an underlying persistent volume claim (PVC). Data volumes are integrated with OKD Virtualization, and they prevent a virtual machine from being started before the PVC has been prepared.

Creating a new virtual machine from a cloned persistent volume claim by using a data volume template

You can create a virtual machine that clones the persistent volume claim (PVC) of an existing virtual machine into a data volume. Reference a dataVolumeTemplate in the virtual machine manifest and the source PVC is cloned to a data volume, which is then automatically used for the creation of the virtual machine.

When a data volume is created as part of the data volume template of a virtual machine, the lifecycle of the data volume is then dependent on the virtual machine. If the virtual machine is deleted, the data volume and associated PVC are also deleted.

Prerequisites

  • Determine the PVC of an existing virtual machine disk to use. You must power down the virtual machine that is associated with the PVC before you can clone it.

  • Install the OpenShift CLI (oc).

Procedure

  1. Examine the virtual machine you want to clone to identify the name and namespace of the associated PVC.

  2. Create a YAML file for a VirtualMachine object. The following virtual machine example clones my-favorite-vm-disk, which is located in the source-namespace namespace. The 2Gi data volume called favorite-clone is created from my-favorite-vm-disk.

    For 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. pvc:
    32. accessModes:
    33. - ReadWriteOnce
    34. resources:
    35. requests:
    36. storage: 2Gi
    37. source:
    38. pvc:
    39. namespace: "source-namespace"
    40. name: "my-favorite-vm-disk"
    1The virtual machine to create.
  3. Create the virtual machine with the PVC-cloned data volume:

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

Template: Data volume virtual machine configuration file

example-dv-vm.yaml

  1. apiVersion: kubevirt.io/v1
  2. kind: VirtualMachine
  3. metadata:
  4. labels:
  5. kubevirt.io/vm: example-vm
  6. name: example-vm
  7. spec:
  8. dataVolumeTemplates:
  9. - metadata:
  10. name: example-dv
  11. spec:
  12. pvc:
  13. accessModes:
  14. - ReadWriteOnce
  15. resources:
  16. requests:
  17. storage: 1G
  18. source:
  19. http:
  20. url: "" (1)
  21. running: false
  22. template:
  23. metadata:
  24. labels:
  25. kubevirt.io/vm: example-vm
  26. spec:
  27. domain:
  28. cpu:
  29. cores: 1
  30. devices:
  31. disks:
  32. - disk:
  33. bus: virtio
  34. name: example-dv-disk
  35. machine:
  36. type: q35
  37. resources:
  38. requests:
  39. memory: 1G
  40. terminationGracePeriodSeconds: 180
  41. volumes:
  42. - dataVolume:
  43. name: example-dv
  44. name: example-dv-disk
1The HTTP source of the image you want to import, if applicable.

CDI supported operations matrix

This matrix shows the supported CDI operations for content types against endpoints, and which of these operations requires scratch space.

Content typesHTTPHTTPSHTTP basic authRegistryUpload

KubeVirt (QCOW2)

✓ QCOW2
✓ GZ
✓ XZ

✓ QCOW2*
✓ GZ

✓ XZ

✓ QCOW2
✓ GZ
✓ XZ

✓ QCOW2
□ GZ
□ XZ

✓ QCOW2
✓ GZ

✓ XZ

KubeVirt (RAW)

✓ RAW
✓ GZ
✓ XZ

✓ RAW
✓ GZ
✓ XZ

✓ RAW
✓ GZ
✓ XZ

✓ RAW
□ GZ
□ XZ

✓ RAW
✓ GZ

✓ XZ*

✓ Supported operation

□ Unsupported operation

* Requires scratch space

** Requires scratch space if a custom certificate authority is required