mkimage revision 1.61
11.31Sjmcneill#!/bin/sh
21.61Sjmcneill# $NetBSD: mkimage,v 1.61 2015/08/01 10:05:51 jmcneill 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.58Shubertf#
361.58Shubertf# Makes a bootable image for the host architecture given.
371.58Shubertf# The host specific functions are pulled in from a /bin/sh script in the
381.58Shubertf# "conf" directory, and is expected to provide the following shell
391.58Shubertf# functions, which are called in the following order:
401.58Shubertf#
411.58Shubertf#  - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}.
421.58Shubertf#    If -m is given, a number of directories are put on a tmpfs RAM disk
431.58Shubertf#  - customize: After unpacking the sets, this gets the system to
441.58Shubertf#    a working state, e. g. by setting up /etc/rc.conf and /dev
451.58Shubertf#  - populate: Add common goods like kernel and bootloader
461.58Shubertf#  - make_label: Prints disklabel to stdout
471.58Shubertf#
481.58Shubertf
491.33Sjmcneillset -e
501.33Sjmcneill
511.17SchristosDIR="$(cd "$(dirname "$0")" && pwd)"
521.17SchristosPROG="$(basename "$0")"
531.17Schristos
541.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel}
551.24SchristosFDISK=${TOOL_FDISK:-fdisk}
561.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs}
571.25SjmcneillMTREE=${TOOL_MTREE:-mtree}
581.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
591.59SchristosMKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
601.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
611.21Schristos
621.17Schristossrc="/usr/src"
631.17Schristossets="base comp etc games man misc modules text"
641.17Schristosxsets="xbase xcomp xetc xfont xserver" 
651.17Schristosminfree="10%"
661.17Schristosbar="==="
671.17Schristos
681.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
691.17Schristosmnt="${tmp}/mnt"
701.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev"
711.23Schristos
721.17Schristostrap "cleanup" 0 1 2 3 15
731.17Schristos
741.17Schristoscleanup() {
751.17Schristos	case "$tmp" in
761.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
771.17Schristos	esac
781.17Schristos}
791.1Sagc
801.17Schristosgetsize() {
811.17Schristos	set -- $(ls -l $1)
821.17Schristos	echo $5
831.1Sagc}
841.1Sagc
851.12Schristosusage() {
861.12Schristos	cat << EOF 1>&2
871.60SmartinUsage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
881.39Schristos
891.39Schristos-b	Boot only, no sets loaded
901.47Schristos-r	root device kind (sd, wd, ld)
911.39Schristos-d	Add the debug sets
921.43Schristos-m	Optimize the OS installation to mimimize disk writes for SSDs
931.57Shubertf-x	Load the X sets too, not just the base ones
941.12SchristosEOF
951.13Schristos	exit 1
961.12Schristos}
971.12Schristos
981.32Sjmcneill# First pass for options to get the host and src directories
991.60SmartinOPTS="B:D:K:S:bc:dh:mr:s:x"
1001.4Schristoswhile getopts "$OPTS" f
1011.4Schristosdo
1021.4Schristos	case $f in
1031.4Schristos	h)	h="$OPTARG";;
1041.32Sjmcneill	S)	src="$OPTARG";;
1051.4Schristos	*)	;;
1061.4Schristos	esac
1071.4Schristosdone
1081.4Schristos
1091.4Schristosif [ -z "$h" ]
1101.4Schristosthen
1111.4Schristos	usage
1121.4Schristosfi
1131.4Schristos
1141.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
1151.4Schristosthen
1161.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1171.4Schristos	exit 1
1181.4Schristosfi
1191.4Schristos
1201.56Sjmcneillresize=false
1211.56Sjmcneill
1221.5Schristos. "${DIR}/conf/${h}.conf"
1231.45Schristosrelease="/usr/obj/${MACHINE}/release"
1241.4Schristos
1251.17Schristosselected_sets="$sets"
1261.50Sskrlldsets_p=false
1271.50Sskrllxsets_p=false
1281.39Schristosminwrites=false
1291.47Schristosrootdev=ld
1301.60Smartinendian=
1311.17Schristos
1321.4SchristosOPTIND=1
1331.4Schristoswhile getopts "$OPTS" f
1341.4Schristosdo
1351.4Schristos	case $f in
1361.60Smartin	B)	endian="-B $OPTARG";;
1371.18Schristos	D)	release="$OPTARG";;
1381.18Schristos	K)	kernel="$OPTARG";;
1391.32Sjmcneill	S)	;;
1401.41Schristos	b)	bootonly=true;;
1411.50Sskrll	d)	dsets_p=true
1421.39Schristos		selected_sets="$selected_sets debug"
1431.50Sskrll		if $xsets_p; then
1441.39Schristos			selected_sets="$selected_sets xdebug"
1451.39Schristos		fi
1461.39Schristos		;;
1471.4Schristos	c)	custom="$OPTARG";;
1481.4Schristos	h)	;;
1491.39Schristos	m)	minwrites=true;;
1501.47Schristos	r)	rootdev="$OPTARG";;
1511.4Schristos	s)	size="$OPTARG";;
1521.50Sskrll	x)	xsets_p=true
1531.39Schristos		selected_sets="$selected_sets $xsets"
1541.50Sskrll		if $dsets_p; then
1551.39Schristos		    selected_sets="$selected_sets xdebug"
1561.39Schristos		fi
1571.39Schristos		;;
1581.6Schristos	*)	usage;;
1591.1Sagc	esac
1601.1Sagcdone
1611.1Sagc
1621.20Sjmcneillshift $(( $OPTIND - 1 ))
1631.4Schristosif [ -n "$1" ]; then
1641.1Sagc	# take the next argument as being the image name
1651.1Sagc	image="$1"
1661.1Sagc	shift
1671.1Sagcfi
1681.1Sagc
1691.22Schristoscase "$image" in
1701.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1711.22Schristos*)	compress=false;;
1721.22Schristosesac
1731.22Schristos
1741.45Schristosif [ -z "${bootonly}" ]; then
1751.36Sgarbled	echo ${bar} configuring sets ${bar}
1761.51Sskrll	(cat "${release}/etc/mtree/NetBSD.dist"
1771.36Sgarbled	for i in $selected_sets; do
1781.36Sgarbled		s="${release}/etc/mtree/set.$i"
1791.36Sgarbled		if [ -f "$s" ]; then
1801.36Sgarbled			cat "$s"
1811.36Sgarbled		fi
1821.36Sgarbled	done) > "$tmp/selected_sets"
1831.36Sgarbledfi
1841.1Sagc
1851.4Schristosmake_fstab
1861.17Schristoscustomize
1871.17Schristospopulate
1881.1Sagc
1891.45Schristosif [ -n "${msdosid}" ]; then
1901.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1911.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1921.41Schristos	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
1931.41Schristos	    ${image} ${mnt}/boot
1941.1Sagcfi
1951.1Sagc
1961.45Schristosif [ -z "${bootonly}" ]; then
1971.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
1981.60Smartin	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
1991.46Schristos	    -O ${ffsoffset} \
2001.61Sjmcneill	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
2011.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
2021.36Sgarbledfi
2031.1Sagc
2041.23Schristosif [ "${size}" = 0 ]; then
2051.23Schristos	size="$(getsize "${image}")"
2061.17Schristosfi
2071.23Schristosnewsize=$((${size} / 2 / 1024))
2081.49Schristoscompare=$((${newsize} * 2 * 1024))
2091.49Schristoswhile [ "${compare}" != "${size}" ]
2101.49Schristosdo    
2111.49Schristos	size="$((size + size - compare))"  
2121.49Schristos	newsize="$((${size} / 2 / 1024))"
2131.49Schristos	compare="$((${newsize} * 2 * 1024))"
2141.49Schristosdone                      
2151.1Sagc
2161.17Schristosecho ${bar} Adding label ${bar}
2171.17Schristosmake_label > ${tmp}/label
2181.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
2191.45Schristosif [ -n "${msdosid}" ]; then
2201.17Schristos	echo ${bar} Running fdisk ${bar}
2211.30Sjmcneill	initsecs=$((${init} * 1024))
2221.30Sjmcneill	bootsecs=$((${boot} * 1024))
2231.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
2241.45Schristoselif [ -n "${netbsdid}" ]; then
2251.45Schristos	echo ${bar} Running fdisk ${bar}
2261.45Schristos	${FDISK} -f -i ${image}
2271.49Schristos	${FDISK} -f -a -u -0 -s 169/${init} ${image}
2281.45Schristos	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
2291.2Sagcfi
2301.22Schristos
2311.22Schristosif $compress; then
2321.22Schristos	echo ${bar} Compressing image ${bar}
2331.23Schristos	rm -f "${image}.gz"
2341.44Sast	${GZIP_CMD} -9 ${image}
2351.22Schristos	image="${image}.gz"
2361.22Schristosfi
2371.22Schristos
2381.17Schristosecho ${bar} Image is ${image} ${bar}
239