mkimage revision 1.33
11.31Sjmcneill#!/bin/sh
21.33Sjmcneill# $NetBSD: mkimage,v 1.33 2013/02/11 18:11:20 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.33Sjmcneillset -e
361.33Sjmcneill
371.17SchristosDIR="$(cd "$(dirname "$0")" && pwd)"
381.17SchristosPROG="$(basename "$0")"
391.17Schristos
401.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel}
411.24SchristosFDISK=${TOOL_FDISK:-fdisk}
421.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs}
431.25SjmcneillMTREE=${TOOL_MTREE:-mtree}
441.21Schristos
451.17Schristossrc="/usr/src"
461.17Schristosrelease="/usr/obj/evbarm/release"
471.17Schristossets="base comp etc games man misc modules text"
481.17Schristosxsets="xbase xcomp xetc xfont xserver" 
491.17Schristosminfree="10%"
501.17Schristosbar="==="
511.17Schristos
521.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
531.17Schristosmnt="${tmp}/mnt"
541.17Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
551.23Schristos
561.17Schristostrap "cleanup" 0 1 2 3 15
571.17Schristos
581.17Schristoscleanup() {
591.17Schristos	case "$tmp" in
601.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
611.17Schristos	esac
621.17Schristos}
631.1Sagc
641.17Schristosgetsize() {
651.17Schristos	set -- $(ls -l $1)
661.17Schristos	echo $5
671.1Sagc}
681.1Sagc
691.12Schristosusage() {
701.12Schristos	cat << EOF 1>&2
711.20SjmcneillUsage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
721.12SchristosEOF
731.13Schristos	exit 1
741.12Schristos}
751.12Schristos
761.32Sjmcneill# First pass for options to get the host and src directories
771.18SchristosOPTS="K:D:S:c:h:s:x"
781.4Schristoswhile getopts "$OPTS" f
791.4Schristosdo
801.4Schristos	case $f in
811.4Schristos	h)	h="$OPTARG";;
821.32Sjmcneill	S)	src="$OPTARG";;
831.4Schristos	*)	;;
841.4Schristos	esac
851.4Schristosdone
861.4Schristos
871.4Schristosif [ -z "$h" ]
881.4Schristosthen
891.4Schristos	usage
901.4Schristosfi
911.4Schristos
921.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
931.4Schristosthen
941.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
951.4Schristos	exit 1
961.4Schristosfi
971.4Schristos
981.5Schristos. "${DIR}/conf/${h}.conf"
991.4Schristos
1001.17Schristosselected_sets="$sets"
1011.17Schristos
1021.4SchristosOPTIND=1
1031.4Schristoswhile getopts "$OPTS" f
1041.4Schristosdo
1051.4Schristos	case $f in
1061.18Schristos	D)	release="$OPTARG";;
1071.18Schristos	K)	kernel="$OPTARG";;
1081.32Sjmcneill	S)	;;
1091.4Schristos	c)	custom="$OPTARG";;
1101.4Schristos	h)	;;
1111.4Schristos	s)	size="$OPTARG";;
1121.17Schristos	x)	selected_sets="$sets $xsets";;
1131.6Schristos	*)	usage;;
1141.1Sagc	esac
1151.1Sagcdone
1161.1Sagc
1171.20Sjmcneillshift $(( $OPTIND - 1 ))
1181.4Schristosif [ -n "$1" ]; then
1191.1Sagc	# take the next argument as being the image name
1201.1Sagc	image="$1"
1211.1Sagc	shift
1221.1Sagcfi
1231.1Sagc
1241.22Schristoscase "$image" in
1251.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1261.22Schristos*)	compress=false;;
1271.22Schristosesac
1281.22Schristos
1291.17Schristosecho ${bar} configuring sets ${bar}
1301.17Schristos(echo '/set type=dir uname=root gname=wheel mode=0755'
1311.17Schristosfor i in $selected_sets; do
1321.17Schristos	s="${release}/etc/mtree/set.$i"
1331.17Schristos	[ -f "$s" ] && cat "$s"
1341.17Schristosdone) > "$tmp/selected_sets"
1351.1Sagc
1361.4Schristosmake_fstab
1371.17Schristoscustomize
1381.17Schristospopulate
1391.1Sagc
1401.26Schristos(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
1411.26Schristos    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
1421.17Schristosif [ -n ${msdosid} ]; then
1431.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1441.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1451.27Schristos	    -O $((${init} / 2))m -s $((${boot} / 2))m ${image} ${mnt}/boot
1461.1Sagcfi
1471.1Sagc
1481.17Schristosecho ${bar} Populating ffs filesystem ${bar}
1491.27Schristos${MAKEFS} -N ${release}/etc -t ffs -rx \
1501.27Schristos    -O $(((${init} + ${boot} + ${swap}) / 2))m \
1511.17Schristos    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1521.1Sagc
1531.23Schristosif [ "${size}" = 0 ]; then
1541.23Schristos	size="$(getsize "${image}")"
1551.17Schristosfi
1561.23Schristosnewsize=$((${size} / 2 / 1024))
1571.1Sagc
1581.17Schristosecho ${bar} Adding label ${bar}
1591.17Schristosmake_label > ${tmp}/label
1601.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
1611.17Schristosif [ -n ${msdosid} ]; then
1621.17Schristos	echo ${bar} Running fdisk ${bar}
1631.30Sjmcneill	initsecs=$((${init} * 1024))
1641.30Sjmcneill	bootsecs=$((${boot} * 1024))
1651.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
1661.2Sagcfi
1671.22Schristos
1681.22Schristosif $compress; then
1691.22Schristos	echo ${bar} Compressing image ${bar}
1701.23Schristos	rm -f "${image}.gz"
1711.22Schristos	gzip -9 ${image}
1721.22Schristos	image="${image}.gz"
1731.22Schristosfi
1741.22Schristos
1751.17Schristosecho ${bar} Image is ${image} ${bar}
176