Disk Utility – Creating New Partition in Unallocated Space Using Diskutil on MacOS

command linedisk-utilitymacospartition

I have deleted the first partition on my external HD such that it now has 100GB of free space at the beginning of the partition. I'd like to reclaim that space, but Disk Utility doesn't let me create a new partition there (it just does nothing when I click 'Apply'). Is there a way to accomplish this with diskutil? I’ve only found ways to change (or erase, or split etc.) existing partitions by e.g. entering disk2s2 as device ID, but the free space has no such device ID.

Outputs:

$ diskutil list disk2
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.5 TB     disk2
   1:                        EFI                         209.7 MB   disk2s1
   2:                  Apple_HFS Shared                  199.3 GB   disk2s2
   3:                 Apple_Boot Recovery HD             784.2 MB   disk2s3
   4:          Apple_CoreStorage                         500.0 GB   disk2s4
   5:                 Apple_Boot Boot OS X               134.2 MB   disk2s5
   6:          Apple_CoreStorage                         699.6 GB   disk2s6
   7:                 Apple_Boot Boot OS X               134.2 MB   disk2s7
$ sudo gpt -r show 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   195575768         
   195985408   389353696      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
   585339104     1531680      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
   586870784   976562504      4  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  1563433288      262144      5  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  1563695432  1366319552      6  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  2930014984      262144      7  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  2930277128           7         
  2930277135          32         Sec GPT table
  2930277167           1         Sec GPT header
$ sudo fdisk /dev/disk2
Disk: /dev/disk2    geometry: -5415437/4/63 [-1364690128 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE 1023 254  63 - 1023 254  63 [         1 - -1364690129] <Unknown ID>
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
$ sudo pdisk --list /dev/disk2
pdisk: No valid block 1 on '/dev/disk2'

Best Answer

(Before proceeding, please make sure the disk in question is still disk2, you have backups of your data, etc. - that said, the changes here are not particularly dangerous. Read through the whole instructions before doing anything to make sure you understand all the steps.)

OK, your partition tables look fine (a valid GPT and a correct protective MBR), so I don't know why Disk Utility is failing you in this instance, but you should be able to create a partition in the empty space using the gpt command-line utility. Your GPT looks like this:

       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   195575768         
   195985408   389353696      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
   585339104     1531680      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
   586870784   976562504      4  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  1563433288      262144      5  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  1563695432  1366319552      6  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  2930014984      262144      7  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  2930277128           7         
  2930277135          32         Sec GPT table
  2930277167           1         Sec GPT header

The free space is indicated by this line (the numbers are 512-byte sectors, so just under 100GB free space:

      409640   195575768         

Apple requires 128MiB (262144 sectors) free space following a partition, so the new partition can be 195575768-262144=195313624 sectors. Unmount all the volumes on the disk using:

diskutil unmountDisk disk2

Then, this command will create an HFS+ partition in the free space:

sudo gpt add -b 409640 -s 195313624 -t hfs disk2

That only creates the partition, not the file system. To check that it went well, you should now see a disk2s8 if you run diskutil list disk2. If this is indeed the case, you can format the partition like so:

sudo newfs_hfs -v "Volume Name" -J /dev/rdisk2s8

If disk2s8 hasn't appeared, you'll need to reboot before formatting. Instead of running newfs_hfs you should also be able to erase the partition in Disk Utility.

The gpt utility doesn't re-order the partition numbers but this shouldn't be a problem in practice. If you make any further changes with disk utility, that will probably fix the ordering anyway.