drewreece
2/4/2015 - 7:02 AM

Install Apple Hardware Test to a volume and bless the AHT

Install Apple Hardware Test to a volume and bless the AHT

#!/bin/sh
#
# AHT My Disk
#
# Copy Apple Hardware Test to a volume and bless the hardware test to be bootable
#
# Useful list of AHT & models from upekkha - https://github.com/upekkha/AppleHardwareTest  thx u :)
# find your model details & get the AHT disk image
## sysctl hw.model | awk '{ print $2 }'
## ioreg -l | grep board-id | awk -F\" '{ print $4 }'
#
#
#
#
bn=`basename "$0"`

USAGE=$(cat <<HEREDOC
USAGE:
    $bn disk-image-path /path-to-disk

    Destination will not be erased, just copied into
    Legacy disk image formats will be converted to work correctly
    Visit https://github.com/upekkha/AppleHardwareTest for AHT image links
    to determine version & model details for obtaining AHT...

        sysctl hw.model | awk '{ print $2 }'
        ioreg -l | grep board-id | awk -F\" '{ print $4 }'
HEREDOC
)

usage (){
    echo "${USAGE}"
    exit 1
}

# HELP
if [ ! -f "$1"  ]
then
    echo "ERROR: argument 1 need to be the source AHT disk image"
    echo ""
    usage
fi

if [ ! -d "$2"  ]
then
    echo "ERROR: argument 2 need to be the destination disk"
    echo ""
    usage
fi

SOURCE="$1"
DEST="$2"

# Do not overwrite any existing System folder
if [ -d "${DEST}/System" ]
then
    echo "--> Warning!"
    echo "--> Found System on destination: ${DEST}"
    echo "--> Check this is correct & remove from: ${DEST}, exiting..."
    exit 1
fi

echo "Will copy content from ${SOURCE} into ${DEST}..."
echo "Do you want to continue? [ Y or N ]"
read answer
if [[ $answer == 'Y' ]] || [[ $answer == 'y' ]]; then
    echo "--> Will use sudo for bless stage - enter admin password at prompt"
else
    echo "--> Cancelled"
    exit 1
fi

# Convert older disk image formats if required
# UDCO = legacy format, UDZO = good
FORMAT=$(hdiutil imageinfo -format "${SOURCE}")
if [[ "$FORMAT" == "UDCO" ]]; then
    echo "--> Disk image appears to be legacy format, converting..."
    hdiutil convert "${SOURCE}" -format UDZO -o "${SOURCE}-converted.dmg" -quiet
    mv "${SOURCE}" "${SOURCE}-original.dmg"
    mv "${SOURCE}-converted.dmg" "${SOURCE}"
    echo "--> Original  : ${SOURCE}-original.dmg"
    echo "--> Converted : ${SOURCE}"
fi


# enable perms on dest?
echo "--> Mounting disk image..."
DISK=`hdiutil attach "${SOURCE}" | grep '/Volumes/' | cut -f3`
# If this attach fails we copy from /System !
if [ -z "${DISK}" ]
then
    echo "--> Failed to mount disk image, exiting..."
    exit 1
fi

# copy system to dest
echo "--> Copying system..."
cp -r "${DISK}/" "${DEST}/"
echo "Disk image: `basename ${SOURCE}`" >> "${DEST}/AHT-version.txt"
echo "Creation  : $(date)" >> "${DEST}/AHT-version.txt"
echo "Source    : https://gist.github.com/drewreece/2e5eed7dbfbd5dd7e929" >> "${DEST}/AHT-version.txt"
echo "Created by: $bn" >> "${DEST}/AHT-version.txt"

# Create command file to bless AHT via clickable file on AHT
echo "#!/bin/sh" > "${DEST}/bless-this-AHT.command"
echo "sudo -p 'Enter admin password to use bless: ' bless --folder ${DEST}/System/Library/CoreServices/.diagnostics --setBoot --file ${DEST}/System/Library/CoreServices/.diagnostics/diags.efi" >> "${DEST}/bless-this-AHT.command"
chmod +x "${DEST}/bless-this-AHT.command"

echo "--> Bless system..."
# Bless... (use --mount instead of --folder?) --folder will edit partition data (probing it for info) --mount doesn't edit partition, may be safer on some systems (according to refind)
sudo -p "Enter admin password: " bless --folder "${DEST}/System/Library/CoreServices/.diagnostics" --setBoot --file "${DEST}/System/Library/CoreServices/.diagnostics/diags.efi"  #--verbose
hdiutil detach "${DISK}" -quiet
#bless --info "${DEST}"
# To see what we made
# open "${DEST}"


exit  0