buildfloppies.sh revision 1.11 1 #!/bin/sh
2 #
3 # $NetBSD: buildfloppies.sh,v 1.11 2005/09/04 19:29:31 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 # Try to accurately summarise free space
105 #
106 [ "$files" = "boot netbsd" ] && {
107 set -- $(ls -ln boot)
108 boot_size=$5
109 boot_pad=$(($(roundup $boot_size 512) * 512 - $boot_size))
110 set -- $(ls -ln netbsd)
111 netbsd_size=$5
112 netbsd_pad=$(($(roundup $netbsd_size 512) * 512 - $netbsd_size))
113 echo Free space: boot $boot_pad, netbsd $netbsd_pad, \
114 at end $(($maxdisks * ($floppysize - 16) * 512 \
115 - 512 - $boot_size - $boot_pad \
116 - 512 - $netbsd_size - $netbsd_pad \
117 - 512 * 2))
118 }
119
120 # create tar file
121 #
122 dd if=/dev/zero of=${floppy} bs=8k count=1 2>/dev/null
123 ${PAX} -O -w -b8k ${files} >> ${floppy} || exit 1
124 # XXX: use pax metafile and set perms?
125 if [ -n "$instboot" ]; then
126 instboot=$( echo $instboot | sed -e s/@IMAGE@/${floppy}/ )
127 echo "Running instboot: ${instboot}"
128 eval ${instboot} || exit 1
129 fi
130
131 # check size against available number of disks
132 #
133 bytes=$( ls -ln "${floppy}" | awk '{print $5}' )
134 blocks=$(roundup ${bytes} 512)
135 # when calculating numdisks, take into account:
136 # a) the image already has an 8K tar header prepended
137 # b) each floppy needs an 8K tar volume header
138 numdisks=$(roundup ${blocks}-16 ${floppysize}-16)
139 if [ -z "${maxdisks}" ]; then
140 maxdisks=${numdisks}
141 fi
142
143 if [ ${numdisks} -gt ${maxdisks} ]; then
144 excess=$(( (${blocks}-16 - (${floppysize}-16) * ${maxdisks}) * 512 ))
145 echo 1>&2 \
146 "$prog: Image is ${excess} bytes ($(( ${excess} / 1024 )) KB)"\
147 "too big to fit on ${maxdisks} disk"$(plural ${maxdisks})
148 exit 1
149 fi
150
151 padsize=$(( ${floppysize} * ${maxdisks} ))
152 padcount=$(( ${padsize} - ${blocks} ))
153 if [ -n "${pad}" ]; then
154 echo \
155 "Writing $(( ${padsize} * 512 )) bytes ($(( ${padsize} / 2 )) KB)" \
156 "on ${numdisks} disk"$(plural ${numdisks})"," \
157 "padded by $(( ${padcount} * 512 )) bytes" \
158 "($(( ${padcount} / 2 )) KB)"
159 else
160 echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
161 "on ${numdisks} disk"$(plural ${numdisks})"," \
162 "free space $(( ${padcount} * 512 )) bytes" \
163 "($(( ${padcount} / 2 )) KB)"
164 fi
165
166 # write disks
167 #
168 curdisk=1
169 image=
170 seek=0
171 skip=0
172 floppysize8k=$(( ${floppysize} / 16 ))
173 while [ ${curdisk} -le ${numdisks} ]; do
174 image="${floppybase}${curdisk}${suffix}"
175 echo "Creating disk ${curdisk} to ${image}"
176 if [ ${curdisk} -eq 1 ]; then
177 : > ${image}
178 else
179 echo USTARFS ${curdisk} > ${image}
180 fi
181 count=$(( ${floppysize8k} - ${seek} ))
182 dd bs=8k conv=sync seek=${seek} skip=${skip} count=${count} \
183 if=${floppy} of=${image} 2>/dev/null
184
185 curdisk=$(( ${curdisk} + 1 ))
186 skip=$(( $skip + $count ))
187 seek=1
188 done
189
190 # pad last disk if necessary
191 #
192 if [ -n "${pad}" ]; then
193 dd if=$image of=$image conv=notrunc conv=sync bs=${floppysize}b count=1
194 fi
195
196
197 # final status
198 #
199 echo "Final result:"
200 ls -l ${floppybase}?${suffix}
201
202 exit 0
203