mkimage revision 1.66
11.31Sjmcneill#!/bin/sh
21.66Sjmcneill# $NetBSD: mkimage,v 1.66 2017/07/06 00:17:04 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.66SjmcneillMAKE=${TOOL_MAKE:-make}
551.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel}
561.24SchristosFDISK=${TOOL_FDISK:-fdisk}
571.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs}
581.25SjmcneillMTREE=${TOOL_MTREE:-mtree}
591.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
601.59SchristosMKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
611.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
621.21Schristos
631.17Schristossrc="/usr/src"
641.65Schristossets="base comp etc games man misc modules tests text"
651.17Schristosxsets="xbase xcomp xetc xfont xserver" 
661.17Schristosminfree="10%"
671.17Schristosbar="==="
681.17Schristos
691.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
701.17Schristosmnt="${tmp}/mnt"
711.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev"
721.23Schristos
731.17Schristostrap "cleanup" 0 1 2 3 15
741.17Schristos
751.17Schristoscleanup() {
761.17Schristos	case "$tmp" in
771.17Schristos	/tmp/$PROG.*)	rm -fr "$tmp";;
781.17Schristos	esac
791.17Schristos}
801.1Sagc
811.17Schristosgetsize() {
821.17Schristos	set -- $(ls -l $1)
831.17Schristos	echo $5
841.1Sagc}
851.1Sagc
861.63Sjmcneillgetsectors() {
871.63Sjmcneill	case "$1" in
881.63Sjmcneill	*g)
891.63Sjmcneill		m=1073741824
901.63Sjmcneill		v=${1%g}
911.63Sjmcneill		;;
921.63Sjmcneill	*m)
931.63Sjmcneill		m=1048576
941.63Sjmcneill		v=${1%m}
951.63Sjmcneill		;;
961.63Sjmcneill	*k)
971.63Sjmcneill		m=1024
981.63Sjmcneill		v=${1%k}
991.63Sjmcneill		;;
1001.63Sjmcneill	*[0-9b])
1011.63Sjmcneill		m=1
1021.63Sjmcneill		v=${1%b}
1031.63Sjmcneill		;;
1041.63Sjmcneill	esac
1051.63Sjmcneill	echo $((m * v / 512))
1061.63Sjmcneill}
1071.63Sjmcneill
1081.12Schristosusage() {
1091.12Schristos	cat << EOF 1>&2
1101.60SmartinUsage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
1111.39Schristos
1121.39Schristos-b	Boot only, no sets loaded
1131.47Schristos-r	root device kind (sd, wd, ld)
1141.39Schristos-d	Add the debug sets
1151.43Schristos-m	Optimize the OS installation to mimimize disk writes for SSDs
1161.57Shubertf-x	Load the X sets too, not just the base ones
1171.12SchristosEOF
1181.13Schristos	exit 1
1191.12Schristos}
1201.12Schristos
1211.32Sjmcneill# First pass for options to get the host and src directories
1221.60SmartinOPTS="B:D:K:S:bc:dh:mr:s:x"
1231.4Schristoswhile getopts "$OPTS" f
1241.4Schristosdo
1251.4Schristos	case $f in
1261.4Schristos	h)	h="$OPTARG";;
1271.32Sjmcneill	S)	src="$OPTARG";;
1281.4Schristos	*)	;;
1291.4Schristos	esac
1301.4Schristosdone
1311.4Schristos
1321.4Schristosif [ -z "$h" ]
1331.4Schristosthen
1341.4Schristos	usage
1351.4Schristosfi
1361.4Schristos
1371.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
1381.4Schristosthen
1391.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1401.4Schristos	exit 1
1411.4Schristosfi
1421.4Schristos
1431.56Sjmcneillresize=false
1441.56Sjmcneill
1451.5Schristos. "${DIR}/conf/${h}.conf"
1461.45Schristosrelease="/usr/obj/${MACHINE}/release"
1471.4Schristos
1481.17Schristosselected_sets="$sets"
1491.50Sskrlldsets_p=false
1501.50Sskrllxsets_p=false
1511.39Schristosminwrites=false
1521.47Schristosrootdev=ld
1531.60Smartinendian=
1541.17Schristos
1551.4SchristosOPTIND=1
1561.4Schristoswhile getopts "$OPTS" f
1571.4Schristosdo
1581.4Schristos	case $f in
1591.60Smartin	B)	endian="-B $OPTARG";;
1601.18Schristos	D)	release="$OPTARG";;
1611.18Schristos	K)	kernel="$OPTARG";;
1621.32Sjmcneill	S)	;;
1631.41Schristos	b)	bootonly=true;;
1641.50Sskrll	d)	dsets_p=true
1651.39Schristos		selected_sets="$selected_sets debug"
1661.50Sskrll		if $xsets_p; then
1671.39Schristos			selected_sets="$selected_sets xdebug"
1681.39Schristos		fi
1691.39Schristos		;;
1701.4Schristos	c)	custom="$OPTARG";;
1711.4Schristos	h)	;;
1721.39Schristos	m)	minwrites=true;;
1731.47Schristos	r)	rootdev="$OPTARG";;
1741.4Schristos	s)	size="$OPTARG";;
1751.50Sskrll	x)	xsets_p=true
1761.39Schristos		selected_sets="$selected_sets $xsets"
1771.50Sskrll		if $dsets_p; then
1781.39Schristos		    selected_sets="$selected_sets xdebug"
1791.39Schristos		fi
1801.39Schristos		;;
1811.6Schristos	*)	usage;;
1821.1Sagc	esac
1831.1Sagcdone
1841.1Sagc
1851.20Sjmcneillshift $(( $OPTIND - 1 ))
1861.4Schristosif [ -n "$1" ]; then
1871.1Sagc	# take the next argument as being the image name
1881.1Sagc	image="$1"
1891.1Sagc	shift
1901.1Sagcfi
1911.1Sagc
1921.22Schristoscase "$image" in
1931.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1941.22Schristos*)	compress=false;;
1951.22Schristosesac
1961.22Schristos
1971.45Schristosif [ -z "${bootonly}" ]; then
1981.36Sgarbled	echo ${bar} configuring sets ${bar}
1991.51Sskrll	(cat "${release}/etc/mtree/NetBSD.dist"
2001.36Sgarbled	for i in $selected_sets; do
2011.36Sgarbled		s="${release}/etc/mtree/set.$i"
2021.36Sgarbled		if [ -f "$s" ]; then
2031.36Sgarbled			cat "$s"
2041.36Sgarbled		fi
2051.36Sgarbled	done) > "$tmp/selected_sets"
2061.36Sgarbledfi
2071.1Sagc
2081.4Schristosmake_fstab
2091.17Schristoscustomize
2101.17Schristospopulate
2111.1Sagc
2121.45Schristosif [ -n "${msdosid}" ]; then
2131.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
2141.63Sjmcneill	${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
2151.63Sjmcneill	    -O $((${init} / 2))m -s $((${boot} / 2))m \
2161.41Schristos	    ${image} ${mnt}/boot
2171.1Sagcfi
2181.1Sagc
2191.45Schristosif [ -z "${bootonly}" ]; then
2201.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
2211.60Smartin	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
2221.46Schristos	    -O ${ffsoffset} \
2231.61Sjmcneill	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
2241.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
2251.36Sgarbledfi
2261.1Sagc
2271.23Schristosif [ "${size}" = 0 ]; then
2281.23Schristos	size="$(getsize "${image}")"
2291.17Schristosfi
2301.23Schristosnewsize=$((${size} / 2 / 1024))
2311.49Schristoscompare=$((${newsize} * 2 * 1024))
2321.49Schristoswhile [ "${compare}" != "${size}" ]
2331.49Schristosdo    
2341.49Schristos	size="$((size + size - compare))"  
2351.49Schristos	newsize="$((${size} / 2 / 1024))"
2361.49Schristos	compare="$((${newsize} * 2 * 1024))"
2371.49Schristosdone                      
2381.1Sagc
2391.45Schristosif [ -n "${msdosid}" ]; then
2401.17Schristos	echo ${bar} Running fdisk ${bar}
2411.30Sjmcneill	initsecs=$((${init} * 1024))
2421.30Sjmcneill	bootsecs=$((${boot} * 1024))
2431.63Sjmcneill	${FDISK} -f -i ${image}
2441.62Sskrll	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
2451.63Sjmcneill	if [ -z "${bootonly}" ]; then
2461.63Sjmcneill		ffsstart="$(getsectors ${ffsoffset})"
2471.63Sjmcneill		imagesize="$(getsize "${image}")"
2481.63Sjmcneill		imagesecs="$(getsectors ${imagesize})"
2491.63Sjmcneill		ffssize="$(expr ${imagesecs} - ${ffsstart})"
2501.63Sjmcneill		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
2511.63Sjmcneill	fi
2521.64Sjmcneill
2531.64Sjmcneill	echo ${bar} Adding label ${bar}
2541.64Sjmcneill	make_label > ${tmp}/label
2551.64Sjmcneill	${DISKLABEL} -R -F ${image} ${tmp}/label
2561.45Schristoselif [ -n "${netbsdid}" ]; then
2571.64Sjmcneill	echo ${bar} Adding label ${bar}
2581.64Sjmcneill	make_label > ${tmp}/label
2591.64Sjmcneill	${DISKLABEL} -R -F ${image} ${tmp}/label
2601.64Sjmcneill
2611.45Schristos	echo ${bar} Running fdisk ${bar}
2621.45Schristos	${FDISK} -f -i ${image}
2631.49Schristos	${FDISK} -f -a -u -0 -s 169/${init} ${image}
2641.45Schristos	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
2651.2Sagcfi
2661.22Schristos
2671.22Schristosif $compress; then
2681.22Schristos	echo ${bar} Compressing image ${bar}
2691.23Schristos	rm -f "${image}.gz"
2701.44Sast	${GZIP_CMD} -9 ${image}
2711.22Schristos	image="${image}.gz"
2721.22Schristosfi
2731.22Schristos
2741.17Schristosecho ${bar} Image is ${image} ${bar}
275