Tuesday 2011-02-15

Say you have a centos 5.5 dom0, an iSCSI SAN, and you want your Xen guests to have online resize for storage. Xen guests don't (as of 2011-02-15) have a way to notice that their xvda has resized. To get around that, we'll dynamically allocate new disks (xvdb, xvdc, etc.) to the guest.

With an iSCSI SAN, we can grow our LUN, allowing us to add another partition. However, the msdos partition table type only supports 4 primary partitions, so use parted to setup a GUID partition table on the iSCSI disk, we can then resize the guest's disk 100+ times (more can be done by tweaking the partition table header ).

Then setup your guest with LVM, so its partitions can grow ad-hoc. Resizing a guest's disk online now looks like:

# don't actually run this script, just read it ;)
export DISK="/dev/whatever"
export GUEST="domw00w00"
export GUEST_IP=""

# extend the LUN exported by your SAN, then in dom0
iscsiadm -m session -R

# verify that dom0 sees the expanded disk
parted $DISK unit s print
# parted will complain about the backup MBR being gone, Fix.
#     GUID PT keeps the backup at the end of the disk, which just moved. ;)
# parted will also say it has more space, Fix.
export S_BEGIN=$( parted -s $DISK unit s print | awk '/[0-9]s *[0-9]*s/ { end=$3 } END{ print end }' )
export S_END=$(   parted -s $DISK unit s print | awk '/^Disk/ { print $NF }' )
export PART=$(    parted -s $DISK unit s print | awk '/[0-9]s *[0-9]*s/ { end=$1 } END{ print end+1 }')

parted $DISK mkpart $PART $S_BEGIN $S_END
# parted will note that you're off by one sector, Fix.

# make the new partition available to the guest
export XVDN=$( ssh $GUEST_IP 'ls /dev/xvd*' | tail -1 )
virsh attach-disk $GUEST "${DISK}${PART}" $XVDN
# xm block-attach only populates /sys/devices, we don't get a new /sys/block/...

# add the $XVDN to the guest's xen config

xm console $GUEST
# re-export the XVDN from above
parted /dev/$XVDN mklabel msdos
export S_END=$(   parted -s $DISK unit s print | awk '/^Disk/ { print $NF }' )
parted /dev/$XVDN mkpart 1 0 $S_END

# add the partition to the LVM vg needed and resize
pvcreate /dev/$XVDN
vgextend ...
lvextend ...
resize2fs ...