Bookmark this page

Attaching and Accessing Disks on Virtual Machines

Objectives

  • Attach disks to a VM.

Connect a Virtual Machine to Multiple Disks

Data volumes enable OpenShift to dynamically create the PVCs that represent the used disks in a VM. The VM disks can be imported from a URL or cloned from an existing PVC. You can attach several data volume resources to a VM. Inside the VM, each data volume shows as a disk that you can partition, format, and then mount by using the operating system tools. The disk type inside the VM depends on the selected interface when you attach the data volume:

SCSI

The disk shows as a standard SCSI device that most operating systems recognize. Linux systems name these disks in the /dev/sdX format.

VirtIO

The operating system uses a VirtIO driver that communicates with the virtualization layer for optimal performance. Some operating systems do not provide that driver by default. Linux systems include the driver and name these disks in the /dev/vdX format.

Use the OpenShift web console to create a data volume and attach it to a VM as an additional disk. Navigate to VirtualizationVirtualMachines, select the VM, and then navigate to ConfigurationDisks. Click Add disk and then complete the form.

Figure 5.3: Add a disk to a virtual machine

Note

When you hot plug a disk to a running VM, SCSI is the only available interface. To use the optimized VirtIO interface, stop the VM before attaching the new disk.

Select a Data Volume Source

The source section of a data volume resource provides the details of the disk image to inject into the PV. The following screen capture shows that you can select the source from the Add disk window when using the OpenShift web console.

Figure 5.4: Select the data source
Blank (creates PVC)

The new PVC provides an unformatted raw device. You can use your operating system tools to partition and then format the new block device from inside the VM.

PVC (creates PVC)

OpenShift Virtualization copies the PVC that you provide and then uses that copy as the VM disk. Because the source PVC must not be in use for the cloning process to start, you must stop any pods or VMs that are actively using it.

Container (ephemeral)

OpenShift Virtualization downloads the container image from a container registry and uses the disk in that container image as the VM disk. This source option is similar to the registry option, except that OpenShift Virtualization does not create a data volume, PVC, or PV resources, and therefore does not consume space on your back-end storage.

You can mount the disk read/write to the VM, but the data that you write is not persistent. Whenever you restart the VM, OpenShift Virtualization discards your data and restores the disk image to its initial state.

You can use ephemeral storage when you do not need to preserve data, such as temporary files.

PVC auto-import (uses DataSource)

Specify the project and name of a data source resource. The data source contains the reference of the original PVC that is imported to a new PVC.

URL (creates PVC)

Red Hat OpenShift Virtualization downloads the virtual disk image from the URL that you provide and then extracts it to the new volume. The virtual disk image must be in the raw format or in the QEMU copy on write version 2 (qcow2) format. The disk usually includes file systems that you can mount to access their data from inside the VM. Because the disk is read/write, Red Hat OpenShift Virtualization preserves all your changes across VM restarts.

Use an existing PVC

OpenShift Virtualization attaches the PVC that you provide as a disk. That PVC might come from a data volume that you detached from another VM and that you want to reuse with your VM.

Warning

Do not use a PVC that is currently attached to another VM.

Even though Red Hat OpenShift allows pods and VMs to concurrently access volumes in ReadWriteMany access mode, the file systems on these volumes might not support that mode. The ext4 or XFS file systems, for example, do not support concurrent mounting from multiple systems. Doing so can result in file system corruption or inconsistencies when attempting to read data.

Registry (creates PVC)

OpenShift Virtualization downloads the container image from a container registry. The container image contains a disk image in the raw or qcow2 format in the /disk/ directory. OpenShift Virtualization extracts that disk image and copies it to the volume.

This source option is similar to importing disks from URLs, and is useful when a container registry is already deployed in your organization. Instead of installing a new file server to distribute your disk images, you can store these images in your container registry.

Because container registries can manage only images, you must store your disk images in container images in the /disk/ directory. These images are not meant to be run as containers, and they usually contain only the /disk/ directory and the disk image.

Upload (Upload a new file to PVC)

You can upload an existing qcow2 disk image to the cluster and use it in a VM. The selected image is uploaded and imported to a PVC that is attached to the VM. You can create the disk image with tools such as qemu-img or virt-make-fs.

Detach a Disk from a Virtual Machine

You can detach disks only from stopped VMs. After you stop the VM, navigate to ConfigurationDisks, click the vertical ellipsis icon to the right of the disk to detach, and then click Detach. When a confirmation message appears, click Detach to confirm.

Delete the Virtual Machine Disk Data

The data is preserved after the disk is detached from a VM. To delete the PVC that contains the disk data, navigate to StoragePersistentVolumeClaims, and select the project where the VM is located. Locate the PVC with the VM name followed by the disk name, and then click the vertical ellipsis icon at the end of the row. Finally click Delete PersistentVolumeClaim.

