Mac Build Scripts

From FreekiWiki
Revision as of 23:09, 15 February 2007 by Tonyr (talk | contribs)
Jump to navigation Jump to search

Generate fdisk script

This script takes a raw device argument like /dev/hda and writes out a set of commands that can be fed to fdisk to automatically define a partition table for a Mac. The commands are written to a file named fdisk.script, but that can be changed to stdout, if this script needs to called from another script, for example. It is currently only tested for G3 towers, and needs more work and testing


#!/bin/sh
#
# create a set of input commands for fdisk to use to partition a blank
# drive for Macintosh machines.
#
# Partitions are created in the same order that an Ubuntu installation creates
# them: 
#   1. partition map
#   2. boot partition
#   3. linux partition
#   4. swap partition
# The partition map is automatically created with length 63.  The boot
# partition start block (64) and size (1954) are known (from the Ubuntu model). 
# The swap partition size is fixed arbitrary size, chosen to support 512Mb
# of ram and to allow the swap and linux partition lengths to be even.
 
 scriptName="fdisk.script"
 
 genScript () {
     # generate and write the commands, creating the script file
     #
  
     local target scriptName
 
     # initialize partition table, answer disk length query, and show the result
     echo i > $scriptName
     echo "" >>  $scriptName
     echo p >> $scriptName
     
     # create a boot partition with explicit partition size/type, and show result
     echo C >> $scriptName
     echo $bootstart >> $scriptName
     echo $bootlen >> $scriptName
     echo untitled >> $scriptName
     echo Apple_Bootstrap >> $scriptName
     echo p >> $scriptName
    
     # create a linux native partition, and show result
     echo c >> $scriptName
     echo $linuxstart >> $scriptName
     echo $linuxlen >> $scriptName
     echo c >> $scriptName
     echo p >> $scriptName
     
     # create the swap partition. and show result
     echo $swapstart >> $scriptName
     echo $swaplen >> $scriptName
     echo swap >> $scriptName
     echo p >> $scriptName
    
     # write out the partition map and confirm
     echo w >> $scriptName
     echo "" >> $scriptName
    
     # we done partishin' now
     echo q >> $scriptName
}
    
# first of all, best be root to do this
if [ $(whoami) != 'root' ]; then
    echo "You must be root to run this script; try using sudo"
    exit 1
    fi

# need one argument
# target device
if [ -z $1 ]; then
    echo "no target specified"
    exit 2
else
    target=$1
fi

# target device has to exist
hdparm -g $target > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
    echo "device $target does not seem to exist"
    exit 3
    fi

# target device should have no partitions
## need to find out what hdparm actually writes when no partition map ##
# n_partitions=$(( $(fdisk -l $target | grep $target | wc -l) - 1))
# if [ $n_partitions -ne 0 ]; then
#     response=n
#     echo "There appear to be existing partitions on $target"
#     echo -n "Are you sure you want to continue? [Y/n]"
#     read -r response
#     if [ $response != 'Y' && $response != 'y' ]; then
#         echo "Quitting"
#         exit 4
#         fi
#     fi

# length of target drive
## get the hd geometry (hdparm), from that get (awk) the line that has 
## "sectors" in it, and spit out the number of sectors from that line 
## (sixth field); then remove (cut) the ',' from the end of the number;
## The hd length used for partition length calculations is actually one
## less that the true disk length, since block zero is ignored.
hdlen=$(hdparm -g $target | awk '/sector/ {print $6}' | cut -d ',' -f 1)
hdlen=$(($hdlen - 1))

# starts and lengths for boot, linux and swap partitions
#
# known values
mapstart=1
maplen=63
bootlen=1954
swaplen=1494848

#calculated values
bootstart=$(($mapstart + $maplen))
linuxstart=$(($bootstart + $bootlen))
linuxlen=$(($hdlen-($maplen + $bootlen + $swaplen)))
swapstart=$(($linuxstart+$linuxlen))

# debug
#echo "target=           $target"
#echo "hdlen=            $hdlen"
#echo "bootstart=        $bootstart"
#echo "bootlen=          $bootlen"
#echo "linuxstart=       $linuxstart"
#echo "linuxlen=         $linuxlen"
#echo "swapstart=        $swapstart"
#echo "swaplen=          $swaplen"
#calclen=$((63+$bootlen+$linuxlen+$swaplen))
#echo "calculated hdlen= $calclen"

genScript
exit 0

Replicate Mac Ubuntu Disk


#!/bin/sh
#
# Replicate a Mac Ubuntu disk onto a 'clean' disk
#
# 1. Take two arguments: a source device and a target device
# 2. Verify that the source device has a Mac Ubuntu installation
# 3. Verify that the target device is a 'clean' disk
# 4. Create partitions on the target device
# 5. Create an ext3 filesystem on the linux partition of the target device
# 6. Use dd to copy the source boot partition to the target boot partition
# 7. Use rsync to copy the source linux partition to the target linux partition