6.5. Resizing a Disk
In this section we will resize the root disk of a virtual machine.
Requirements
Resizing disks depends on the Kubernetes storage provider. The CSI driver must support resizing volumes and must be configured with AllowVolumeExpansion.
Further, it may depend on the operating system you use. Whenever the volume is resized, your VM might see the change
in disk size immediately. But there might still be the need to resize the partition and filesystem. Fedora Cloud for instance
has the package cloud-utils-growpart installed. This rewrites the partition table so that the partition takes up all
the space available. This makes it a very handy choice for resizing disk images.
Task 6.5.1: Create a volume and a virtual machine
In a first step we are going to create a Fedora disk. Create the file dv_lab06-expand-disk.yaml in the folder labs/lab06 with the following content:
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: lab06-expand-disk
spec:
source:
registry:
url: "docker://quay.io/containerdisks/fedora:43"
pvc:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 6Gi
Create the data volume in the cluster:
kubectl apply -f labs/lab06/dv_lab06-expand-disk.yaml --namespace lab-<username>
Create the file vm_lab06-expand.yaml in the folder labs/lab06 and use the following yaml specification:
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: lab06-expand
spec:
runStrategy: Halted
template:
metadata:
labels:
kubevirt.io/domain: lab06-expand
spec:
domain:
cpu:
cores: 1
devices:
disks:
- name: datavolumedisk
disk:
bus: virtio
- name: cloudinitdisk
disk:
bus: virtio
interfaces:
- name: default
masquerade: {}
resources:
requests:
memory: 2Gi
networks:
- name: default
pod: {}
volumes:
- name: datavolumedisk
persistentVolumeClaim:
claimName: lab06-expand-disk
- name: cloudinitdisk
cloudInitNoCloud:
secretRef:
name: lab06-cloudinit
Create the virtual machine with:
kubectl apply -f labs/lab06/vm_lab06-expand.yaml --namespace lab-<username>
Start the virtual machine with:
virtctl start lab06-expand --namespace lab-<username>
Check the disk size
Start the virtual machines’ console and log in (user: fedora, password: kubevirt):
virtctl console lab06-expand --namespace lab-<username>
Check your block devices with:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
zram0 251:0 0 1.9G 0 disk [SWAP]
vda 252:0 0 5.7G 0 disk
├─vda1 252:1 0 2M 0 part
├─vda2 252:2 0 100M 0 part /boot/efi
├─vda3 252:3 0 1000M 0 part /boot
└─vda4 252:4 0 4.6G 0 part /var
/home
/
vdb 252:16 0 1M 0 disk
Task 6.5.2: Resize the disk
Triggering a resize of a PVC in Kubernetes can be done with editing the PVC size request. Get the PersistentVolumeClaim manifest with:
kubectl get pvc lab06-expand-disk -o yaml --namespace lab-<username>
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: lab06-expand-disk
[...]
spec:
resources:
requests:
storage: 6Gi
[...]
Now, patch the PVC to increase the disk size to 8Gi:
kubectl patch pvc lab06-expand-disk --type='json' -p='[{"op": "replace", "path": "/spec/resources/requests/storage", "value":"8Gi"}]' --namespace lab-<username>
persistentvolumeclaim/lab06-expand-disk patched
It might take some time for the storage provider to resize the persistent volume. You can see details about the process in the events section when describing the PersistentVolumeClaim resource:
kubectl describe pvc lab06-expand-disk --namespace lab-<username>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning ExternalExpanding 2m56s volume_expand waiting for an external controller to expand this PVC
Normal Resizing 2m56s external-resizer driver.longhorn.io External resizer is resizing volume pvc-bc3e89f3-1372-4e10-852a-0c94ea343ab4
Normal FileSystemResizeRequired 2m46s external-resizer driver.longhorn.io Require file system resize of volume on node
Normal FileSystemResizeSuccessful 2m8s kubelet MountVolume.NodeExpandVolume succeeded for volume "pvc-bc3e89f3-1372-4e10-852a-0c94ea343ab4" training-worker-2
If you still have a console open in your virtual machine you see that there was a message about the capacity change:
[ 896.201742] virtio_blk virtio3: [vda] new size: 15853568 512-byte logical blocks (8.12 GB/7.56 GiB)
[ 896.202409] vda: detected capacity change from 11890688 to 15853568
Recheck your block devices:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
zram0 251:0 0 1.9G 0 disk [SWAP]
vda 252:0 0 7.6G 0 disk
├─vda1 252:1 0 2M 0 part
├─vda2 252:2 0 100M 0 part /boot/efi
├─vda3 252:3 0 1000M 0 part /boot
└─vda4 252:4 0 4.6G 0 part /var
/home
/
vdb 252:16 0 1M 0 disk
You will see that the capacity change is visible from within the virtual machine. But at this time our partitions still have the same size and do not use all available disk space.
Issue a reboot to let the system expand the partitions:
sudo reboot
After the reboot you have to log in and check lsblk again:
lsblk
You can see that, e.g., vda4 has been resized from 5.7G to 7.6G.
End of lab
Cleanup resources
You have reached the end of this lab. Please stop your running virtual machines to save resources on the cluster.
Delete your VirtualMachine resources:
kubectl delete vm lab06-storage --namespace lab-<username>
kubectl delete vm lab06-cirros --namespace lab-<username>
kubectl delete vm lab06-snapshot --namespace lab-<username>
kubectl delete vm lab06-expand --namespace lab-<username>
Delete your disks:
kubectl delete dv lab06-fs-disk --namespace lab-<username>
kubectl delete dv lab06-block-disk --namespace lab-<username>
kubectl delete dv lab06-cirros-disk --namespace lab-<username>
kubectl delete dv lab06-expand-disk --namespace lab-<username>
kubectl delete dv lab06-hotplug-disk --namespace lab-<username>
Delete your VirtualMachineSnapshots:
kubectl delete vmsnapshot lab06-snapshot-snap --namespace lab-<username>