mkimage revision 1.75 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.75 2020/07/17 15:16:34 jmcneill 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 GPT=${TOOL_GPT:-gpt}
58 MAKEFS=${TOOL_MAKEFS:-makefs}
59 MTREE=${TOOL_MTREE:-mtree}
60 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
61 MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
62 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
63
64 src="/usr/src"
65 sets="base comp etc games man misc modules rescue tests text"
66 xsets="xbase xcomp xetc xfont xserver"
67 minfree="10%"
68 bar="==="
69
70 tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
71 mnt="${tmp}/mnt"
72 mkdir -p "${mnt}/etc" "${mnt}/dev"
73
74 trap "cleanup" 0 1 2 3 15
75
76 cleanup() {
77 case "$tmp" in
78 "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";;
79 esac
80 }
81
82 fail() {
83 IFS=' '
84 echo >&2 "${PROG}: $*"
85 exit 1
86 }
87
88 getsize() {
89 set -- $(ls -l $1)
90 echo $5
91 }
92
93 getsectors() {
94 case "$1" in
95 *g)
96 m=1073741824
97 v=${1%g}
98 ;;
99 *m)
100 m=1048576
101 v=${1%m}
102 ;;
103 *k)
104 m=1024
105 v=${1%k}
106 ;;
107 *[0-9b])
108 m=1
109 v=${1%b}
110 ;;
111 esac
112 echo $((m * v / 512))
113 }
114
115 usage() {
116 cat << EOF 1>&2
117 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
118
119 -b Boot only, no sets loaded
120 -r root device kind (sd, wd, ld)
121 -d Add the debug sets
122 -m Optimize the OS installation to mimimize disk writes for SSDs
123 -x Load the X sets too, not just the base ones
124 EOF
125 exit 1
126 }
127
128 # First pass for options to get the host and src directories
129 OPTS="B:D:K:S:bc:dh:mr:s:x"
130 while getopts "$OPTS" f
131 do
132 case $f in
133 h) h="$OPTARG";;
134 S) src="$OPTARG";;
135 *) ;;
136 esac
137 done
138
139 if [ -z "$h" ]
140 then
141 usage
142 fi
143
144 if [ ! -f "${DIR}/conf/${h}.conf" ]
145 then
146 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
147 exit 1
148 fi
149
150 resize=false
151 gpt=false
152 gpt_hybrid=false
153
154 . "${DIR}/conf/${h}.conf"
155 release="/usr/obj/${MACHINE}/release"
156
157 selected_sets="$sets"
158 dsets_p=false
159 xsets_p=false
160 minwrites=false
161 rootdev=ld
162 endian=
163
164 OPTIND=1
165 while getopts "$OPTS" f
166 do
167 case $f in
168 B) endian="-B $OPTARG";;
169 D) release="$OPTARG";;
170 K) kernel="$OPTARG";;
171 S) ;;
172 b) bootonly=true;;
173 d) dsets_p=true
174 selected_sets="$selected_sets debug"
175 if $xsets_p; then
176 selected_sets="$selected_sets xdebug"
177 fi
178 ;;
179 c) custom="$OPTARG";;
180 h) ;;
181 m) minwrites=true;;
182 r) rootdev="$OPTARG";;
183 s) size="$OPTARG";;
184 x) xsets_p=true
185 selected_sets="$selected_sets $xsets"
186 if $dsets_p; then
187 selected_sets="$selected_sets xdebug"
188 fi
189 ;;
190 *) usage;;
191 esac
192 done
193
194 shift $(( $OPTIND - 1 ))
195 if [ -n "$1" ]; then
196 # take the next argument as being the image name
197 image="$1"
198 shift
199 fi
200
201 case "$image" in
202 *.gz) compress=true; image="${image%.gz}";;
203 *) compress=false;;
204 esac
205
206 if [ -z "${bootonly}" ]; then
207 echo ${bar} configuring sets ${bar}
208 (cat "${release}/etc/mtree/NetBSD.dist"
209 for i in $selected_sets; do
210 s="${release}/etc/mtree/set.$i"
211 if [ -f "$s" ]; then
212 cat "$s"
213 fi
214 done) > "$tmp/selected_sets"
215 fi
216
217 make_fstab
218 customize
219 populate
220
221 if [ ! "${MKDTB}" = "no" ]; then
222 #
223 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
224 # the rest on FFS. Split it up here.
225 #
226 echo ${bar} Installing devicetree blobs ${bar}
227 mkdir -p "${mnt}/boot"
228 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
229
230 mkdir -p "${mnt}/etc/mtree"
231 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
232 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
233
234 mkdir -p "${mnt}/var/db/obsolete"
235 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
236 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
237 fi
238
239 if [ -n "${msdosid}" ]; then
240 echo ${bar} Populating msdos filesystem ${bar}
241
242 case $(( ${msdosid} )) in
243 1) fat_opt=",fat_type=12";;
244 4|6|14) fat_opt=",fat_type=16";;
245 11|12) fat_opt=",fat_type=32";;
246 *) fat_opt=;;
247 esac
248 ${MAKEFS} -N ${release}/etc -t msdos \
249 -o "volume_label=NETBSD${fat_opt}" \
250 -O $((${init} / 2))m -s $((${boot} / 2))m \
251 ${image} ${mnt}/boot
252 fi
253
254 if [ -z "${bootonly}" ]; then
255 echo ${bar} Populating ffs filesystem ${bar}
256 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
257 -O ${ffsoffset} \
258 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
259 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
260 fi
261
262 if [ "${size}" = 0 ]; then
263 size="$(getsize "${image}")"
264 fi
265 newsize=$((${size} / 2 / 1024))
266 compare=$((${newsize} * 2 * 1024))
267 while [ "${compare}" != "${size}" ]
268 do
269 size="$((size + size - compare))"
270 newsize="$((${size} / 2 / 1024))"
271 compare="$((${newsize} * 2 * 1024))"
272 done
273
274 if $gpt; then
275 if $gpt_hybrid; then
276 gpt_flags="-H"
277 fi
278 initsecs=$((${init} * 1024))
279 bootsecs=$((${boot} * 1024))
280 ffsstart="$(getsectors ${ffsoffset})"
281
282 echo ${bar} Clearing existing partitions ${bar}
283 ${GPT} ${gpt_flags} ${image} destroy || true
284
285 echo ${bar} Creating partitions ${bar}
286 ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
287 ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
288 ${GPT} ${gpt_flags} ${image} set -a required -i 1
289 ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
290 ${GPT} ${gpt_flags} ${image} show
291 if $gpt_hybrid; then
292 echo ${bar} Creating hybrid MBR ${bar}
293 ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
294 ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
295 ${FDISK} -F ${image}
296 fi
297 else
298 if [ -n "${msdosid}" ]; then
299 echo ${bar} Running fdisk ${bar}
300 initsecs=$((${init} * 1024))
301 bootsecs=$((${boot} * 1024))
302 ${FDISK} -f -i ${image}
303 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
304 if [ -z "${bootonly}" ]; then
305 ffsstart="$(getsectors ${ffsoffset})"
306 imagesize="$(getsize "${image}")"
307 imagesecs="$(getsectors ${imagesize})"
308 ffssize="$(expr ${imagesecs} - ${ffsstart})"
309 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
310 fi
311
312 echo ${bar} Adding label ${bar}
313 make_label > ${tmp}/label
314 ${DISKLABEL} -R -F ${image} ${tmp}/label
315 elif [ -n "${netbsdid}" ]; then
316 echo ${bar} Adding label ${bar}
317 make_label > ${tmp}/label
318 ${DISKLABEL} -R -F ${image} ${tmp}/label
319
320 echo ${bar} Running fdisk ${bar}
321 ${FDISK} -f -i ${image}
322 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
323 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
324 fi
325 fi
326
327 if $compress; then
328 echo ${bar} Compressing image ${bar}
329 rm -f "${image}.gz"
330 ${GZIP_CMD} -9 ${image}
331 image="${image}.gz"
332 fi
333
334 echo ${bar} Image is ${image} ${bar}
335