How to increase the disk size of a live Ubuntu VM

Live resize Ubuntu VM

This article describes how I successfully increased the size of the disk used by a Ubuntu 14.04 server running on a ESXI 5.5 VM. I don't know if decreasing the disk size is possible. Anyone?

I said it's a live resize But you need to stop the VM to resize it's disk in VMWare, and reboot at least once after that. Also, I did not use lvm for this VM.

Expand the VM disk

First, you need to resize the VM disk from the VM settings in your vSphere Client. I added 100 GB to the disk. If you have more than one disk, this also applies. Then, restart the VM.

Expand partitions

Now, the tricky part happens in the OS. To be able to resize the disk you need to first recreate the table partitioning. It's a bit scary and to be honnest I wasn't very confident during the process. So let me be clear here. You need to backup your system before you do this! If you do it right you should not lose anything but as Forrest Gump says... sometimes shit happens!

Yee Bee Warned!

Now, we will use fdisk as root to remove all partitions and recreate a new partition table that will accomodate the new disk space. There is one little detail that we need to look after. For a given partition, if we want to keep it's data, we can increase the partition BUT we must keep the same Start address. Only the End address can be moved further so that we can keep the data.

So for example I have this /dev/sda disk:

$ sudo fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   838862848   419430400+  83  Linux
/dev/sda2       838862849   943718399    52427775+   5  Extended
/dev/sda5       838862912   943718399    52427744   82  Linux swap / Solaris

Command (m for help): q

$

Notice I have 3 partitions, One primary which is a linux partition, then an extended partition in which there is a Linux swap partition.

I wanted to increase /dev/sda1 so that's the partition that needs to keep its start address constant. The Start for this partition is 2048! This is the only thing we need to note down. I really dont care about the 2 other partitions, and by the way only the /dev/sda5 is really used as swap. So if I want to increase /dev/sda1 I need to remove the other partitions, then increase the size of partition 1 and then recreate the extended partition and the swap partition in the space left.

But in fact fdisk does not allow to resize a partition, so we will also remove partition 1 and recreate it not without having first noted the start offset of the partition.

Stop the swap

$ sudo swapoff /dev/sda5

Remove extended and swap partitions

$ sudo fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   838862848   419430400+  83  Linux
/dev/sda2       838862849   943718399    52427775+   5  Extended
/dev/sda5       838862912   943718399    52427744   82  Linux swap / Solaris

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

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   838862848   419430400+  83  Linux

Command (m for help):

Remove the primary partition

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

At this point there are no more partitions. But if you don't write the partition table you can safely exit (command q) if you made a mistake or if you changed your mind ;).

Recreate the primary partition

Now we will recreate the primary partition stating at 2048!

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-943718399, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-943718399, default 943718399): +400G      

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   838862847   419430400   83  Linux

Command (m for help):

Recreate the extended partition and the swap partition

First create the extended partition:

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (838862848-943718399, default 838862848):
Using default value 838862848
Last sector, +sectors or +size{K,M,G} (838862848-943718399, default 943718399):
Using default value 943718399

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   838862847   419430400   83  Linux
/dev/sda2       838862848   943718399    52427776    5  Extended

Then the swap (logical) partition within the extended partition:

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (838864896-943718399, default 838864896): 
Using default value 838864896
Last sector, +sectors or +size{K,M,G} (838864896-943718399, default 943718399): 
Using default value 943718399

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   838862847   419430400   83  Linux
/dev/sda2       838862848   943718399    52427776    5  Extended
/dev/sda5       838864896   943718399    52426752   83  Linux

Change it's type to be a swap partition:

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

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   838862847   419430400   83  Linux
/dev/sda2       838862848   943718399    52427776    5  Extended
/dev/sda5       838864896   943718399    52426752   82  Linux swap / Solaris

Command (m for help):

Add bootable flag

Command (m for help): a
Partition number (1-5): 1

Command (m for help): p

Disk /dev/sda: 483.2 GB, 483183820800 bytes
255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f2065

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   838862847   419430400   83  Linux
/dev/sda2       838862848   943718399    52427776    5  Extended
/dev/sda5       838864896   943718399    52426752   82  Linux swap / Solaris

Command (m for help):

Now everything is set right I think. So type the command "w" to write the table to the disk and quit fdisk (command q), then reboot.

Resize the filesystem

If your VM did reboot (hum) great! now let's resize the filesystem:

$ sudo resize2fs /dev/sda1

Recreate the swap

$ sudo swapon /dev/sda5

And update /etc/fstab with the UUID given back by the swapon command.