Difference between revisions of "Mac Build Scripts"

From FreekiWiki
Jump to navigation Jump to search
(new stuff)
(update fdisk scriptgenl; start replicate script)
Line 1: Line 1:
 
==Generate fdisk script==
 
==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
+
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
 
  #!/bin/sh
 
  #
 
  #
 
  # create a set of input commands for fdisk to use to partition a blank
 
  # create a set of input commands for fdisk to use to partition a blank
# drive for Macintosh machines.
+
  # drive for Macintosh machines.
 
  #
 
  #
 
  # Partitions are created in the same order that an Ubuntu installation creates
 
  # Partitions are created in the same order that an Ubuntu installation creates
Line 16: Line 18:
 
  # The swap partition size is fixed arbitrary size, chosen to support 512Mb
 
  # 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.
 
  # 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
 
  # target device
 
  target=$1
 
  target=$1
  test $target || echo "no target specified"
+
  if [ ! $target ]; then
  test $target || exit 1
+
    echo "no target specified"
 
+
    exit 2
[[Category:Macintosh]]
+
    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
 
  # starts and lengths for boot, linux and swap partitions
 
  #
 
  #
 
  # known values
 
  # known values
  bootstart=64
+
  mapstart=1
 +
maplen=63
 
  bootlen=1954
 
  bootlen=1954
 
  swaplen=1494848
 
  swaplen=1494848
  linuxstart=2018
+
   
 
+
  #calculated values
  # calculated values
+
bootstart=$(($mapstart + $maplen))
  linuxlen=$(($hdlen-(2017+$swaplen)))
+
linuxstart=$(($bootstart + $bootlen))
 +
  linuxlen=$(($hdlen-($maplen + $bootlen + $swaplen)))
 
  swapstart=$(($linuxstart+$linuxlen))
 
  swapstart=$(($linuxstart+$linuxlen))
 
   
 
   
Line 48: Line 132:
 
  #echo "calculated hdlen= $calclen"
 
  #echo "calculated hdlen= $calclen"
 
   
 
   
  # write an input script for fdisk
+
genScript
 +
exit 0
 +
 
 +
==Replicate Mac Ubuntu Disk==
 +
------------------------------------------------------------
 +
  #!/bin/sh
 
  #
 
  #
  # initialize partition table, answer disk length query, and show the result
+
  # Replicate a Mac Ubuntu disk onto a 'clean' disk
  echo i > fdisk.script
+
  #
  echo "" >> fdisk.script
+
  # 1. Take two arguments: a source device and a target device
echo p >> fdisk.script
+
  # 2. Verify that the source device has a Mac Ubuntu installation
+
  # 3. Verify that the target device is a 'clean' disk
# create a boot partition with explicit partition size/type, and show result
+
  # 4. Create partitions on the target device
  echo C >> fdisk.script
+
  # 5. Create an ext3 filesystem on the linux partition of the target device
  echo $bootstart >> fdisk.script
+
  # 6. Use dd to copy the source boot partition to the target boot partition
  echo $bootlen >> fdisk.script
+
  # 7. Use rsync to copy the source linux partition to the target linux partition
  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
 
  
  
  
 
+
[[Category:Macintosh]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
---------------------------------------
 
#!/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
 
target=$1
 
if [ ! $target ]; then
 
    echo "no target specified"
 
    exit 2
 
    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
 
------------------------------------------------------------
 
#!/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
 

Revision as of 22:08, 15 February 2007

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
target=$1
if [ ! $target ]; then
    echo "no target specified"
    exit 2
    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