Home | History | Annotate | Line # | Download | only in common
buildfloppies.sh revision 1.1
      1 #!/bin/sh
      2 #
      3 # $NetBSD: buildfloppies.sh,v 1.1 2002/04/16 04:28:23 lukem Exp $
      4 #
      5 # Copyright (c) 2002 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] 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 	base		basename of generated floppies
     54 	size		size of a floppy in 512 byte blocks
     55 	file [...]	file(s) to build
     56 _USAGE_
     57 	exit 1
     58 }
     59 
     60 plural()
     61 {
     62 	[ $1 -ne 1 ] && echo "s"
     63 }
     64 
     65 
     66 #	parse and check arguments
     67 #
     68 
     69 while getopts i:m:p opt; do
     70 	case ${opt} in
     71 	i)
     72 		instboot=${OPTARG} ;;
     73 	m)
     74 		maxdisks=${OPTARG} ;;
     75 	p)
     76 		pad=1 ;;
     77 	\?|*)
     78 		usage
     79 		;;
     80 	esac
     81 done
     82 
     83 shift $(( ${OPTIND} - 1 ))
     84 [ $# -lt 3 ] && usage
     85 floppybase=$1
     86 floppysize=$2
     87 shift 2
     88 files=$*
     89 
     90 #	setup temp file, remove existing images
     91 #
     92 floppy=floppy.$$.tar
     93 trap "rm -f ${floppy}" 0 1 2 3			# EXIT HUP INT QUIT
     94 rm -f ${floppybase}?.fs
     95 
     96 
     97 #	create tar file
     98 #
     99 dd if=/dev/zero of=${floppy} bs=8k count=1 2>/dev/null
    100 ${PAX} -w ${files} >> ${floppy} || exit 1
    101 	# XXX: use pax metafile and set perms?
    102 if [ -n "$instboot" ]; then
    103 	instboot=$( echo $instboot | sed -e s/@IMAGE@/${floppy}/ )
    104 	echo "Running instboot: ${instboot}"
    105 	eval ${instboot}
    106 fi
    107 
    108 #	check size against available number of disks
    109 #
    110 set -- `ls -l ${floppy}`
    111 bytes=$5
    112 blocks=$(( ${bytes} / 512 ))
    113 numdisks=$(( (${blocks} - 1) / ${floppysize} + 1 ))
    114 if [ -z "${maxdisks}" ]; then
    115 	maxdisks=${numdisks}
    116 fi
    117 
    118 if [ ${numdisks} -gt ${maxdisks} ]; then
    119 	excess=$(( ( ${blocks} - ${floppysize} * ${maxdisks} ) * 512 ))
    120 	echo 1>&2 \
    121 	    "$prog: Image is ${excess} bytes ($(( ${excess} / 1024 )) KB)"\
    122 	    "too big to fit on ${maxdisks} disk"$(plural ${maxdisks})
    123 	exit 1
    124 fi
    125 
    126 if [ -n "${pad}" ]; then
    127 	padsize=$(( ${floppysize} * ${maxdisks} ))
    128 	padcount=$(( ${padsize} - ${blocks} ))
    129 	echo \
    130 	    "Writing $(( ${padsize} * 512 )) bytes ($(( ${padsize} / 2 )) KB)" \
    131 	    "on ${numdisks} disk"$(plural ${numdisks})"," \
    132 	    "padded by $(( ${padcount} * 512 )) bytes" \
    133 	    "($(( ${padcount} / 2 )) KB)"
    134 else
    135 	echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
    136 	    "on ${numdisks} disk"$(plural ${numdisks})
    137 fi
    138 
    139 #	write disks
    140 #
    141 curdisk=1
    142 image=
    143 floppysize8k=$(( ${floppysize} / 16 ))
    144 while [ ${curdisk} -le ${numdisks} ]; do
    145 	image="${floppybase}${curdisk}.fs"
    146 	echo "Creating disk ${curdisk} to ${image}"
    147 	if [ ${curdisk} -eq 1 ]; then
    148 		seek=0
    149 		skip=0
    150 		: > ${image}
    151 	else
    152 		seek=1
    153 		skip=$(( (${curdisk} - 1) * (${floppysize8k} - 1) + 1 ))
    154 		echo USTARFS ${curdisk} > ${image}
    155 	fi
    156 	count=$(( ${floppysize8k} - ${seek} ))
    157 # echo 1>&2 " DEBUG: disk ${curdisk} seek=${seek} skip=${skip} count=${count}"
    158 	dd bs=8k conv=sync seek=${seek} skip=${skip} count=${count} \
    159 	    if=${floppy} of=${image} 2>/dev/null
    160 
    161 	curdisk=$(( ${curdisk} + 1 ))
    162 done
    163 
    164 #
    165 # XXX:	the old bootfloppy generation code used to zero the last 0.5k of the
    166 #	end of the image in single disk configs; that possibly trashed real
    167 #	data???
    168 #	is that functionality still required?
    169 #
    170 
    171 #	pad last disk if necessary
    172 #
    173 if [ -n "${pad}" ]; then
    174 	padseek=$(( ${floppysize} - ${padcount} ))
    175 # echo 1>&2 " DEBUG: padding ${image} with $(( ${padcount} * 512 )) at offset $(( ${padseek} * 512 ))"
    176 	dd seek=${padseek} count=${padcount} \
    177 	    if=/dev/zero of=${image} 2>/dev/null
    178 fi
    179 
    180 
    181 #	final status
    182 #
    183 echo "Final result:"
    184 ls -l ${floppybase}?.fs
    185 
    186 exit 0
    187