#!/bin/bash # Create a bootable harddisk (or compact flash) image from # a partition definition given from command line # # (C) 2006, 2011 Pengutronix # Usage() { cat <<-EOF Usage: `basename "$0"` OPTIONS -h this help -o resulting hd image -i filesystem image -p partition definition (can be given multiple times) A partition definition has the form start_sector:end_sector:partition_type:[imagefile] EOF } part_opts() { pstart=$(( $(echo ${PART[$1]} | awk -F: '{print $1}') )) pend=$(( $(echo ${PART[$1]} | awk -F: '{print $2}') )) ptype=$(( $(echo ${PART[$1]} | awk -F: '{print $3}') )) pimage=$(echo ${PART[$1]} | awk -F: '{print $4}') psize=$(($pend - $pstart + 1)) } partcount=0 while getopts "hm:n:o:p:" OPT do case "$OPT" in h) Usage exit 1 ;; o) IMAGEFILE="$OPTARG" ;; p) partcount=$(($partcount + 1)) PART[$partcount]="$OPTARG" ;; esac done if [ "$partcount" == 0 ]; then echo "need at least one partition definition" exit 1 fi if [ "$partcount" -gt 4 ]; then echo "a maximum of four partitions is allowed" exit 1 fi if [ -z "$IMAGEFILE" ]; then echo "no output image file given" exit 1 fi echo "------------------------" echo "Creating hard disk's MBR" echo "------------------------" # create the first 512 byte sector (MBR) # fill it up to offset 446 dd if=/dev/zero bs=446 count=1 > $IMAGEFILE 2>/dev/null # append the partitions # mark the first partition bootable extraarg="-c" echo "------------------------------------" echo "Creating hard disk's partition table" echo "------------------------------------" for i in 1 2 3 4; do part_opts "$i" if [ $i -gt $partcount ]; then psize=0 fi if [ ${psize} -ne 0 ]; then echo "Partition $i (start=$pstart, size=$psize type=$ptype)" fi # add 0x55aa magic to last partition in table if [ $i == 4 ]; then extraarg="-m" fi genpart $extraarg -b $pstart -s $psize -t $ptype >> $IMAGEFILE extraarg="" done for i in 1 2 3 4; do part_opts "$i" if [ -z "${pimage}" ]; then continue fi echo " Initializing partition $i with '$(basename $pimage)'" dd if="$pimage" of="$IMAGEFILE" bs=512 count="$psize" seek="$pstart" conv=notrunc 2>/dev/null done