My script for deploying Mavericks images to computers without use Recovery HD

Chris_abi-najm
New Contributor II

Hey everyone. I've started planning an upgrade for 1200+ Macs over this summer and wanted to find the most efficient way to deploy the images to the computers with very little intervention from the technologists in the field. I figured the less they have to interface with the computer, the less that can go wrong, also, given our limited staff and geographically disperse systems, it could take several months to complete the upgrade if they were given a USB or hard drive and told to reimage the computers. Part of my migration is collapsing the four existing disparate JSS environments into one. Upon enrollment, I have policies set up to monitor and push the necessary files to the workstations. Being that we're running a mixture of Leopard and Snow Leopard machines, the challenge was to get the image to the machine seamlessly and allow the technologists to initiate the imaging process without having to use an external device or recovery hd (since 10.5 and 10.6 don't have the Recovery HD). I developed a process and script based on some of the others that were posted here on JAMF Nation and I think what i've come up with is a pretty robust delivery and prep method, so I wanted to share with you all what has taken me several weeks to perfect.

There are two pre-requisites that I needed before the script can work properly. The first is a DISK IMAGE of the Recovery HD from a known, good system. I took an image of my Mavericks machine and packaged that as a DMG and added it to my payload. In addition to that is Recovery HD Update for Snow Leopard. Both are added to Composer, packaged and pushed through the JSS policy and set to install in an arbitrary place (I chose /private/tmp). The next requirement is the Mavericks image itself. This is quite large (mine was 19gb) for going over a WAN. I am in the process of creating multiple distribution points in each local network where the Macs reside so that traffic stays local. In the same policy as the Recovery HD DMG and Recovery HD Update, add the Mavericks Image and set it to Cache only. Add the Script to the payload and set it to execute AFTER.

One of the challenges I ran into when I was trying to get the Recovery HD to work with Snow Leopard was the dmtest command to run. Since it seems like the libraries the command is trying to use aren't loaded because they're calling from 10.7+, I had to come up with another way to get the Recovery HD image on the disk. I searched but couldn't find a way to load the 10.7+ specific libraries on 10.6.

After the Recovery HD is extracted and put in place through by script, the Mavericks image is moved to the MavericksOSImage partition. The only manual part of this process is rebooting the computer and holding the Option key, the selecting the Recovery HD will initiate the Recovery mode. From there, run an asr restore of the image from the Terminal and you're good to go. Please feel free to use or modify as necessary for your own environments. Let me know if you have any questions or problems.

Note: I haven't tested this with 10.5 yet but am in the process of doing so.

#!/bin/sh
##### Mavericks OS Prep
##### Installs recovery partition on hard drive for Leopard, Snow Leopard, Lion and Mountain Lion
##### By: Chris Abi-Najm
##### Version 1.1
##### Date: 5-30-2014

#####  1) Create the recovery partition and image partition 
#####  Define variables

ImageFileName=Mavericks-Image-20140319.dmg
OSImagePartExist=`diskutil list | grep "MacintoshOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
RecoveryPartExist=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
MavericksPartSize=28000000000
RecPartSize=650000000
BootVolume=`bless -getboot`

OSVerFull=$(sw_vers -productVersion)
OSVerMajor=${OSVerFull:0:4}

echo "Current OS image version is $ImageFileName"
echo "Boot disk is on device $BootVolume"
echo "You are running $OSVerMajor"
echo "Inspecting the current system configuration for $OSVerMajor. Searching for Recovery HD and MacOSImage volumes."

case $OSVerMajor in
    10.5*)
        echo "Reconfiguring the disks for Leopard"
        if [ ! -b "/dev/$OSImagePartExist" ] && [ ! -b "/dev/$RecoveryPartExist" ]
        then
            ## Determine the size of the hard drive
            CurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d ":" -f1 | rev | cut -d " " -f1`
            ## Calculate the size of the new partition
            echo "The current disk size is $CurrentPartSize"
            NewVolume=`expr $CurrentPartSize - $MavericksPartSize`
            echo "After we partition, the new disk size will be $NewVolume"
            echo "Creating a new partition called MavericksOSImage that is ""$MavericksPartSize""b large."
                        sudo diskutil resizevolume $BootVolume "$NewVolume"b JHFS+ MavericksOSImage "$MavericksPartSize"b
            NewCurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d ":" -f1 | rev | cut -d " " -f1`
                        NewRecVolume=`expr $NewCurrentPartSize - $RecPartSize`
            sudo diskutil resizevolume $BootVolume "$NewRecVolume"b JHFS+ Recovery HD "$RecPartSize"b

            ## Get the device ID
            MavPart=`diskutil list | grep "MavericksOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
            RecPart=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1`

            echo "The Mavericks OS Image partition has been assigned $MavPart."
            echo "The Recovery HD partition has been assigned $RecPart."
        else
            echo "The Mac OS Image partition already exists, let's see if an image resides on the partition."
        fi
    ;;
    10.6)
        echo "Reconfiguring the disks for Snow Leopard"
        if [ ! -b "/dev/$OSImagePartExist" ] && [ ! -b "/dev/$RecoveryPartExist" ]
        then
            ## Determine the size of the hard drive
            CurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
            ## Calculate the size of the new partition
            echo "The current disk size is $CurrentPartSize"
            NewVolume=`expr $CurrentPartSize - $MavericksPartSize`
            echo "After we partition, the new disk size will be $NewVolume"
            echo "Creating a new partition called MavericksOSImage that is ""$MavericksPartSize""b large."
                        sudo diskutil resizevolume $BootVolume "$NewVolume"b JHFS+ MavericksOSImage "$MavericksPartSize"b
            NewCurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
                        NewRecVolume=`expr $NewCurrentPartSize - $RecPartSize`
            sudo diskutil resizevolume $BootVolume "$NewRecVolume"b JHFS+ Recovery HD "$RecPartSize"b

            ## Get the device ID
            MavPart=`diskutil list | grep "MavericksOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
            RecPart=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1`

            echo "The Mavericks OS Image partition has been assigned $MavPart."
            echo "The Recovery HD partition has been assigned $RecPart."
        else
            echo "The Mac OS Image partition already exists, let's see if an image resides on the partition."
        fi
    ;;
    10.7*)
        echo "Reconfiguring the disks for Lion"
        if [ ! -b "/dev/$OSImagePartExist" ] && [ ! -b "/dev/$RecoveryPartExist" ]
        then
            ## Determine the size of the hard drive
            CurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
            ## Calculate the size of the new partition
            echo "The current disk size is $CurrentPartSize"
            NewVolume=`expr $CurrentPartSize - $MavericksPartSize`
            echo "After we partition, the new disk size will be $NewVolume"
            echo "Creating a new partition called MavericksOSImage that is ""$MavericksPartSize""b large."
                        sudo diskutil resizevolume $BootVolume "$NewVolume"b JHFS+ MavericksOSImage "$MavericksPartSize"b
            NewCurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
                        NewRecVolume=`expr $NewCurrentPartSize - $RecPartSize`
            sudo diskutil resizevolume $BootVolume "$NewRecVolume"b JHFS+ Recovery HD "$RecPartSize"b

            ## Get the device ID
            MavPart=`diskutil list | grep "MavericksOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
            RecPart=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1`

            echo "The Mavericks OS Image partition has been assigned $MavPart."
            echo "The Recovery HD partition has been assigned $RecPart."  
        else
            echo "The Mac OS Image partition already exists, let's see if an image resides on the partition."
        fi
    ;;
    10.8*)
        echo "Reconfiguring the disks for Mountain Lion"
        if [ ! -b "/dev/$OSImagePartExist" ] && [ ! -b "/dev/$RecoveryPartExist" ]
        then
            ## Determine the size of the hard drive
            CurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
            ## Calculate the size of the new partition
            echo "The current disk size is $CurrentPartSize"
            NewVolume=`expr $CurrentPartSize - $MavericksPartSize`
            echo "After we partition, the new disk size will be $NewVolume"
            echo "Creating a new partition called MavericksOSImage that is ""$MavericksPartSize""b large."
                        sudo diskutil resizevolume $BootVolume "$NewVolume"b JHFS+ MavericksOSImage "$MavericksPartSize"b
            NewCurrentPartSize=`diskutil resizevolume $BootVolume limits | grep "Current" | rev | cut -d "(" -f1 | rev | cut -d " " -f1`
                        NewRecVolume=`expr $NewCurrentPartSize - $RecPartSize`
            sudo diskutil resizevolume $BootVolume "$NewRecVolume"b JHFS+ Recovery HD "$RecPartSize"b

            ## Get the device ID
            MavPart=`diskutil list | grep "MavericksOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
            RecPart=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1`

            echo "The Mavericks OS Image partition has been assigned $MavPart."
            echo "The Recovery HD partition has been assigned $RecPart."      
        else
            echo "The Mac OS Image partition already exists, let's see if an image resides on the partition."
        fi
    ;;
esac

#####  2) Copy the OS Image files and extract the Recovery HD partition and put it in place 
#####  Define variables
OSImageDiskExist=`diskutil list | grep "MavericksOSImage" | rev | cut -d " " -f1 | rev | cut -d " " -f1`
RecoveryDiskExist=`diskutil list | grep "Recovery HD" | rev | cut -d " " -f1 | rev | cut -d " " -f1 | grep "disk0"`

##### Copying files
diskutil mount /dev/$OSImageDiskExist

if [ -f /Volumes/MavericksOSImage/$ImageFileName ]
then
    echo "There is already a Mavericks OS image residing on this volume: $ImageFileName"
else
    echo $ImageFileName
    echo "Moving the Mavericks OS image to the cache volume"
    sudo mv /Library/Application Support/JAMF/Waiting Room/Mavericks-Image*.dmg /Volumes/MavericksOSImage
    echo "Unmounting $OSImageDiskExist"
    diskutil unmount /dev/$OSImageDiskExist
fi

#####  3) Hide the Recovery HD partition and make them bootable. Unmount all of the other volumes.

echo $RecoveryDiskExist

diskutil mount /dev/$RecoveryDiskExist
if [ -d /Volumes/Recovery HD ]
then
    ## Restoring files from Recovery HD disk image to hidden share
    echo "Restoring disk image to Recovery HD partition."
    sudo asr restore --source /private/tmp/MavRecoveryHD/Recovery HD.dmg --target /dev/$RecoveryDiskExist --erase --noverify --noprompt

    ## Change device to Apple boot device and hiding it from the OS
        echo "Hiding and securing $RecoveryDiskExist from the OS."
        sudo diskutil unmount /dev/$RecoveryDiskExist   
        echo "Changing /dev/$RecoveryDiskExist to Apple_Boot device."
        sudo asr adjust --target /dev/$RecoveryDiskExist -settype Apple_Boot

fi
exit 0
0 REPLIES 0