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