preimaging and postimaging with user segregation

franton
Valued Contributor III

Hi Folks,

So i've been doing a LOT of work lately on our deployment strategy, especially with regards to Bootcamp. One of the huge headaches that i've whinged on about involves Apple's new 3TB hard drive equipped iMacs and the very strange partitioning structure that you have to implement to get Bootcamp working. (Short version: it NEEDS CoreStorage or they're very unhappy).

I have finally got a solution for this. It also implements a separate user partition for the Users folder. Credit where credit is due: I've lifted major implementation ideas from Marko Jung's JNUC slides as well as chunks of bash code from people like Ben Toms.

I present two scripts that are to be run at imaging time. You need to set the pre image script in Casper Admin to run "Before" and the post image script to run "After". That'll ensure the order is correct.

Now I should point out that while my testing has been pretty exhaustive, you should do major amounts of testing yourself. Neither myself or where I work should be held responsible for any data loss! This could be pretty dangerous stuff to your hard drives, your data and your sanity!

First of all, here's the pre image script. What this should do is to check the hard drive to see if any bootcamp or user partitions exist. If not, the drive is destructively partitioned so that they do exist. If they do, there's a check to see if they're in CoreStorage format. If they are, they are demoted down to standard partitioning. (In testing i've found Casper 8.62 really doesn't like imaging if they are).

#!/bin/bash

# Pre-imaging script for Casper Imaging (Bootcamp Version)

# Author      : r.purves@arts.ac.uk
# Version 1.0 : 05-04-2013 - Initial Version

# This script will destructively partition a hard drive, IF the partitions don't exist.
# The partition list it will create are as follows.
# 0 - GUID_partition_scheme
# 1 - EFI - 209.7 MB
# 2 - Macintosh HD - 30%
# 3 - BOOTCAMP - 30%
# 4 - Users - whatever is left.

# Check to see if the Users partition exists.

CHECKusers=$( diskutil list | grep -c Users )

# Bootcamp is a special case where the Volume name could be anything,
# We shall check for the Microsoft Basic Partition GUID instead. (STDERR directed to /dev/null)
# Reason for checking that is there will only be one of those present tops.

CHECKbootcamp=$( gpt -r show rdisk0 2>/dev/null | grep -c EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 )

# Finally let's see if there are any pre-existing CoreStorage volumes present.
# We'll get a return of 0 for yes and 1 for no.

ISCS=$( diskutil cs list | grep -c "No " )

# Ok let's start by completely destroying the existing partition structure
# and replacing it with our own, but only IF the Bootcamp and Users folders do not exist.

if [ $CHECKbootcamp -lt 1 ] && [ $CHECKusers -lt 1 ] ;
then

# Volumes exist, but are they CoreStorage? If so, delete the Logical Volume Groups
# diskutil will not be happy if they are still there at re-partitioning time.

    if [ $ISCS -eq 0 ];
    then

# Loop and delete the LVG's

        while read -r line;
        do
            diskutil cs delete $line
        done < <( diskutil cs list | grep "Logical Volume Group" | awk '{print$5}' )

    fi

# Create the new partition structure

    diskutil partitionDisk disk0 3 GPT JHFS+ Macintosh HD 30% FAT32 BOOTCAMP 30% JHFS+ Users R

fi

# At this point there should be Macintosh HD, Users and BOOTCAMP Volumes present
# Are any of these in CoreStorage?

if [ $ISCS -eq 0 ];
then

# Loop and revert the Logical Volume's back to standard partitioning.
# This is needed to Casper Imaging 8.62 can actually function properly.
# (We'll put CS back on in the postimage script)

    while read -r line;
    do
        diskutil cs revert $line
    done < <( diskutil cs list | grep "Logical Volume" | grep -v "Logical Volume Family" | grep -v "Logical Volume Group" | cut -c 28- )

fi

# OS needs time for things to settle.

sleep 10

# All done!

exit 0

Now here is the post imaging script. What this should do is to follow Ben Toms' excellent fstab creation script and set all that up. Afterwards, it'll convert the existing Macintosh HD and User volumes back into CoreStorage format.

#!/bin/sh

# Post-imaging script for Casper Imaging.
# This will set up the user segregation and then put Macintosh HD and Users into CoreStorage
# Script lovingly stolen from http://macmule.com/2012/07/31/how-to-use-fstab-within-a-casper-imaging-workflow/

# Implemented : r.purves@arts.ac.uk
# Version 1.0 : 05-04-2013 - Initial Version

# Find the UUID of the Users partition

usersUUID=`diskutil info Users | grep Volume UUID: | awk '{print $3}'`

# Create/Append to the fstab file on the boot partition

echo > /Volumes/Macintosh HD/private/etc/fstab "UUID=$usersUUID /Users hfs rw"

# Fix the permissions on the fstab file

chown root /Volumes/Macintosh HD/private/etc/fstab
chmod 755 /Volumes/Macintosh HD/private/etc/fstab

# Now we have to convert the appropriate partitions to CoreStorage
# This is for compatibility with 3TB HD iMacs

# Find out which disk identifier is used by which partition

MACHD=$( diskutil list | grep "Macintosh HD" | grep -v "Macintosh HD 1" | awk '{print $7}' )
USERHD=$( diskutil list | grep Users | awk '{print $6}' )

# Do the CoreStorage conversion here

diskutil cs convert $MACHD
diskutil cs convert $USERHD

# OS needs time for things to settle.

sleep 10

# All done!

exit 0

I'm fairly sure this isn't as technically nice as the Oxford University scripts but i'm sure they can point you all in the right direction.

Once again I stress that you test test and TEST again before you go live with these scripts. I'll be spending most of next week doing exactly that.

0 REPLIES 0