mkimage revision 1.21
11.17Schristos#!/bin/sh
21.21Schristos# $NetBSD: mkimage,v 1.21 2013/02/09 18:50:11 christos 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.21SchristosFDISK=${TOOL_DISKLABEL:-fdisk}
401.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs}
411.21Schristos
421.17Schristossrc="/usr/src"
431.17Schristosrelease="/usr/obj/evbarm/release"
441.17Schristossets="base comp etc games man misc modules text"
451.17Schristosxsets="xbase xcomp xetc xfont xserver" 
461.17Schristosminfree="10%"
471.17Schristosbar="==="
481.17Schristos
491.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
501.17Schristosmnt="${tmp}/mnt"
511.17Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
521.17Schristostrap "cleanup" 0 1 2 3 15
531.17Schristos
541.17Schristoscleanup() {
551.17Schristos	case "$tmp" in
561.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
571.17Schristos	esac
581.17Schristos}
591.1Sagc
601.17Schristosgetsize() {
611.17Schristos	set -- $(ls -l $1)
621.17Schristos	echo $5
631.1Sagc}
641.1Sagc
651.12Schristosusage() {
661.12Schristos	cat << EOF 1>&2
671.20SjmcneillUsage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
681.12SchristosEOF
691.13Schristos	exit 1
701.12Schristos}
711.12Schristos
721.4Schristos# First pass for options to get the host
731.18SchristosOPTS="K:D:S:c:h:s:x"
741.4Schristoswhile getopts "$OPTS" f
751.4Schristosdo
761.4Schristos	case $f in
771.4Schristos	h)	h="$OPTARG";;
781.4Schristos	*)	;;
791.4Schristos	esac
801.4Schristosdone
811.4Schristos
821.4Schristosif [ -z "$h" ]
831.4Schristosthen
841.4Schristos	usage
851.4Schristosfi
861.4Schristos
871.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
881.4Schristosthen
891.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
901.4Schristos	exit 1
911.4Schristosfi
921.4Schristos
931.5Schristos. "${DIR}/conf/${h}.conf"
941.4Schristos
951.17Schristosselected_sets="$sets"
961.17Schristos
971.4SchristosOPTIND=1
981.4Schristoswhile getopts "$OPTS" f
991.4Schristosdo
1001.4Schristos	case $f in
1011.18Schristos	D)	release="$OPTARG";;
1021.18Schristos	K)	kernel="$OPTARG";;
1031.17Schristos	S)	src="$OPTARG";;
1041.4Schristos	c)	custom="$OPTARG";;
1051.4Schristos	h)	;;
1061.4Schristos	s)	size="$OPTARG";;
1071.17Schristos	x)	selected_sets="$sets $xsets";;
1081.6Schristos	*)	usage;;
1091.1Sagc	esac
1101.1Sagcdone
1111.1Sagc
1121.17Schristostrap cleanup 0 1 2 3 15
1131.8Sjmcneill
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.17Schristosecho ${bar} configuring sets ${bar}
1221.17Schristos(echo '/set type=dir uname=root gname=wheel mode=0755'
1231.17Schristosfor i in $selected_sets; do
1241.17Schristos	s="${release}/etc/mtree/set.$i"
1251.17Schristos	[ -f "$s" ] && cat "$s"
1261.17Schristosdone) > "$tmp/selected_sets"
1271.1Sagc
1281.4Schristosmake_fstab
1291.17Schristoscustomize
1301.17Schristospopulate
1311.1Sagc
1321.17Schristos(cd ${mnt}; mtree -c -k all | mtree -C -k all) >> "$tmp/selected_sets"
1331.17Schristosif [ -n ${msdosid} ]; then
1341.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1351.21Schristos	${MAKEFS} -t msdos -O $((${init} / 2))m -s $((${boot} / 2))m \
1361.17Schristos	    ${image} ${mnt}/boot
1371.1Sagcfi
1381.1Sagc
1391.17Schristosecho ${bar} Populating ffs filesystem ${bar}
1401.21Schristos${MAKEFS} -t ffs -rx -O $(((${init} + ${boot} + ${swap}) / 2))m \
1411.17Schristos    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1421.1Sagc
1431.17Schristosif [ -z "$size" ]; then
1441.17Schristos	size=$(getsize ${image})
1451.17Schristosfi
1461.17Schristosnewsize=$((size / 2 / 1024))
1471.1Sagc
1481.17Schristosecho ${bar} Adding label ${bar}
1491.17Schristosmake_label > ${tmp}/label
1501.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
1511.17Schristosif [ -n ${msdosid} ]; then
1521.17Schristos	echo ${bar} Running fdisk ${bar}
1531.21Schristos	${FDISK} -f -u -0 -s ${msdosid}/${init}/${boot} -F ${image}
1541.2Sagcfi
1551.17Schristosecho ${bar} Image is ${image} ${bar}
156