mkimage revision 1.64 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.64 2017/04/11 21:06:30 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 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
55 FDISK=${TOOL_FDISK:-fdisk}
56 MAKEFS=${TOOL_MAKEFS:-makefs}
57 MTREE=${TOOL_MTREE:-mtree}
58 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
59 MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
60 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
61
62 src="/usr/src"
63 sets="base comp etc games man misc modules text"
64 xsets="xbase xcomp xetc xfont xserver"
65 minfree="10%"
66 bar="==="
67
68 tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
69 mnt="${tmp}/mnt"
70 mkdir -p "${mnt}/etc" "${mnt}/dev"
71
72 trap "cleanup" 0 1 2 3 15
73
74 cleanup() {
75 case "$tmp" in
76 /tmp/$PROG.*) rm -fr "$tmp";;
77 esac
78 }
79
80 getsize() {
81 set -- $(ls -l $1)
82 echo $5
83 }
84
85 getsectors() {
86 case "$1" in
87 *g)
88 m=1073741824
89 v=${1%g}
90 ;;
91 *m)
92 m=1048576
93 v=${1%m}
94 ;;
95 *k)
96 m=1024
97 v=${1%k}
98 ;;
99 *[0-9b])
100 m=1
101 v=${1%b}
102 ;;
103 esac
104 echo $((m * v / 512))
105 }
106
107 usage() {
108 cat << EOF 1>&2
109 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
110
111 -b Boot only, no sets loaded
112 -r root device kind (sd, wd, ld)
113 -d Add the debug sets
114 -m Optimize the OS installation to mimimize disk writes for SSDs
115 -x Load the X sets too, not just the base ones
116 EOF
117 exit 1
118 }
119
120 # First pass for options to get the host and src directories
121 OPTS="B:D:K:S:bc:dh:mr:s:x"
122 while getopts "$OPTS" f
123 do
124 case $f in
125 h) h="$OPTARG";;
126 S) src="$OPTARG";;
127 *) ;;
128 esac
129 done
130
131 if [ -z "$h" ]
132 then
133 usage
134 fi
135
136 if [ ! -f "${DIR}/conf/${h}.conf" ]
137 then
138 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
139 exit 1
140 fi
141
142 resize=false
143
144 . "${DIR}/conf/${h}.conf"
145 release="/usr/obj/${MACHINE}/release"
146
147 selected_sets="$sets"
148 dsets_p=false
149 xsets_p=false
150 minwrites=false
151 rootdev=ld
152 endian=
153
154 OPTIND=1
155 while getopts "$OPTS" f
156 do
157 case $f in
158 B) endian="-B $OPTARG";;
159 D) release="$OPTARG";;
160 K) kernel="$OPTARG";;
161 S) ;;
162 b) bootonly=true;;
163 d) dsets_p=true
164 selected_sets="$selected_sets debug"
165 if $xsets_p; then
166 selected_sets="$selected_sets xdebug"
167 fi
168 ;;
169 c) custom="$OPTARG";;
170 h) ;;
171 m) minwrites=true;;
172 r) rootdev="$OPTARG";;
173 s) size="$OPTARG";;
174 x) xsets_p=true
175 selected_sets="$selected_sets $xsets"
176 if $dsets_p; then
177 selected_sets="$selected_sets xdebug"
178 fi
179 ;;
180 *) usage;;
181 esac
182 done
183
184 shift $(( $OPTIND - 1 ))
185 if [ -n "$1" ]; then
186 # take the next argument as being the image name
187 image="$1"
188 shift
189 fi
190
191 case "$image" in
192 *.gz) compress=true; image="${image%.gz}";;
193 *) compress=false;;
194 esac
195
196 if [ -z "${bootonly}" ]; then
197 echo ${bar} configuring sets ${bar}
198 (cat "${release}/etc/mtree/NetBSD.dist"
199 for i in $selected_sets; do
200 s="${release}/etc/mtree/set.$i"
201 if [ -f "$s" ]; then
202 cat "$s"
203 fi
204 done) > "$tmp/selected_sets"
205 fi
206
207 make_fstab
208 customize
209 populate
210
211 if [ -n "${msdosid}" ]; then
212 echo ${bar} Populating msdos filesystem ${bar}
213 ${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
214 -O $((${init} / 2))m -s $((${boot} / 2))m \
215 ${image} ${mnt}/boot
216 fi
217
218 if [ -z "${bootonly}" ]; then
219 echo ${bar} Populating ffs filesystem ${bar}
220 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
221 -O ${ffsoffset} \
222 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
223 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
224 fi
225
226 if [ "${size}" = 0 ]; then
227 size="$(getsize "${image}")"
228 fi
229 newsize=$((${size} / 2 / 1024))
230 compare=$((${newsize} * 2 * 1024))
231 while [ "${compare}" != "${size}" ]
232 do
233 size="$((size + size - compare))"
234 newsize="$((${size} / 2 / 1024))"
235 compare="$((${newsize} * 2 * 1024))"
236 done
237
238 if [ -n "${msdosid}" ]; then
239 echo ${bar} Running fdisk ${bar}
240 initsecs=$((${init} * 1024))
241 bootsecs=$((${boot} * 1024))
242 ${FDISK} -f -i ${image}
243 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
244 if [ -z "${bootonly}" ]; then
245 ffsstart="$(getsectors ${ffsoffset})"
246 imagesize="$(getsize "${image}")"
247 imagesecs="$(getsectors ${imagesize})"
248 ffssize="$(expr ${imagesecs} - ${ffsstart})"
249 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
250 fi
251
252 echo ${bar} Adding label ${bar}
253 make_label > ${tmp}/label
254 ${DISKLABEL} -R -F ${image} ${tmp}/label
255 elif [ -n "${netbsdid}" ]; then
256 echo ${bar} Adding label ${bar}
257 make_label > ${tmp}/label
258 ${DISKLABEL} -R -F ${image} ${tmp}/label
259
260 echo ${bar} Running fdisk ${bar}
261 ${FDISK} -f -i ${image}
262 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
263 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
264 fi
265
266 if $compress; then
267 echo ${bar} Compressing image ${bar}
268 rm -f "${image}.gz"
269 ${GZIP_CMD} -9 ${image}
270 image="${image}.gz"
271 fi
272
273 echo ${bar} Image is ${image} ${bar}
274