Nuke and Pave Internal Drive Script for APFS

TJ_Edgerly
New Contributor III

Hello All,

We currently use the following script to unmount, (re)partition and remount an internal HD from our netboot environment. Currently is sits on our dock in our netboot environment (currently 10.12.6- haven't rebuild netboot OS to 10.13 which i know is required for APFS).

#!/bin/sh

# Author: Jared F. Nichols
# Purpose: Nuke and pave the first internal drive to prepare for imaging.

clear
echo "Do you wish to nuke the internal drive?"
echo "THERE IS NO RECOVERY FROM THIS!"
printf "Y/N? "
read response

case $response in
    Y|y|YES|yes|Yes|yEs|yeS|YEs|yES)
        echo
        echo
        sudo diskutil partitionDisk /dev/disk0 1 gpt jhfs+ "Macintosh HD" 100%
        echo
        echo "Formatting complete."
        echo "Continue with Casper Imaging."
        echo
        echo
        exit 0 # Normal Exit
        ;;
    N|n|NO|no|No|nO)
        echo
        echo
        echo "Quitting"
        echo
        echo
        exit 1 # User quit
        ;;
esac

Now with APFS becoming the standard, is it just a matter of changing the "jhfs+" to "apfs" before laying down a 10.13.x flavor OS?

Still really new to scripting and i can't take down our netboot server until a scheduled maint. window so just wanted to have everything ready.

Or if anyone has a better option, i'm open.

5 REPLIES 5

AVmcclint
Honored Contributor

I have found when I netboot a new Mac to image and I use Disk Utility to wipe the drive, I still need to reboot the computer one more time because Disk Utility still identifies it as an APFS Collection (or something to that effect.) Only after I reboot am I able to put the image on it. Does this script take care of that?

mm2270
Legendary Contributor III

I've seen the same thing as @AVmcclint in testing. Wiping the drive one time isn't enough to get rid of the APFS format. I'm looking to see if there's a method of getting rid of it in one shot, or a double reformat maybe. Needing to reboot again to be able to image a drive is just stupid.

jmahlman
Valued Contributor

We had an issue where our already deployed Macs needed to have their fusion drives re-linked, and we wanted to remove any extra partitioning that was done on them. I found a script by Calum Hunter somewhere online (sorry, I don't remember) that worked really well for detecting possible fusion drives, relinking them, and making everything back to factory. I made the script run at startup on a Netboot (very dangerous, it automatically wiped the drive but we only enabled it on the rooms we were imaging at the time) and it worked really well!

Fast forward to APFS drives...it didn't work obviously, so I added a function in the script to detect APFS and then delete the container and bring the drive back to HFS+ for jamf imaging, which would convert it to APFS if we selected a 10.13 image. We don't deploy 10.13 yet, so this isn't tested in a barge environment, but it works on my test machines. I put this as a .command file on the desktop of my netboot set and it does it's job and doesn't;t require a reboot.

REPAIR FUSION DRIVE.sh

Lines 41-54 are my additions:

CHECK_FOR_APFS(){
    # Check to see is this is an APFS formatted drive so we can remove it and let the rest of the script go
    # FYI! THIS IS PROBABLY NOT THE SAFEST METHOD FOR DOING THIS AND MAY NOT WORK FOR EVERYONE!  USE WITH CAUTION!
    echo $(date "+%a %b %d %H:%M:%S") " - Checking for APFS Container..."
    FSTYPE=$( diskutil apfs list ) # this check is ugly but it works
    if [ "$FSTYPE" != "No APFS Containers found" ]; then
      APFSCONTAINER=$( diskutil apfs list | grep 'APFS Container Reference' | cut -d':' -f 2 | tr -d ' ' )
      echo $(date "+%a %b %d %H:%M:%S") " - [OK] Detected APFS Container at $APFSCONTAINER"
      diskutil apfs deleteContainer $APFSCONTAINER
      echo $(date "+%a %b %d %H:%M:%S") " - [OK] Deleted APFS Container"
    else
      echo $(date "+%a %b %d %H:%M:%S") " - [WARN] No APFS Container found"
    fi
}

jmahlman
Valued Contributor

Argh, I can't edit the post. Anyway, I found the original script here.

easyedc
Valued Contributor II

@TJ.Edgerly How are you laying your firmware update down in the restore for 10.13?