Skip to content

Instantly share code, notes, and snippets.

@sideshow
Created August 29, 2013 01:52
Show Gist options
  • Save sideshow/6373465 to your computer and use it in GitHub Desktop.
Save sideshow/6373465 to your computer and use it in GitHub Desktop.
:#!/bin/bash
# Script Created by Christopher Silvertooth. Inspiration from Clay Caviness.
# http://musings.silvertooth.us
# Version 2.0 Mountain Lion compatibility
# 2.0.1 added verify disk check
# 2.0.2 added Retina Macbook Pro and 2012 Air check
# 2.0.3 Fixed bug in OS detection. Reported by Steve Silvertooth.
# 2.0.4 Fixed diskutil check so that it doesn't break with multiple drives.
# 2.0.5 Bug fixes. Wrong recovery partition added.
## Check OS Version and Sanity Check
osversionlong=`sw_vers -productVersion`
osvers=${osversionlong:3:1}
if [ "$osvers" == "7" -o "$osvers" == "8" ]; then
echo -e "\n \n"
echo -e "You are running 10."$osvers" or later. Proceeding."
else
echo -e "You are not running OS X 10.7 or 10.8. This program will now exit."
exit 1
fi
# Gather all disks
disks=(`diskutil list | grep "GUID_partition_scheme" | awk '{print $5}'`)
if [ "${#disks[@]}" != 1 ]; then
echo -e "\n \n"
echo "You have multiple disks attached to your Mac. I will assume that disk0 is your Macintosh HD boot volume."
else
echo -e "\n \n"
echo "Preparing to check your hard drive for errors."
fi
## Validate that the drive or drives do not need any repairs.
#for (( i=0; i < "${#disks[@]}"; i++))
#do
#diskutil verifyDisk ${disks[$i]}
diskutil verifyDisk disk0
RetVal=`echo $?`
if [ $RetVal == 1 ]; then
echo "Macintosh HD needs to be repaired. The easiest way to do this is to boot off another drive or boot disk running Lion or Mountain Lion."
exit 1
else
echo -e "\n \n"
echo "Your disk appears to be OK. The script will now continue."
fi
## Set Working Directory
workingDirectory="`dirname \"$0\"`"
cd "$workingDirectory"
Recovery="RecoveryHDUpdate.dmg"
MtnLion="Install OS X Mountain Lion.app"
Lion="Install OS X Lion.app"
if [ ${osvers} == 8 ]; then
if [ ! -d "/Applications/$MtnLion" ]; then
echo -e "\n \n"
echo -e "The Mountain Lion installer is not located in the Applications folder. Re-download Mountain Lion from the app store or place it in the Applications directory."
exit 1
fi
fi
if [ ! -f $Recovery ]; then
echo -e "\n \n"
read -p "No RecoveryHDUpdate.dmg present. If this is a mistake select no to quit the script and place the RecoveryHDUpdate.dmg in the directory where this is being run from and restart this script. If you don't have one hit yes and it will download it from Apple. Do you wish to Continue? (y/n)?" choice
case "$choice" in
y|Y ) echo "yes"
## Downloads the RecoveryHDUpdate.dmg. This is need for both the Lion and Mtn. Lion Recovery Partition.
curl -O http://supportdownload.apple.com/download.info.apple.com/Apple_Support_Area/Apple_Software_Updates/Mac_OS_X/downloads/041-2768.20111012.cd14A/RecoveryHDUpdate.dmg
echo -e "\n \n"
read -p "This script installs a recovery partition. It is advisable to backup your data in case this goes horribly wrong. That said, it has worked for me. Do you wish to Continue? (y/n)?" choice
case "$choice" in
y|Y ) echo "yes"
## Mount Recovery image from Apple. This is needed for the dmtest utility and the recovery partition for OS X 10.7 non-retina computers.
hdiutil attach RecoveryHDUpdate.dmg
## Expand RecoveryHDUpdate and place in /tmp
pkgutil --expand /Volumes/Mac\ OS\ X\ Lion\ Recovery\ HD\ Update/RecoveryHDUpdate.pkg /tmp/RecoveryHDUpdate
## Expand the meta partition
#hdiutil attach /tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/RecoveryHDMeta.dmg
## Unmount Recovery DMG to reduce conflicts
diskutil unmount /Volumes/Mac\ OS\ X\ Lion\ Recovery\ HD\ Update/
## This is for Mountain Lion
if [ ${osvers} == 8 ]; then
## Mount the Installer DMG inside of the Mtn. Lion Application
hdiutil attach "/Applications/Install OS X Mountain Lion.app/Contents/SharedSupport/InstallESD.dmg"
## Install recovery parition using dmtest tool from RecoveryPartition.dmg
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.dmg 0 0 /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.chunklist
hdiutil eject "/Volumes/Mac OS X Install ESD"
fi
## This is for Lion
if [ ${osvers} == 7 ]; then
## Check for Retina Display MacBook and 2012 Air check
Model=`system_profiler SPHardwareDataType | grep "Model Identifier:" | awk '{print $3}'`
if [ "$Model" == "MacBookPro10,1" -o "$Model" == "MacBookPro10,2" -o "$Model" == "MacBookAir5,1" -o "$Model" == "MacBookAir5,2" ]; then
echo "You are a $Model model which is a 2012 Macbook Pro or Air. This hardware model requires the Lion Installer in order to recover the parition. You will need to re-download the Lion Install App from the App Store in order to proceed."
if [ ! -d "/Applications/$Lion" ]; then
echo -e "\n \n"
echo -e "The Lion installer is not located in the Applications folder. Re-download Lion from the app store or place it in the Applications directory."
exit 1
fi
## Expand the meta partition
hdiutil attach /tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/RecoveryHDMeta.dmg
## Mount the Installer DMG inside of the Lion Application. This is only needed for 2012 Retina and Airs.
hdiutil attach "/Applications/Install OS X Lion.app/Contents/SharedSupport/InstallESD.dmg"
## Install recovery parition using dmtest tool from RecoveryPartition.dmg
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.dmg 0 0 /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.chunklist
hdiutil eject "/Volumes/Mac OS X Install ESD"
fi
else
## This part takes the mounted image and creates the recovery partition.
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Recovery\ HD\ Update/BaseSystem.dmg 0 0 /Volumes/Recovery\ HD\ Update/BaseSystem.chunklist
hdiutil eject "/Volumes/Recovery HD Update"
hdiutil eject "/Volumes/Mac OS X Lion Recovery HD Update"
fi
exit 1;;
n|N ) echo "no";;
* ) echo "invalid";;
esac
;;
n|N ) echo "no";;
* ) echo "invalid";;
esac
else
echo -e "\n \n"
read -p "This script installs a recovery partition. It is advisable to backup your data in case this goes horribly wrong. That said, it has worked for me. Do you wish to Continue? (y/n)?" choice
case "$choice" in
y|Y ) echo "yes"
hdiutil attach RecoveryHDUpdate.dmg
pkgutil --expand /Volumes/Mac\ OS\ X\ Lion\ Recovery\ HD\ Update/RecoveryHDUpdate.pkg /tmp/RecoveryHDUpdate
#hdiutil attach /tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/RecoveryHDMeta.dmg
## Unmount Recovery DMG to reduce conflicts
diskutil unmount /Volumes/Mac\ OS\ X\ Lion\ Recovery\ HD\ Update/
if [ ${osvers} == 8 ]; then
hdiutil attach "/Applications/Install OS X Mountain Lion.app/Contents/SharedSupport/InstallESD.dmg"
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.dmg 0 0 /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.chunklist
hdiutil eject "/Volumes/Mac OS X Install ESD"
fi
## This is for Lion
if [ ${osvers} == 7 ]; then
## Check for Retina Display MacBook and 2012 Air check
Model=`system_profiler SPHardwareDataType | grep "Model Identifier:" | awk '{print $3}'`
if [ "$Model" == "MacBookPro10,1" -o "$Model" == "MacBookPro10,2" -o "$Model" == "MacBookAir5,1" -o "$Model" == "MacBookAir5,2" ]; then
echo "You are a $Model model which is a 2012 Macbook Pro or Air. This hardware model requires the Lion Installer in order to recover the parition. You will need to re-download the Lion Install App from the App Store in order to proceed."
if [ ! -d "/Applications/$Lion" ]; then
echo -e "\n \n"
echo -e "The Lion installer is not located in the Applications folder. Re-download Lion from the app store or place it in the Applications directory."
exit 1
fi
## Expand the meta partition
hdiutil attach /tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/RecoveryHDMeta.dmg
## Mount the Installer DMG inside of the Lion Application. This is only needed for 2012 Retina and Airs.
hdiutil attach "/Applications/Install OS X Lion.app/Contents/SharedSupport/InstallESD.dmg"
## Install recovery parition using dmtest tool from RecoveryPartition.dmg
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.dmg 0 0 /Volumes/Mac\ OS\ X\ Install\ ESD/BaseSystem.chunklist
hdiutil eject "/Volumes/Mac OS X Install ESD"
else
## This part takes the mounted image and creates the recovery partition.
/tmp/RecoveryHDUpdate/RecoveryHDUpdate.pkg/Scripts/Tools/dmtest ensureRecoveryPartition / /Volumes/Recovery\ HD\ Update/BaseSystem.dmg 0 0 /Volumes/Recovery\ HD\ Update/BaseSystem.chunklist
fi
hdiutil eject "/Volumes/Recovery HD Update"
hdiutil eject "/Volumes/Mac OS X Lion Recovery HD Update"
fi
;;
n|N ) echo "no";;
* ) echo "invalid";;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment