DEP and Partitioning machines

Valued Contributor

So, I'm working through getting our workflow adjusted thanks to DEP. DEP obviously does't do partitioning, and Casper Imaging can't partition the drive if you're booted from it. I guess I'm looking at a shell script to partition the machine's drive.

Of course now that a machine could be using HFS+J or APFS that complicates things. Anyone have a script for this already? APFS is still a bit confusing - I guess i'll be doing "diskutil apfs addVolume"?


Valued Contributor III

If you want to put a FAT32 partition called WINDOWS on an APFS volume this is what I created to do it in our environment.
You might need to find some analogous commands for none APFS or other formats than FAT32.
It will default to around 50% otherwise you can pass it a preferred MacOS size as a percent.
Diskutil is annoying in that you pass it a % and it says I can't do percentages, so someone took the time to create an error message specifically about percentages instead of just using that time to create the calculations for the actual partition size.

if [[ ! "$MacOS_Preferred_Size" ]];then

Boot_File_Type=$(diskutil info / | awk '/File System Personality:/ {print $NF}')
Boot_Drive_Used=$(diskutil info / | awk '/Volume Used Space:/ {print $NF}' | sed -e 's/^(//g' -e 's/[.].*//g')
Windows_Partition=$(diskutil list | awk '/Microsoft Basic Data/ || /WINDOWS/ {print $NF}')

if [[ "$Boot_File_Type" == "APFS" ]] && [[ "$Boot_Drive_Used" -lt 80 ]] && [[ "$Windows_Partition" == "" ]]; then
echo "Do"
macOS_Size=$((10 + Boot_Drive_Used))
if [[ "$macOS_Size" -lt $MacOS_Preferred_Size ]]; then
Container_ID=$(diskutil info / | awk '/Part of Whole:/{print $NF}')
Old_Container_Size=$(diskutil info / | awk '/Volume Total Space:/' | sed -e 's/ Bytes.*//g' -e 's/^.*(//g')
New_Container_Size=$((Old_Container_Size / 100 * macOS_Size))
echo "The APFS volume will be shrunk to $New_Container_Size Bytes and the remainder will be made into WINDOWS"
if [[ "$New_Container_Size" -gt 0 ]]; then
diskutil APFS resizecontainer $Container_ID $New_Container_Size FAT32 WINDOWS 0
echo "Do not"