Extend partition in disk in xen linux guest

After searching the web for a method to resize a partition in a disk in a logical volume as opposed to just a partition in a logical volume (notice the difference ‘partition in’ or ‘disk in’). I could not find a method however if you have enough space you could use the following method.

Note that I used this to resize the partition of a virtual machine running in a logical volume (it should work with physical stuff too but then you might have to put a disk in another physical host or mount it using a live cd).

Util is a machine with the same version of the OS. tbe is the one we want to extend.

From a high level the procedure is as follows:

  • Mount the disk with partitions you want to extend on another virtual machine (VM)
  • Use cp to backup the disks
  • Use fdisk to extend the partitions
  • Use cp to restore the backup to the resized partitions
  • On the VM with, now, resized disks, run resize2fs
  • Bonus; resize and or add the swap partition

Everything is done as root. Prefix with sudo if you want or need to.

xl shutdown util 
xl shutdown tbe 

Extend the disk on the host.

lvextend -L8G /dev/<vg>/<lvm>

Unfortunately although the logical volume is extended the disk inside isn’t using the extra size. Since we do not simply mount the disk but have a complete disk within the lv we cannot simply use resize something. Instead we have to mount it on, preferably the same version of the OS and then make a backup of the partition(s). Drop everyting and create new partitions then copy it back:

edit util .cfg

disk = [ '/dev/<VG>/<lv util>,raw,xvda,rw', '/dev/<VG>/<lv tbe>,raw,xvdb,rw' ]

Bring our util system backup

xl create util.cfg

Login to util then:

ls -tarl /dev/xvdb*

Make a backup of all the regular partitions (no need to backup swap of course).

mkdir /backup
# Repeat for all other partitions (you could skip swap if you want but I would be on the safe side and backup all just in case you got the partition wrong)
cp /dev/xvdb1 /backup/xvdb1backup1
cp /dev/xvdb2 /backup/xvdb1backup2
# etc.. 
fdisk /dev/xvdb

Command (m for help): p
Disk /dev/xvdb: 14 GiB, 15032385536 bytes, 29360128 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x20230da1

Device Boot Start End Sectors Size Id Type
/dev/xvdb1 2048 8388607 8386560 4G 83 Linux
/dev/xvdb2 8388608 16777215 8388608 4G 82 Linux swap / Solaris

Now remove all the partitions listed (you didn’t skip the backup step, right…).

Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.


Then create new partitions make sure specify the size you want. The calculation is as follows:

Let’s say we want two partitions.

  1. Primary partition 13GB
  2. Swap partition remainder of the disk.

For the start value we use the start value that fdisk will print when pressing n, p. The number we have to add to this to get a 13GB partition is as follows. The first partition must be a primary partition. The following can be extended (if you need more than 3 other partitions) or more primary ones. The sector size is printed by fdisk (see above):

Diff = PartitionSizeInGB * 1024 * 1024 *1024 / SectorSizeInBytes

For instance

Diff = 13 * 1024 * 1024 * 1024 / 512 = 27262976

Therefore the last sector is:

LastSector = 27262976 + 2048 = 27265024


Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-29360127, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-29360127, default 29360127): 27265024

Created a new partition 1 of type 'Linux' and of size 13 GiB.

And lastly create a swap partition using the rest of the disk i.e. just go with the default values.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (27265025-29360127, default 27267072):
Last sector, +sectors or +size{K,M,G,T,P} (27267072-29360127, default 29360127):

Created a new partition 2 of type 'Linux' and of size 1022 MiB.

We now have to change the type to linux swap which has code 82.

Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help):

Write the results to disk:


Copy the backup back to the disk

cp /backup/xvdb1backup1 /xvdb1
#etc for all partitions

Shutdown util

shutdown -h now

In the host remove the disk again from util.cfg Now start the tbe

xl create tbe.cfg

Logon to tbe

df -k 

Still no luck huh? 😉

su - root
resize2fs /dev/<the device you extended>
df -k

Should now list the new size.

If you configured a swap parition check it is working:

swapon -s

If not you have to add the swap partition. Let’s say it was on xvdb2 when you configured it in util then it probably is in xvda2 (make sure you get it right i.e. check if it exists in /dev isn’t mounted df -k). In the following I assume it is on /dev/xvda2

Now for ubuntu execute the following:

blkid /dev/xvda2

Copy the UUID and open /etc/fstab. Copy the swap line and change it as follows (use the printed UUID instead of the one below).

UUID=7340ea5f-3325-466a-8e2f-52c1cf15ae94 / ext4 errors=remount-ro 0 1
# swap was on /dev/xvda5 during installation
#UUID=75ecaea3-ff0d-4f90-b5e2-ea757bde5399 none swap sw 0 0
UUID=582dae7a-5d2a-4b4c-9685-993fc8454200 /dev/xvda2 swap sw 0 0

shutdown -r now

Log back in:

swapon -s
Filename                                Type            Size    Used    Priority
/dev/xvda2                              partition       1046524 0       -1

If satisfied don’t forget to start Util and cleanup the backups created.

Leave a Reply