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