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