57

I have two partitions, one of them has a file system and the other is unallocated.

How to merge two partitions?

0

2 Answers 2

51

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

3
  • thanx, using gpart was very good. in my problem - itwas a linux-swap between /dev/sda1 and /dev/sda3. /sda3 was empty. i format both linux swap and dev3, then extend dev1 (100%of hdd - 300mb for swap). and add(format left pice of hdd) linux swap. Mar 6, 2017 at 10:43
  • The instructions for merging a partition that has data on it with another (Unallocated) that does not have data on it does not work for me in Ubuntu 17.04 using the current version of GParted when my unallocated space is before the partition that has the data on it. Could you clarify your post to explain how to resolve this? Sep 21, 2017 at 2:38
  • 2
    Am I right in assuming that this solution only works if the two partitions are next to each other? What should I do if they aren't?
    – Nearoo
    Feb 12, 2019 at 21:31
32

If the unused partition comes 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 at the same location (here, 63) and ends where sda2 starts (here, 8016434). And double-check that the end of sda1 is immediately before the start of sda2 (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. :)

5
  • 1
    I think you have a typo: ends where sda2 ended (here, 8016434) should be ends where sda2 ended (here, 1953520064) Apr 1, 2014 at 0:06
  • I killed my swap (and maybe all other partitions) doing this. But this works. I certainly resized sda1 to all space p.s. OS installed on it works fine
    – vladkras
    Mar 19, 2016 at 6:38
  • 2
    What if it is before?
    – Daksh Shah
    Feb 19, 2018 at 14:35
  • Yes, I partitioned sda1 and I am out of luck. Thanks. :(
    – ssi-anik
    Aug 25, 2018 at 20:18
  • Attention to use fdisk not fsck
    – testing_22
    Jun 11, 2022 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .