Xenserver 6.2 - extending a disk on a Linux VM

By Simon, 10 September, 2013

I've been slowly converting some of our VM infrastructure over to XenServer from VMWare. Now that XenServer 6.2 is fully open source, it seems to be a good place to transition. Sometimes incredibly simple tasks take you down a rabbit hole you did not plan on going down.

I must confess I spent hours trying different methods and partition schemes. The K.I.S.S. approach is really the way to go here. I found this article to be the simplest and most practical solution, and have updated it here to cover any CentOS 6.4 / XenServer 6.2 issues. It's based on the assumption that you don't have an overly complicated VM partition scheme.

I've used this method successfully on live production CentOS 6.4 VMs. However, I suggest you create a test VM from scratch to try this out first, using XenServer's default settings (8GB of disk space) with three primary partitions: a 200MB ext4 /boot partition, a 2GB swap partition, and the rest of the default disk space as an ext4 / partition.

Three things need to happen to increase the disk available to a Linux VM:

  1. Increase the disk size available to a VM in XenCenter
  2. Increase the size of the VM partition in the VM’s partition table
  3. Grow the size of the ext3 filesystem in the VM

 
Step 1: Increase Disk Space in XenCenter

First of all, the VM needs to be shut down in order to extend the size of the drive in XenCenter. On the storage tab of the VM, click on properties and increase the size of the storage in XenCenter. For a test VM you can just extend it from 8GB to 12GB.

Step 2: Change the Partition Table in the VM

Next, start up the VM, as you'll need to increase the size of the linux / partition using fdisk. The utility parted should allow you to resize partitions as well and be much smarter about it and let you move filesystem data around as well, but parted does not allow you to edit running partitions. In this case since I want to edit /dev/xvda3 which is my / partition and it is at the end of the virtual disk I only need to extend the size of the partition and not move it around. On the running VM use fdisk to increase the / partition (/dev/xvda3):

# fdisk /dev/xvda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/xvda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000928ad

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              26         281     2048000   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/xvda3             281        1045     6134784   83  Linux

Command (m for help): d
Partition number (1-4): 3

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (281-1566, default 281): 281
Last cylinder, +cylinders or +size{K,M,G} (281-1566, default 1566): 1566

Command (m for help): p

Disk /dev/xvda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000928ad

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              26         281     2048000   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/xvda3             281        1566    10325071   83  Linux

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.
# 

 
Step 3: Reboot the VM

Now that last warning is accurate, you need to reboot the VM again in order for the kernel to pickup the changes to the partition geometry. However, rebooting does not change the available disk space size of the / partition:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda3            5.8G  829M  4.7G  15% /
tmpfs                 497M     0  497M   0% /dev/shm
/dev/xvda1            194M   45M  140M  25% /boot

You can fix this by running resize2fs in the running VM which will automatically notice the difference between the partition geometry and the size of the filesystem and will extend the filesystem to its maximum size:

# resize2fs /dev/xvda3
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvda3 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/xvda3 to 2581267 (4k) blocks.
The filesystem on /dev/xvda3 is now 2581267 blocks long.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda3            9.7G  830M  8.4G   9% /
tmpfs                 497M     0  497M   0% /dev/shm
/dev/xvda1            194M   45M  140M  25% /boot
#