A confirmation message indicates that the associated data volume is deleted with the PVC. Click Delete to proceed.

You can also delete the data volume resource by using the following command. The underlying persistent volume claim and persistent volume are removed when you delete the data volume.

[user@host ~]$ oc delete datavolume/mariadb-server-my-disk
datavolume.cdi.kubevirt.io "mariadb-server-my-disk" deleted

Attach an Existing Disk to a Virtual Machine

After you detach a disk from a VM, you can attach it to a different VM. You can also reattach the disk to the same VM, if needed.

To attach the disk, stop the VM, and navigate to ConfigurationDisks. Click Add disk and then complete the form:

Attach an existing PVC to a VM

1

Select Use an existing PVC for the source.

2

Select the PVC to attach to the VM. OpenShift Virtualization constructs the name of the PVCs from the name of the initial machine for which you created the disk and from the name that you gave to the disk.

Warning

Do not use a PVC that is currently attached to another VM. Doing so can result in file system corruption or inconsistencies when attempting to read data.

Preallocating Disk Space

When OpenShift Virtualization copies a disk image to a PV, it allocates only the space that the image uses.

For example, when you create a 10 GiB blank disk, OpenShift Virtualization does not reserve that space with the back-end storage. Instead, OpenShift Virtualization progressively allocates the space when you start writing data to the disk. This mechanism improves usage of your back-end storage, because it consumes only the space that you use.

However, write speed is often reduced, because the system must allocate the disk blocks before allowing the write operations to proceed.

To preallocate the whole disk space, select the Enable preallocation checkbox when adding a disk to a VM. In a data volume resource file, use the preallocation parameter:

apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: datadisk
spec:
  preallocation: true
  storage:
    resources:
      requests:
        storage: 10Gi
    storageClassName: ocs-external-storagecluster-ceph-rbd-​virtualization
  source:
    blank: {}

With this option enabled, OpenShift Virtualization reserves the entire requested space with the back-end storage. This operation consists of writing zeros to the device's blocks for some back-end storage.

Note

OpenShift Virtualization always reports the disk size that you request, even if you do not enable preallocation.

Resize a Virtual Machine Disk

RHOCP enables resizing of PVCs. When you change the size of a PVC, RHOCP automatically resizes the associated PV. Because most VM disks are based on PVCs, you resize a disk by resizing its PVC.

Warning

You can only expand PVCs, not shrink them.

Confirm that the Storage Class Allows Expansion

Each storage class has a field that specifies whether volume expansion is supported. When volume expansion is available, the storage class resource allowVolumeExpansion field is set to true.

[user@host ~]$ oc get storageclass \
  ocs-external-storagecluster-ceph-rbd-​virtualization -o yaml | \
  grep allowVolumeExpansion
allowVolumeExpansion: true

The storage class does not support volume expansion if the allowVolumeExpansion field is absent or is not set to true.

[user@host ~]$ oc get storageclass nfs-storage -o yaml | \
  grep allowVolumeExpansion	1

1

The output of this command is empty because the grep command does not find the allowVolumeExpansion string in the YAML representation of the nfs-storage storage class resource.

You can navigate to StorageStorageClasses, select the name of the storage class, and click YAML to inspect the YAML definition and verify whether the storage class supports volume expansion.

Expand a Persistent Volume Claim

To expand a PVC by using the OpenShift web console, navigate to StoragePersistentVolumeClaim, select the PVC to edit, click ActionsExpand PVC, type the new size, and click Expand to confirm.

Figure 5.5: Expand a PVC

You can expand a PVC by editing its resource file and modifying the spec.resources.requests.storage parameter.

[user@host ~]$ oc edit pvc/mariadb-server-my-disk-rbd
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb-server-my-disk-rbd
  namespace: storage-multiple
  ...output omitted...
spec:
  accessModes:
  - ReadWriteMany
  ...output omitted...
  resources:
    requests:
      storage: 8Gi
  storageClassName: ocs-external-storagecluster-ceph-rbd-​virtualization
  volumeMode: Block
  volumeName: pvc-c87ee674-4ddc-3efe-a74e-dfe25da5d7b3
...output omitted...

Expand the Virtual Machine File System

Resizing a PVC does not require stopping the VM that is using it. However, the VM detects the new disk size only after a restart.

Most operating systems report the new size but do not automatically expand the partitions and the file systems. On Linux systems that enable the cloud-init service, these operations might automatically happen during boot.

References

For more information about resizing volumes, refer to the Expanding Persistent Volumes chapter in the Red Hat OpenShift Container Platform Storage guide at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/storage/index#expanding-persistent-volumes

KubeVirt Containerized Data Importer

KubeVirt Data Volumes

KubeVirt Data Volume Preallocation

Revision: do316-4.14-d8a6b80