mkimage revision 1.59 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.59 2015/05/24 17:08:21 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 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] [-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="K:D: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
131 OPTIND=1
132 while getopts "$OPTS" f
133 do
134 case $f in
135 D) release="$OPTARG";;
136 K) kernel="$OPTARG";;
137 S) ;;
138 b) bootonly=true;;
139 d) dsets_p=true
140 selected_sets="$selected_sets debug"
141 if $xsets_p; then
142 selected_sets="$selected_sets xdebug"
143 fi
144 ;;
145 c) custom="$OPTARG";;
146 h) ;;
147 m) minwrites=true;;
148 r) rootdev="$OPTARG";;
149 s) size="$OPTARG";;
150 x) xsets_p=true
151 selected_sets="$selected_sets $xsets"
152 if $dsets_p; then
153 selected_sets="$selected_sets xdebug"
154 fi
155 ;;
156 *) usage;;
157 esac
158 done
159
160 shift $(( $OPTIND - 1 ))
161 if [ -n "$1" ]; then
162 # take the next argument as being the image name
163 image="$1"
164 shift
165 fi
166
167 case "$image" in
168 *.gz) compress=true; image="${image%.gz}";;
169 *) compress=false;;
170 esac
171
172 if [ -z "${bootonly}" ]; then
173 echo ${bar} configuring sets ${bar}
174 (cat "${release}/etc/mtree/NetBSD.dist"
175 for i in $selected_sets; do
176 s="${release}/etc/mtree/set.$i"
177 if [ -f "$s" ]; then
178 cat "$s"
179 fi
180 done) > "$tmp/selected_sets"
181 fi
182
183 make_fstab
184 customize
185 populate
186
187 if [ -n "${msdosid}" ]; then
188 echo ${bar} Populating msdos filesystem ${bar}
189 ${MAKEFS} -N ${release}/etc -t msdos \
190 -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
191 ${image} ${mnt}/boot
192 fi
193
194 if [ -z "${bootonly}" ]; then
195 echo ${bar} Populating ffs filesystem ${bar}
196 ${MAKEFS} -r -N ${release}/etc -t ffs -rx \
197 -O ${ffsoffset} \
198 -o d=4096,f=2048,b=16384 -b $((${extra}))m \
199 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
200 fi
201
202 if [ "${size}" = 0 ]; then
203 size="$(getsize "${image}")"
204 fi
205 newsize=$((${size} / 2 / 1024))
206 compare=$((${newsize} * 2 * 1024))
207 while [ "${compare}" != "${size}" ]
208 do
209 size="$((size + size - compare))"
210 newsize="$((${size} / 2 / 1024))"
211 compare="$((${newsize} * 2 * 1024))"
212 done
213
214 echo ${bar} Adding label ${bar}
215 make_label > ${tmp}/label
216 ${DISKLABEL} -R -F ${image} ${tmp}/label
217 if [ -n "${msdosid}" ]; then
218 echo ${bar} Running fdisk ${bar}
219 initsecs=$((${init} * 1024))
220 bootsecs=$((${boot} * 1024))
221 ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
222 elif [ -n "${netbsdid}" ]; then
223 echo ${bar} Running fdisk ${bar}
224 ${FDISK} -f -i ${image}
225 ${FDISK} -f -a -u -0 -s 169/${init} ${image}
226 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
227 fi
228
229 if $compress; then
230 echo ${bar} Compressing image ${bar}
231 rm -f "${image}.gz"
232 ${GZIP_CMD} -9 ${image}
233 image="${image}.gz"
234 fi
235
236 echo ${bar} Image is ${image} ${bar}
237