Howto Resize a KVM Guest LVM Block Device

KVM Guest LVM Block Device Resize

This guide explains how to resize the block device of a running KVM guest using LVM.

Considering that you cannot resize a LVM partition on a live box, it is a better idea to use multile images for scaling disk capacity of guests.

Get the path of the block device to resize:

source=`virsh domblklist kp01 | awk '/^vda/{print $2}' | head -n1`

Resize the block device:

#virsh blockresize <domain> <path to image> <new size>
virsh blockresize kp01 $source 10G

Delete the partition and recreate it.

# fdisk /dev/vda
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.


Command (m for help): p

Disk /dev/vda: 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: 0x00074224

  Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    16777215     7875584   8e  Linux LVM

Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

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

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

Command (m for help): p

Disk /dev/vda: 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: 0x00074224

  Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Reboot.

reboot

After rebooting, check the Physical Volume.

# pvdisplay
--- Physical volume ---
PV Name               /dev/vda2
VG Name               centos
PV Size               7.51 GiB / not usable 3.00 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              1922
Free PE               10
Allocated PE          1912
PV UUID               HImcGW-ufAE-bdYQ-IZJG-qhtQ-1HEG-mDR4P1

Notice that the Free Physical Extents still hasn't changed.

Resize the PV.

# pvresize /dev/vda2
Physical volume "/dev/vda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Now see that the FreePE has been updated:

# pvdisplay
--- Physical volume ---
PV Name               /dev/vda2
VG Name               centos
PV Size               9.51 GiB / not usable 2.00 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              2434
Free PE               522
Allocated PE          1912
PV UUID               HImcGW-ufAE-bdYQ-IZJG-qhtQ-1HEG-mDR4P1

Next resize the Logical Volume, claiming all remaing free space.

# lvresize /dev/mapper/centos-root -l +100%FREE
Size of logical volume centos/root changed from 6.67 GiB (1707 extents) to 8.71 GiB (2229 extents).
Logical volume root successfully resized.

Finally, grow the filesystem to use the available capacity.

# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=436992 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1747968, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1747968 to 2282496