Mac Build Scripts

From FreekiWiki
Revision as of 14:14, 15 February 2007 by Tonyr (talk | contribs) (new stuff)
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.

# target device
target=$1
test $target || echo "no target specified"
test $target || exit 1
 
# starts and lengths for boot, linux and swap partitions
#
# known values
bootstart=64
bootlen=1954
swaplen=1494848
linuxstart=2018
 
# calculated values
linuxlen=$(($hdlen-(2017+$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"

# write an input script for fdisk
#
# initialize partition table, answer disk length query, and show the result
echo i > fdisk.script
echo "" >> fdisk.script
echo p >> fdisk.script

# create a boot partition with explicit partition size/type, and show result
echo C >> fdisk.script
echo $bootstart >> fdisk.script
echo $bootlen >> fdisk.script
echo untitled >> fdisk.script
echo Apple_Bootstrap >> fdisk.script
echo p >> fdisk.script

# create a linux native partition, and show result
echo c >> fdisk.script
echo $linuxstart >> fdisk.script
echo $linuxlen >> fdisk.script
echo c >> fdisk.script
echo p >> fdisk.script

# create the swap partition. and show result
echo $swapstart >> fdisk.script
echo $swaplen >> fdisk.script
echo swap >> fdisk.script
echo p >> fdisk.script

# write out the partition map and confirm
echo w >> fdisk.script
echo "" >> fdisk.script

# we done partishin' now
echo q >> fdisk.script







  1. !/bin/sh
  2. create a set of input commands for fdisk to use to partition a blank
  3. drive for Macintosh machines.
  4. Partitions are created in the same order that an Ubuntu installation creates
  5. them:
  6. 1. partition map
  7. 2. boot partition
  8. 3. linux partition
  9. 4. swap partition
  10. The partition map is automatically created with length 63. The boot
  11. partition start block (64) and size (1954) are known (from the Ubuntu model).
  12. The swap partition size is fixed arbitrary size, chosen to support 512Mb
  13. 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

}

  1. 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
  1. need one argument
  2. target device

target=$1 if [ ! $target ]; then

   echo "no target specified"
   exit 2
   fi
  1. 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
  1. target device should have no partitions
    1. need to find out what hdparm actually writes when no partition map ##
  2. n_partitions=$(( $(fdisk -l $target | grep $target | wc -l) - 1))
  3. if [ $n_partitions -ne 0 ]; then
  4. response=n
  5. echo "There appear to be existing partitions on $target"
  6. echo -n "Are you sure you want to continue? [Y/n]"
  7. read -r response
  8. if [ $response != 'Y' && $response != 'y' ]; then
  9. echo "Quitting"
  10. exit 4
  11. fi
  12. fi
  1. length of target drive
    1. get the hd geometry (hdparm), from that get (awk) the line that has
    2. "sectors" in it, and spit out the number of sectors from that line
    3. (sixth field); then remove (cut) the ',' from the end of the number;
    4. The hd length used for partition length calculations is actually one
    5. 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))

  1. starts and lengths for boot, linux and swap partitions
  2. known values

mapstart=1 maplen=63 bootlen=1954 swaplen=1494848

  1. calculated values

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

  1. debug
  2. echo "target= $target"
  3. echo "hdlen= $hdlen"
  4. echo "bootstart= $bootstart"
  5. echo "bootlen= $bootlen"
  6. echo "linuxstart= $linuxstart"
  7. echo "linuxlen= $linuxlen"
  8. echo "swapstart= $swapstart"
  9. echo "swaplen= $swaplen"
  10. calclen=$((63+$bootlen+$linuxlen+$swaplen))
  11. echo "calculated hdlen= $calclen"

genScript exit 0


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