Cloud Platform Engineering, DevOps, SRE, Kubernetes, KOPS

Out of disk space in KVM guest – easy to increase

If you are running your VMs on KVM and badly need to increase available disk space on one of the VMs there is a good news, it’s super easy. If your guest instances are using LVM then whole process becomes pretty much seamless.

Ok, first identify VM you want to modify:

sudo virsh

then run list then pool-list and vol-list with name of pool from pool-list

virsh # list
Id Name State
----------------------------------------------------
1 mongodb_01 running

virsh # pool-list
Name State Autostart
-------------------------------------------
default active yes

virsh # vol-list default
Name Path
------------------------------------------------------------------------------
mongodb_01.qcow2 /var/lib/libvirt/images/mongodb_01.qcow2

virsh #

Ok, at this point we know /var/lib/libvirt/images/ is where images live and our domain (VM) name is mongodb_01. To create new image run:

sudo qemu-img create -f qcow2 /var/lib/libvirt/images/mongodb_01-b.qcow2 20G

This will create qcow2 img in default vol directory. It will be allocated max 20GB (will grow over time).

Now you need to attach it to the VM. As we are using qcow2 there are few extra parameters we need to pass

sudo virsh attach-disk mongodb_01 \
 --source /var/lib/libvirt/images/mongodb_01-b.qcow2 \
 --target vdb --persistent --driver qemu --subdriver qcow2 \
 --targetbus virtio

One note here: –target vdb may have to be vdd, vde, etc – depending what targets you have already attached to given domain.

Once that’s done ssh into your VM and run fdisk to find new device:

[root@mongodb_01 marcin]# fdisk -l | grep 'Disk /dev/vd'
Disk /dev/vda: 37.6 GB, 37580963840 bytes, 73400320 sectors
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors

You can confirm there are no partitions on vdb by running

[root@mongodb_01 marcin]# fdisk -l /dev/vdb

You can compare result with

[root@mongodb_01 marcin]# fdisk -l /dev/vda

Ok, let’s create new lvm partition on vdb:

[root@mongodb_01 marcin]# fdisk /dev/vdb

and execute following commands:

n - for new partition
p - for primary
1 - for partition number
accept defaults for first and last sector, then
t - to toggle partition type:
and:
w - write changes to partition table (you do it all at your own responsibility, right?)

Ok, now you can expand your LVM volume:

#create new physical volume
pvcreate /dev/vdd1
#display all to confirm it looks good
pvdisplay
#display available volume groups
vgdisplay
#Myself I'm interested in extending:
--- Volume group ---
VG Name centos
#so, let's extend it:
vgextend centos /dev/vdd1
#now, display logical volumes:
lvdisplay
#this time I'm interested in extending:
--- Logical volume ---
LV Path /dev/centos/data
lvextend -r -l +100%FREE /dev/centos/data

Above command will allocate 100% of free volume group space to logival volume /dev/centos/data. Also, by specifying flag -r operation will resize underlying filesystem together with the logical volume – that may not work for some file systems.

Check with

df -h

If your resize gone as planned. Happy resizing 🙂

Conclusion

Increasing disk space on KVM guests with LVM is pretty straightforward and seamless. Those are not sort of tasks you would be doing every day but when you have to, you will appreciate seamless process and ease of execution.