Using a script to deploy a restore partition

milesleacy
Valued Contributor

Hello all,

In the interest of promoting 'no-touch deploys', here is a script that I run
as a 'before' script to deploy a Restore partition when imaging new machines
with Casper.

This runs and has been tested in 10.5.2. Even if you are running Tiger in
your organization, you can use this script if your restore/utility images
are Leopard.

Note: In my environment, the /Users directory lives on a partition called
Data. I recommend this configuration as it allows you to do whatever you
like to the boot drive without worrying about your clients' data.

################################################
#!/bin/sh

## SCRIPT PARAMETERS
volume='/Volumes/Macintosh HD'
mac='20g'
restore='20g'
data='0b'

# If restore partition does not exist, resize Macintosh HD and create
Restore and Data partitions.
if test -e /Volumes/Restore then # Restore partition already exists. echo "Restore partition already exists." else # Restore partition does not exist. echo "Resizing Partitions..." /usr/sbin/diskutil resizeVolume "$volume" $mac JHFS+ Restore
$restore JHFS+ Data $data
fi

# Copy the restore image to the data partition
echo "Copying Restore image..."
ditto /Volumes/CasperShare/Packages/Restore_Image_Leopard.dmg
/Volumes/Data/Packages/Restore_Image_Leopard.dmg

# Apply Restore image with asr
echo "Restore the Restore with ASR"
/usr/sbin/asr restore -source
/Volumes/Data/Packages/Restore_Image_Leopard.dmg -target /Volumes/Restore
-erase -noprompt -noverify

# Remove cached package from Data partition
rm -Rf /Volumes/Data/Packages

echo "Restore image has been deployed."

################################################

-- Miles Leacy
Senior Macintosh Technician
Polo Ralph Lauren
212-318-7603
miles.leacy at poloralphlauren.com

11 REPLIES 11

tlarkin
Honored Contributor

Thanks,

I can also see this script being useful if deploying dual boot images of OS X and another OS.

Thomas Larkin
TIS Department
KCKPS USD500
tlarki at kckps.org
cell: 913-449-7589
office: 913-627-0351

jchurch
Contributor II

ok, so if i just want to resize macintosh hd and add a 15gig restore partition how would i go about it?

you have to specify what size you want macintosh hd to be after the resize. my machines all have different size hard drives so a given size for macintosh hd or the % option won't give constant results.

how would i get macintosh hd to be just 15g smaller then it is now and make the rest my restore partition?

thanks

Joe

davidacland
Honored Contributor II
Honored Contributor II

@jchurch

Assuming you've got a core storage volume, you can use diskutil cs resizeVolume. From the man page, something like:

diskutil cs resizeVolume C5A4E081-2CF1-4518-B31B-80E0AC011333 480g HFS+ Recovery 0b

Replacing "C5A4E081-2CF1-4518-B31B-80E0AC011333" with the actual UUID for your Macintosh HD.

jchurch
Contributor II

but this is assuming that you know the initial size of the drive. in our environment drive size range from under 100gig to over 3tb and everything in between.

i need to be able to run the same script on machines with 250 gig drive and 2tb drives and end up with a 15 gig restore partition the rest the boot volume and no waisted space.

i just can't figure out how.

thanks in advance.

davidacland
Honored Contributor II
Honored Contributor II

You would use diskutil cs list to get the data, grep to get the right line and sed or awk to grab the specific string you need. Then you calculate the desired partition size based on the size of the drive.

Probably something like

if [ value -lt drive size ]; then

Partition to size x

fi

Happy to write it out if you're stuck but getting to grips with grep, sed and awk is a good skill to get the hang of.

jchurch
Contributor II

that would be huge. i just can't wrap my head around getting it to do the math to find the new size.

and i agree. I've seen grep, sed, and awk used a lot. def need to brush up on them.

thanks

davidacland
Honored Contributor II
Honored Contributor II

Hi,

Here's the script. Make sure you test it on a spare Mac with no important data on it as distil commands always have a risk of data loss.

#!/bin/sh

# Check there is only one volume to partition
UUIDCount=$(diskutil cs list | grep "Logical Volume" | grep -v -E -c 'Logical Volume Group|Logical Volume Family')
if [ $UUIDCount -gt 1 ]; then
    echo "There is more than one partition, exiting script"
    exit 1
fi

# Get the UUID of your CS volume
UUID=$(diskutil cs list | grep "Logical Volume" | grep -v -E 'Logical Volume Group|Logical Volume Family' | awk '{print $4}')
echo "UUID is $UUID"

# Get the size of the disk in GBs
volumeSize=$(diskutil cs list | grep "Size (Total):" | awk '{print $5}' | sed 's/(//g' | awk '{printf("%d
",$1 + 0.5)}')
echo "Volume size is currently ${volumeSize}GB"

# Work out volume size, less 15GB
newVolumeSize=$(expr $volumeSize - 15)
echo "Intended new volume size is ${newVolumeSize}GB"

# Resize volume
diskutil cs resizeVolume $UUID ${newVolumeSize}g HFS+ Recovery 0b

exit 0

milesleacy
Valued Contributor

Thanks @davidacland! I agree 100% with your solution. I'm only commenting to address another point from my original post.

It that post, I recommended moving home folders to a separate volume from the boot volume. That was over seven years ago. I've learned some things since then and some things have changed. As a result, I don't really recommend this anymore. My recommendation re: user data these days is to use an automated backup tool. There are many on the market. I might happen to prefer one that helps you Plan for Crashes, but you should do your due diligence when selecting a product.

davidacland
Honored Contributor II
Honored Contributor II

@milesleacy totally agree about home folders. I used to like the separate partition method but stopped using it a few OS versions ago. It just started creating more problems than it solved for me.

Does this hold the record for the longest living jamfnation thread?

milesleacy
Valued Contributor

@davidacland Probably. At least until some wiseguy reads this and resurrects an older one. ;-)

jchurch
Contributor II

perfect, I'm not working with core storage volumes, but i can adjust for that .

thanks