mkimage revision 1.92 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.92 2025/04/01 00:04:00 gutteridge 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, which 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 SED=${TOOL_SED:-sed}
64 PWD_MKDB=${TOOL_PWD_MKDB:-pwd_mkdb}
65
66 postfix=false
67 [ "${MKPOSTFIX:-yes}" = no ] || postfix=true
68
69 src="/usr/src"
70 sets="base comp etc games gpufw man manhtml misc modules rescue tests text"
71 xsets="xbase xcomp xetc xfont xserver"
72 minfree="10%"
73 bar="==="
74
75 tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
76 mnt="${tmp}/mnt"
77 mkdir -p "${mnt}/etc" "${mnt}/dev"
78
79 trap "cleanup" 0 1 2 3 15
80
81 cleanup() {
82 case "$tmp" in
83 "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";;
84 esac
85 }
86
87 fail() {
88 IFS=' '
89 echo >&2 "${PROG}: $*"
90 exit 1
91 }
92
93 getsize() {
94 set -- $(ls -l $1)
95 echo $5
96 }
97
98 getsectors() {
99 case "$1" in
100 *g)
101 m=1073741824
102 v=${1%g}
103 ;;
104 *m)
105 m=1048576
106 v=${1%m}
107 ;;
108 *k)
109 m=1024
110 v=${1%k}
111 ;;
112 *[0-9b])
113 m=1
114 v=${1%b}
115 ;;
116 esac
117 echo $((m * v / 512))
118 }
119
120 minwrites_fstab_entries() {
121 $minwrites || return 0
122 cat << EOF
123 tmpfs /var/log tmpfs rw,union,-s32M
124 tmpfs /var/run tmpfs rw,union,-s1M
125 tmpfs /var/mail tmpfs rw,union,-s10M
126 tmpfs /var/chroot tmpfs rw,union,-s10M
127 EOF
128 if $postfix; then
129 cat << EOF
130 tmpfs /var/spool/postfix tmpfs rw,union,-s20M
131 tmpfs /var/db/postfix tmpfs rw,union,-s1M
132 EOF
133 fi
134 }
135
136 make_fstab_gpt() {
137 local boot=$1
138 local rootopts=
139 if $minwrites; then
140 rootopts=,log,nodevmtime
141 fi
142
143 cat > ${mnt}/etc/fstab << EOF
144 # NetBSD /etc/fstab
145 # See /usr/share/examples/fstab/ for more examples.
146 NAME=${gpt_label_ffs:-netbsd-root} / ffs rw,noatime${rootopts} 1 1
147 NAME=${gpt_label_boot:-$boot} /boot msdos rw 1 1
148 ptyfs /dev/pts ptyfs rw
149 procfs /proc procfs rw
150 tmpfs /var/shm tmpfs rw,-m1777,-sram%25
151 EOF
152 minwrites_fstab_entries >> ${mnt}/etc/fstab
153 }
154
155 # From Richard Neswold's:
156 # http://rich-tbp.blogspot.com/2013/03/netbsd-on-rpi-minimizing-disk-writes.html
157 # Also for the postfix stuff below
158 make_fstab_normal() {
159 local rootopts=
160 if $minwrites; then
161 rootopts=,nodevmtime
162 fi
163 cat > ${mnt}/etc/fstab << EOF
164 # NetBSD /etc/fstab
165 # See /usr/share/examples/fstab/ for more examples.
166 ROOT.a / ffs rw,noatime${rootopts} 1 1
167 ROOT.e /boot msdos rw 1 1
168 ptyfs /dev/pts ptyfs rw
169 procfs /proc procfs rw
170 tmpfs /tmp tmpfs rw,-s32M
171 tmpfs /var/shm tmpfs rw,-m1777,-sram%25
172 EOF
173 minwrites_fstab_entries >> ${mnt}/etc/fstab
174 }
175
176 make_fstab_default() {
177 if $gpt; then
178 make_fstab_gpt "$@"
179 else
180 make_fstab_normal
181 fi
182 echo "./etc/fstab type=file uname=root gname=wheel mode=0644" \
183 >> "$tmp/selected_sets"
184
185 # Missing mount points from fstab
186 echo "./proc type=dir uname=root gname=wheel mode=0755" \
187 >> "$tmp/selected_sets"
188 }
189
190 usage() {
191 cat << EOF 1>&2
192 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
193
194 -b Boot only, no sets loaded
195 -r root device kind (sd, wd, ld)
196 -d Add the debug sets
197 -m Optimize the OS installation to mimimize disk writes for SSDs
198 -x Load the X sets too, not just the base ones
199 EOF
200 exit 1
201 }
202
203 # First pass for options to get the host and src directories
204 OPTS="B:D:K:S:bc:dh:mr:s:x"
205 while getopts "$OPTS" f
206 do
207 case $f in
208 h) h="$OPTARG";;
209 S) src="$OPTARG";;
210 *) ;;
211 esac
212 done
213
214 if [ -z "$h" ]
215 then
216 usage
217 fi
218
219 if [ ! -f "${DIR}/conf/${h}.conf" ]
220 then
221 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
222 exit 1
223 fi
224
225 resize=false
226 gpt=false
227 gpt_hybrid=false
228 fsize=8192
229 bsize=65536
230 ffsversion=1
231
232 . "${DIR}/conf/${h}.conf"
233 release="/usr/obj/${MACHINE}/release"
234
235 selected_sets="$sets"
236 dsets_p=false
237 xsets_p=false
238 minwrites=false
239 rootdev=ld
240 endian=
241
242 OPTIND=1
243 while getopts "$OPTS" f
244 do
245 case $f in
246 B) endian="-B $OPTARG";;
247 D) release="$OPTARG";;
248 K) kernel="$OPTARG";;
249 S) ;;
250 b) bootonly=true;;
251 d) dsets_p=true
252 selected_sets="$selected_sets debug"
253 if $xsets_p; then
254 selected_sets="$selected_sets xdebug"
255 fi
256 ;;
257 c) custom="$OPTARG";;
258 h) ;;
259 m) minwrites=true;;
260 r) rootdev="$OPTARG";;
261 s) size="$OPTARG";;
262 x) xsets_p=true
263 selected_sets="$selected_sets $xsets"
264 if $dsets_p; then
265 selected_sets="$selected_sets xdebug"
266 fi
267 ;;
268 *) usage;;
269 esac
270 done
271 if [ -n "${MKREPRO_TIMESTAMP}" ]; then
272 timestamp_opt="-T ${MKREPRO_TIMESTAMP}"
273 volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))"
274 fi
275
276 shift $(( $OPTIND - 1 ))
277 if [ -n "$1" ]; then
278 # take the next argument as being the image name
279 image="$1"
280 shift
281 fi
282
283 case "$image" in
284 *.gz) compress=true; image="${image%.gz}";;
285 *) compress=false;;
286 esac
287
288 if [ -z "${bootonly}" ]; then
289 echo ${bar} configuring sets ${bar}
290 (cat "${release}/etc/mtree/NetBSD.dist"
291 for i in $selected_sets; do
292 s="${release}/etc/mtree/set.$i"
293 if [ -f "$s" ]; then
294 cat "$s"
295 fi
296 done) > "$tmp/selected_sets"
297 fi
298
299 make_fstab
300 customize
301 populate
302
303 if [ ! "${MKDTB}" = "no" ]; then
304 #
305 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
306 # the rest on FFS. Split it up here.
307 #
308 echo ${bar} Installing devicetree blobs ${bar}
309 mkdir -p "${mnt}/boot"
310 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
311
312 mkdir -p "${mnt}/etc/mtree"
313 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
314 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
315
316 mkdir -p "${mnt}/var/db/obsolete"
317 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
318 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
319 fi
320
321 if [ -n "${msdosid}" ]; then
322 echo ${bar} Populating msdos filesystem ${bar}
323
324 case $(( ${msdosid} )) in
325 1) fat_opt=",fat_type=12";;
326 4|6|14) fat_opt=",fat_type=16";;
327 11|12) fat_opt=",fat_type=32";;
328 *) fat_opt=;;
329 esac
330 ${MAKEFS} -N ${release}/etc -t msdos \
331 -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \
332 -O $((${init} / 2))m -s $((${boot} / 2))m \
333 ${image} ${mnt}/boot
334 fi
335
336 if [ -z "${bootonly}" ]; then
337 echo ${bar} Populating ffs filesystem ${bar}
338 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
339 -O ${ffsoffset} ${timestamp_opt} \
340 -o d=4096,f=${fsize},b=${bsize},v=${ffsversion} -b $((${extra}))m \
341 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
342 fi
343
344 if [ "${size}" = 0 ]; then
345 size="$(getsize "${image}")"
346 # Round up to a multiple of 4m and add 1m of slop.
347 alignunit=$((4*1024*1024))
348 alignsize=$((alignunit*((size + alignunit - 1)/alignunit)))
349 alignsize=$((alignsize + 1024*1024))
350 if [ "${size}" -lt "${alignsize}" ]; then
351 dd bs=1 count="$((alignsize - size))" if=/dev/zero \
352 >> "${image}" 2> /dev/null
353 size="${alignsize}"
354 fi
355 fi
356
357 if $gpt; then
358 if $gpt_hybrid; then
359 gpt_flags="-H"
360 fi
361 gpt_flags="${gpt_flags} ${timestamp_opt}"
362 initsecs=$((${init} * 1024))
363 bootsecs=$((${boot} * 1024))
364 ffsstart="$(getsectors ${ffsoffset})"
365
366 echo ${bar} Clearing existing partitions ${bar}
367 ${GPT} ${gpt_flags} ${image} destroy || true
368
369 echo ${bar} Creating partitions ${bar}
370 ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
371 ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
372 ${GPT} ${gpt_flags} ${image} set -a required -i 1
373 ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
374 ${GPT} ${gpt_flags} ${image} show
375 if $gpt_hybrid; then
376 echo ${bar} Creating hybrid MBR ${bar}
377 ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
378 ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
379 ${FDISK} -F ${image}
380 fi
381 else
382 if [ -n "${msdosid}" ]; then
383 echo ${bar} Running fdisk ${bar}
384 initsecs=$((${init} * 1024))
385 bootsecs=$((${boot} * 1024))
386 ${FDISK} -f -i ${image}
387 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
388 if [ -z "${bootonly}" ]; then
389 ffsstart="$(getsectors ${ffsoffset})"
390 imagesize="$(getsize "${image}")"
391 imagesecs="$(getsectors ${imagesize})"
392 ffssize="$(expr ${imagesecs} - ${ffsstart})"
393 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
394 fi
395
396 echo ${bar} Adding label ${bar}
397 make_label > ${tmp}/label
398 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
399 elif [ -n "${netbsdid}" ]; then
400 echo ${bar} Adding label ${bar}
401 make_label > ${tmp}/label
402 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
403
404 echo ${bar} Running fdisk ${bar}
405 ${FDISK} -f -i ${image}
406 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
407 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
408 fi
409 fi
410
411 if $compress; then
412 echo ${bar} Compressing image ${bar}
413 rm -f "${image}.gz"
414 ${GZIP_CMD} -n -9 ${image}
415 image="${image}.gz"
416 fi
417
418 cd "${IMAGEDIR}"
419 ${CKSUM} -a MD5 "$(basename "${image}")" > MD5
420 ${CKSUM} -a SHA512 "$(basename "${image}")" > SHA512
421
422 echo ${bar} Image is ${image} ${bar}
423