mkimage revision 1.31
11.31Sjmcneill#!/bin/sh
21.31Sjmcneill# $NetBSD: mkimage,v 1.31 2013/02/10 16:56:38 jmcneill Exp $
31.13Schristos#
41.17Schristos# Copyright (c) 2013 The NetBSD Foundation, Inc.
51.1Sagc# All rights reserved.
61.1Sagc#
71.17Schristos# This code is derived from software contributed to The NetBSD Foundation
81.17Schristos# by Christos Zoulas.
91.17Schristos#
101.1Sagc# Redistribution and use in source and binary forms, with or without
111.1Sagc# modification, are permitted provided that the following conditions
121.1Sagc# are met:
131.1Sagc# 1. Redistributions of source code must retain the above copyright
141.1Sagc#    notice, this list of conditions and the following disclaimer.
151.1Sagc# 2. Redistributions in binary form must reproduce the above copyright
161.1Sagc#    notice, this list of conditions and the following disclaimer in the
171.1Sagc#    documentation and/or other materials provided with the distribution.
181.17Schristos# 3. Neither the name of The NetBSD Foundation nor the names of its
191.17Schristos#    contributors may be used to endorse or promote products derived
201.17Schristos#    from this software without specific prior written permission.
211.17Schristos#
221.17Schristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
231.17Schristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
241.17Schristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
251.17Schristos# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
261.17Schristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
271.17Schristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
281.17Schristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
291.17Schristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
301.17Schristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
311.17Schristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
321.17Schristos# POSSIBILITY OF SUCH DAMAGE.
331.1Sagc#
341.1Sagc
351.17SchristosDIR="$(cd "$(dirname "$0")" && pwd)"
361.17SchristosPROG="$(basename "$0")"
371.17Schristos
381.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel}
391.24SchristosFDISK=${TOOL_FDISK:-fdisk}
401.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs}
411.25SjmcneillMTREE=${TOOL_MTREE:-mtree}
421.21Schristos
431.17Schristossrc="/usr/src"
441.17Schristosrelease="/usr/obj/evbarm/release"
451.17Schristossets="base comp etc games man misc modules text"
461.17Schristosxsets="xbase xcomp xetc xfont xserver" 
471.17Schristosminfree="10%"
481.17Schristosbar="==="
491.17Schristos
501.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
511.17Schristosmnt="${tmp}/mnt"
521.17Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
531.23Schristos
541.17Schristostrap "cleanup" 0 1 2 3 15
551.17Schristos
561.17Schristoscleanup() {
571.17Schristos	case "$tmp" in
581.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
591.17Schristos	esac
601.17Schristos}
611.1Sagc
621.17Schristosgetsize() {
631.17Schristos	set -- $(ls -l $1)
641.17Schristos	echo $5
651.1Sagc}
661.1Sagc
671.12Schristosusage() {
681.12Schristos	cat << EOF 1>&2
691.20SjmcneillUsage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
701.12SchristosEOF
711.13Schristos	exit 1
721.12Schristos}
731.12Schristos
741.4Schristos# First pass for options to get the host
751.18SchristosOPTS="K:D:S:c:h:s:x"
761.4Schristoswhile getopts "$OPTS" f
771.4Schristosdo
781.4Schristos	case $f in
791.4Schristos	h)	h="$OPTARG";;
801.4Schristos	*)	;;
811.4Schristos	esac
821.4Schristosdone
831.4Schristos
841.4Schristosif [ -z "$h" ]
851.4Schristosthen
861.4Schristos	usage
871.4Schristosfi
881.4Schristos
891.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
901.4Schristosthen
911.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
921.4Schristos	exit 1
931.4Schristosfi
941.4Schristos
951.5Schristos. "${DIR}/conf/${h}.conf"
961.4Schristos
971.17Schristosselected_sets="$sets"
981.17Schristos
991.4SchristosOPTIND=1
1001.4Schristoswhile getopts "$OPTS" f
1011.4Schristosdo
1021.4Schristos	case $f in
1031.18Schristos	D)	release="$OPTARG";;
1041.18Schristos	K)	kernel="$OPTARG";;
1051.17Schristos	S)	src="$OPTARG";;
1061.4Schristos	c)	custom="$OPTARG";;
1071.4Schristos	h)	;;
1081.4Schristos	s)	size="$OPTARG";;
1091.17Schristos	x)	selected_sets="$sets $xsets";;
1101.6Schristos	*)	usage;;
1111.1Sagc	esac
1121.1Sagcdone
1131.1Sagc
1141.20Sjmcneillshift $(( $OPTIND - 1 ))
1151.4Schristosif [ -n "$1" ]; then
1161.1Sagc	# take the next argument as being the image name
1171.1Sagc	image="$1"
1181.1Sagc	shift
1191.1Sagcfi
1201.1Sagc
1211.22Schristoscase "$image" in
1221.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1231.22Schristos*)	compress=false;;
1241.22Schristosesac
1251.22Schristos
1261.17Schristosecho ${bar} configuring sets ${bar}
1271.17Schristos(echo '/set type=dir uname=root gname=wheel mode=0755'
1281.17Schristosfor i in $selected_sets; do
1291.17Schristos	s="${release}/etc/mtree/set.$i"
1301.17Schristos	[ -f "$s" ] && cat "$s"
1311.17Schristosdone) > "$tmp/selected_sets"
1321.1Sagc
1331.4Schristosmake_fstab
1341.17Schristoscustomize
1351.17Schristospopulate
1361.1Sagc
1371.26Schristos(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
1381.26Schristos    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
1391.17Schristosif [ -n ${msdosid} ]; then
1401.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1411.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1421.27Schristos	    -O $((${init} / 2))m -s $((${boot} / 2))m ${image} ${mnt}/boot
1431.1Sagcfi
1441.1Sagc
1451.17Schristosecho ${bar} Populating ffs filesystem ${bar}
1461.27Schristos${MAKEFS} -N ${release}/etc -t ffs -rx \
1471.27Schristos    -O $(((${init} + ${boot} + ${swap}) / 2))m \
1481.17Schristos    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1491.1Sagc
1501.23Schristosif [ "${size}" = 0 ]; then
1511.23Schristos	size="$(getsize "${image}")"
1521.17Schristosfi
1531.23Schristosnewsize=$((${size} / 2 / 1024))
1541.1Sagc
1551.17Schristosecho ${bar} Adding label ${bar}
1561.17Schristosmake_label > ${tmp}/label
1571.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
1581.17Schristosif [ -n ${msdosid} ]; then
1591.17Schristos	echo ${bar} Running fdisk ${bar}
1601.30Sjmcneill	initsecs=$((${init} * 1024))
1611.30Sjmcneill	bootsecs=$((${boot} * 1024))
1621.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
1631.2Sagcfi
1641.22Schristos
1651.22Schristosif $compress; then
1661.22Schristos	echo ${bar} Compressing image ${bar}
1671.23Schristos	rm -f "${image}.gz"
1681.22Schristos	gzip -9 ${image}
1691.22Schristos	image="${image}.gz"
1701.22Schristosfi
1711.22Schristos
1721.17Schristosecho ${bar} Image is ${image} ${bar}
173