11

I recently had trouble with the MBR/partition table on my laptop. I managed to rebuild the partition table using testdisk, and install GRUB to get it booting properly again (I'm using a dual-boot with Windows 7). However, I can no longer run gparted properly as I get the error Can't have a partition outside the disk!.

fdisk -l output looks like this:

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x188f12a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         154     1228800    7  HPFS/NTFS/exFAT
/dev/sda2             154       13446   106775171    7  HPFS/NTFS/exFAT
/dev/sda3           13447       28745   122880000   83  Linux
/dev/sda4           28745       30402    13317664+   f  W95 Ext'd (LBA)
/dev/sda5           28745       29127     3069944   82  Linux swap / Solaris
/dev/sda6           29127       30402    10240000    7  HPFS/NTFS/exFAT

So the disk has 30401 cylinders, but sda6 ends at cylinder 30402; presumably that's where the problem is.

When I run testdisk it has the 6th partition ending at cylinder 30401, but writing it to the partition table does not make any difference.

Is there an easy way to fix this?

I've read elsewhere that I could fix this by manually editing the partition table, but I'd like really specific instructions as I don't really know much about this area!

jwaddell
  • 253
  • 1
  • 2
  • 8
  • 1
    Are you able to run `gparted /dev/sda6`? Can you run the partition editor off a Live CD? Otherwise manual may be the way to go... – Rasman Jun 14 '11 at 14:46
  • The same thing happens when using a Live CD. I'll try `gparted /dev/sda6` tonight. – jwaddell Jun 14 '11 at 22:45

2 Answers2

13

Use fdisk. Put it into sector mode with the u command, then p to print the table, d to delete the partition, and then n to recreate it. When you recreate it, use the same starting sector, but an ending sector that actually fits within the disk. When you are done and have double checked ( p again ), save and quit with w.

psusi
  • 37,033
  • 2
  • 68
  • 106
  • 1
    It turned out to be the extended partition that was the problem, but `fdisk` worked well to recreate it and the logical partitions inside it. `gparted` now opens correctly. – jwaddell Jun 18 '11 at 10:51
  • Wouldn't this cause the data to be lost? A warning, perhaps... – ADTC Apr 11 '17 at 18:46
  • @ADTC, no; the whole point is to recover data. – psusi Apr 12 '17 at 18:20
  • Excuse me, but how will you recover data by deleting a partition and creating a new partition? The new one will be blank. Yes, you can recover data by using deep scan and recovery tools but you made no mention of that. In any case, the question was about fixing partition table, in which case, yes deleting and creating will help fix it. But your data in the existing partition will be lost. My point is to warn in your answer that that one should backup their data before following the instructions. – ADTC Apr 15 '17 at 11:18
  • 1
    @ADTC, the new one will not be blank because deleting and recreating a partition does not touch the data in the partition; it only sets the boundaries in the partition table. By deleting and recreating the partition with the same start but a different end, you are effectively fixing the size of the partition. – psusi Apr 17 '17 at 15:00
  • 1
    Yes, it's important to understand what a partition is - it's simply a bit of data on the hard disk that says 'start here, end here, call it number 4..'. The partition table is just a bit of data holding all those little mappings. The data is still there, you just need to tell the operating system *where* the data is. – John Hunt Apr 19 '17 at 15:12
  • Huh, I don't know what voodoo this is. How does the OS know where the data is? I have never heard that deleting a partition will not do anything to your data. Data loss is the first warning you always get before deleting one. Yeah I know the data is not physically erased from the disk but deleting a partition means you're also deleting the file system used to find the data.. a new partition doesn't have any file system and when you format that partition with a file system, it's a blank partition. Besides, moving the start sector has absolutely no guarantee that your data would remain readable. – ADTC Apr 19 '17 at 16:08
  • @ADTC, the partition table says where the partitions are. Changing the partition table has no effect on the data on the rest of the disk. Making the partition table point to that data wrongly can render it inaccessible... fixing the partition table to point to it correct renders it accessible again. The OP has an incorrect partition table, and the instructions are to fix it. The file system is simply the data inside the partition that describes where the files are within the partition. The filesystem was there all along, and never re-formatted; only the size of the partition was changed. – psusi Apr 20 '17 at 01:24
1

Download and install Boot Repair, an automated recovery tool that usually solves these kind of problems.

Tom Brossman
  • 12,953
  • 11
  • 66
  • 134
Julien Chau
  • 1,174
  • 1
  • 11
  • 23
  • will this work for non-boot partitions? – Sridhar Sarnobat Sep 06 '20 at 23:33
  • 1
    If you will repair a partition table try to do it with gparted. Gparted if not installed ,is in the software package of Ubuntu .You can also download a live cd. Boot repair will repair boot problems. So if there is a problem with partition table fist repair this and then repair the boot problem if necessary (boot problem) with Boot Repair. – Julien Chau Sep 08 '20 at 07:50