mkimage revision 1.71 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.71 2019/10/30 14:16:15 martin Exp $
3 #
4 # Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
5 # All rights reserved.
6 #
7 # This code is derived from software contributed to The NetBSD Foundation
8 # by Christos Zoulas.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # 3. Neither the name of The NetBSD Foundation nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34
35 #
36 # Makes a bootable image for the host architecture given.
37 # The host specific functions are pulled in from a /bin/sh script in the
38 # "conf" directory, and is expected to provide the following shell
39 # functions, which are called in the following order:
40 #
41 # - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}.
42 # If -m is given, a number of directories are put on a tmpfs RAM disk
43 # - customize: After unpacking the sets, this gets the system to
44 # a working state, e. g. by setting up /etc/rc.conf and /dev
45 # - populate: Add common goods like kernel and bootloader
46 # - make_label: Prints disklabel to stdout
47 #
48
49 set -e
50
51 DIR="$(cd "$(dirname "$0")" && pwd)"
52 PROG="$(basename "$0")"
53
54 MAKE=${TOOL_MAKE:-make}
55 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
56 FDISK=${TOOL_FDISK:-fdisk}
57 MAKEFS=${TOOL_MAKEFS:-makefs}
58 MTREE=${TOOL_MTREE:-mtree}
59 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
60 MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
61 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
62
63 src="/usr/src"
64 sets="base comp etc games man misc modules rescue tests text"
65 xsets="xbase xcomp xetc xfont xserver"
66 minfree="10%"
67 bar="==="
68
69 tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
70 mnt="${tmp}/mnt"
71 mkdir -p "${mnt}/etc" "${mnt}/dev"
72
73 trap "cleanup" 0 1 2 3 15
74
75 cleanup() {
76 case "$tmp" in
77 "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";;
78 esac
79 }
80
81 fail() {
82 IFS=' '
83 echo >&2 "${PROG}: $*"
84 exit 1
85 }
86
87 getsize() {
88 set -- $(ls -l $1)
89 echo $5
90 }
91
92 getsectors() {
93 case "$1" in
94 *g)
95 m=1073741824
96 v=${1%g}
97 ;;
98 *m)
99 m=1048576
100 v=${1%m}
101 ;;
102 *k)
103 m=1024
104 v=${1%k}
105 ;;
106 *[0-9b])
107 m=1
108 v=${1%b}
109 ;;
110 esac
111 echo $((m * v / 512))
112 }
113
114 usage() {
115 cat << EOF 1>&2
116 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
117
118 -b Boot only, no sets loaded
119 -r root device kind (sd, wd, ld)
120 -d Add the debug sets
121 -m Optimize the OS installation to mimimize disk writes for SSDs
122 -x Load the X sets too, not just the base ones
123 EOF
124 exit 1
125 }
126
127 # First pass for options to get the host and src directories
128 OPTS="B:D:K:S:bc:dh:mr:s:x"
129 while getopts "$OPTS" f
130 do
131 case $f in
132 h) h="$OPTARG";;
133 S) src="$OPTARG";;
134 *) ;;
135 esac
136 done
137
138 if [ -z "$h" ]
139 then
140 usage
141 fi
142
143 if [ ! -f "${DIR}/conf/${h}.conf" ]
144 then
145 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
146 exit 1
147 fi
148
149 resize=false
150
151 . "${DIR}/conf/${h}.conf"
152 release="/usr/obj/${MACHINE}/release"
153
154 selected_sets="$sets"
155 dsets_p=false
156 xsets_p=false
157 minwrites=false
158 rootdev=ld
159 endian=
160
161 OPTIND=1
162 while getopts "$OPTS" f
163 do
164 case $f in
165 B) endian="-B $OPTARG";;
166 D) release="$OPTARG";;
167 K) kernel="$OPTARG";;
168 S) ;;
169 b) bootonly=true;;
170 d) dsets_p=true
171 selected_sets="$selected_sets debug"
172 if $xsets_p; then
173 selected_sets="$selected_sets xdebug"
174 fi
175 ;;
176 c) custom="$OPTARG";;
177 h) ;;
178 m) minwrites=true;;
179 r) rootdev="$OPTARG";;
180 s) size="$OPTARG";;
181 x) xsets_p=true
182 selected_sets="$selected_sets $xsets"
183 if $dsets_p; then
184 selected_sets="$selected_sets xdebug"
185 fi
186 ;;
187 *) usage;;
188 esac
189 done
190
191 shift $(( $OPTIND - 1 ))
192 if [ -n "$1" ]; then
193 # take the next argument as being the image name
194 image="$1"
195 shift
196 fi
197
198 case "$image" in
199 *.gz) compress=true; image="${image%.gz}";;
200 *) compress=false;;
201 esac
202
203 if [ -z "${bootonly}" ]; then
204 echo ${bar} configuring sets ${bar}
205 (cat "${release}/etc/mtree/NetBSD.dist"
206 for i in $selected_sets; do
207 s="${release}/etc/mtree/set.$i"
208 if [ -f "$s" ]; then
209 cat "$s"
210 fi
211 done) > "$tmp/selected_sets"
212 fi
213
214 make_fstab
215 customize
216 populate
217
218 if [ -n "${msdosid}" ]; then
219 echo ${bar} Populating msdos filesystem ${bar}
220 case $(( ${msdosid} )) in
221 1) fat_opt=",fat_type=12";;
222 4|6|14) fat_opt=",fat_type=16";;
223 11|12) fat_opt=",fat_type=32";;
224 *) fat_opt=;;
225 esac
226 ${MAKEFS} -N ${release}/etc -t msdos \
227 -o "volume_label=NETBSD${fat_opt}" \
228 -O $((${init} / 2))m -s $((${boot} / 2))m \
229 ${image} ${mnt}/boot
230 fi
231
232 if [ -z "${bootonly}" ]; then
233 echo ${bar} Populating ffs filesystem ${bar}
234 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
235 -O ${ffsoffset} \
236 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
237 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
238 fi
239
240 if [ "${size}" = 0 ]; then
241 size="$(getsize "${image}")"
242 fi
243 newsize=$((${size} / 2 / 1024))
244 compare=$((${newsize} * 2 * 1024))
245 while [ "${compare}" != "${size}" ]
246 do
247 size="$((size + size - compare))"
248 newsize="$((${size} / 2 / 1024))"
249 compare="$((${newsize} * 2 * 1024))"
250 done
251
252 if [ -n "${msdosid}" ]; then
253 echo ${bar} Running fdisk ${bar}
254 initsecs=$((${init} * 1024))
255 bootsecs=$((${boot} * 1024))
256 ${FDISK} -f -i ${image}
257 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
258 if [ -z "${bootonly}" ]; then
259 ffsstart="$(getsectors ${ffsoffset})"
260 imagesize="$(getsize "${image}")"
261 imagesecs="$(getsectors ${imagesize})"
262 ffssize="$(expr ${imagesecs} - ${ffsstart})"
263 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
264 fi
265
266 echo ${bar} Adding label ${bar}
267 make_label > ${tmp}/label
268 ${DISKLABEL} -R -F ${image} ${tmp}/label
269 elif [ -n "${netbsdid}" ]; then
270 echo ${bar} Adding label ${bar}
271 make_label > ${tmp}/label
272 ${DISKLABEL} -R -F ${image} ${tmp}/label
273
274 echo ${bar} Running fdisk ${bar}
275 ${FDISK} -f -i ${image}
276 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
277 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
278 fi
279
280 if $compress; then
281 echo ${bar} Compressing image ${bar}
282 rm -f "${image}.gz"
283 ${GZIP_CMD} -9 ${image}
284 image="${image}.gz"
285 fi
286
287 echo ${bar} Image is ${image} ${bar}
288