mkimage revision 1.58
11.31Sjmcneill#!/bin/sh
21.58Shubertf# $NetBSD: mkimage,v 1.58 2015/04/19 17:56:57 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.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.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
601.21Schristos
611.17Schristossrc="/usr/src"
621.17Schristossets="base comp etc games man misc modules text"
631.17Schristosxsets="xbase xcomp xetc xfont xserver" 
641.17Schristosminfree="10%"
651.17Schristosbar="==="
661.17Schristos
671.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
681.17Schristosmnt="${tmp}/mnt"
691.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev"
701.23Schristos
711.17Schristostrap "cleanup" 0 1 2 3 15
721.17Schristos
731.17Schristoscleanup() {
741.17Schristos	case "$tmp" in
751.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
761.17Schristos	esac
771.17Schristos}
781.1Sagc
791.17Schristosgetsize() {
801.17Schristos	set -- $(ls -l $1)
811.17Schristos	echo $5
821.1Sagc}
831.1Sagc
841.12Schristosusage() {
851.12Schristos	cat << EOF 1>&2
861.41SchristosUsage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
871.39Schristos
881.39Schristos-b	Boot only, no sets loaded
891.47Schristos-r	root device kind (sd, wd, ld)
901.39Schristos-d	Add the debug sets
911.43Schristos-m	Optimize the OS installation to mimimize disk writes for SSDs
921.57Shubertf-x	Load the X sets too, not just the base ones
931.12SchristosEOF
941.13Schristos	exit 1
951.12Schristos}
961.12Schristos
971.32Sjmcneill# First pass for options to get the host and src directories
981.47SchristosOPTS="K:D:S:bc:dh:mr:s:x"
991.4Schristoswhile getopts "$OPTS" f
1001.4Schristosdo
1011.4Schristos	case $f in
1021.4Schristos	h)	h="$OPTARG";;
1031.32Sjmcneill	S)	src="$OPTARG";;
1041.4Schristos	*)	;;
1051.4Schristos	esac
1061.4Schristosdone
1071.4Schristos
1081.4Schristosif [ -z "$h" ]
1091.4Schristosthen
1101.4Schristos	usage
1111.4Schristosfi
1121.4Schristos
1131.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
1141.4Schristosthen
1151.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1161.4Schristos	exit 1
1171.4Schristosfi
1181.4Schristos
1191.56Sjmcneillresize=false
1201.56Sjmcneill
1211.5Schristos. "${DIR}/conf/${h}.conf"
1221.45Schristosrelease="/usr/obj/${MACHINE}/release"
1231.4Schristos
1241.17Schristosselected_sets="$sets"
1251.50Sskrlldsets_p=false
1261.50Sskrllxsets_p=false
1271.39Schristosminwrites=false
1281.47Schristosrootdev=ld
1291.17Schristos
1301.4SchristosOPTIND=1
1311.4Schristoswhile getopts "$OPTS" f
1321.4Schristosdo
1331.4Schristos	case $f in
1341.18Schristos	D)	release="$OPTARG";;
1351.18Schristos	K)	kernel="$OPTARG";;
1361.32Sjmcneill	S)	;;
1371.41Schristos	b)	bootonly=true;;
1381.50Sskrll	d)	dsets_p=true
1391.39Schristos		selected_sets="$selected_sets debug"
1401.50Sskrll		if $xsets_p; then
1411.39Schristos			selected_sets="$selected_sets xdebug"
1421.39Schristos		fi
1431.39Schristos		;;
1441.4Schristos	c)	custom="$OPTARG";;
1451.4Schristos	h)	;;
1461.39Schristos	m)	minwrites=true;;
1471.47Schristos	r)	rootdev="$OPTARG";;
1481.4Schristos	s)	size="$OPTARG";;
1491.50Sskrll	x)	xsets_p=true
1501.39Schristos		selected_sets="$selected_sets $xsets"
1511.50Sskrll		if $dsets_p; then
1521.39Schristos		    selected_sets="$selected_sets xdebug"
1531.39Schristos		fi
1541.39Schristos		;;
1551.6Schristos	*)	usage;;
1561.1Sagc	esac
1571.1Sagcdone
1581.1Sagc
1591.20Sjmcneillshift $(( $OPTIND - 1 ))
1601.4Schristosif [ -n "$1" ]; then
1611.1Sagc	# take the next argument as being the image name
1621.1Sagc	image="$1"
1631.1Sagc	shift
1641.1Sagcfi
1651.1Sagc
1661.22Schristoscase "$image" in
1671.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1681.22Schristos*)	compress=false;;
1691.22Schristosesac
1701.22Schristos
1711.45Schristosif [ -z "${bootonly}" ]; then
1721.36Sgarbled	echo ${bar} configuring sets ${bar}
1731.51Sskrll	(cat "${release}/etc/mtree/NetBSD.dist"
1741.36Sgarbled	for i in $selected_sets; do
1751.36Sgarbled		s="${release}/etc/mtree/set.$i"
1761.36Sgarbled		if [ -f "$s" ]; then
1771.36Sgarbled			cat "$s"
1781.36Sgarbled		fi
1791.36Sgarbled	done) > "$tmp/selected_sets"
1801.36Sgarbledfi
1811.1Sagc
1821.4Schristosmake_fstab
1831.17Schristoscustomize
1841.17Schristospopulate
1851.1Sagc
1861.45Schristosif [ -n "${msdosid}" ]; then
1871.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
1881.27Schristos	${MAKEFS} -N ${release}/etc -t msdos \
1891.41Schristos	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
1901.41Schristos	    ${image} ${mnt}/boot
1911.1Sagcfi
1921.1Sagc
1931.45Schristosif [ -z "${bootonly}" ]; then
1941.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
1951.52Sskrll	${MAKEFS} -r -N ${release}/etc -t ffs -rx \
1961.46Schristos	    -O ${ffsoffset} \
1971.54Sjmcneill	    -o d=4096,f=2048,b=16384 -b $((${extra}))m \
1981.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
1991.36Sgarbledfi
2001.1Sagc
2011.23Schristosif [ "${size}" = 0 ]; then
2021.23Schristos	size="$(getsize "${image}")"
2031.17Schristosfi
2041.23Schristosnewsize=$((${size} / 2 / 1024))
2051.49Schristoscompare=$((${newsize} * 2 * 1024))
2061.49Schristoswhile [ "${compare}" != "${size}" ]
2071.49Schristosdo    
2081.49Schristos	size="$((size + size - compare))"  
2091.49Schristos	newsize="$((${size} / 2 / 1024))"
2101.49Schristos	compare="$((${newsize} * 2 * 1024))"
2111.49Schristosdone                      
2121.1Sagc
2131.17Schristosecho ${bar} Adding label ${bar}
2141.17Schristosmake_label > ${tmp}/label
2151.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label
2161.45Schristosif [ -n "${msdosid}" ]; then
2171.17Schristos	echo ${bar} Running fdisk ${bar}
2181.30Sjmcneill	initsecs=$((${init} * 1024))
2191.30Sjmcneill	bootsecs=$((${boot} * 1024))
2201.30Sjmcneill	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
2211.45Schristoselif [ -n "${netbsdid}" ]; then
2221.45Schristos	echo ${bar} Running fdisk ${bar}
2231.45Schristos	${FDISK} -f -i ${image}
2241.49Schristos	${FDISK} -f -a -u -0 -s 169/${init} ${image}
2251.45Schristos	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
2261.2Sagcfi
2271.22Schristos
2281.22Schristosif $compress; then
2291.22Schristos	echo ${bar} Compressing image ${bar}
2301.23Schristos	rm -f "${image}.gz"
2311.44Sast	${GZIP_CMD} -9 ${image}
2321.22Schristos	image="${image}.gz"
2331.22Schristosfi
2341.22Schristos
2351.17Schristosecho ${bar} Image is ${image} ${bar}
236