LLI Wiki — My Digital Knowledge Base

Growing a KVM virtual machine disk with LVM and ext4

Procedure to grow a QEMU/KVM guest disk image, then the partition, the LVM volumes and the ext4 file systems.

This procedure grows the raw disk image of a QEMU/KVM guest and then extends the partition, the LVM volumes and the ext4 file systems inside the guest. It only covers growing a disk. Shrinking is different and carries a much greater risk of data loss. The approach follows this original article.

Overview

  1. On the host: resize the guest image file.
  2. In the guest: enlarge the disk partition.
  3. In the guest: enlarge the LVM physical volume and logical volumes.
  4. In the guest: grow the file systems.

The example guest uses a raw image of 20 GB that is grown by 25 GB to 45 GB. It has an extended partition holding an LVM logical partition (/dev/vda5), a volume group vg01 and the logical volumes home, root, tmp and var.

1. Record the current layout and shut down the guest

In the guest, note the current physical volumes, logical volumes and file systems.

sudo pvs
sudo lvs
df -h

Then shut the guest down.

sudo shutdown -P now

2. Resize the image on the host

sudo qemu-img resize /var/vm/guest.img +25G

ls -lhs on the image directory shows the new apparent size (45 GB here). Because the image is a sparse file, the space actually used on the host stays unchanged. Start the guest again and log in.

sudo virsh start guest
ssh guest

3. Enlarge the partition in the guest

Use fdisk on the disk (/dev/vda in this example) and print the partition table with p. The disk is now larger than the partitions it contains.

sudo fdisk /dev/vda

In this layout the boot partition /dev/vda1 is followed by an extended partition /dev/vda2 that contains the LVM logical partition /dev/vda5 (type 8e, Linux LVM). The partitions are enlarged by deleting them and recreating them with the same type, the same first sector and the largest possible last sector:

  1. d, then 5 to delete the logical partition.
  2. d, then 2 to delete the extended partition.
  3. n, e, 2, then accept the default first and last sectors to create the extended partition.
  4. n, l, then accept the default first and last sectors to create the logical partition (number 5).
  5. t, 5, 8e to set the type back to Linux LVM.
  6. p to check that the new table has the same structure as before but covers the whole disk, then w to write it.

Your device names, start sectors and type IDs will differ. Reuse the values of your own partition table. Recreating a partition with a different first sector destroys access to its data.

fdisk may warn that the kernel still uses the old table because the device is busy. In that case reboot the guest so that the new table is read.

sudo shutdown -r now

4. Enlarge the LVM volumes

After the reboot, resize the physical volume so that it takes the whole partition. The free space then appears in pvs and vgs.

sudo pvs
sudo pvresize /dev/vda5
sudo pvs
sudo vgs

Extend the logical volumes you want to grow. In the example home gets 15 GB and var gets 10 GB, using the full path /dev/<volume_group>/<logical_volume>.

sudo lvextend -L +15G /dev/vg01/home
sudo lvextend -L +10G /dev/vg01/var
sudo vgs
sudo lvs

At this point the volume group has no free space left, but the file systems still have their old size.

5. Grow the file systems

For ext4, resize2fs can grow a mounted file system online. With other file systems or older kernels you may have to unmount it first.

sudo resize2fs /dev/vg01/home
sudo resize2fs /dev/vg01/var
df -h

The resize can take some time. df -h then shows the larger /home and /var file systems.

Notes

Mastodon LLIMastodon SocialFosstodon