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