Home | History | Annotate | Line # | Download | only in common
buildfloppies.sh revision 1.8
      1 #!/bin/sh
      2 #
      3 # $NetBSD: buildfloppies.sh,v 1.8 2004/06/12 18:39:53 dsl Exp $
      4 #
      5 # Copyright (c) 2002-2003 The NetBSD Foundation, Inc.
      6 # All rights reserved.
      7 #
      8 # This code is derived from software contributed to The NetBSD Foundation
      9 # by Luke Mewburn of Wasabi Systems.
     10 #
     11 # Redistribution and use in source and binary forms, with or without
     12 # modification, are permitted provided that the following conditions
     13 # are met:
     14 # 1. Redistributions of source code must retain the above copyright
     15 #    notice, this list of conditions and the following disclaimer.
     16 # 2. Redistributions in binary form must reproduce the above copyright
     17 #    notice, this list of conditions and the following disclaimer in the
     18 #    documentation and/or other materials provided with the distribution.
     19 # 3. All advertising materials mentioning features or use of this software
     20 #    must display the following acknowledgement:
     21 #        This product includes software developed by the NetBSD
     22 #        Foundation, Inc. and its contributors.
     23 # 4. Neither the name of The NetBSD Foundation nor the names of its
     24 #    contributors may be used to endorse or promote products derived
     25 #    from this software without specific prior written permission.
     26 #
     27 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37 # POSSIBILITY OF SUCH DAMAGE.
     38 #
     39 
     40 # set defaults
     41 #
     42 : ${PAX=pax}
     43 prog=${0##*/}
     44 
     45 
     46 usage()
     47 {
     48 	cat 1>&2 << _USAGE_
     49 Usage: ${prog} [-i instboot] [-m max] [-p] [-s suffix] base size file [...]
     50 	-i instboot	run instboot to install a bootstrap on @IMAGE@
     51 	-m max		maximum number of floppies to build
     52 	-p		pad last floppy to floppy size
     53 	-s suffix	suffix for floppies
     54 	base		basename of generated floppies
     55 	size		size of a floppy in 512 byte blocks
     56 	file [...]	file(s) to build
     57 _USAGE_
     58 	exit 1
     59 }
     60 
     61 plural()
     62 {
     63 	[ $1 -ne 1 ] && echo "s"
     64 }
     65 
     66 roundup()
     67 {
     68 	echo $(( ( $1 + $2 - 1 ) / ( $2 ) ))
     69 }
     70 
     71 
     72 #	parse and check arguments
     73 #
     74 
     75 while getopts i:m:ps: opt; do
     76 	case ${opt} in
     77 	i)
     78 		instboot=${OPTARG} ;;
     79 	m)
     80 		maxdisks=${OPTARG} ;;
     81 	p)
     82 		pad=1 ;;
     83 	s)
     84 		suffix=${OPTARG} ;;
     85 	\?|*)
     86 		usage
     87 		;;
     88 	esac
     89 done
     90 
     91 shift $(( ${OPTIND} - 1 ))
     92 [ $# -lt 3 ] && usage
     93 floppybase=$1
     94 floppysize=$2
     95 shift 2
     96 files=$*
     97 
     98 #	setup temp file, remove existing images
     99 #
    100 floppy=floppy.$$.tar
    101 trap "rm -f ${floppy}" 0 1 2 3			# EXIT HUP INT QUIT
    102 rm -f ${floppybase}?${suffix}
    103 
    104 #	create tar file
    105 #
    106 dd if=/dev/zero of=${floppy} bs=8k count=1 2>/dev/null
    107 ${PAX} -O -w ${files} >> ${floppy} || exit 1
    108 	# XXX: use pax metafile and set perms?
    109 if [ -n "$instboot" ]; then
    110 	instboot=$( echo $instboot | sed -e s/@IMAGE@/${floppy}/ )
    111 	echo "Running instboot: ${instboot}"
    112 	eval ${instboot} || exit 1
    113 fi
    114 
    115 #	check size against available number of disks
    116 #
    117 bytes=$( ls -l "${floppy}" | awk '{print $5}' )
    118 blocks=$(roundup ${bytes} 512)
    119 	# when calculating numdisks, take into account:
    120 	#	a) the image already has an 8K tar header prepended
    121 	#	b) each floppy needs an 8K tar volume header
    122 numdisks=$(roundup ${blocks}-16 ${floppysize}-16)
    123 if [ -z "${maxdisks}" ]; then
    124 	maxdisks=${numdisks}
    125 fi
    126 
    127 if [ ${numdisks} -gt ${maxdisks} ]; then
    128 	excess=$(( ( ${blocks} - (${floppysize} - 16) * ${maxdisks} ) * 512 ))
    129 	echo 1>&2 \
    130 	    "$prog: Image is ${excess} bytes ($(( ${excess} / 1024 )) KB)"\
    131 	    "too big to fit on ${maxdisks} disk"$(plural ${maxdisks})
    132 	exit 1
    133 fi
    134 
    135 padsize=$(( ${floppysize} * ${maxdisks} ))
    136 padcount=$(( ${padsize} - ${blocks} ))
    137 if [ -n "${pad}" ]; then
    138 	echo \
    139 	    "Writing $(( ${padsize} * 512 )) bytes ($(( ${padsize} / 2 )) KB)" \
    140 	    "on ${numdisks} disk"$(plural ${numdisks})"," \
    141 	    "padded by $(( ${padcount} * 512 )) bytes" \
    142 	    "($(( ${padcount} / 2 )) KB)"
    143 else
    144 	echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
    145 	    "on ${numdisks} disk"$(plural ${numdisks})"," \
    146 	    "free space $(( ${padcount} * 512 )) bytes" \
    147 	    "($(( ${padcount} / 2 )) KB)"
    148 fi
    149 
    150 #	write disks
    151 #
    152 curdisk=1
    153 image=
    154 seek=0
    155 skip=0
    156 floppysize8k=$(( ${floppysize} / 16 ))
    157 while [ ${curdisk} -le ${numdisks} ]; do
    158 	image="${floppybase}${curdisk}${suffix}"
    159 	echo "Creating disk ${curdisk} to ${image}"
    160 	if [ ${curdisk} -eq 1 ]; then
    161 		: > ${image}
    162 	else
    163 		echo USTARFS ${curdisk} > ${image}
    164 	fi
    165 	count=$(( ${floppysize8k} - ${seek} ))
    166 	dd bs=8k conv=sync seek=${seek} skip=${skip} count=${count} \
    167 	    if=${floppy} of=${image} 2>/dev/null
    168 
    169 	curdisk=$(( ${curdisk} + 1 ))
    170 	skip=$(( $skip + $count ))
    171 	seek=1
    172 done
    173 
    174 #	pad last disk if necessary
    175 #
    176 if [ -n "${pad}" ]; then
    177 	dd if=$image of=$image conv=notrunc conv=sync bs=${floppysize}b count=1
    178 fi
    179 
    180 
    181 #	final status
    182 #
    183 echo "Final result:"
    184 ls -l ${floppybase}?${suffix}
    185 
    186 exit 0
    187