Extending Ubuntu Partition size with the unallocated space (swap is between these two)

My Ubuntu 12.04 is set up on virtualbox, the host system is Windows 7.

When setting up my system, I created 8GB VDI hard drive. In a short time I run out of free space, so I increased the size of the VDI to ~50GB via Windows.

Now I'm trying to increase the size of my Ubuntu partition with the unallocated space gained by increasing the size of the virtual hdd.

Therefore, I've booted Ubuntu with the LiveCD and started up GParted. The problem is that I cannot increase the size of the desired partition, probably because swap is just between it and the unallocated space.

My question is - how can I move my allocated space to the neighborhood of Ubuntu partition and then enlarge it?

Here is how it looks like in GParted:

enter image description here

I'm also pasting the output of fdisk -l:

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x000c35eb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    12582911     6290432   83  Linux
/dev/sda2        12584958    16775167     2095105    5  Extended
/dev/sda5        12584960    16775167     2095104   82  Linux swap / Solaris

Solution 1:

It is also possible to do without removing the swap partition.

  1. Extend partition "Extended" (the wrapper for the swap partition) so it takes all the unallocated space.
  2. Move the swap partition to the end of the space (make sure the size stays the same)
  3. Shrink "Extended" to the size of swap partition.
  4. Expand your main partition to fill the new space between.

To do these steps you will need to make sure your partitions are unmounted (found under partition in the menu bar). Like what Mitch said it is a good idea to back up first.

Solution 2:

I can't stress this enough:

It is highly recommended to backup any important files before doing resize/move/extend operations.

The way to do it, is by deleting the swap partition, extend the partition that you want, and then re-create the swap partition.

Swap partition can only be deleted after swap is turned off:

SWAP off: sudo /sbin/swapoff -a SWAP on: sudo /sbin/swapon -a

The steps:

  1. Boot from the Ubuntu LiveCD
  2. Open terminal, and turn swap off
  3. Install GParted
  4. Extend the partiton that you want, but leave space for swap
  5. commit the changes
  6. create swap partition
  7. turn swap on

Note: If your swap is not your primary hard drive, then see SwapFaq on how to activate the swap partition

It is highly recommended to backup any important files before doing resize/move/extend operations.