Apple - APFS partition inaccessible, container missing

Note: Posting images of the output from the Terminal application can often lead to users not posting an answer to a question. You should copy and paste the text to your question. Once pasted, highlight the text, then click on the code sample button {} to format the text.

Your current APFS partition (disk0s2) does not appear to be valid for several reasons. Below are three reasons.

  1. The magic number stored in the header is incorrect. Also, the header defines the block size to be 2,385,839,421 bytes and the APFS container to be 2,777,634,813,198,357 TB in size. The magic number should be BSXN. The block size should be 4096 bytes and the container should be less than or equal to 883,222,061,056 bytes in size.
  2. The APFS partition size of 1,725,043,095 logical sectors is not divisible by 8.
  3. The APFS partition should appear either immediately and soon after the EFI partition (disk0s1). Here, the partition starts 116,773,068,800 bytes after the end of the EFI partition.

It appears that an APFS partition of 960,018,657,280 bytes in size did occur immediately after the EFI partition (disk0s1). The problem is the data stored in the APFS partition was partially overwritten by the data now stored in disk0s3. The type and location of disk0s3 would indicate that macOS Sierra may have been installed close or immediately before disk0s3. If this is true, then additional data stored in the APFS partition may have been overwritten.

Note: The partition entries stored in the GPT are not in ascending order. While this does not create problems for Ubuntu, this condition oftend leads to an incorrectly functioning macOS.

To correct the GPT, you need to remove the second and third entries. The commands are given below.

Note: You must unmount all volumes from disk0 before entering the commands given below.

sudo gpt remove -i 2 /dev/disk0
sudo gpt remove -i 3 /dev/disk0

The command below will add the APFS container to the GPT.

sudo gpt add -i 2 -b 409640 -s 1875036440 -t apfs /dev/disk0

There is no guarantee fixing the GPT will automatically lead to a correctly functioning APFS partition.

When finished, the output from sudo gpt -r show /dev/disk0 should appear as shown below.

     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  1875036440      2  GPT part - 7C3457EF-0000-11AA-AA11-00306543ECAC
1875446080    78079055         
1953525135          32         Sec GPT table
1953525167           1         Sec GPT header

David Anderson's answer above is gold. Do bear in mind, that the values used in the command below (1875036440 blocks(512)) used here is as per the situation given by OP.

sudo gpt add -i 2 -b 409640 -s 1875036440 -t apfs /dev/disk0

Change those values according to the output of your sudo gpt -r show disk0. Replace them with the size of your messed up partition.

For example, I had the exact same prob as OP. I had 3 partitions for my SDD. The first parition (APFS) got messed up. The rest of the partitions (partition2, partition3) were HFS.

In the gpt add command I entered the size of my partition , and voila, on restarting my computer, MacOS came back.