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