2.2. Create a DataVolume

Create a DataVolume to import data into a PVC

Provisioning a Virtual Machine Disk

In the following lab, we will provision a disk from a given url. We will use a DataVolume to create the Disk. We then create a VM with the disk attached.

We want to provision a VM running an Alpine Cloud Image1.

Task 2.2.1: Write your own DataVolume manifest

Create a file dv_lab02-alpinedisk.yaml in folder labs/lab02and start with the content from the previous section:

apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: lab02-blankdv
spec:
  source:
    blank: {}
  contentType: "kubevirt"
  storage:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 64Mi

Now adapt your DataVolume to fulfill the following specifications:

  1. Download a Disk Image from a given URL
    • Use https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-cloudinit-r0.qcow2 as URL for the disk image
  2. Create a KubeVirt VM disk
  3. Requested storage should be 256Mi
  4. Name the volume lab02-alpinedisk
  5. Set the accessMode to ReadWriteOnce
Task hint

Your manifest should look like this:

apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: lab02-alpinedisk
spec:
  source:
    http:
      url: "https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-cloudinit-r0.qcow2"
  contentType: "kubevirt"
  storage:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 256Mi

Before you apply your DataVolume to the cluster, check the currently available PVCs:

kubectl get pvc --namespace lab-<username>

The output should be similar to:

NAME                    STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
<username>-webshell          Bound    pvc-594cf281-4c34-4e7c-b345-2bf2692bbb78   1Gi        RWO            longhorn       <unset>                 1d
<username>-webshell-docker   Bound    pvc-86e4bc75-396f-4630-940e-b0a4b0cf23fa   10Gi       RWO            longhorn       <unset>                 1d

Now create the DataVolume on the Kubernetes cluster with:

kubectl apply -f labs/lab02/dv_lab02-alpinedisk.yaml --namespace lab-<username>

The output should be:

datavolume.cdi.kubevirt.io/lab02-alpinedisk created

This will trigger the CDI Operator which will start an importer pod to provision your PVC. If you are fast enough you may see the pod with:

kubectl get pods --namespace lab-<username>

The output should be similar to:

NAME                                                  READY   STATUS              RESTARTS   AGE
importer-prime-36720196-c64a-42d8-8db5-af31b75de034   0/1     ContainerCreating   0          9s
<username>-webshell-885dbc579-lwhtd                        2/2     Running             0          1d

After some time, the pod will complete and your PVC should be provisioned. Let’s check for the existence of the PVC:

kubectl get pvc --namespace lab-<username>

The output should be similar to:

NAME                    STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
lab02-alpinedisk        Bound    pvc-fe1faa27-048b-4270-a3ac-c2abf4a24aca   272Mi      RWO            longhorn       <unset>                 72s
user4-webshell          Bound    pvc-594cf281-4c34-4e7c-b345-2bf2692bbb78   1Gi        RWO            longhorn       <unset>                 1d
user4-webshell-docker   Bound    pvc-86e4bc75-396f-4630-940e-b0a4b0cf23fa   10Gi       RWO            longhorn       <unset>                 1d

Task 2.2.2: Recap the provisioning process

You have successfully provisioned a disk image which is ready to be used within a virtual machine.

We have used the Alpine image as it is reasonably small. Would there have been another way to provide the Disk Image for every participant without the need to download the image multiple times?

Task hint

We could have provided a provisioned PVC in a central namespace and created our DataVolume with a pvc source, then let the CDI Operator clone the central PVC to a PVC in your namespace.