This guide shows how to extend a QCOW2 virtual disk on a KVM/libvirt host and then expand the LVM logical volume and ext4 filesystem inside the Linux virtual machine.
Shutdown the Virtual Machine
Stop the VM before resizing its QCOW2 disk:
root@kvm-hyp:~# virsh shutdown ubuntu-guest
Find the Virtual Disk
Check which disk image is attached to the virtual machine:
root@kvm-hyp:/var/lib/libvirt/images# virsh domblklist ubuntu-guest
Target Source
------------------------------------------------
hda /var/lib/libvirt/images/ubuntu-guest.qcow2
Check the Current QCOW2 Disk Size
Display information about the virtual disk before resizing it:
root@kvm-hyp:/var/lib/libvirt/images# qemu-img info /var/lib/libvirt/images/ubuntu-guest.qcow2
image: /var/lib/libvirt/images/ubuntu-guest.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 12G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
Extend the Virtual Disk
Add 50 GB to the existing QCOW2 virtual disk:
qemu-img resize /var/lib/libvirt/images/ubuntu-guest.qcow2 +50G
Image resized.
Start the Virtual Machine
root@kvm-hyp:/var/lib/libvirt/images# virsh start ubuntu-guest
Domain ubuntu-guest started
Check the Current LVM and Filesystem Size
Inside the virtual machine, check the current physical volume and filesystem usage before extending them:
root@ubuntu-guest:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- 18.22g 8.22g
root@ubuntu-guest:~# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 9.8G 7.7G 1.7G 83% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.8G 253M 1.4G 16% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
Extend the LVM Partition
The LVM physical volume is located on partition 3. Extend it to use the newly available virtual disk space:
root@ubuntu-guest:~# growpart /dev/sda 3
CHANGED: partition=3 start=3719168 old: size=38221824 end=41940992 new: size=143081439 end=146800607
Resize the LVM Physical Volume
After expanding the partition, resize the LVM physical volume so LVM can use the additional space:
Verify that the additional space is now visible:
Extend the Logical Volume
Increase the root logical volume by 55 GB:
root@ubuntu-guest:~# lvextend -L+55G /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 10.00 GiB (2560 extents) to 65.00 GiB (16640 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@ubuntu-guest:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- 68.22g 3.22g
Resize the ext4 Filesystem
The logical volume is now larger, but the ext4 filesystem must also be expanded to use the new space:
root@ubuntu-guest:~# resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 9
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 17039360 (4k) blocks long.
root@ubuntu-guest:~# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.1M 196M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 64G 7.7G 54G 13% /
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.8G 253M 1.4G 16% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
Resize Sequence
The complete resize process is:
virsh shutdown ubuntu-guest
qemu-img resize /var/lib/libvirt/images/ubuntu-guest.qcow2 +50G
virsh start ubuntu-guest
growpart /dev/sda 3
pvresize /dev/sda3
lvextend -L+55G /dev/ubuntu-vg/ubuntu-lv
resize2fs /dev/ubuntu-vg/ubuntu-lv
df -h