mkimage revision 1.88 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.88 2025/01/11 14:33:31 jmmv 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 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,log,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
229 . "${DIR}/conf/${h}.conf"
230 release="/usr/obj/${MACHINE}/release"
231
232 selected_sets="$sets"
233 dsets_p=false
234 xsets_p=false
235 minwrites=false
236 rootdev=ld
237 endian=
238
239 OPTIND=1
240 while getopts "$OPTS" f
241 do
242 case $f in
243 B) endian="-B $OPTARG";;
244 D) release="$OPTARG";;
245 K) kernel="$OPTARG";;
246 S) ;;
247 b) bootonly=true;;
248 d) dsets_p=true
249 selected_sets="$selected_sets debug"
250 if $xsets_p; then
251 selected_sets="$selected_sets xdebug"
252 fi
253 ;;
254 c) custom="$OPTARG";;
255 h) ;;
256 m) minwrites=true;;
257 r) rootdev="$OPTARG";;
258 s) size="$OPTARG";;
259 x) xsets_p=true
260 selected_sets="$selected_sets $xsets"
261 if $dsets_p; then
262 selected_sets="$selected_sets xdebug"
263 fi
264 ;;
265 *) usage;;
266 esac
267 done
268 if [ -n "${MKREPRO_TIMESTAMP}" ]; then
269 timestamp_opt="-T ${MKREPRO_TIMESTAMP}"
270 volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))"
271 fi
272
273 shift $(( $OPTIND - 1 ))
274 if [ -n "$1" ]; then
275 # take the next argument as being the image name
276 image="$1"
277 shift
278 fi
279
280 case "$image" in
281 *.gz) compress=true; image="${image%.gz}";;
282 *) compress=false;;
283 esac
284
285 if [ -z "${bootonly}" ]; then
286 echo ${bar} configuring sets ${bar}
287 (cat "${release}/etc/mtree/NetBSD.dist"
288 for i in $selected_sets; do
289 s="${release}/etc/mtree/set.$i"
290 if [ -f "$s" ]; then
291 cat "$s"
292 fi
293 done) > "$tmp/selected_sets"
294 fi
295
296 make_fstab
297 customize
298 populate
299
300 if [ ! "${MKDTB}" = "no" ]; then
301 #
302 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
303 # the rest on FFS. Split it up here.
304 #
305 echo ${bar} Installing devicetree blobs ${bar}
306 mkdir -p "${mnt}/boot"
307 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
308
309 mkdir -p "${mnt}/etc/mtree"
310 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
311 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
312
313 mkdir -p "${mnt}/var/db/obsolete"
314 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
315 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
316 fi
317
318 if [ -n "${msdosid}" ]; then
319 echo ${bar} Populating msdos filesystem ${bar}
320
321 case $(( ${msdosid} )) in
322 1) fat_opt=",fat_type=12";;
323 4|6|14) fat_opt=",fat_type=16";;
324 11|12) fat_opt=",fat_type=32";;
325 *) fat_opt=;;
326 esac
327 ${MAKEFS} -N ${release}/etc -t msdos \
328 -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \
329 -O $((${init} / 2))m -s $((${boot} / 2))m \
330 ${image} ${mnt}/boot
331 fi
332
333 if [ -z "${bootonly}" ]; then
334 echo ${bar} Populating ffs filesystem ${bar}
335 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
336 -O ${ffsoffset} ${timestamp_opt} \
337 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
338 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
339 fi
340
341 if [ "${size}" = 0 ]; then
342 size="$(getsize "${image}")"
343 # Round up to a multiple of 4m and add 1m of slop.
344 alignunit=$((4*1024*1024))
345 alignsize=$((alignunit*((size + alignunit - 1)/alignunit)))
346 alignsize=$((alignsize + 1024*1024))
347 if [ "${size}" -lt "${alignsize}" ]; then
348 dd bs=1 count="$((alignsize - size))" if=/dev/zero \
349 >> "${image}" 2> /dev/null
350 size="${alignsize}"
351 fi
352 fi
353
354 if $gpt; then
355 if $gpt_hybrid; then
356 gpt_flags="-H"
357 fi
358 gpt_flags="${gpt_flags} ${timestamp_opt}"
359 initsecs=$((${init} * 1024))
360 bootsecs=$((${boot} * 1024))
361 ffsstart="$(getsectors ${ffsoffset})"
362
363 echo ${bar} Clearing existing partitions ${bar}
364 ${GPT} ${gpt_flags} ${image} destroy || true
365
366 echo ${bar} Creating partitions ${bar}
367 ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
368 ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
369 ${GPT} ${gpt_flags} ${image} set -a required -i 1
370 ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
371 ${GPT} ${gpt_flags} ${image} show
372 if $gpt_hybrid; then
373 echo ${bar} Creating hybrid MBR ${bar}
374 ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
375 ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
376 ${FDISK} -F ${image}
377 fi
378 else
379 if [ -n "${msdosid}" ]; then
380 echo ${bar} Running fdisk ${bar}
381 initsecs=$((${init} * 1024))
382 bootsecs=$((${boot} * 1024))
383 ${FDISK} -f -i ${image}
384 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
385 if [ -z "${bootonly}" ]; then
386 ffsstart="$(getsectors ${ffsoffset})"
387 imagesize="$(getsize "${image}")"
388 imagesecs="$(getsectors ${imagesize})"
389 ffssize="$(expr ${imagesecs} - ${ffsstart})"
390 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
391 fi
392
393 echo ${bar} Adding label ${bar}
394 make_label > ${tmp}/label
395 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
396 elif [ -n "${netbsdid}" ]; then
397 echo ${bar} Adding label ${bar}
398 make_label > ${tmp}/label
399 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
400
401 echo ${bar} Running fdisk ${bar}
402 ${FDISK} -f -i ${image}
403 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
404 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
405 fi
406 fi
407
408 if $compress; then
409 echo ${bar} Compressing image ${bar}
410 rm -f "${image}.gz"
411 ${GZIP_CMD} -n -9 ${image}
412 image="${image}.gz"
413 fi
414
415 echo ${bar} Image is ${image} ${bar}
416