Growing disk space for a Xen guest

Problem

You have Xen VM guest with LVs created on the Xen host, your Xen VM guest (from now on named ‘foo’) has 3 partitions, but your mount point /root on /dev/xvda2 (could be /home, or whatever) is running low on space, so you need to grow the disk, but you notice that the swap /dev/xvda3 is in the way, this is how to do it, which in a nutshell, involves deleting partitions xvda2 and 3, recreating them one at a time, growing the file system into the new space and then checking it all.

I basing this on a VM that has a 8.3GB virtual disk, we are going to increase by 4GB thus giving it 12.3GB, and then use the extra (4GB) space for /root (xvda2)

Original VM disk size – the Start/End/blocks etc are just a guide, I can’t remember the original values

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          16      128488+  83  Linux    (128MB)
/dev/xvda2              17         783     10000000  83  Linux    (8GB)   
/dev/xvda3             784        1306     2088450   82  Linux swap / Solaris    (256MB)

And we want to grow xvda2 from 8GB to 12GB, so it will look like this:

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          16      128488+  83  Linux    (128MB)
/dev/xvda2              17        1306     10361925  83  Linux    (12GB)   
/dev/xvda3            1307        1566     2088450   82  Linux swap / Solaris    (256MB)

Make a note of the Start/End values for the original as you will need this later.

1.  Check what we already have for our VM

fdisk -l

Disk /dev/xvda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          16      128488+  83  Linux
/dev/xvda2              17        1306    10361925   83  Linux
/dev/xvda3            1307        1566     2088450   82  Linux swap / Solaris

2.  Check current disk space for VM on the host

lvscan
ACTIVE            ‘/dev/xen_vg/foo.lv’ [8.00 GB] inherit

3. Check to see if we have enough spare disk space on the host which we can take

vgdisplay

 — Volume group —
  VG Name               xen_vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  163
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                12
  Open LV               4
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               240.00 GB
  PE Size               4.00 MB
  Total PE              61439
  Alloc PE / Size       43264 / 169.00 GB
  Free  PE / Size       18175 / 71.00 GB  <===== some free space
  VG UUID               s8cAAA-GMzz-AUXS-w7ie-E123-J333-uh4dd3

As you can see we have 71GB free,  so we are good there.

4. Okay lets start

Power down Xen VM
 shutdown now -h
Take snapshot, if you have a means to do this, or back up, up to you, but you could lose everything.

5.  First thing, login to your Xen host, then extend the size of the logical partition (foo.lv)

lvextend -L +4G /dev/xen_vg/foo.lv

Now  do a lvscan, you should notice that the original 8GB has now been increased to 12GB

lvscan
ACTIVE            ‘/dev/xen_vg/foo.lv’ [12.00 GB] inherit

So our VM now has available a new virtual disk that is 12.3GB in size…which is nice.

6.  Now we use fdisk to delete the partitions

fdisk  /dev/xen_vg/foo.lv

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

Repeat for partition 3.

7.  Now we use fdisk to add the partitions, but you need to change the default value for the last cylinder, so enter +4000M, this will use the extra 4GB we made available.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (17-763, default 17):
Using default value 14
Last cylinder or +size or +sizeM or +sizeK (14-2480, default 2480):+4000M
Using value 4000M

Repeat for partition 3 – you can accept the default this time as should go to the end of the disk

Save it

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

Now, remember, partition 3 is swap, so we have to give it the correct hex ID for swap

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap)      

Save it

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

8. We use kpartx to create 3 temporary device maps in /dev/mapper, named foo.lv1, foo.lv2 and foo.lv3, one for each partition, this enables us to modify/check each partition, and once completed we delete them.

kpartx -av /dev/xen_vg/foo.lv

9. Now we need to run fsck on the 2 partitions before we extend the file system

e2fsck -f /dev/mapper/foo.lv2

and

e2fsck -f /dev/mapper/foo.lv3

10. Time to extend the file system:

resize2fs /dev/mapper/foo.lv2

and

resize2fs /dev/mapper/foo.lv3

11. Delete the temprorary device maps:

kpartx -d /dev/xen_vg/foo.lv

12. Your done, now start up you Xen VM

xm create foo -c

13. Take a look at the space

fdisk -l

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          16      128488+  83  Linux    (128MB)
/dev/xvda2              17        1306     10361925  83  Linux    (12GB)   
/dev/xvda3            1307        1566     2088450   82  Linux swap / Solaris    (256MB)

Advertisements

About hedscratchers

A UK ex-pat now living in the USA.
This entry was posted in Linux & Solaris. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s