mkimage revision 1.32 1 #!/bin/sh
2 # $NetBSD: mkimage,v 1.32 2013/02/10 21:51:05 jmcneill Exp $
3 #
4 # Copyright (c) 2013 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 DIR="$(cd "$(dirname "$0")" && pwd)"
36 PROG="$(basename "$0")"
37
38 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
39 FDISK=${TOOL_FDISK:-fdisk}
40 MAKEFS=${TOOL_MAKEFS:-makefs}
41 MTREE=${TOOL_MTREE:-mtree}
42
43 src="/usr/src"
44 release="/usr/obj/evbarm/release"
45 sets="base comp etc games man misc modules text"
46 xsets="xbase xcomp xetc xfont xserver"
47 minfree="10%"
48 bar="==="
49
50 tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
51 mnt="${tmp}/mnt"
52 mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
53
54 trap "cleanup" 0 1 2 3 15
55
56 cleanup() {
57 case "$tmp" in
58 /tmp/$PROG.*) rm -fr "$tmp";;
59 esac
60 }
61
62 getsize() {
63 set -- $(ls -l $1)
64 echo $5
65 }
66
67 usage() {
68 cat << EOF 1>&2
69 Usage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
70 EOF
71 exit 1
72 }
73
74 # First pass for options to get the host and src directories
75 OPTS="K:D:S:c:h:s:x"
76 while getopts "$OPTS" f
77 do
78 case $f in
79 h) h="$OPTARG";;
80 S) src="$OPTARG";;
81 *) ;;
82 esac
83 done
84
85 if [ -z "$h" ]
86 then
87 usage
88 fi
89
90 if [ ! -f "${DIR}/conf/${h}.conf" ]
91 then
92 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
93 exit 1
94 fi
95
96 . "${DIR}/conf/${h}.conf"
97
98 selected_sets="$sets"
99
100 OPTIND=1
101 while getopts "$OPTS" f
102 do
103 case $f in
104 D) release="$OPTARG";;
105 K) kernel="$OPTARG";;
106 S) ;;
107 c) custom="$OPTARG";;
108 h) ;;
109 s) size="$OPTARG";;
110 x) selected_sets="$sets $xsets";;
111 *) usage;;
112 esac
113 done
114
115 shift $(( $OPTIND - 1 ))
116 if [ -n "$1" ]; then
117 # take the next argument as being the image name
118 image="$1"
119 shift
120 fi
121
122 case "$image" in
123 *.gz) compress=true; image="${image%.gz}";;
124 *) compress=false;;
125 esac
126
127 echo ${bar} configuring sets ${bar}
128 (echo '/set type=dir uname=root gname=wheel mode=0755'
129 for i in $selected_sets; do
130 s="${release}/etc/mtree/set.$i"
131 [ -f "$s" ] && cat "$s"
132 done) > "$tmp/selected_sets"
133
134 make_fstab
135 customize
136 populate
137
138 (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all |
139 ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
140 if [ -n ${msdosid} ]; then
141 echo ${bar} Populating msdos filesystem ${bar}
142 ${MAKEFS} -N ${release}/etc -t msdos \
143 -O $((${init} / 2))m -s $((${boot} / 2))m ${image} ${mnt}/boot
144 fi
145
146 echo ${bar} Populating ffs filesystem ${bar}
147 ${MAKEFS} -N ${release}/etc -t ffs -rx \
148 -O $(((${init} + ${boot} + ${swap}) / 2))m \
149 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
150
151 if [ "${size}" = 0 ]; then
152 size="$(getsize "${image}")"
153 fi
154 newsize=$((${size} / 2 / 1024))
155
156 echo ${bar} Adding label ${bar}
157 make_label > ${tmp}/label
158 ${DISKLABEL} -R -F ${image} ${tmp}/label
159 if [ -n ${msdosid} ]; then
160 echo ${bar} Running fdisk ${bar}
161 initsecs=$((${init} * 1024))
162 bootsecs=$((${boot} * 1024))
163 ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
164 fi
165
166 if $compress; then
167 echo ${bar} Compressing image ${bar}
168 rm -f "${image}.gz"
169 gzip -9 ${image}
170 image="${image}.gz"
171 fi
172
173 echo ${bar} Image is ${image} ${bar}
174