mkimage revision 1.20
11.17Schristos#!/bin/sh
21.20Sjmcneill# $NetBSD: mkimage,v 1.20 2013/02/09 11:20:56 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.17Schristossrc="/usr/src"
391.17Schristosrelease="/usr/obj/evbarm/release"
401.17Schristossets="base comp etc games man misc modules text"
411.17Schristosxsets="xbase xcomp xetc xfont xserver" 
421.17Schristosminfree="10%"
431.17Schristosbar="==="
441.17Schristos
451.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
461.17Schristosmnt="${tmp}/mnt"
471.17Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
481.17Schristostrap "cleanup" 0 1 2 3 15
491.17Schristos
501.17Schristoscleanup() {
511.17Schristos	case "$tmp" in
521.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
531.17Schristos	esac
541.17Schristos}
551.1Sagc
561.17Schristosgetsize() {
571.17Schristos	set -- $(ls -l $1)
581.17Schristos	echo $5
591.1Sagc}
601.1Sagc
611.12Schristosusage() {
621.12Schristos	cat << EOF 1>&2
631.20SjmcneillUsage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
641.12SchristosEOF
651.13Schristos	exit 1
661.12Schristos}
671.12Schristos
681.4Schristos# First pass for options to get the host
691.18SchristosOPTS="K:D:S:c:h:s:x"
701.4Schristoswhile getopts "$OPTS" f
711.4Schristosdo
721.4Schristos	case $f in
731.4Schristos	h)	h="$OPTARG";;
741.4Schristos	*)	;;
751.4Schristos	esac
761.4Schristosdone
771.4Schristos
781.4Schristosif [ -z "$h" ]
791.4Schristosthen
801.4Schristos	usage
811.4Schristosfi
821.4Schristos
831.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
841.4Schristosthen
851.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
861.4Schristos	exit 1
871.4Schristosfi
881.4Schristos
891.5Schristos. "${DIR}/conf/${h}.conf"
901.4Schristos
911.17Schristosselected_sets="$sets"
921.17Schristos
931.4SchristosOPTIND=1
941.4Schristoswhile getopts "$OPTS" f
951.4Schristosdo
961.4Schristos	case $f in
971.18Schristos	D)	release="$OPTARG";;
981.18Schristos	K)	kernel="$OPTARG";;
991.17Schristos	S)	src="$OPTARG";;
1001.4Schristos	c)	custom="$OPTARG";;
1011.4Schristos	h)	;;
1021.4Schristos	s)	size="$OPTARG";;
1031.17Schristos	x)	selected_sets="$sets $xsets";;
1041.6Schristos	*)	usage;;
1051.1Sagc	esac
1061.1Sagcdone
1071.1Sagc
1081.17Schristostrap cleanup 0 1 2 3 15
1091.8Sjmcneill
1101.20Sjmcneillshift $(( $OPTIND - 1 ))
1111.4Schristosif [ -n "$1" ]; then
1121.1Sagc	# take the next argument as being the image name
1131.1Sagc	image="$1"
1141.1Sagc	shift
1151.1Sagcfi
1161.1Sagc
1171.20Sjmcneill. "${DIR}/conf/${h}.conf"
1181.11Schristos
1191.17Schristosecho ${bar} configuring sets ${bar}
1201.17Schristos(echo '/set type=dir uname=root gname=wheel mode=0755'
1211.17Schristosfor i in $selected_sets; do
1221.17Schristos	s="${release}/etc/mtree/set.$i"
1231.17Schristos	[ -f "$s" ] && cat "$s"
1241.17Schristosdone) > "$tmp/selected_sets"
1251.1Sagc
1261.4Schristosmake_fstab
1271.17Schristoscustomize
1281.17Schristospopulate
1291.1Sagc
1301.17Schristos(cd ${mnt}; mtree -c -k all | mtree -C -k all) >> "$tmp/selected_sets"
1311.17Schristosif [ -n ${msdosid} ]; then
1321.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1331.17Schristos	makefs -t msdos -O $((${init} / 2))m -s $((${boot} / 2))m \
1341.17Schristos	    ${image} ${mnt}/boot
1351.1Sagcfi
1361.1Sagc
1371.17Schristosecho ${bar} Populating ffs filesystem ${bar}
1381.17Schristosmakefs -t ffs -rx -O $(((${init} + ${boot} + ${swap}) / 2))m \
1391.17Schristos    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1401.1Sagc
1411.17Schristosif [ -z "$size" ]; then
1421.17Schristos	size=$(getsize ${image})
1431.17Schristosfi
1441.17Schristosnewsize=$((size / 2 / 1024))
1451.1Sagc
1461.17Schristosecho ${bar} Adding label ${bar}
1471.17Schristosmake_label > ${tmp}/label
1481.17Schristosdisklabel -R -F ${image} ${tmp}/label
1491.17Schristosif [ -n ${msdosid} ]; then
1501.17Schristos	echo ${bar} Running fdisk ${bar}
1511.17Schristos	fdisk -f -u -0 -s ${msdosid}/${init}/${boot} -F ${image}
1521.2Sagcfi
1531.17Schristosecho ${bar} Image is ${image} ${bar}
154