gpt recover: unable to open device '/dev/disk2': Resource busy

I am trying to repair an external hard drive that is showing up under diskutil list and the Disk Utility app, but says "First Aid process has failed. If possible back up the data on this volume. Click Done to continue.". I have tried these things:

$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         250.8 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +250.8 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            243.7 GB   disk1s1
   2:                APFS Volume Preboot                 22.6 MB    disk1s2
   3:                APFS Volume Recovery                515.0 MB   disk1s3
   4:                APFS Volume VM                      2.1 GB     disk1s4

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS MyDisk                  999.8 GB   disk2s2
$ diskutil repairDisk /dev/disk2
Repairing the partition map might erase disk2s1, proceed? (y/N) y
Started partition map repair on disk2
Checking prerequisites
Checking the partition list
Adjusting partition map to fit whole disk as required
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
Reviewing boot support loaders
Checking Core Storage Physical Volume partitions
The partition map appears to be OK
Finished partition map repair on disk2
$ sudo gpt recover /dev/disk2s2
gpt recover: unable to open device '/dev/disk2s2': Resource busy
$ sudo diskutil mount readOnly /dev/disk2
Volume on disk2 timed out waiting to mount
$ sudo mkdir /Volumes/USB
$ sudo mount -t hfs /dev/disk2 /Volumes/USB
mount_hfs: error on mount(): error = -1.
mount_hfs: Resource busy
$ diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful
$ diskutil eject /dev/disk2
Volume timed out while waiting to eject

What else can be done to fix this, at least to get it readonly so I can copy the data out of it to another drive.

https://github.com/gcunhase/PaperNotes/blob/master/notes/mymacnotes.md


By default the gpt command requires a exclusive lock on a drive before preforming an write operation to the drive. This can not happen, when a volume residing on the drive is mounted. With respect to disk2, the typical solution would be enter the following command, before entering the gpt command.

diskutil unmountdisk disk2

The drive is identified as disk2. The volume containing user data on this drive is identified as disk2s2. To mount this volume as readonly, first the volume has to be unmounted, then mounted readonly. The commands are given below.

diskutil unmount disk2s2
diskutil mount readonly disk2s2 

I should note that the volume you were trying to access appeared to be mounted, because you were getting an error message when trying to use the gpt command. The command give below will output text, if the volume is mounted.

mount | grep disk2s2