Macintosh‎ > ‎

Dual boot shell script

posted May 25, 2008, 11:23 PM by Tony Williams   [ updated May 25, 2008, 11:29 PM ]
Here is the shell script I developed for fixing a dual boot Mac after imaging both halves. See my blog post for an explanation of how it was developed and why.


# Some code for post Windows image install but before sysprep
# this code assumes you have either installed ntfs-progs or grabbed
# the statically linked files from a copy of NetRestore and installed in
# /usr/local/tools
# Also assumes MacFUSE and ntfs-3g are installed
# MUST run with root privileges

# name, password and location of the SMB share with our Mac drivers

# grab the details of Windows partition (shouldn't be ours, should be first fusefs)

# ask df to print details of any Windows volumes and get awk to print first field
# of 2nd line from df output
# (take careful note of the ticks in line below
#  outside are backticks then ordinary around awk command)
WIN_PART=`df -T fusefs | awk 'NR == 2 { print $1 }' `

if ( ! $WIN_PART ) ; then exit ; fi # we don't have a windows partition so give up now

# then repeat with field 6 for volume name
WIN_NAME=`df -T fusefs | awk 'NR == 2 { print $6 }' `

# then use shell variable rewriting (substitute the bit we don't want with nothing)
# to grab the slice number off the partition name

# grab MBR and stash in /tmp/mbr
# (magic numbers in skip and count courtesy of Mike Bombich's NetRestore notes)
/bin/dd if=$WIN_NAME/WINDOWS/system32/dmadmin.exe of=/tmp/mbr \
 skip=216616 count=446 bs=1

# rewrite boot.ini
# (also courtesy of Mike)
/usr/bin/perl -p -i -e "s/partition\(.\)/partition\($WIN_SLICE\)/g" \

# mount share that hold drivers  (\\rumbold\express\drivers\IntelMac)
mkdir /Volumes/drivers

/sbin/mount_smbfs $DRIVERS_MNT /Volumes/drivers

# copy drivers from share to the right place for sysprep
cp -r /Volumes/drivers /Volumes/Untitled

umount -f $WIN_NAME

# Sync MBR

/usr/local/tools/gptrefresh -w -a $WIN_SLICE -f -u -m /tmp/mbr /dev/disk0

# bless windows partition
/usr/sbin/bless --device /dev/$WIN_PART --setBoot --legacy

# fiddle the MBR by stuffing the commands into fdisk
printf "flag ${WIN_SLICE}\nwrite\ny\nq\n" | /usr/sbin/fdisk -e /dev/rdisk0

# shutting down here to let EFI reload everything then have sysprep run
# exit with fanfares and joy - we slew the mighty Windows

shutdown -h now