How to add extra space to Linux VM in XenServer

Before we start you will need to do the following in XenCenter

  • Shutdown the VM
  • Increase the virtual hard drive size of the Linux VM
  • Boot the VM back up

Before starting any of the following, it is a good idea to backup any data you would not want to lose.  You should not lose any data following these steps, but there is always the possibility for something to go wrong.

What is going to happen

  1. Delete the main partition.  We are going to recreate it.
  2. Create a new partition that starts on the same boundary as the previous partition
  3. Write changes to disk and reboot
  4. Resize the Disk
  5. Check that everything went well

The path to the disk is “/dev/xvda” the LVM path/name is “/dev/VolGroup/lv_root”  The goal is to increase lv_root’s size from about 8GB to about 12GB. If for some reason your drive is not “xvda” or your LVM name is different, change the commands accordingly.

Here are the commands in a nutshell.

fdisk /dev/xvda                        <-- Edit the Partition Table
reboot                                 <-- Reboot to apply the partition table updates
pvdisplay                              <-- Display Physical Volume info
pvresize /dev/xvda2                    <-- Resize Physical Volume
lvresize /dev/xvda2 -l +100%FREE       <-- Resize Logical Volume
resize2fs /dev/VolGroup/lv_root        <-- Resize File System

Example:

You may be able to substitute all the fdisk commands with
`parted /dev/xvda resizepart 2 100%`
Change 2 for the actual partition you need to resize.

All the keys and command that were hit and executed are in bold.

[root@localhost ~]# 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: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64        1045     7875584   8e  Linux LVM
Command (m for help): d
Partition number (1-4): 2

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: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (64-1566, default 64): 
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: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64        1566    12065871   83  Linux

Command (m for help): wq
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.
[root@localhost ~]# reboot

Show the current size of the Physical Volume

pvdisplay
[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               VolGroup
  PV Size               7.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1922
  Free PE               0
  Allocated PE          1922
  PV UUID               zKmGEt-Uf0A-I14h-NDYc-53rf-micT-VxNqsP
   
[root@localhost ~]#

Resize the Physical Volume

pvresize /dev/xvda2
[root@localhost ~]# pvresize /dev/xvda2
  Physical volume "/dev/xvda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@localhost ~]#

Run pvdisplay again.  You should see more space under PV Size.

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               VolGroup
  PV Size               11.51 GiB / not usable 2.08 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2945
  Free PE               1023
  Allocated PE          1922
  PV UUID               zKmGEt-Uf0A-I14h-NDYc-53rf-micT-VxNqsP
   
[root@localhost ~]#

Notice the “Free PE” section above.  If it says 0 then you won’t be able to run the next command.

Resize LVM

the “+100%FREE” part of the command tells it to uses up all of the free space available

lvresize /dev/VolGroup/lv_root -l +100%FREE
[root@localhost ~]# lvresize /dev/VolGroup/lv_root -l +100%FREE
  Extending logical volume lv_root to 10.71 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]#

Resize the File System

resize2fs /dev/VolGroup/lv_root
[root@localhost ~]# lvresize /dev/VolGroup/lv_root -l +100%FREE
  Extending logical volume lv_root to 10.71 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]#

And that is it.  Check out the extra space.

[root@localhost ~]# df -h /
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   11G  733M  9.3G   8% /
[root@localhost ~]# exit