mkimage revision 1.62 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.62 2017/03/14 06:37:39 skrll 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 usage() {
86 cat << EOF 1>&2
87 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
88
89 -b Boot only, no sets loaded
90 -r root device kind (sd, wd, ld)
91 -d Add the debug sets
92 -m Optimize the OS installation to mimimize disk writes for SSDs
93 -x Load the X sets too, not just the base ones
94 EOF
95 exit 1
96 }
97
98 # First pass for options to get the host and src directories
99 OPTS="B:D:K:S:bc:dh:mr:s:x"
100 while getopts "$OPTS" f
101 do
102 case $f in
103 h) h="$OPTARG";;
104 S) src="$OPTARG";;
105 *) ;;
106 esac
107 done
108
109 if [ -z "$h" ]
110 then
111 usage
112 fi
113
114 if [ ! -f "${DIR}/conf/${h}.conf" ]
115 then
116 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
117 exit 1
118 fi
119
120 resize=false
121
122 . "${DIR}/conf/${h}.conf"
123 release="/usr/obj/${MACHINE}/release"
124
125 selected_sets="$sets"
126 dsets_p=false
127 xsets_p=false
128 minwrites=false
129 rootdev=ld
130 endian=
131
132 OPTIND=1
133 while getopts "$OPTS" f
134 do
135 case $f in
136 B) endian="-B $OPTARG";;
137 D) release="$OPTARG";;
138 K) kernel="$OPTARG";;
139 S) ;;
140 b) bootonly=true;;
141 d) dsets_p=true
142 selected_sets="$selected_sets debug"
143 if $xsets_p; then
144 selected_sets="$selected_sets xdebug"
145 fi
146 ;;
147 c) custom="$OPTARG";;
148 h) ;;
149 m) minwrites=true;;
150 r) rootdev="$OPTARG";;
151 s) size="$OPTARG";;
152 x) xsets_p=true
153 selected_sets="$selected_sets $xsets"
154 if $dsets_p; then
155 selected_sets="$selected_sets xdebug"
156 fi
157 ;;
158 *) usage;;
159 esac
160 done
161
162 shift $(( $OPTIND - 1 ))
163 if [ -n "$1" ]; then
164 # take the next argument as being the image name
165 image="$1"
166 shift
167 fi
168
169 case "$image" in
170 *.gz) compress=true; image="${image%.gz}";;
171 *) compress=false;;
172 esac
173
174 if [ -z "${bootonly}" ]; then
175 echo ${bar} configuring sets ${bar}
176 (cat "${release}/etc/mtree/NetBSD.dist"
177 for i in $selected_sets; do
178 s="${release}/etc/mtree/set.$i"
179 if [ -f "$s" ]; then
180 cat "$s"
181 fi
182 done) > "$tmp/selected_sets"
183 fi
184
185 make_fstab
186 customize
187 populate
188
189 if [ -n "${msdosid}" ]; then
190 echo ${bar} Populating msdos filesystem ${bar}
191 ${MAKEFS} -N ${release}/etc -t msdos \
192 -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
193 ${image} ${mnt}/boot
194 fi
195
196 if [ -z "${bootonly}" ]; then
197 echo ${bar} Populating ffs filesystem ${bar}
198 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
199 -O ${ffsoffset} \
200 -o d=4096,f=8192,b=65536 -b $((${extra}))m \
201 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
202 fi
203
204 if [ "${size}" = 0 ]; then
205 size="$(getsize "${image}")"
206 fi
207 newsize=$((${size} / 2 / 1024))
208 compare=$((${newsize} * 2 * 1024))
209 while [ "${compare}" != "${size}" ]
210 do
211 size="$((size + size - compare))"
212 newsize="$((${size} / 2 / 1024))"
213 compare="$((${newsize} * 2 * 1024))"
214 done
215
216 echo ${bar} Adding label ${bar}
217 make_label > ${tmp}/label
218 ${DISKLABEL} -R -F ${image} ${tmp}/label
219 if [ -n "${msdosid}" ]; then
220 echo ${bar} Running fdisk ${bar}
221 initsecs=$((${init} * 1024))
222 bootsecs=$((${boot} * 1024))
223 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
224 elif [ -n "${netbsdid}" ]; then
225 echo ${bar} Running fdisk ${bar}
226 ${FDISK} -f -i ${image}
227 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
228 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
229 fi
230
231 if $compress; then
232 echo ${bar} Compressing image ${bar}
233 rm -f "${image}.gz"
234 ${GZIP_CMD} -9 ${image}
235 image="${image}.gz"
236 fi
237
238 echo ${bar} Image is ${image} ${bar}
239