How to merge partitions?

Solution 1:

You can use gpartedusc_logo for this. Also installable by sudo apt-get update && sudo apt-get install gparted

To make one big fresh partition (deletes all data on your filesystem):

  • First you'll have to make sure that both the partitions - unallocated space and the other partition to merge are either both logical partitions or both primary partitions. One logical and one primary will not work.

  • After checking and confirming the first step, delete the partition holding a filesystem.

  • Third, the unallocated space is now bigger since you have deleted a partition.

  • Create a partition of required size.

To increase the size of the partition holding data: (Although this is mostly safe, it's always wise to make a backup of your data!)

  • First you'll have to make sure that both the partitions - unallocated space and the other partition to merge are either both logical partitions or both primary partitions. One logical and one primary will not work.

  • Second, right-click on the partition in question and click resize. Fill in the size as required.

  • Click resize/move and then click Apply all operations

screenshot_gparted

Solution 2:

If the unused partition is physically after the partition with the filesystem, you can resize the partition on the disk, reboot, and then grow the filesystem (if it was a growable filesystem like ext3, ext4, etc).

For example, if you have /dev/sda1 as the filesystem, and /dev/sda2 as the unused partition, check the partitions with fdisk /dev/sda:

# fdisk /dev/sda
...    
Command (m for help): p
...
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63     8016434     4008186   83  Linux
/dev/sda2         8016435  1953520064   972751815   83  Linux

You need to make sure that your new sda1 starts in the same location (here, 63) and ends where sda2 starts (here, 8016434). And double-check that where sda1 ends is immediately before where sda2 starts (here, 8016434 is immediately followed by 8016435) just to be sure.

Then delete the unused partition, and the filesystem partition:

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

And finally, recreate the filesystem partition:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4, default 1): 1
First sector (63-1953520064, default: 63): 63
...
Last sector, +sectors or +size{K,M,G} (63-1953520064, default 1953520064): 1953520064

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 83

And make sure you've got what you're expecting:

Command (m for help): p
...
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63  1953520064   976760001   83  Linux

Finally, save it:

Command (m for help): w

If any partitions were mounted on the disk, you'll have to reboot first, and then you can grow the filesystem:

# resize2fs /dev/sda1

Be careful and good luck. Always back up your data first. :)