macOS – Fixing Partition Startup Volume Issues

apfsdiskutilmacospartitionrecovery

I attempted to fix an issue I encountered yesterday my MacBook Pro (13-inch, Mid 2012) following instructions in the top answer of this thread. I'm afraid I may have inadvertently destroyed my data by rewriting the partition table to the values indicated in this answer instead of the proper values.

The affected disk and volume in my problem is disk2, volume 2. Volume 3 was previously a Bootcamp partition that I erased and installed macOS to before remembering I had a back up drive to put in my computer (disk1). Here is the results of the diskutil list command I ran at the beginning of this endeavor:

Trevors-MBP:~ trevorraney$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         499.9 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +499.9 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            487.3 GB   disk1s1
   2:                APFS Volume Preboot                 23.6 MB    disk1s2
   3:                APFS Volume Recovery                511.0 MB   disk1s3
   4:                APFS Volume VM                      3.2 GB     disk1s4

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2: FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF               439.9 GB   disk2s2
   3:                 Apple_APFS Container disk3         59.9 GB    disk2s3

/dev/disk3 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +59.9 GB    disk3
                                 Physical Store disk2s3
   1:                APFS Volume Untitled                12.9 GB    disk3s1
   2:                APFS Volume Preboot                 24.1 MB    disk3s2
   3:                APFS Volume Recovery                509.5 MB   disk3s3
   4:                APFS Volume VM                      2.1 GB     disk3s4

Below is what I entered in my attempt to fix this drive. Specifically the command sudo gpt add -i 2 -b 409640 -s 372637568 -t 7C3457EF-0000-11AA-AA11-00306543ECAC disk2. Notice that disk2s2 is now 190 gigabytes, and the creation of an erroneous disk3.

Trevors-MBP:~ trevorraney$ gpt -r show /dev/disk2
gpt show: unable to open device '/dev/disk2': Permission denied
Trevors-MBP:~ trevorraney$ sudo gpt -r show /dev/disk2
Password:
      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  859181016      2  GPT part - FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF
  859590656  116920320      3  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
  976510976     262111         
  976773087         32         Sec GPT table
  976773119          1         Sec GPT header
Trevors-MBP:~ trevorraney$ diskutil umountDisk disk2
Unmount of all volumes on disk2 was successful
Trevors-MBP:~ trevorraney$ gpt remove -i 2 disk2
gpt remove: unable to open device 'disk2': Permission denied
Trevors-MBP:~ trevorraney$ sudo gpt remove -i 2 disk2
disk2s2 removed
Trevors-MBP:~ trevorraney$ diskutil umountDisk disk2
Unmount of all volumes on disk2 was successful
Trevors-MBP:~ trevorraney$ gpt add -i 2 -b 409640 -s 372637568 -t 7C3457EF-0000-11AA-AA11-00306543ECAC disk2
gpt add: unable to open device 'disk2': Permission denied
Trevors-MBP:~ trevorraney$ sudo gpt add -i 2 -b 409640 -s 372637568 -t 7C3457EF-0000-11AA-AA11-00306543ECAC disk2
disk2s2 added
Trevors-MBP:~ trevorraney$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         499.9 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +499.9 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            487.3 GB   disk1s1
   2:                APFS Volume Preboot                 23.6 MB    disk1s2
   3:                APFS Volume Recovery                511.0 MB   disk1s3
   4:                APFS Volume VM                      3.2 GB     disk1s4

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                 Apple_APFS Container disk3         190.8 GB   disk2s2
   3:                 Apple_APFS Container disk4         59.9 GB    disk2s3

/dev/disk3 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +ERROR      disk3
                                 Physical Store disk2s2

/dev/disk4 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +59.9 GB    disk4
                                 Physical Store disk2s3
   1:                APFS Volume Untitled                12.9 GB    disk4s1
   2:                APFS Volume Preboot                 24.1 MB    disk4s2
   3:                APFS Volume Recovery                509.5 MB   disk4s3
   4:                APFS Volume VM                      2.1 GB     disk4s4

Lastly, here is the list of errors I got after running diskutil verifyVolume on disk2s2.

