Trying to script partitioning Macintosh HD in command line? I am trying to script partitioning the Macintosh HD. I am running 

diskutil partitionDisk /dev/disk0 GPT JHFS+ New 100g


in command line and I'm getting this error.

Started partitioning on disk0
Unmounting disk
Error: -69877: Couldn't open device
(Is a disk in use by a storage system such as AppleRAID, CoreStorage, or APFS?)


So it seems like it's having problems dismounting Macintosh HD because that's what my laptop is running on. How do I partition Macintosh HD while booted on it?

diskutil list

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE           IDENTIFIER
   0:      GUID_partition_scheme                        *500.3 GB       disk0
   1:                        EFI EFI                     209.7 MB       disk0s1
   2:          Apple_CoreStorage Macintosh HD            499.4 GB       disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB       disk0s3

/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE           IDENTIFIER
   0:                  Apple_HFS Macintosh HD           +499.1 GB       disk1
                             Logical Volume on disk0s2
                             EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A54
                             Unlocked Encrypted


diskutil coreStorage list

CoreStorage logical volume groups (1 found)
|
+-- Logical Volume Group 5E012656-B5D2-43E3-999F-5EF2E2D0C138
=========================================================
Name:         Macintosh HD
Status:       Online
Size:         499418034176 B (499.4 GB)
Free Space:   10645504 B (10.6 MB)
|
+-&lt; Physical Volume 404807DA-ADE9-4CAE-B449-279488379018
|   ----------------------------------------------------
|   Index:    0
|   Disk:     disk0s2
|   Status:   Online
|   Size:     499418034176 B (499.4 GB)
|
+-&gt; Logical Volume Family 78187C7A-2A55-48A1-AEE3-25AD3A9E3538
    ----------------------------------------------------------
    Encryption Type:         AES-XTS
    Encryption Status:       Unlocked
    Conversion Status:       Complete
    High Level Queries:      Fully Secure
    |                        Passphrase Required
    |                        Accepts New Users
    |                        Has Visible Users
    |                        Has Volume Key
    |
    +-&gt; Logical Volume EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A54
        ---------------------------------------------------
        Disk:                  disk1
        Status:                Online
        Size (Total):          499055067136 B (499.1 GB)
        Revertible:            Yes (unlock and decryption required)
        Revert Status:         Reboot required
        LV Name:               Macintosh HD
        Volume Name:           Macintosh HD
        Content Hint:          Apple_HFS


diskutil apfs list

No APFS Containers found
Your main disk is already partitioned. You can't use diskutil partitionDisk /dev/disk0 GPT ... booted to the same disk.



In my opinion you have three options:


Resize the current Logical Volume "Macintosh HD" and create a second logical volume in the LVG
Resize the whole stack and add a fourth volume at the end
Create a growing sparsebundle image in your encrypted volume "Macintosh HD"





Resize the logical volume:


resize the logical volume:

diskutil cs resizeVolume EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A54 399055m

add a second logical volume in the free LVG space:

diskutil cs createVolume 5E012656-B5D2-43E3-999F-5EF2E2D0C138 jhfsx "dev" 100%

Result:

/dev/disk0 (internal, physical):
  #:                       TYPE NAME                    SIZE           IDENTIFIER
  0:      GUID_partition_scheme                        *500.3 GB       disk0
  1:                        EFI EFI                     209.7 MB       disk0s1
  2:          Apple_CoreStorage Macintosh HD            499.4 GB       disk0s2
  3:                 Apple_Boot Recovery HD             650.0 MB       disk0s3

/dev/disk1 (internal, virtual):
  #:                       TYPE NAME                    SIZE           IDENTIFIER
  0:                  Apple_HFS Macintosh HD           +399.1 GB       disk1
                         Logical Volume on disk0s2
                         EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A5
                         Unlocked Encrypted
/dev/disk2 (internal, virtual):
  #:                       TYPE NAME                    SIZE           IDENTIFIER
  0:                  Apple_HFS dev                     100.0 GB       disk2
                         Logical Volume on disk0s2
                         C7C75FC4-4062-42E7-B2F5-576CFCA257854
                         Unencrypted


Resize the stack:


resize the whole stack:

diskutil cs resizeStack EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A54 399055m

add a fourth volume with gdisk (download required)

sudo gdisk /dev/disk0

Enter the following key sequence: N/Enter/Enter/Enter/AF00/W/Y Check the help with `?`!

format case-sensitive after getting the new device identifier

diskutil list
sudo newfs_hfs -v dev -J -s disk0s4 #use the appropriate device identifier here

modify volume permissions with chgrp/chmod if necessary
Result:

/dev/disk0 (internal, physical):
  #:                       TYPE NAME                    SIZE           IDENTIFIER
  0:      GUID_partition_scheme                        *500.3 GB       disk0
  1:                        EFI EFI                     209.7 MB       disk0s1
  2:          Apple_CoreStorage Macintosh HD            399.4 GB       disk0s2
  3:                 Apple_Boot Recovery HD             650.0 MB       disk0s3
  4:                  Apple_HFS dev                     100.0 GB       disk0s4        
/dev/disk1 (internal, virtual):
  #:                       TYPE NAME                    SIZE           IDENTIFIER
  0:                  Apple_HFS Macintosh HD           +399.1 GB       disk1
                         Logical Volume on disk0s2
                         EFDB0B2F-EEBB-4CAC-8AFE-69F4C1803A5
                         Unlocked Encrypted


Create a growing sparsebundle:

hdiutil create -type SPARSEBUNDLE -size 100g -fs JHFS+X -volname dev dev


Check man hdiutil for other formats like SPARSE etc.




Scripting this is rather difficult but not impossible - except the last proposal which is easy to implement.