These steps were carried out with Fedora based VM running on a libvirt KVM/QEMU host, so the commands will be specific to that setup in certain places. However the tools and process should be the same for most flavours of Linux. There are plenty of guides that help with managing LVM, but none covered all the steps I needed here.

Increasing the virtual disk size

Check the status of the disk image with qemu-img info /path/to/server1/disk1.qcow2 -U This will confirm the current size. For a running VM we need to discover the correct block device to expand that QEMU has allocated. In the output below drive-virtio-disk0 is the device identifier.

$ sudo virsh qemu-monitor-command server1 --hmp "info block"

drive-virtio-disk0 (#block151): /path/to/server1/disk1.qcow2 (qcow2)
    Attached to:      /machine/peripheral/virtio-disk0/virtio-backend
    Cache mode:       writeback, direct

Using the identifier the following command will execute an online disk expansion. 30G defines the new total size of the disk, not the amount to increase by.

sudo virsh qemu-monitor-command server1 block_resize drive-virtio-disk0 30G --hmp

If the VM powered off qemu-img can be used instead.

qemu-img resize /path/to/server1/disk1.qcow2 +5GB

Extending the partition

In the OS use parted to check that disk increase has been detected, this should happen automatically for any recent version of Linux.

$ sudo parted /dev/vda print free

Number  Start   End     Size    Type     File system  Flags
        1024B   1049kB  1048kB           Free Space
 1      1049kB  1075MB  1074MB  primary  ext4         boot
 2      1075MB  26.8GB  25.8GB  primary               lvm
        26.8GB  32.2GB  5369MB           Free Space

Then extend the LVM partition to use all the free space (assuming it is the last partition on the disk, this should be the case for a typical setup).

sudo parted /dev/vda resizepart 2 100%

Extending the LVM volume

Use pvdisplay to identify the physical volume, "PV Name" should give the path to the correct device. Then pvsresize will extend the volume to fill the free space.

sudo pvresize /dev/vda2

Use lvdisplay to identify the logical volume. For thin provisioned LVs the pool must be extended as well as the mounted volume. It is a good idea to do these both together as it is possible to over provision the pool.

sudo lvextend -L +2G /dev/fedora/pool00
sudo lvextend -L +2G /dev/fedora/root

Finally resize the file system to use the extra space, in this case for ext4.

sudo resize2fs /dev/fedora/root

The extra space will now be available to the OS, df -h / or similar will confirm this.

LVM thin volumes and free space

Thin volumes are enabled by two hidden "pool" volumes that are created during the initial configuration process. _tdata is the volume available for data and _tmeta is for tracking which thin volume is mapping which extents out of the _tdata volume. We can use lvs to show an overview of logical volumes.

$ sudo lvs -a

  LV              VG     Attr       LSize  Pool   Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home            fedora Vwi-aotz-- <5.17g pool00        72.44                                  
  [lvol0_pmspare] fedora ewi------- 12.00m                                                      
  pool00          fedora twi-aotz-- 19.17g               91.73  78.29                           
  [pool00_tdata]  fedora Twi-ao---- 19.17g                                                      
  [pool00_tmeta]  fedora ewi-ao---- 12.00m                                                      
  root            fedora Vwi-aotz-- 14.00g pool00        98.88                                  
  swap            fedora -wi-ao----  2.00g         

In this example pool00 holds the data for root and home. The "LSize" property of pool00 will be the total of root and home, plus a little extra from _tmeta. Any other partitions not thin provisioned (eg. swap) will not count to the pool00 LV total.

vgdisplay shows the allocated physical and free space (as extents) for a volume group.

  Total PE              6143
  Alloc PE / Size       5426 / <21.20 GiB
  Free  PE / Size       717 / 2.80 GiB

lvdisplay shows the extents available to a logical volume.

  LV Size                14.00 GiB
  Mapped size            98.88%
  Current LE             3584

"Mapped size" is the percentage of those extents that have been actually allocated. This doesn't mean the file system currently has 98% usage, but that at some point the file system was filled enough for those extents to be allocated. If data is deleted the extents are not unallocated. To free the extents these need to be released at the file system level using fstrim.

When using a fully sparse virtual disk then the total of the "Mapped size" for each LV – or the value of "Alloc PE" for the VG – will be the size the virtual disk is expanded to.

The following man page covers thin volumes in much more detail


comments powered by Disqus