How to recover lost partition after diskutil erasevolume noformat

disk-utilitydiskutilhard drivepartition

I previously had 2 partitions on my 1TB hard drive…
– Partition 1 (disk0s4): Macintosh HD (which had OS-X and all my data)
– Partition 2 (disk0s2): Apple HD (which was a back-up partition)

I erased partition 2 and tried merging it with partition 1, but Disk Utility didn't allow it (the "-" button was inactive). I then used the command diskutil eraseVolume free %noformat% disk0s2, but now I've completely lost the space of partition 2.
When I type diskutil list, the following appears:

   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.0 GB   disk0s4
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s7

I've tried resizing disk0s4 with the command diskutil resize volume disk0s4 R, but I get the following:

Resizing to full size (fit to fill)
Started partitioning on disk0s4 Macintosh HD
Verifying the disk
Verifying file system
Using live mode
Performing live verification
Checking Journaled HFS Plus volume
Checking extents overflow file
Checking catalog file
Checking multi-linked files
Checking catalog hierarchy
Checking extended attributes file
Checking volume bitmap
Checking volume information
The volume Macintosh HD appears to be OK
File system check exit code is 0
Resizing
Error: -69742: The requested size change for the target disk or a related disk is too small; please try a different disk or partition, or make a larger change

Also, if I type diskutil cs list, I get:
No CoreStorage logical volume groups found

Is there a way to restore disk0s2? I'm afraid that if I reboot without recovering this partition, I might lose everything… Ideally, I'd prefer to recover the missing space from disk0s2 and merge it into disk0s4.

Best Answer

The problem has finally been resolved... I went into Internet Recovery (but normal Recovery will do as well) and "reactive" the partition with Disk Utility. The old version of Disk Utility that is used when in Recovery mode allows to do this whereas the new version in System Preferences has pretty limited functions.

Basically, the partition had just disappeared before... When using the command sudo gpt -r show /dev/disk0 I had 4 partitions, but one without any identifier. As a result, I was unable to either mount or restore this lost partition with diskutil, as there was nothing to identify it. Sorry for not being any clearer, but since I restored everything, I don't have the output to demonstrate it anymore.

I still thank you for answering back!