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