mkimage revision 1.18 1 1.17 christos #!/bin/sh
2 1.18 christos # $NetBSD: mkimage,v 1.18 2013/02/08 19:14:14 christos Exp $
3 1.13 christos #
4 1.17 christos # Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 agc # All rights reserved.
6 1.1 agc #
7 1.17 christos # This code is derived from software contributed to The NetBSD Foundation
8 1.17 christos # by Christos Zoulas.
9 1.17 christos #
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.17 christos # 3. Neither the name of The NetBSD Foundation nor the names of its
19 1.17 christos # contributors may be used to endorse or promote products derived
20 1.17 christos # from this software without specific prior written permission.
21 1.17 christos #
22 1.17 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.17 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.17 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.17 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.17 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.17 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.17 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.17 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.17 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.17 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.17 christos # POSSIBILITY OF SUCH DAMAGE.
33 1.1 agc #
34 1.1 agc
35 1.17 christos DIR="$(cd "$(dirname "$0")" && pwd)"
36 1.17 christos PROG="$(basename "$0")"
37 1.17 christos
38 1.17 christos src="/usr/src"
39 1.17 christos release="/usr/obj/evbarm/release"
40 1.17 christos sets="base comp etc games man misc modules text"
41 1.17 christos xsets="xbase xcomp xetc xfont xserver"
42 1.17 christos minfree="10%"
43 1.17 christos bar="==="
44 1.17 christos
45 1.17 christos tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
46 1.17 christos mnt="${tmp}/mnt"
47 1.17 christos mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
48 1.17 christos trap "cleanup" 0 1 2 3 15
49 1.17 christos
50 1.17 christos cleanup() {
51 1.17 christos case "$tmp" in
52 1.17 christos /tmp/$PROG.*) rm -fr "$tmp";;
53 1.17 christos esac
54 1.17 christos }
55 1.1 agc
56 1.17 christos getsize() {
57 1.17 christos set -- $(ls -l $1)
58 1.17 christos echo $5
59 1.1 agc }
60 1.1 agc
61 1.12 christos usage() {
62 1.12 christos cat << EOF 1>&2
63 1.17 christos Usage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-R <releasedir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
64 1.12 christos EOF
65 1.13 christos exit 1
66 1.12 christos }
67 1.12 christos
68 1.4 christos # First pass for options to get the host
69 1.18 christos OPTS="K:D:S:c:h:s:x"
70 1.4 christos while getopts "$OPTS" f
71 1.4 christos do
72 1.4 christos case $f in
73 1.4 christos h) h="$OPTARG";;
74 1.4 christos *) ;;
75 1.4 christos esac
76 1.4 christos done
77 1.4 christos
78 1.4 christos if [ -z "$h" ]
79 1.4 christos then
80 1.4 christos usage
81 1.4 christos fi
82 1.4 christos
83 1.5 christos if [ ! -f "${DIR}/conf/${h}.conf" ]
84 1.4 christos then
85 1.5 christos echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
86 1.4 christos exit 1
87 1.4 christos fi
88 1.4 christos
89 1.5 christos . "${DIR}/conf/${h}.conf"
90 1.4 christos
91 1.17 christos selected_sets="$sets"
92 1.17 christos
93 1.4 christos OPTIND=1
94 1.4 christos while getopts "$OPTS" f
95 1.4 christos do
96 1.4 christos case $f in
97 1.18 christos D) release="$OPTARG";;
98 1.18 christos K) kernel="$OPTARG";;
99 1.17 christos S) src="$OPTARG";;
100 1.4 christos c) custom="$OPTARG";;
101 1.4 christos h) ;;
102 1.4 christos s) size="$OPTARG";;
103 1.17 christos x) selected_sets="$sets $xsets";;
104 1.6 christos *) usage;;
105 1.1 agc esac
106 1.1 agc done
107 1.1 agc
108 1.17 christos trap cleanup 0 1 2 3 15
109 1.8 jmcneill
110 1.4 christos shift $(( "$OPTIND" - 1 ))
111 1.4 christos if [ -n "$1" ]; then
112 1.1 agc # take the next argument as being the image name
113 1.1 agc image="$1"
114 1.1 agc shift
115 1.1 agc fi
116 1.1 agc
117 1.17 christos . "$DIR/conf/rpi.conf"
118 1.11 christos
119 1.17 christos echo ${bar} configuring sets ${bar}
120 1.17 christos (echo '/set type=dir uname=root gname=wheel mode=0755'
121 1.17 christos for i in $selected_sets; do
122 1.17 christos s="${release}/etc/mtree/set.$i"
123 1.17 christos [ -f "$s" ] && cat "$s"
124 1.17 christos done) > "$tmp/selected_sets"
125 1.1 agc
126 1.4 christos make_fstab
127 1.17 christos customize
128 1.17 christos populate
129 1.1 agc
130 1.17 christos (cd ${mnt}; mtree -c -k all | mtree -C -k all) >> "$tmp/selected_sets"
131 1.17 christos if [ -n ${msdosid} ]; then
132 1.17 christos echo ${bar} Populating msdos filesystem ${bar}
133 1.17 christos makefs -t msdos -O $((${init} / 2))m -s $((${boot} / 2))m \
134 1.17 christos ${image} ${mnt}/boot
135 1.1 agc fi
136 1.1 agc
137 1.17 christos echo ${bar} Populating ffs filesystem ${bar}
138 1.17 christos makefs -t ffs -rx -O $(((${init} + ${boot} + ${swap}) / 2))m \
139 1.17 christos -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
140 1.1 agc
141 1.17 christos if [ -z "$size" ]; then
142 1.17 christos size=$(getsize ${image})
143 1.17 christos fi
144 1.17 christos newsize=$((size / 2 / 1024))
145 1.1 agc
146 1.17 christos echo ${bar} Adding label ${bar}
147 1.17 christos make_label > ${tmp}/label
148 1.17 christos disklabel -R -F ${image} ${tmp}/label
149 1.17 christos if [ -n ${msdosid} ]; then
150 1.17 christos echo ${bar} Running fdisk ${bar}
151 1.17 christos fdisk -f -u -0 -s ${msdosid}/${init}/${boot} -F ${image}
152 1.2 agc fi
153 1.17 christos echo ${bar} Image is ${image} ${bar}
154