KVM Add Block Device to LVM and Grow XFS Filesystem

KVM Add Block Device to LVM and Grow XFS Filesystem

A short guide to increasing disk capacity on a KVM guest by adding a new block device, extending the logical volume and growing the overlying XFS filesystem.

The sequence is:

  1. Create new block device on KVM host
  2. Attach device to guest
  3. Create a new partition table on new device on guest
  4. Create a new LVM Physical Device on guest
  5. Extend the Volume Group
  6. Resize the XFS filesystem

Create New Block Device

The first step is to create a new block device on the KVM host.

qemu-img create -f raw /var/kvm/images/kp01.02.img 10G

Then modify the permissions accordingly.

chown qemu:qemu /var/kvm/images/kp01.02.img
chmod go-r /var/kvm/images/kp01.02.img

Attach Block Device to Guest

Next, attach the new disk image to the appropriate guest.

virsh attach-disk kp01 --source /var/kvm/images/kp01.02.img --target vdb --persistent

Partition Block Device on Guest

On the guest, create a new partition layout using the LVM type.

fdisk /dev/vdb

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x6a1377de.

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6a1377de

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6a1377de

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    20971519    10484736   83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6a1377de

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    20971519    10484736   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create a New Physical Device on Guest

On the guest, create a new LVM physical device from the new LVM partition.

pvcreate /dev/vdb1

Extend The New Volume Group on the Guest

On the guest, extend the appropriate Volume Group to use the new Physical Volume.

vgextend centos /dev/vdb1

Extend the Logical Volume

On the guest, extend the appropriate Logical Volume.

lvextend /dev/centos/root /dev/vdb1

Grow the / Filesystem

On the guest, grow the XFS filesystem.

xfs_growfs /