How to fix overlapped partitions in the MBR table?

My Ubuntu installed on PC is stucked at boot screen. So, I tried to install anew, but partition table is shown empty at installation wizard. I learned that my partitions overlapped.

I found this link to fix problem http://gparted.org/h2-fix-msdos-pt.php. But it doesn't seem to make sense for my fdisk output.

$ sudo fdisk -l -u /dev/sda
Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 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: 0x49fec944

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *      616448   257441624   128412588+   7  HPFS/NTFS/exFAT
/dev/sda2       452753408   484210687    15728640    7  HPFS/NTFS/exFAT
/dev/sda3       484210688   488394751     2092032    c  W95 FAT32 (LBA)
/dev/sda4       257433598   452753407    97659905    5  Extended
/dev/sda5       257433600   452753407    97659904   83  Linux
Partition table entries are not in disk order

Could you help me?


Solution 1:

Fixing the partition table with sfdisk:

  1. Boot with live Ubuntu disk;

  2. Confirm the problem on your disk device, here /dev/sda with parted e.g.

    sudo parted /dev/sda unit s print
    

    which should show:

    Error: Can't have overlapping partitions.
    
  3. Partition details can be checked with:

    sudo fdisk -l -u /dev/sda
    

    which, for you, according to your post is:

    Disk /dev/sda: 250.1 GB, 250059350016 bytes
    255 heads, 63 sectors/track, 30401 cylinders, total 488397168 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: 0x49fec944
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *      616448   257441624   128412588+   7  HPFS/NTFS/exFAT
    /dev/sda2       452753408   484210687    15728640    7  HPFS/NTFS/exFAT
    /dev/sda3       484210688   488394751     2092032    c  W95 FAT32 (LBA)
    /dev/sda4       257433598   452753407    97659905    5  Extended
    /dev/sda5       257433600   452753407    97659904   83  Linux
    
  4. Checking the overlaps: You can see that your end of primary partition /dev/sda1 overlaps the beginning of extended partition /dev/sda4.

    sda1end = 257441624

    sda4start = 257433598

  5. As suggested in the documentation that - "In cases where we do not know if the starting or ending sector is the problem, we assume that the starting sector of each partition is correct, and that the ending sector might be in error", we assume that the starting sector of extended partition sda4 is correct. Hence we will be looking to change the end sector of primary partition sda1.

    Calculations:

    sda1newEnd = sda4start - 1 = 257433598 - 1 = 257433597

    sda1newSize = sda1newEnd - sda1start + 1 = 257433597 - 616448 + 1 = 256817150

  6. Dumping a copy of the partition table in an file using the sfdisk command:

    sudo sfdisk -d /dev/sda should dump the partition table details. This can be dumped to a file, which after necessary corrections are made, can be fed back to sfdisk. [To OP: Please edit your Question and include the output of sudo sfdisk -d /dev/sda]

    Dump a copy of partition table with:

    sudo sfdisk -d /dev/sda > sda-backup.txt
    

    which for you would look something like this:

    # partition table of /dev/sda
    unit: sectors
    
    /dev/sda1 : start=   616448, size=256825177, Id= 7, bootable
    /dev/sda2 : start=452753408, size= 31457279, Id= 7
    /dev/sda3 : start=484210688, size=  4184064, Id= c
    /dev/sda4 : start=257433598, size=195319810, Id= 5
    /dev/sda5 : start=257433600, size=195319808, Id=83
    
  7. Open the file with root privilege, created in the previous step, using text editor of your choice. In the example I'll use nano.

    sudo nano sda-backup.txt
    

    (sda-backup.txt assuming the file is in the current directory, else repalce it with the file's absolute path.)

    Change the old size of sda1 (256825177) to the corrected size (256817150) so that your new partition table dump would look something like:

    # partition table of /dev/sda
    unit: sectors
    
    /dev/sda1 : start=   616448, size=256817150, Id= 7, bootable
    /dev/sda2 : start=452753408, size= 31457279, Id= 7
    /dev/sda3 : start=484210688, size=  4184064, Id= c
    /dev/sda4 : start=257433598, size=195319810, Id= 5
    /dev/sda5 : start=257433600, size=195319808, Id=83
    

    Save the file (Ctrl+O for nano) and close the editor (Ctrl+X for nano).

  8. Feeding back the corrected partition details to the partition table using the sfdisk command:

    sudo sfdisk /dev/sda < sda-backup.txt
    
  9. Confirm if the problem is resolved by running parted on your disk device:

    sudo parted /dev/sda unit s print
    
  10. If step 9 confirm that the partition table is fixed, you can then use GParted or other partition editors with the device.


The GParted documentition also suggests an alternative method, using testdisk to scan the disk device to rebuild the partition table. The testdisk application is included on GParted Live. So if you are not comfortable with the command-line way, you can try the alternative.

source