mkimage revision 1.59
1#!/bin/sh
2# $NetBSD: mkimage,v 1.59 2015/05/24 17:08:21 christos Exp $
3#
4# Copyright (c) 2013, 2014 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#
36# Makes a bootable image for the host architecture given.
37# The host specific functions are pulled in from a /bin/sh script in the
38# "conf" directory, and is expected to provide the following shell
39# functions, which are called in the following order:
40#
41#  - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}.
42#    If -m is given, a number of directories are put on a tmpfs RAM disk
43#  - customize: After unpacking the sets, this gets the system to
44#    a working state, e. g. by setting up /etc/rc.conf and /dev
45#  - populate: Add common goods like kernel and bootloader
46#  - make_label: Prints disklabel to stdout
47#
48
49set -e
50
51DIR="$(cd "$(dirname "$0")" && pwd)"
52PROG="$(basename "$0")"
53
54DISKLABEL=${TOOL_DISKLABEL:-disklabel}
55FDISK=${TOOL_FDISK:-fdisk}
56MAKEFS=${TOOL_MAKEFS:-makefs}
57MTREE=${TOOL_MTREE:-mtree}
58INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
59MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
60GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
61
62src="/usr/src"
63sets="base comp etc games man misc modules text"
64xsets="xbase xcomp xetc xfont xserver" 
65minfree="10%"
66bar="==="
67
68tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
69mnt="${tmp}/mnt"
70mkdir -p "${mnt}/etc" "${mnt}/dev"
71
72trap "cleanup" 0 1 2 3 15
73
74cleanup() {
75	case "$tmp" in
76	/tmp/$PROG.*)	rm -fr "$tmp";;
77	esac
78}
79
80getsize() {
81	set -- $(ls -l $1)
82	echo $5
83}
84
85usage() {
86	cat << EOF 1>&2
87Usage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
88
89-b	Boot only, no sets loaded
90-r	root device kind (sd, wd, ld)
91-d	Add the debug sets
92-m	Optimize the OS installation to mimimize disk writes for SSDs
93-x	Load the X sets too, not just the base ones
94EOF
95	exit 1
96}
97
98# First pass for options to get the host and src directories
99OPTS="K:D:S:bc:dh:mr:s:x"
100while getopts "$OPTS" f
101do
102	case $f in
103	h)	h="$OPTARG";;
104	S)	src="$OPTARG";;
105	*)	;;
106	esac
107done
108
109if [ -z "$h" ]
110then
111	usage
112fi
113
114if [ ! -f "${DIR}/conf/${h}.conf" ]
115then
116	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
117	exit 1
118fi
119
120resize=false
121
122. "${DIR}/conf/${h}.conf"
123release="/usr/obj/${MACHINE}/release"
124
125selected_sets="$sets"
126dsets_p=false
127xsets_p=false
128minwrites=false
129rootdev=ld
130
131OPTIND=1
132while getopts "$OPTS" f
133do
134	case $f in
135	D)	release="$OPTARG";;
136	K)	kernel="$OPTARG";;
137	S)	;;
138	b)	bootonly=true;;
139	d)	dsets_p=true
140		selected_sets="$selected_sets debug"
141		if $xsets_p; then
142			selected_sets="$selected_sets xdebug"
143		fi
144		;;
145	c)	custom="$OPTARG";;
146	h)	;;
147	m)	minwrites=true;;
148	r)	rootdev="$OPTARG";;
149	s)	size="$OPTARG";;
150	x)	xsets_p=true
151		selected_sets="$selected_sets $xsets"
152		if $dsets_p; then
153		    selected_sets="$selected_sets xdebug"
154		fi
155		;;
156	*)	usage;;
157	esac
158done
159
160shift $(( $OPTIND - 1 ))
161if [ -n "$1" ]; then
162	# take the next argument as being the image name
163	image="$1"
164	shift
165fi
166
167case "$image" in
168*.gz)	compress=true; image="${image%.gz}";;
169*)	compress=false;;
170esac
171
172if [ -z "${bootonly}" ]; then
173	echo ${bar} configuring sets ${bar}
174	(cat "${release}/etc/mtree/NetBSD.dist"
175	for i in $selected_sets; do
176		s="${release}/etc/mtree/set.$i"
177		if [ -f "$s" ]; then
178			cat "$s"
179		fi
180	done) > "$tmp/selected_sets"
181fi
182
183make_fstab
184customize
185populate
186
187if [ -n "${msdosid}" ]; then
188	echo ${bar} Populating msdos filesystem ${bar}
189	${MAKEFS} -N ${release}/etc -t msdos \
190	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
191	    ${image} ${mnt}/boot
192fi
193
194if [ -z "${bootonly}" ]; then
195	echo ${bar} Populating ffs filesystem ${bar}
196	${MAKEFS} -r -N ${release}/etc -t ffs -rx \
197	    -O ${ffsoffset} \
198	    -o d=4096,f=2048,b=16384 -b $((${extra}))m \
199	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
200fi
201
202if [ "${size}" = 0 ]; then
203	size="$(getsize "${image}")"
204fi
205newsize=$((${size} / 2 / 1024))
206compare=$((${newsize} * 2 * 1024))
207while [ "${compare}" != "${size}" ]
208do    
209	size="$((size + size - compare))"  
210	newsize="$((${size} / 2 / 1024))"
211	compare="$((${newsize} * 2 * 1024))"
212done                      
213
214echo ${bar} Adding label ${bar}
215make_label > ${tmp}/label
216${DISKLABEL} -R -F ${image} ${tmp}/label
217if [ -n "${msdosid}" ]; then
218	echo ${bar} Running fdisk ${bar}
219	initsecs=$((${init} * 1024))
220	bootsecs=$((${boot} * 1024))
221	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
222elif [ -n "${netbsdid}" ]; then
223	echo ${bar} Running fdisk ${bar}
224	${FDISK} -f -i ${image}
225	${FDISK} -f -a -u -0 -s 169/${init} ${image}
226	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
227fi
228
229if $compress; then
230	echo ${bar} Compressing image ${bar}
231	rm -f "${image}.gz"
232	${GZIP_CMD} -9 ${image}
233	image="${image}.gz"
234fi
235
236echo ${bar} Image is ${image} ${bar}
237