mkimage revision 1.57
11.31Sjmcneill#!/bin/sh
21.57Shubertf# $NetBSD: mkimage,v 1.57 2015/04/18 22:06:48 hubertf Exp $
31.13Schristos#
41.45Schristos# Copyright (c) 2013, 2014 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.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
451.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
461.21Schristos
471.17Schristossrc="/usr/src"
481.17Schristossets="base comp etc games man misc modules text"
491.17Schristosxsets="xbase xcomp xetc xfont xserver" 
501.17Schristosminfree="10%"
511.17Schristosbar="==="
521.17Schristos
531.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
541.17Schristosmnt="${tmp}/mnt"
551.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev"
561.23Schristos
571.17Schristostrap "cleanup" 0 1 2 3 15
581.17Schristos
591.17Schristoscleanup() {
601.17Schristos	case "$tmp" in
611.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
621.17Schristos	esac
631.17Schristos}
641.1Sagc
651.17Schristosgetsize() {
661.17Schristos	set -- $(ls -l $1)
671.17Schristos	echo $5
681.1Sagc}
691.1Sagc
701.12Schristosusage() {
711.12Schristos	cat << EOF 1>&2
721.41SchristosUsage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
731.39Schristos
741.39Schristos-b	Boot only, no sets loaded
751.47Schristos-r	root device kind (sd, wd, ld)
761.39Schristos-d	Add the debug sets
771.43Schristos-m	Optimize the OS installation to mimimize disk writes for SSDs
781.57Shubertf-x	Load the X sets too, not just the base ones
791.12SchristosEOF
801.13Schristos	exit 1
811.12Schristos}
821.12Schristos
831.32Sjmcneill# First pass for options to get the host and src directories
841.47SchristosOPTS="K:D:S:bc:dh:mr:s:x"
851.4Schristoswhile getopts "$OPTS" f
861.4Schristosdo
871.4Schristos	case $f in
881.4Schristos	h)	h="$OPTARG";;
891.32Sjmcneill	S)	src="$OPTARG";;
901.4Schristos	*)	;;
911.4Schristos	esac
921.4Schristosdone
931.4Schristos
941.4Schristosif [ -z "$h" ]
951.4Schristosthen
961.4Schristos	usage
971.4Schristosfi
981.4Schristos
991.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
1001.4Schristosthen
1011.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1021.4Schristos	exit 1
1031.4Schristosfi
1041.4Schristos
1051.56Sjmcneillresize=false
1061.56Sjmcneill
1071.5Schristos. "${DIR}/conf/${h}.conf"
1081.45Schristosrelease="/usr/obj/${MACHINE}/release"
1091.4Schristos
1101.17Schristosselected_sets="$sets"
1111.50Sskrlldsets_p=false
1121.50Sskrllxsets_p=false
1131.39Schristosminwrites=false
1141.47Schristosrootdev=ld
1151.17Schristos
1161.4SchristosOPTIND=1
1171.4Schristoswhile getopts "$OPTS" f
1181.4Schristosdo
1191.4Schristos	case $f in
1201.18Schristos	D)	release="$OPTARG";;
1211.18Schristos	K)	kernel="$OPTARG";;
1221.32Sjmcneill	S)	;;
1231.41Schristos	b)	bootonly=true;;
1241.50Sskrll	d)	dsets_p=true
1251.39Schristos		selected_sets="$selected_sets debug"
1261.50Sskrll		if $xsets_p; then
1271.39Schristos			selected_sets="$selected_sets xdebug"
1281.39Schristos		fi
1291.39Schristos		;;
1301.4Schristos	c)	custom="$OPTARG";;
1311.4Schristos	h)	;;
1321.39Schristos	m)	minwrites=true;;
1331.47Schristos	r)	rootdev="$OPTARG";;
1341.4Schristos	s)	size="$OPTARG";;
1351.50Sskrll	x)	xsets_p=true
1361.39Schristos		selected_sets="$selected_sets $xsets"
1371.50Sskrll		if $dsets_p; then
1381.39Schristos		    selected_sets="$selected_sets xdebug"
1391.39Schristos		fi
1401.39Schristos		;;
1411.6Schristos	*)	usage;;
1421.1Sagc	esac
1431.1Sagcdone
1441.1Sagc
1451.20Sjmcneillshift $(( $OPTIND - 1 ))
1461.4Schristosif [ -n "$1" ]; then
1471.1Sagc	# take the next argument as being the image name
1481.1Sagc	image="$1"
1491.1Sagc	shift
1501.1Sagcfi
1511.1Sagc
1521.22Schristoscase "$image" in
1531.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1541.22Schristos*)	compress=false;;
1551.22Schristosesac
1561.22Schristos
1571.45Schristosif [ -z "${bootonly}" ]; then
1581.36Sgarbled	echo ${bar} configuring sets ${bar}
1591.51Sskrll	(cat "${release}/etc/mtree/NetBSD.dist"
1601.36Sgarbled	for i in $selected_sets; do
1611.36Sgarbled		s="${release}/etc/mtree/set.$i"
1621.36Sgarbled		if [ -f "$s" ]; then
1631.36Sgarbled			cat "$s"
1641.36Sgarbled		fi
1651.36Sgarbled	done) > "$tmp/selected_sets"
1661.36Sgarbledfi
1671.1Sagc
1681.4Schristosmake_fstab
1691.17Schristoscustomize
1701.17Schristospopulate
1711.1Sagc
1721.45Schristosif [ -n "${msdosid}" ]; then
1731.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1741.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1751.41Schristos	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
1761.41Schristos	    ${image} ${mnt}/boot
1771.1Sagcfi
1781.1Sagc
1791.45Schristosif [ -z "${bootonly}" ]; then
1801.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
1811.52Sskrll	${MAKEFS} -r -N ${release}/etc -t ffs -rx \
1821.46Schristos	    -O ${ffsoffset} \
1831.54Sjmcneill	    -o d=4096,f=2048,b=16384 -b $((${extra}))m \
1841.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1851.36Sgarbledfi
1861.1Sagc
1871.23Schristosif [ "${size}" = 0 ]; then
1881.23Schristos	size="$(getsize "${image}")"
1891.17Schristosfi
1901.23Schristosnewsize=$((${size} / 2 / 1024))
1911.49Schristoscompare=$((${newsize} * 2 * 1024))
1921.49Schristoswhile [ "${compare}" != "${size}" ]
1931.49Schristosdo    
1941.49Schristos	size="$((size + size - compare))"  
1951.49Schristos	newsize="$((${size} / 2 / 1024))"
1961.49Schristos	compare="$((${newsize} * 2 * 1024))"
1971.49Schristosdone                      
1981.1Sagc
1991.17Schristosecho ${bar} Adding label ${bar}
2001.17Schristosmake_label > ${tmp}/label
2011.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
2021.45Schristosif [ -n "${msdosid}" ]; then
2031.17Schristos	echo ${bar} Running fdisk ${bar}
2041.30Sjmcneill	initsecs=$((${init} * 1024))
2051.30Sjmcneill	bootsecs=$((${boot} * 1024))
2061.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
2071.45Schristoselif [ -n "${netbsdid}" ]; then
2081.45Schristos	echo ${bar} Running fdisk ${bar}
2091.45Schristos	${FDISK} -f -i ${image}
2101.49Schristos	${FDISK} -f -a -u -0 -s 169/${init} ${image}
2111.45Schristos	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
2121.2Sagcfi
2131.22Schristos
2141.22Schristosif $compress; then
2151.22Schristos	echo ${bar} Compressing image ${bar}
2161.23Schristos	rm -f "${image}.gz"
2171.44Sast	${GZIP_CMD} -9 ${image}
2181.22Schristos	image="${image}.gz"
2191.22Schristosfi
2201.22Schristos
2211.17Schristosecho ${bar} Image is ${image} ${bar}
222