mkimage revision 1.80 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.80 2024/02/11 17:29:50 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
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 gpufw 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 if [ -n ${MKREPRO_TIMESTAMP} ]; then
194 timestamp_opt="-T ${MKREPRO_TIMESTAMP}"
195 volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))"
196 fi
197
198 shift $(( $OPTIND - 1 ))
199 if [ -n "$1" ]; then
200 # take the next argument as being the image name
201 image="$1"
202 shift
203 fi
204
205 case "$image" in
206 *.gz) compress=true; image="${image%.gz}";;
207 *) compress=false;;
208 esac
209
210 if [ -z "${bootonly}" ]; then
211 echo ${bar} configuring sets ${bar}
212 (cat "${release}/etc/mtree/NetBSD.dist"
213 for i in $selected_sets; do
214 s="${release}/etc/mtree/set.$i"
215 if [ -f "$s" ]; then
216 cat "$s"
217 fi
218 done) > "$tmp/selected_sets"
219 fi
220
221 make_fstab
222 customize
223 populate
224
225 if [ ! "${MKDTB}" = "no" ]; then
226 #
227 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
228 # the rest on FFS. Split it up here.
229 #
230 echo ${bar} Installing devicetree blobs ${bar}
231 mkdir -p "${mnt}/boot"
232 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
233
234 mkdir -p "${mnt}/etc/mtree"
235 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
236 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
237
238 mkdir -p "${mnt}/var/db/obsolete"
239 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
240 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
241 fi
242
243 if [ -n "${msdosid}" ]; then
244 echo ${bar} Populating msdos filesystem ${bar}
245
246 case $(( ${msdosid} )) in
247 1) fat_opt=",fat_type=12";;
248 4|6|14) fat_opt=",fat_type=16";;
249 11|12) fat_opt=",fat_type=32";;
250 *) fat_opt=;;
251 esac
252 ${MAKEFS} -N ${release}/etc -t msdos \
253 -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \
254 -O $((${init} / 2))m -s $((${boot} / 2))m \
255 ${image} ${mnt}/boot
256 fi
257
258 if [ -z "${bootonly}" ]; then
259 echo ${bar} Populating ffs filesystem ${bar}
260 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
261 -O ${ffsoffset} ${timestamp_opt} \
262 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
263 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
264 fi
265
266 if [ "${size}" = 0 ]; then
267 size="$(getsize "${image}")"
268 # Round up to a multiple of 4m and add 1m of slop.
269 alignunit=$((4*1024*1024))
270 alignsize=$((alignunit*((size + alignunit - 1)/alignunit)))
271 alignsize=$((alignsize + 1024*1024))
272 if [ "${size}" -lt "${alignsize}" ]; then
273 dd bs=1 count="$((alignsize - size))" if=/dev/zero \
274 >> "${image}" 2> /dev/null
275 size="${alignsize}"
276 fi
277 fi
278
279 if $gpt; then
280 if $gpt_hybrid; then
281 gpt_flags="-H"
282 fi
283 gpt_flags="${gpt_flags} ${timestamp_opt}"
284 initsecs=$((${init} * 1024))
285 bootsecs=$((${boot} * 1024))
286 ffsstart="$(getsectors ${ffsoffset})"
287
288 echo ${bar} Clearing existing partitions ${bar}
289 ${GPT} ${gpt_flags} ${image} destroy || true
290
291 echo ${bar} Creating partitions ${bar}
292 ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
293 ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
294 ${GPT} ${gpt_flags} ${image} set -a required -i 1
295 ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
296 ${GPT} ${gpt_flags} ${image} show
297 if $gpt_hybrid; then
298 echo ${bar} Creating hybrid MBR ${bar}
299 ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
300 ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
301 ${FDISK} -F ${image}
302 fi
303 else
304 if [ -n "${msdosid}" ]; then
305 echo ${bar} Running fdisk ${bar}
306 initsecs=$((${init} * 1024))
307 bootsecs=$((${boot} * 1024))
308 ${FDISK} -f -i ${image}
309 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
310 if [ -z "${bootonly}" ]; then
311 ffsstart="$(getsectors ${ffsoffset})"
312 imagesize="$(getsize "${image}")"
313 imagesecs="$(getsectors ${imagesize})"
314 ffssize="$(expr ${imagesecs} - ${ffsstart})"
315 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
316 fi
317
318 echo ${bar} Adding label ${bar}
319 make_label > ${tmp}/label
320 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
321 elif [ -n "${netbsdid}" ]; then
322 echo ${bar} Adding label ${bar}
323 make_label > ${tmp}/label
324 ${DISKLABEL} -m -R -F ${image} ${tmp}/label
325
326 echo ${bar} Running fdisk ${bar}
327 ${FDISK} -f -i ${image}
328 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
329 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
330 fi
331 fi
332
333 if $compress; then
334 echo ${bar} Compressing image ${bar}
335 rm -f "${image}.gz"
336 ${GZIP_CMD} -n -9 ${image}
337 image="${image}.gz"
338 fi
339
340 echo ${bar} Image is ${image} ${bar}
341