mkimage revision 1.39
11.31Sjmcneill#!/bin/sh
21.39Schristos# $NetBSD: mkimage,v 1.39 2013/05/20 19:27:17 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.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.39SchristosUsage: $PROG -h <host-arch> [-bmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
721.39Schristos
731.39Schristos-b	Boot only, no sets loaded
741.39Schristos-d	Add the debug sets
751.39Schristos-m	Mimimize disk writes for sd cards
761.39Schristos-x	Load the x sets too, not just the base ones
771.12SchristosEOF
781.13Schristos	exit 1
791.12Schristos}
801.12Schristos
811.32Sjmcneill# First pass for options to get the host and src directories
821.39SchristosOPTS="K:D:S:bc:dh:ms:x"
831.4Schristoswhile getopts "$OPTS" f
841.4Schristosdo
851.4Schristos	case $f in
861.4Schristos	h)	h="$OPTARG";;
871.32Sjmcneill	S)	src="$OPTARG";;
881.4Schristos	*)	;;
891.4Schristos	esac
901.4Schristosdone
911.4Schristos
921.4Schristosif [ -z "$h" ]
931.4Schristosthen
941.4Schristos	usage
951.4Schristosfi
961.4Schristos
971.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
981.4Schristosthen
991.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1001.4Schristos	exit 1
1011.4Schristosfi
1021.4Schristos
1031.5Schristos. "${DIR}/conf/${h}.conf"
1041.4Schristos
1051.17Schristosselected_sets="$sets"
1061.39Schristosdsets=false
1071.39Schristosxsets=false
1081.39Schristosminwrites=false
1091.17Schristos
1101.4SchristosOPTIND=1
1111.4Schristoswhile getopts "$OPTS" f
1121.4Schristosdo
1131.4Schristos	case $f in
1141.18Schristos	D)	release="$OPTARG";;
1151.18Schristos	K)	kernel="$OPTARG";;
1161.32Sjmcneill	S)	;;
1171.36Sgarbled	b)	bootonly="true";;
1181.39Schristos	d)	dsets=true
1191.39Schristos		selected_sets="$selected_sets debug"
1201.39Schristos		if $xsets; then
1211.39Schristos			selected_sets="$selected_sets xdebug"
1221.39Schristos		fi
1231.39Schristos		;;
1241.4Schristos	c)	custom="$OPTARG";;
1251.4Schristos	h)	;;
1261.39Schristos	m)	minwrites=true;;
1271.4Schristos	s)	size="$OPTARG";;
1281.39Schristos	x)	xsets=true
1291.39Schristos		selected_sets="$selected_sets $xsets"
1301.39Schristos		if $dsets; then
1311.39Schristos		    selected_sets="$selected_sets xdebug"
1321.39Schristos		fi
1331.39Schristos		;;
1341.6Schristos	*)	usage;;
1351.1Sagc	esac
1361.1Sagcdone
1371.1Sagc
1381.20Sjmcneillshift $(( $OPTIND - 1 ))
1391.4Schristosif [ -n "$1" ]; then
1401.1Sagc	# take the next argument as being the image name
1411.1Sagc	image="$1"
1421.1Sagc	shift
1431.1Sagcfi
1441.1Sagc
1451.22Schristoscase "$image" in
1461.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1471.22Schristos*)	compress=false;;
1481.22Schristosesac
1491.22Schristos
1501.36Sgarbledif [ -z "$bootonly" ]; then
1511.36Sgarbled	echo ${bar} configuring sets ${bar}
1521.36Sgarbled	(echo '/set type=dir uname=root gname=wheel mode=0755'
1531.36Sgarbled	for i in $selected_sets; do
1541.36Sgarbled		s="${release}/etc/mtree/set.$i"
1551.36Sgarbled		if [ -f "$s" ]; then
1561.36Sgarbled			cat "$s"
1571.36Sgarbled		fi
1581.36Sgarbled	done) > "$tmp/selected_sets"
1591.36Sgarbledfi
1601.1Sagc
1611.4Schristosmake_fstab
1621.17Schristoscustomize
1631.17Schristospopulate
1641.1Sagc
1651.36Sgarbledif [ -z "$bootonly" ]; then
1661.36Sgarbled	(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
1671.36Sgarbled	    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
1681.36Sgarbledfi
1691.17Schristosif [ -n ${msdosid} ]; then
1701.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1711.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1721.36Sgarbled	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m ${image} ${mnt}/boot
1731.1Sagcfi
1741.1Sagc
1751.36Sgarbledif [ -z "$bootonly" ]; then
1761.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
1771.36Sgarbled	${MAKEFS} -N ${release}/etc -t ffs -rx \
1781.36Sgarbled	    -O $(((${init} + ${boot} + ${swap}) / 2))m \
1791.38Shtodd		-o d=4096 \
1801.38Shtodd	    -b $((${extra}))m \
1811.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1821.36Sgarbledfi
1831.1Sagc
1841.23Schristosif [ "${size}" = 0 ]; then
1851.23Schristos	size="$(getsize "${image}")"
1861.17Schristosfi
1871.23Schristosnewsize=$((${size} / 2 / 1024))
1881.1Sagc
1891.17Schristosecho ${bar} Adding label ${bar}
1901.17Schristosmake_label > ${tmp}/label
1911.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
1921.17Schristosif [ -n ${msdosid} ]; then
1931.17Schristos	echo ${bar} Running fdisk ${bar}
1941.30Sjmcneill	initsecs=$((${init} * 1024))
1951.30Sjmcneill	bootsecs=$((${boot} * 1024))
1961.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
1971.2Sagcfi
1981.22Schristos
1991.22Schristosif $compress; then
2001.22Schristos	echo ${bar} Compressing image ${bar}
2011.23Schristos	rm -f "${image}.gz"
2021.22Schristos	gzip -9 ${image}
2031.22Schristos	image="${image}.gz"
2041.22Schristosfi
2051.22Schristos
2061.17Schristosecho ${bar} Image is ${image} ${bar}
207