Trevors-MBP:~ trevorraney$ diskutil verifyDisk disk2
Started partition map verification on disk2
Checking prerequisites
Checking the partition list
Checking the partition map size
Checking for an EFI system partition
Checking the EFI system partition's size
Checking the EFI system partition's file system
Checking the EFI system partition's folder content
Checking all HFS data partition loader spaces
Checking booter partitions
Checking Core Storage Physical Volume partitions
The partition map appears to be OK
Finished partition map verification on disk2
Trevors-MBP:~ trevorraney$ diskutil verifyVolume disk2s2
Started file system verification on disk2s2
Verifying storage system
Performing fsck_apfs -n -x /dev/disk2s2
warning: nx_block_count is 107397627, while device block count is 46579696
Checking the container superblock
warning: dev_read(60086901, 1): Device not configured
warning: dev_read(60086902, 1): Device not configured
warning: dev_read(60086903, 1): Device not configured
warning: dev_read(60086904, 1): Device not configured
warning: dev_read(60086905, 1): Device not configured
warning: dev_read(60086906, 1): Device not configured
warning: dev_read(60086907, 1): Device not configured
warning: dev_read(60086908, 1): Device not configured
warning: dev_read(60086909, 1): Device not configured
warning: dev_read(60086910, 1): Device not configured
warning: dev_read(60086911, 1): Device not configured
warning: dev_read(60086912, 1): Device not configured
warning: dev_read(60086913, 1): Device not configured
warning: dev_read(60086914, 1): Device not configured
warning: dev_read(60086915, 1): Device not configured
warning: dev_read(60086916, 1): Device not configured
warning: dev_read(60086917, 1): Device not configured
warning: dev_read(60086918, 1): Device not configured
warning: dev_read(60086919, 1): Device not configured
warning: dev_read(60086920, 1): Device not configured
warning: dev_read(60086921, 1): Device not configured
warning: dev_read(60086922, 1): Device not configured
warning: dev_read(60086923, 1): Device not configured
warning: dev_read(60086924, 1): Device not configured
warning: dev_read(60086925, 1): Device not configured
warning: dev_read(60086926, 1): Device not configured
warning: dev_read(60086927, 1): Device not configured
warning: dev_read(60086928, 1): Device not configured
warning: dev_read(60086929, 1): Device not configured
warning: dev_read(60086930, 1): Device not configured
warning: dev_read(60086931, 1): Device not configured
warning: dev_read(60086932, 1): Device not configured
warning: dev_read(60086933, 1): Device not configured
warning: dev_read(60086934, 1): Device not configured
warning: dev_read(60086935, 1): Device not configured
warning: dev_read(60086936, 1): Device not configured
warning: dev_read(60086937, 1): Device not configured
warning: dev_read(60086938, 1): Device not configured
warning: dev_read(60086939, 1): Device not configured
warning: dev_read(60086940, 1): Device not configured
warning: dev_read(60086941, 1): Device not configured
warning: dev_read(60086942, 1): Device not configured
warning: dev_read(60086943, 1): Device not configured
warning: dev_read(60086944, 1): Device not configured
warning: dev_read(60086945, 1): Device not configured
warning: dev_read(60086946, 1): Device not configured
warning: dev_read(60086947, 1): Device not configured
warning: dev_read(60086948, 1): Device not configured
warning: dev_read(60086949, 1): Device not configured
too many warnings generated; suppressing subsequent ones
error: no valid checkpoint found
Most recent checkpoint is invalid
The volume /dev/disk2s2 could not be verified completely
Storage system check exit code is 8
Error: -69716: Storage system verify or repair failed
Underlying error: 8
Trevors-MBP:~ trevorraney$

I assume that the partition was in the APFS format, but could possibly have been HFS+. I am hoping that since I didn't attempt to repair the disk in Terminal that the damage I've done is reversible and I can be able to recover some or all of my data. Any help would be greatly appreciated.

