Extending (Resizing) LVM/XFS Xen images

Documentation on this procedure out on the interwebs seems rather sparse, so I thought I’d post the procedure I use to expand Xen images that use LVM and XFS. The same procedure should work fine if you use EXT3, you probably will just have to substitute the proper e2/3fs command for xfs_growfs.

## FROM THE VM HOST:
### Shutdown VM
xm shutdown VMName

## Backup the image
## (Optional, though highly suggested until you get this procedure tested in your deployment
cp -rp /var/lib/xen/images/VMName.img /var/lib/xen/images/VMName.img.bak

## Add 5GB to the image (takes approximately 4 seconds per Gig)
dd if=/dev/zero bs=1M count=5000 >> /var/lib/xen/images/VMName.img

## Loop the image
kpartx -av /var/lib/xen/images/VMName.img

## Run "fdisk /dev/loop2"
## Enter these commands
## (double check to make sure the partition numbers apply in your case)
#p-rint
#d-elete
#2-(partition 2)
#n-ew
#p-rimary
#2-(partition 2)
#<enter>-(Defaults to first available cylinder)
#<enter>-(Defaults to last available cylinder)
#t-ype
#2-(partition 2)
#8e-(Linux LVM)
#p-print
#w-rite

# Delete the loop and re-add it (to reload partiton table)
kpartx -dv /var/lib/xen/images/VMName.img
kpartx -av /var/lib/xen/images/VMName.img

# Check the Physical Volume size
pvdisplay

#  — Physical volume —
#  PV Name               /dev/dm-3
#  VG Name               VolGroup00
PV Size               3.71 GB / not usable 20.42 MB
#  Allocatable           yes
#  PE Size (KByte)       32768
#  Total PE              118
Free PE               1
#  Allocated PE          117
#  PV UUID               OjLnup-7iYu-ErtB-WMvN-gY0v-FbLw-gW6cEB

## Resize the Physical Volume
pvresize /dev/mapper/loop2p2

# Check the PV size again
# Take note of the Free PE (physical extents), 158 in this case
pvdisplay

#  — Physical volume —
#  PV Name               /dev/dm-3
#  VG Name               VolGroup00
PV Size               8.59 GB / not usable 1.05 MB
#  Allocatable           yes
#  PE Size (KByte)       32768
#  Total PE              275
Free PE               158
#  Allocated PE          117
#  PV UUID               OjLnup-7iYu-ErtB-WMvN-gY0v-FbLw-gW6cEB

## Delete the loop
kpartx -dv /var/lib/xen/images/VMName.img

## Boot the VM
xm start VMName

### FROM WITHIN THE VM
## Check the LV size of LogVol00
lvdisplay

## Resize the logical volume using the free extents from above (158).
lvresize -l +158 /dev/VolGroup00/LogVol00

## Check the LV size again to confirm the increased space
lvdisplay

## Resize the filesystem (in this case, XFS)
xfs_growfs /dev/mapper/VolGroup00-LogVol00

# Check for the increased space
df -h

## Finally, delete the backup FROM THE VM HOST:
/var/lib/xen/images/VMName.img.bak

### You’re Done!

I always enjoy knowing that documentation like this helped someone. Post a comment and let me know if this helped you.

Good luck!