HFS+ invalid number of allocation blocks

Solution 1:

In my opinion "TestDisk" hosed your GPT.

Please compare the TestDisk result with my disks. The disks in my example are equally sized, disk0 contains a CoreStorage partition and disk2 an old-style JHFS+ partition. I'm using two separate disks because it's unknown (at least to me) which formatting type (CS or JHFS+) was used originally.

The PMBR/GPT and the first three partitions (EFI/Macintosh HD/Recovery HD) should look like this, if you had a CoreStorage partition previously:

    root# gpt -r -vv show disk0
gpt show: disk0: mediasize=68719476736; sectorsize=512; blocks=134217728
gpt show: disk0: PMBR at sector 0
gpt show: disk0: Pri GPT at sector 1
gpt show: disk0: Sec GPT at sector 134217727
      start       size  index  contents
          0          1         PMBR
          1          1         Pri GPT header
          2         32         Pri GPT table
         34          6         
         40     409600      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     409640  132538512      2  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  132948152    1269536      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC

or like this, if you had a classical JHFS+ volume previously:

root# gpt -r -vv show disk2
gpt show: disk2: mediasize=68719476736; sectorsize=512; blocks=134217728
gpt show: disk2: PMBR at sector 0
gpt show: disk2: Pri GPT at sector 1
gpt show: disk2: Sec GPT at sector 134217727
      start       size  index  contents
          0          1         PMBR
          1          1         Pri GPT header
          2         32         Pri GPT table
         34          6         
         40     409600      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     409640  132538512      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
  132948152    1269536      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC

(Please consider that the mediasize, the blocks, the sector of the secondary GPT, the size of the 2nd volume and the start block of the 3rd volume are different to yours, because i use smaller example disks here.)

Your problem should be solved by rewriting the GPT once more.

Preparation:

Install a full vanilla system (Mavericks or Yosemite should work) on a thumb drive (or an external drive). A recovery system will not work. Boot to the thumb drive, download and install wxHexEditor. Enable the root user and log-in as root.

Hint: While working with wxHexEditor don't use copy and paste. Enter everything manually! You might accidentally write directly to your disk.

JHFS+ or CoreStorage partition?

First you have to determine, if you had a JHFS+ or CoreStorage partition at index number 2.

Open the Calculator. Open wxHexEditor. Check that you work in read-only mode ("Options" -> "File mode" -> "Read only"). In the menubar go to "Devices" -> "Open disk device" -> choose the appropriate diskNumber. Probably it's disk0. The disk should have further partitions (disk0s1 - disk0s5). Please try to arrange the wxHexEditor window like in the examples below with straight red lines.

Then hit the "Go to offset"-button (marked with the green circle) and enter 409640 exactly like in the picture below. Sometimes you have do that twice to jump to the correct sector. Re-check the correct sector by entering the offset (marked red) in the Calculator and divide it through 512.

The first 3 sectors of a CoreStorage partition look like this:

cs

The first 3 sectors of a JHFS+ look like this:

jhfs+

If you get a fundamentally different picture stop here.

Where does the EFI partition start?

Hit the "Go to offset"-button and enter 40 exactly like in the picture below:

efi

If you see the same entries like in the picture above (XEBSD 4.4...EFI...FAT32) this is the start sector of your EFI-Partition. If there are only zeros this could be valid too.

Where does the Recovery HD partition start?

That's probably the most difficult part because you have to find a string which is not very specific. Jump almost to the end of your 2nd partition (in your case ~400 MB/781250 sectors less than 1164570456 = 1163789206)

Then enter "HFSJ" like in the picture below, search for this string two times and make notes of the different offsets:

rhd

You may have two really different results depending on the partition type:

  1. Calculate the sector number of the first finding. In my example (see picture above) it's 68069452800/512=132948150. Continue searching and calculate the sector of the second finding. In my case it was 68069454848/512=132948154 (no picture).
    The difference between the two finding is 4 blocks (=2 KB).

    This is typical for the boundary between a JHFS+ partition and the Recovery HD. The Recovery HD starts then at the sector of the second finding - 2 (in my example 132948154-2=132948152).

  2. Calculate the sector number of the first finding. In my example it was 67733904384/512=132292782 (no picture). Continue searching and calculate the sector of the second finding. In my case it was 68069454848/512=132948154 (no picture). The difference between the two findings is 655372 (~336 MB)

    This is typical for the boundary between a CoreStorage partition and the Recovery HD. The Recovery HD starts then at the sector of the second finding - 2 (in my example 132948154-2=132948152).

With those results you should be able to restore your GPT properly. Quit wxHexEditor. If you are asked to save changes don't save them!.

Rebuild a proper GPT

Here i assume the identifier of your main disk is disk0. First you have to unmount your main disk:

diskutil umountDisk disk0

Check the partition layout then remove the first three partitions:

gpt -r -vv show /dev/disk0

gpt remove -i 3 disk0
gpt remove -i 2 disk0
gpt remove -i 1 disk0

Since the EFI and the Recovery HD usually have fixed sizes we can calculate the start and end block of your main volume.

First we rebuild the EFI with:

gpt add -b 40 -i 1 -s 409600 -t C12A7328-F81F-11D2-BA4B-00A0C93EC93B disk0

Then we calculate the size of the main volume: the start block is 409640. The end block has been found in the section "Where does the Recovery HD partition start?": 1 less than the start block of the Recovery HD. The size is then StartBlockOfRecoveryHD-409640.

If you've found a classical JHFS+ earlier the following command should fix partition 2:

gpt add -b 409640 -i 2 -s StartBlockOfRecoveryHD-409640 -t 48465300-0000-11AA-AA11-00306543ECAC disk0

If you've found a CoreStorage partition earlier the following command should fix partition 2:

gpt add -b 409640 -i 2 -s StartBlockOfRecoveryHD-409640 -t 53746F72-6167-11AA-AA11-00306543ECAC disk0

To rebuild the Recovery HD enter:

gpt add -b StartBlockOfRecoveryHD -i 3 -s 1269536 -t 426F6F74-0000-11AA-AA11-00306543ECAC disk0

Remount disk0 with:

diskutil mountDisk disk0

Quit Terminal, start Disk Utility and check your main volume (probably Macintosh HD) for errors and try to repair them if necessary.
If you've found a CoreStorage partition earlier, you might have to reboot to your thumb drive before repairing the volumes with Disk Utility, because the CoreStorage logical volume might not be recognized/mounted properly. In your setup - 1 main disk and the thumb drive - the logical volume should be disk2.

I hope this solves your problems.

If you run into problems (e.g you can't find the proper starting sector of your Recovery HD), have doubts or questions immediately stop and contact me with a comment @klanomath!