Rebuild partition table on external HDD – not writable

data-recoveryguidhfs+partition

I have an external hard drive formatted with 3 HFS+ partitions. To cut a long story short I had it connected to a Windows PC and accidentally started formatting the drive to NTFS. As soon as I realised what was happening I disconnected the drive, but the partition table is corrupted. I used TestDisk to gather info about the partitions, and tried to use pDisk to rebuild the partition table, but when I try to do that, I get the message that the partition table is not writable, the device is busy. Anyone know how I can rewrite the partition table?

Update:

The TestDisk results are like this (for settings on Apple Partition Map – I don't know if this is the correct settings, but it's the only one which gives 3 distinct partitions):

Start:     262208 End:  419692607 Size: 419430400
Start:  419954752 End: 1258815551 Size: 838860800
Start: 1259077696 End: 1953525151 Size: 694447456

Does that look OK?

Best Answer

OS X uses three different types of valid partition tables. Only one of those is written to a particular disk:

The default partition table on OS X is the GUID partition table.

Depending on the previously used partition table on your disk you have to use different tools to create/change/write it:

  • gpt for GUID partition table
  • pdisk for Apple Partition Table
  • fdisk for MBR

After additional informations given by the OP (testdisk results) and checking it in a virtual machine it's highly probable that the disk was formatted with an Apple Partition Map. Consequently use pdisk to rebuild the partition map.

pdisk:

A typical disk formatted with an Apple Partition Table looks like this:

Driver Descriptor Map: Block 0              Size:      1 Block
Apple Partition Table: Block 1 - 63         Size:     63 Blocks
Apple_Free:            Block 64 - 262207    Size: 262144 Blocks
1st partition:         Block 262208 - 
Apple_Free:                                 Size: 262144 Blocks
...
Apple_Free:                                 Size: 262144 Blocks
last partition
Apple_Free:                                 Size:     16 Blocks

To recreate the partition map do the following:

  • attach the external drive
  • detach any other external drive
  • open Terminal and enter diskutil list to get the DiskIdentifier (in the step below I assume your DiskIdentifier is disk1
  • enter diskutil unmountDisk /dev/disk1
  • enter sudo pdisk /dev/rdisk1 You will get the following output:

    Edit /dev/rdisk1 -
    Command (? for help):

  • Now enter c then enter the start block, the size of the first partition and the name. You have to hit the enter key after every input. Repeat this for the next two partitions. At the end it should look like this:

    Command (? for help): c
    First block: 262208
    Length in blocks: 419430400
    Name of partition: part1
    Command (? for help): c
    First block: 419954752
    Length in blocks: 838860800
    Name of partition: part2
    Command (? for help): c
    First block: 1259077696
    Length in blocks: 694447456
    Name of partition: part3
    Command (? for help):

  • At the last prompt enter w then y to write the changes to disk and finally qto quit pdisk:

    Command (? for help): w
    Writing the map destroys what was there before. Is that okay? [n/y]: y
    The partition table has been altered!

    Command (? for help):

  • The missing volumes should mount automatically. Otherwise enter diskutil mountDisk /dev/disk1. Enter q at the last prompt to quit pdisk, then quit Terminal, open Disk Utility and check the recovered volumes for errors.


gpt:

In the example below I assume the DiskIdentifier of your external disk is disk1 (check this with diskutil list)

First you have to unmount the external disk:

diskutil umountDisk disk1

Remove the current MBR and create a GPT with gpt:

sudo gpt create -f /dev/disk1

First rebuild the EFI entry with:

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

The EFI partition is a fixed sized partition near the beginning of every GUID partitioned disk (using OS X partitioning tools)

Then add the JHFS+ partition entries with the TestDisk findings:

sudo gpt add -b StartBlock -i IndexNumber -s SizeOfVolume -t 48465300-0000-11AA-AA11-00306543ECAC disk1

Example for the first OS X partition:

sudo gpt add -b 409640 -i 2 -s SizeOfHFSVolume1 -t 48465300-0000-11AA-AA11-00306543ECAC disk1

After adding a partition with gpt you may have to unmount disk1 with diskutil umountDisk disk1 again if your get a "resource is busy" error adding additional partitions.

Increase the index number by 1 for every new partition.

Listed below are answers to similar questions (partly covering other file systems like ExFAT)

If you run into problems, leave a comment to the answer with @klanomath.