mkimage revision 1.3.4.3 1 1.3.4.1 tls #!/bin/sh
2 1.3.4.1 tls # $NetBSD: mkimage,v 1.3.4.3 2014/08/19 23:45:45 tls Exp $
3 1.3.4.1 tls #
4 1.3.4.3 tls # Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
5 1.1 agc # All rights reserved.
6 1.1 agc #
7 1.3.4.1 tls # This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.1 tls # by Christos Zoulas.
9 1.3.4.1 tls #
10 1.1 agc # Redistribution and use in source and binary forms, with or without
11 1.1 agc # modification, are permitted provided that the following conditions
12 1.1 agc # are met:
13 1.1 agc # 1. Redistributions of source code must retain the above copyright
14 1.1 agc # notice, this list of conditions and the following disclaimer.
15 1.1 agc # 2. Redistributions in binary form must reproduce the above copyright
16 1.1 agc # notice, this list of conditions and the following disclaimer in the
17 1.1 agc # documentation and/or other materials provided with the distribution.
18 1.3.4.1 tls # 3. Neither the name of The NetBSD Foundation nor the names of its
19 1.3.4.1 tls # contributors may be used to endorse or promote products derived
20 1.3.4.1 tls # from this software without specific prior written permission.
21 1.3.4.1 tls #
22 1.3.4.1 tls # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.3.4.1 tls # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.3.4.1 tls # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.3.4.1 tls # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.3.4.1 tls # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.3.4.1 tls # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.3.4.1 tls # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.3.4.1 tls # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.3.4.1 tls # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.3.4.1 tls # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.3.4.1 tls # POSSIBILITY OF SUCH DAMAGE.
33 1.3.4.1 tls #
34 1.3.4.1 tls
35 1.3.4.1 tls set -e
36 1.3.4.1 tls
37 1.3.4.1 tls DIR="$(cd "$(dirname "$0")" && pwd)"
38 1.3.4.1 tls PROG="$(basename "$0")"
39 1.3.4.1 tls
40 1.3.4.1 tls DISKLABEL=${TOOL_DISKLABEL:-disklabel}
41 1.3.4.1 tls FDISK=${TOOL_FDISK:-fdisk}
42 1.3.4.1 tls MAKEFS=${TOOL_MAKEFS:-makefs}
43 1.3.4.1 tls MTREE=${TOOL_MTREE:-mtree}
44 1.3.4.3 tls INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
45 1.3.4.3 tls GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
46 1.3.4.1 tls
47 1.3.4.1 tls src="/usr/src"
48 1.3.4.1 tls sets="base comp etc games man misc modules text"
49 1.3.4.1 tls xsets="xbase xcomp xetc xfont xserver"
50 1.3.4.1 tls minfree="10%"
51 1.3.4.1 tls bar="==="
52 1.1 agc
53 1.3.4.1 tls tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
54 1.3.4.1 tls mnt="${tmp}/mnt"
55 1.3.4.3 tls mkdir -p "${mnt}/etc" "${mnt}/dev"
56 1.3.4.1 tls
57 1.3.4.1 tls trap "cleanup" 0 1 2 3 15
58 1.3.4.1 tls
59 1.3.4.1 tls cleanup() {
60 1.3.4.1 tls case "$tmp" in
61 1.3.4.1 tls /tmp/$PROG.*) rm -fr "$tmp";;
62 1.3.4.1 tls esac
63 1.3.4.1 tls }
64 1.1 agc
65 1.3.4.1 tls getsize() {
66 1.3.4.1 tls set -- $(ls -l $1)
67 1.3.4.1 tls echo $5
68 1.1 agc }
69 1.1 agc
70 1.3.4.1 tls usage() {
71 1.3.4.1 tls cat << EOF 1>&2
72 1.3.4.3 tls Usage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
73 1.3.4.2 tls
74 1.3.4.2 tls -b Boot only, no sets loaded
75 1.3.4.3 tls -r root device kind (sd, wd, ld)
76 1.3.4.2 tls -d Add the debug sets
77 1.3.4.3 tls -m Optimize the OS installation to mimimize disk writes for SSDs
78 1.3.4.2 tls -x Load the x sets too, not just the base ones
79 1.3.4.1 tls EOF
80 1.3.4.1 tls exit 1
81 1.1 agc }
82 1.1 agc
83 1.3.4.1 tls # First pass for options to get the host and src directories
84 1.3.4.3 tls OPTS="K:D:S:bc:dh:mr:s:x"
85 1.3.4.1 tls while getopts "$OPTS" f
86 1.3.4.1 tls do
87 1.3.4.1 tls case $f in
88 1.3.4.1 tls h) h="$OPTARG";;
89 1.3.4.1 tls S) src="$OPTARG";;
90 1.3.4.1 tls *) ;;
91 1.1 agc esac
92 1.1 agc done
93 1.1 agc
94 1.3.4.1 tls if [ -z "$h" ]
95 1.3.4.1 tls then
96 1.3.4.1 tls usage
97 1.1 agc fi
98 1.1 agc
99 1.3.4.1 tls if [ ! -f "${DIR}/conf/${h}.conf" ]
100 1.3.4.1 tls then
101 1.3.4.1 tls echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
102 1.3.4.1 tls exit 1
103 1.1 agc fi
104 1.1 agc
105 1.3.4.1 tls . "${DIR}/conf/${h}.conf"
106 1.3.4.3 tls release="/usr/obj/${MACHINE}/release"
107 1.1 agc
108 1.3.4.1 tls selected_sets="$sets"
109 1.3.4.2 tls dsets=false
110 1.3.4.2 tls xsets=false
111 1.3.4.2 tls minwrites=false
112 1.3.4.3 tls rootdev=ld
113 1.1 agc
114 1.3.4.1 tls OPTIND=1
115 1.3.4.1 tls while getopts "$OPTS" f
116 1.3.4.1 tls do
117 1.3.4.1 tls case $f in
118 1.3.4.1 tls D) release="$OPTARG";;
119 1.3.4.1 tls K) kernel="$OPTARG";;
120 1.3.4.1 tls S) ;;
121 1.3.4.3 tls b) bootonly=true;;
122 1.3.4.2 tls d) dsets=true
123 1.3.4.2 tls selected_sets="$selected_sets debug"
124 1.3.4.2 tls if $xsets; then
125 1.3.4.2 tls selected_sets="$selected_sets xdebug"
126 1.3.4.2 tls fi
127 1.3.4.2 tls ;;
128 1.3.4.1 tls c) custom="$OPTARG";;
129 1.3.4.1 tls h) ;;
130 1.3.4.2 tls m) minwrites=true;;
131 1.3.4.3 tls r) rootdev="$OPTARG";;
132 1.3.4.1 tls s) size="$OPTARG";;
133 1.3.4.2 tls x) xsets=true
134 1.3.4.2 tls selected_sets="$selected_sets $xsets"
135 1.3.4.2 tls if $dsets; then
136 1.3.4.2 tls selected_sets="$selected_sets xdebug"
137 1.3.4.2 tls fi
138 1.3.4.2 tls ;;
139 1.3.4.1 tls *) usage;;
140 1.3.4.1 tls esac
141 1.3.4.1 tls done
142 1.1 agc
143 1.3.4.1 tls shift $(( $OPTIND - 1 ))
144 1.3.4.1 tls if [ -n "$1" ]; then
145 1.3.4.1 tls # take the next argument as being the image name
146 1.3.4.1 tls image="$1"
147 1.3.4.1 tls shift
148 1.3.4.1 tls fi
149 1.1 agc
150 1.3.4.1 tls case "$image" in
151 1.3.4.1 tls *.gz) compress=true; image="${image%.gz}";;
152 1.3.4.1 tls *) compress=false;;
153 1.3.4.1 tls esac
154 1.3.4.1 tls
155 1.3.4.3 tls if [ -z "${bootonly}" ]; then
156 1.3.4.2 tls echo ${bar} configuring sets ${bar}
157 1.3.4.2 tls (echo '/set type=dir uname=root gname=wheel mode=0755'
158 1.3.4.2 tls for i in $selected_sets; do
159 1.3.4.2 tls s="${release}/etc/mtree/set.$i"
160 1.3.4.2 tls if [ -f "$s" ]; then
161 1.3.4.2 tls cat "$s"
162 1.3.4.2 tls fi
163 1.3.4.2 tls done) > "$tmp/selected_sets"
164 1.3.4.2 tls fi
165 1.3.4.1 tls
166 1.3.4.1 tls make_fstab
167 1.3.4.1 tls customize
168 1.3.4.1 tls populate
169 1.3.4.1 tls
170 1.3.4.3 tls if [ -z "${bootonly}" ]; then
171 1.3.4.2 tls (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all |
172 1.3.4.2 tls ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
173 1.3.4.2 tls fi
174 1.3.4.3 tls if [ -n "${msdosid}" ]; then
175 1.3.4.1 tls echo ${bar} Populating msdos filesystem ${bar}
176 1.3.4.1 tls ${MAKEFS} -N ${release}/etc -t msdos \
177 1.3.4.3 tls -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
178 1.3.4.3 tls ${image} ${mnt}/boot
179 1.3.4.1 tls fi
180 1.3.4.1 tls
181 1.3.4.3 tls if [ -z "${bootonly}" ]; then
182 1.3.4.2 tls echo ${bar} Populating ffs filesystem ${bar}
183 1.3.4.2 tls ${MAKEFS} -N ${release}/etc -t ffs -rx \
184 1.3.4.3 tls -O ${ffsoffset} \
185 1.3.4.3 tls -o d=4096 -b $((${extra}))m \
186 1.3.4.2 tls -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
187 1.3.4.2 tls fi
188 1.1 agc
189 1.3.4.1 tls if [ "${size}" = 0 ]; then
190 1.3.4.1 tls size="$(getsize "${image}")"
191 1.2 agc fi
192 1.3.4.1 tls newsize=$((${size} / 2 / 1024))
193 1.1 agc
194 1.3.4.1 tls echo ${bar} Adding label ${bar}
195 1.3.4.1 tls make_label > ${tmp}/label
196 1.3.4.1 tls ${DISKLABEL} -R -F ${image} ${tmp}/label
197 1.3.4.3 tls if [ -n "${msdosid}" ]; then
198 1.3.4.1 tls echo ${bar} Running fdisk ${bar}
199 1.3.4.1 tls initsecs=$((${init} * 1024))
200 1.3.4.1 tls bootsecs=$((${boot} * 1024))
201 1.3.4.1 tls ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
202 1.3.4.3 tls elif [ -n "${netbsdid}" ]; then
203 1.3.4.3 tls echo ${bar} Running fdisk ${bar}
204 1.3.4.3 tls ${FDISK} -f -i ${image}
205 1.3.4.3 tls ${FDISK} -f -a -u -0 -s 169 ${image}
206 1.3.4.3 tls ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
207 1.3.4.1 tls fi
208 1.1 agc
209 1.3.4.1 tls if $compress; then
210 1.3.4.1 tls echo ${bar} Compressing image ${bar}
211 1.3.4.1 tls rm -f "${image}.gz"
212 1.3.4.3 tls ${GZIP_CMD} -9 ${image}
213 1.3.4.1 tls image="${image}.gz"
214 1.3.4.1 tls fi
215 1.1 agc
216 1.3.4.1 tls echo ${bar} Image is ${image} ${bar}
217