mkimage revision 1.73 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.73 2020/05/24 14:45:49 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
153 . "${DIR}/conf/${h}.conf"
154 release="/usr/obj/${MACHINE}/release"
155
156 selected_sets="$sets"
157 dsets_p=false
158 xsets_p=false
159 minwrites=false
160 rootdev=ld
161 endian=
162
163 OPTIND=1
164 while getopts "$OPTS" f
165 do
166 case $f in
167 B) endian="-B $OPTARG";;
168 D) release="$OPTARG";;
169 K) kernel="$OPTARG";;
170 S) ;;
171 b) bootonly=true;;
172 d) dsets_p=true
173 selected_sets="$selected_sets debug"
174 if $xsets_p; then
175 selected_sets="$selected_sets xdebug"
176 fi
177 ;;
178 c) custom="$OPTARG";;
179 h) ;;
180 m) minwrites=true;;
181 r) rootdev="$OPTARG";;
182 s) size="$OPTARG";;
183 x) xsets_p=true
184 selected_sets="$selected_sets $xsets"
185 if $dsets_p; then
186 selected_sets="$selected_sets xdebug"
187 fi
188 ;;
189 *) usage;;
190 esac
191 done
192
193 shift $(( $OPTIND - 1 ))
194 if [ -n "$1" ]; then
195 # take the next argument as being the image name
196 image="$1"
197 shift
198 fi
199
200 case "$image" in
201 *.gz) compress=true; image="${image%.gz}";;
202 *) compress=false;;
203 esac
204
205 if [ -z "${bootonly}" ]; then
206 echo ${bar} configuring sets ${bar}
207 (cat "${release}/etc/mtree/NetBSD.dist"
208 for i in $selected_sets; do
209 s="${release}/etc/mtree/set.$i"
210 if [ -f "$s" ]; then
211 cat "$s"
212 fi
213 done) > "$tmp/selected_sets"
214 fi
215
216 make_fstab
217 customize
218 populate
219
220 if [ ! "${MKDTB}" = "no" ]; then
221 #
222 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
223 # the rest on FFS. Split it up here.
224 #
225 echo ${bar} Installing devicetree blobs ${bar}
226 mkdir -p "${mnt}/boot"
227 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
228
229 mkdir -p "${mnt}/etc/mtree"
230 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
231 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
232
233 mkdir -p "${mnt}/var/db/obsolete"
234 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
235 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
236 fi
237
238 if [ -n "${msdosid}" ]; then
239 echo ${bar} Populating msdos filesystem ${bar}
240
241 case $(( ${msdosid} )) in
242 1) fat_opt=",fat_type=12";;
243 4|6|14) fat_opt=",fat_type=16";;
244 11|12) fat_opt=",fat_type=32";;
245 *) fat_opt=;;
246 esac
247 ${MAKEFS} -N ${release}/etc -t msdos \
248 -o "volume_label=NETBSD${fat_opt}" \
249 -O $((${init} / 2))m -s $((${boot} / 2))m \
250 ${image} ${mnt}/boot
251 fi
252
253 if [ -z "${bootonly}" ]; then
254 echo ${bar} Populating ffs filesystem ${bar}
255 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
256 -O ${ffsoffset} \
257 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
258 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
259 fi
260
261 if [ "${size}" = 0 ]; then
262 size="$(getsize "${image}")"
263 fi
264 newsize=$((${size} / 2 / 1024))
265 compare=$((${newsize} * 2 * 1024))
266 while [ "${compare}" != "${size}" ]
267 do
268 size="$((size + size - compare))"
269 newsize="$((${size} / 2 / 1024))"
270 compare="$((${newsize} * 2 * 1024))"
271 done
272
273 if $gpt; then
274 initsecs=$((${init} * 1024))
275 bootsecs=$((${boot} * 1024))
276 ffsstart="$(getsectors ${ffsoffset})"
277
278 echo ${bar} Clearing existing partitions ${bar}
279 ${GPT} ${image} destroy || true
280
281 echo ${bar} Creating partitions ${bar}
282 ${GPT} ${image} create ${gpt_create_flags}
283 ${GPT} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_efi:-EFI} -t efi
284 ${GPT} ${image} set -a required -i 1
285 ${GPT} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
286 ${GPT} ${image} show
287 else
288 if [ -n "${msdosid}" ]; then
289 echo ${bar} Running fdisk ${bar}
290 initsecs=$((${init} * 1024))
291 bootsecs=$((${boot} * 1024))
292 ${FDISK} -f -i ${image}
293 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
294 if [ -z "${bootonly}" ]; then
295 ffsstart="$(getsectors ${ffsoffset})"
296 imagesize="$(getsize "${image}")"
297 imagesecs="$(getsectors ${imagesize})"
298 ffssize="$(expr ${imagesecs} - ${ffsstart})"
299 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
300 fi
301
302 echo ${bar} Adding label ${bar}
303 make_label > ${tmp}/label
304 ${DISKLABEL} -R -F ${image} ${tmp}/label
305 elif [ -n "${netbsdid}" ]; then
306 echo ${bar} Adding label ${bar}
307 make_label > ${tmp}/label
308 ${DISKLABEL} -R -F ${image} ${tmp}/label
309
310 echo ${bar} Running fdisk ${bar}
311 ${FDISK} -f -i ${image}
312 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
313 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
314 fi
315 fi
316
317 if $compress; then
318 echo ${bar} Compressing image ${bar}
319 rm -f "${image}.gz"
320 ${GZIP_CMD} -9 ${image}
321 image="${image}.gz"
322 fi
323
324 echo ${bar} Image is ${image} ${bar}
325