#!/bin/bash
tempfile=/var/tmp/ptemp
pl=""
pl="${pl} Service_Lion=17"
pl="${pl} Service_Snow=15"
pl="${pl} Service_Leopard=23"
pl="${pl} Service_Tiger=12"
pl="${pl} Mac_OS_10.3.4=1"
pl="${pl} Mac_OS_10.3.4_Disc_2=1"
pl="${pl} Mac_OS_10.4.6=4"
pl="${pl} Mac_OS_10.4.7=6"
pl="${pl} Mac_OS_10.5.6=8"
pl="${pl} Mac_OS_10.6.3=9"
pl="${pl} Mac_OS_10.7.0=9"
#pl="${pl} Service_Data" # Service Data will take the remainder
pl=${pl:1}
# count partitions
plc=$(echo "$pl" | sed 's/[^ ]//g')
plc=${#plc}
((plc++)) # add one for diff in spaces vs entries
((plc++)) # add one for Data
# calculate space required (in GB)
nsize=20 # set aside at least this many GB for Service data
p="$pl "
while [ -n "$p" ] ; do
pp=${p%% *}
p=${p#* }
pz=${pp#*=}
((nsize+=pz))
done
# fyi
clear
echo
echo "PHR partitioner"
echo
echo "This script will FORMAT a hard drive. All information on"
echo "the flash drive will be lost! You will need a capacity of"
echo "at least ${nsize} GB."
echo
if [ $EUID != 0 ] ; then
admins=" $(dscl . -read /Groups/admin | grep GroupMembership | cut -c 23-999) "
if [ -z "$(echo "$admins" | grep " ${USER} ")" ] ; then
echo "You must be logged in as an administrator to run this installer."
exit
fi
sudo -k
echo "Enter your administrator password to continue:"
sudo -v
if [ $? != 0 ] ; then
echo "Cannot continue without password. If your password is blank,"
echo "you will need to change it to something non blank."
exit
fi
sudo "$0"
exit
fi
echo
echo "Authenticated as an administrator."
echo
echo
# build list of eligible destinations, into array
echo "Checking attached volumes..."
echo
diskutil list | grep "^/dev/disk" > $tempfile
dc=0
options=""
while read d ; do
echo -n "Found device $d... "
if [ "$d" == "/dev/disk0" ] ; then
echo "(booted)"
continue
elif [ -z "$(diskutil info $d | grep "Ejectable" | grep -i "yes")" ] ; then
echo "(not ejectable)"
continue
elif [ -z "$(diskutil info $d | grep " Read" | grep -i "no")" ] ; then
echo "(read-only)"
continue
elif [ -n "$(diskutil info $d | grep " Protocol" | grep -i "Disk Image")" ] ; then
echo "(disk image)"
continue
else
ds=$(diskutil info $d | grep "^ \+Total Size" | cut -d "(" -f 2 | cut -d " " -f 1)
((ds=ds/1024/1024/1024)) # capacity in GB
if [ $ds -lt $nsize ] ; then
echo "($ds GB is too small to install onto)"
continue
fi
fi
echo "(eligible)"
devs[dc]=$d
((dc++))
options="$options $d"
done < $tempfile
rm $tempfile
# verify at least one eligible device
if [ $dc == 0 ] ; then
echo
echo "No eligible volumes found."
echo
echo "Conncet a hard drive and rerun script to partition for PHR"
echo
exit
fi
options=${options:1}
echo
echo
# list partitions on each eligible device
for ((di=0;di<dc;di++)) ; do
d=${devs[di]}
echo "Listing partitions on device $d..."
diskutil list $d | grep " ${d#/dev/}s[0-9]*$"
echo
done
echo
echo
# select destination device
echo "Select device to repartition:"
select d in $options ; do
if [ -n "$d" ] ; then
echo
echo "Device selected: $d"
break
fi
done
echo
# get device size
ds=$(diskutil info $d | grep "^ \+Total Size" | cut -d "(" -f 2 | cut -d " " -f 1)
((ds=ds/1024/1024/1024)) # convert bytes to gb
echo "$nsize GB is required to install PHR. The selected device is $ds GB in capacity."
# verify adequate capacity
if [ $ds -lt $nsize ] ; then
echo
echo "Sorry, we require at least a $nsize MB flash drive"
echo
exit
fi
# last chance
echo
echo "The following partitions will be ERASED:"
diskutil list $d | grep " ${d#/dev/}s[0-9]*$"
echo
echo -n $'\a\a\a'
while true ; do
read -p "Type \"FORMAT\" to erase and reformat $d: " c
if [[ ("$c" == "FORMAT") || ("$c" == "format") ]] ; then
break
fi
echo $'\a'
done
echo
echo
# unmount volumes before attempting repartition
echo "Unmounting volumes on $d..."
diskutil list $d | grep " ${d#/dev/}s[0-9]*$" | while read p ; do
pd="/dev/${p##* }"
echo -n " unmounting $pd... "
hdiutil unmount $pd 2> /dev/null > /dev/null
if [ $? == 0 ] ; then
echo "done."
else
echo "unable to unmount. Be sure all files on device are closed."
exit
fi
done
echo
echo
# repartition device. takes awhile
echo "Creating partition map. This will take a few minutes:"
echo
c="diskutil partitionDisk $d $plc APM"
for p in $pl ; do
pv=${p%=*}
pg=${p#*=}
pv="'"$(echo "$pv" | sed "s/_/ /g")"'"
c="$c JHFS+ ${pv} ${pg}G"
done
c="$c JHFS+ 'Service Data' R" # Data volume takes remainder
echo
echo "Past this command into terminal:"
echo
echo "$c"
echo
exit