Update: Posting output of sudo gpt -r show /dev/disk2 and sudo dd if=/dev/disk2s2 count=1 bs=512 | hexdump -Cv, as suggested by David Anderson.

    Last login: Sun Sep 30 11:56:40 on ttys000
    Trevors-MBP:~ trevorraney$ sudo gpt -r show /dev/disk2
    Password:
          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  372637568      2  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
      373047208  486543448         
      859590656  116920320      3  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
      976510976     262111         
      976773087         32         Sec GPT table
      976773119          1         Sec GPT header

 Trevors-MBP:~ trevorraney$ sudo dd if=/dev/disk2s2 count=1 bs=512 | hexdump -Cv
    1+0 records in
    1+0 records out
    512 bytes transferred in 0.001883 secs (271937 bytes/sec)
    00000000  1f 32 02 66 46 24 11 49  01 00 00 00 00 00 00 00  |.2.fF$.I........|
    00000010  a2 c1 0d 00 00 00 00 00  01 00 00 80 00 00 00 00  |................|
    00000020  4e 58 53 42 00 10 00 00  fb c1 66 06 00 00 00 00  |NXSB......f.....|
    00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000040  02 00 00 00 00 00 00 00  15 fa 6d 9a a2 2c 45 0f  |..........m..,E.|
    00000050  88 b8 c1 b8 2b c6 6b 13  7b 86 17 00 00 00 00 00  |....+.k.{.......|
    00000060  a3 c1 0d 00 00 00 00 00  18 01 00 00 30 6c 00 00  |............0l..|
    00000070  75 da 94 03 00 00 00 00  e3 01 94 03 00 00 00 00  |u...............|
    00000080  5d 00 00 00 e9 10 00 00  5b 00 00 00 02 00 00 00  |].......[.......|
    00000090  b4 10 00 00 35 00 00 00  ea b9 02 00 00 00 00 00  |....5...........|
    000000a0  04 a9 0e 00 00 00 00 00  01 04 00 00 00 00 00 00  |................|
    000000b0  00 00 00 00 64 00 00 00  03 04 00 00 00 00 00 00  |....d...........|
    000000c0  e1 b9 02 00 00 00 00 00  e3 b9 02 00 00 00 00 00  |................|
    000000d0  e5 5e 03 00 00 00 00 00  00 00 00 00 00 00 00 00  |.^..............|
    000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000000f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000170  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000180  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000200
    Trevors-MBP:~ trevorraney$ 

Best Answer

You entered the following command. This command is wrong.

sudo gpt add -i 2 -b 409640 -s 372637568 -t 7C3457EF-0000-11AA-AA11-00306543ECAC disk2

You should have entered the command shown below.

sudo gpt add -i 2 -b 409640 -s 859181016 -t 7C3457EF-0000-11AA-AA11-00306543ECAC /dev/disk2

So, you need to reenter the following sequence of commands, where the last command has now been replace with the correct command.

diskutil umountDisk disk2
sudo gpt remove -i 2 /dev/disk2
diskutil umountDisk disk2
sudo gpt add -i 2 -b 409640 -s 859181016 -t 7C3457EF-0000-11AA-AA11-00306543ECAC /dev/disk2

Note: You may need to eject the external drive for the changes to take effect.

If you successfully enter the above commands, then the output from sudo gpt -r show /dev/disk2 should appear as follows.

          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  859181016      2  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC       
      859590656  116920320      3  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
      976510976     262111         
      976773087         32         Sec GPT table
      976773119          1         Sec GPT header

Even if the above fix works out, this does not mean you should continue having two APFS containers on this external drive.

For example, if you need both Mojava and High Sierra installed on your external drive, then you should install both to separate volumes inside a single APFS partition.

How I verified disk2s2 is a APFS container partition.

I started with the Apple File System Reference. When a partition is a APFS container, then the partition begins with a container superblock (nx_superblock_t). This structure is described on page 25. The beginning of this structure is repeated below.

struct nx_superblock {
    obj_phys_t  nx_o;
    uint32_t    nx_magic;
    uint32_t    nx_block_size;
    uint64_t    nx_block_count;
              .
              .
              .
}
typedef struct nx_superblock nx_superblock_t;

You provided a hexadecimal and printable ASCII dump of the first 512 bytes of disk2s2. Using both your dump and the Apple File System Reference, I determined the following.

  • The value mx_magic should begin at hexadecimal offset 00000020 and should be the character string "NXSB". Your dump show this to be true.
  • The value of the field nx_block_size. This field is a 4 byte integer which begins at hexadecimal offset 00000024. This value represents the number of bytes contained in each APFS block. To read this value, you have to start with the raw 4 bytes from your dump, which are given below.

    00 10 00 00
    

    Next, the bytes need to be reversed. This is typical when Intel processors are used. The reversed bytes are shown below with the spaces and preceding zeros removed.

    1000
    

    This value can be converted from hexadecimal to decimal by using the Calculator application or by performing the math shown below.

    16*(16*(16*1+0)+0)+0 = 4096 bytes/APFS block
    
  • The value of field nx_block_count. This field is a 8 byte integer which begins at hexadecimal offset 00000028. This value represents number of blocks contained in APFS container. To read this value, you have to start with the raw 8 bytes from your dump, which are given below.

    fb c1 66 06 00 00 00 00
    

    Next, the bytes need to be reversed. The reversed bytes are shown below with the spaces and preceding zeros removed.

    666c1fb
    

    This value can be converted from hexadecimal to decimal by using the Calculator application or by performing the math shown below.

    16*(16*(16*(16*(16*(16*6+6)+6)+12)+1)+15)+11 = 107397627 APFS blocks/container
    

    Note: In the above equation, the hexadecimal letters were replace by their decimal equivalents. Below is a table of these equivalents

    a hexadecimal = 10 decimal
    b hexadecimal = 11 decimal
    c hexadecimal = 12 decimal
    d hexadecimal = 13 decimal
    e hexadecimal = 14 decimal
    f hexadecimal = 15 decimal
    
  • The number of logical blocks in the APFS container. Your posted output from the sudo gpt -r show /dev/disk2 commands shows the size of each GPT table (both Pri GPT table and Sec GPT table) is 32 logical blocks, Using this Wikipedia reference, I computed the logical block size, as shown below.

    (128 entries) * (128 bytes/entry) / (32 logical blocks) = 512 bytes/logical block
    

    Therefore, the conversion from APFS blocks to logical blocks can be determined, as shown below

    (4096 bytes/APFS block) / (512 bytes/Logical block) = 8 logical blocks/APFS block
    

    So, to convert the APFS container size to logical block, I needed to multiply by 8, as shown below.

    (8 logical blocks/APFS block) * (107397627 APFS blocks/container) = 859181016 logical blocks/container
    

    Note: This is the same as your original sudo gpt -r show /dev/disk2 command displayed for the size of the second partition.

Based on the above analysis, I concluded your disk2s2 was a APFS container partition and not a JHFS+ formatted partition.

However, if you still want to try setting to a JHFS+ formatted partition, substitute the command shown below.

sudo gpt add -i 2 -b 409640 -s 859181016 -t hfs /dev/disk2