Growing a filesystem

Growing a Linux filesystem requires the manipulation of each level of abstraction involved from filesystem to disk partition. Here we assume that the disk is a virtual disk image and that the following things must be resized: the virtual disk image, the partition, the LVM volume group, the LVM logical volume, and the filesystem. Other scenarios will require similar procedures. We assume that you want to increase the size of a filesystem within the disk image named disk.img by 16 GB.

First, shut down the virtual machine which contains the disk you want to grow. Next, increase the size of the disk image:

dd if=/dev/zero bs=1G count=16 >> disk.img

You should complete the next steps from within the virtual machine. However, if you are resizing the root filesystem, then you will need to boot the virtual machine using some form of bootable external media. In any case, these instructions assume that the disk you resize is not mounted. Here we assume the disk is known as /dev/sda, that you want to resize partition 2, that the LVM logical volume you wish to resize is known as /dev/mapper/centos-root, and that all of this hosts an XFS filesystem.

  1. Run fdisk /dev/sda to delete and recreate partition 2. Use d to delete, and n to create. When recreating partition 2, use the same start block, but use the new end block.
  2. Run kpartx -va /dev/sda to scan the disk’s LVM logical volumes.
  3. Run pvresize /dev/sda2 to cause LVM to use all of /dev/sda2’s new size.
  4. Run pvdisplay to confirm the change.
  5. Run lvextend -L+16GB /dev/mapper/centos-root to extend the logical volume to span the new disk size.
  6. Resize the filesystem using xfs_growfs /.