mkimage revision 1.64
11.31Sjmcneill#!/bin/sh
21.64Sjmcneill# $NetBSD: mkimage,v 1.64 2017/04/11 21:06:30 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.63Sjmcneillgetsectors() {
861.63Sjmcneill	case "$1" in
871.63Sjmcneill	*g)
881.63Sjmcneill		m=1073741824
891.63Sjmcneill		v=${1%g}
901.63Sjmcneill		;;
911.63Sjmcneill	*m)
921.63Sjmcneill		m=1048576
931.63Sjmcneill		v=${1%m}
941.63Sjmcneill		;;
951.63Sjmcneill	*k)
961.63Sjmcneill		m=1024
971.63Sjmcneill		v=${1%k}
981.63Sjmcneill		;;
991.63Sjmcneill	*[0-9b])
1001.63Sjmcneill		m=1
1011.63Sjmcneill		v=${1%b}
1021.63Sjmcneill		;;
1031.63Sjmcneill	esac
1041.63Sjmcneill	echo $((m * v / 512))
1051.63Sjmcneill}
1061.63Sjmcneill
1071.12Schristosusage() {
1081.12Schristos	cat << EOF 1>&2
1091.60SmartinUsage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
1101.39Schristos
1111.39Schristos-b	Boot only, no sets loaded
1121.47Schristos-r	root device kind (sd, wd, ld)
1131.39Schristos-d	Add the debug sets
1141.43Schristos-m	Optimize the OS installation to mimimize disk writes for SSDs
1151.57Shubertf-x	Load the X sets too, not just the base ones
1161.12SchristosEOF
1171.13Schristos	exit 1
1181.12Schristos}
1191.12Schristos
1201.32Sjmcneill# First pass for options to get the host and src directories
1211.60SmartinOPTS="B:D:K:S:bc:dh:mr:s:x"
1221.4Schristoswhile getopts "$OPTS" f
1231.4Schristosdo
1241.4Schristos	case $f in
1251.4Schristos	h)	h="$OPTARG";;
1261.32Sjmcneill	S)	src="$OPTARG";;
1271.4Schristos	*)	;;
1281.4Schristos	esac
1291.4Schristosdone
1301.4Schristos
1311.4Schristosif [ -z "$h" ]
1321.4Schristosthen
1331.4Schristos	usage
1341.4Schristosfi
1351.4Schristos
1361.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ]
1371.4Schristosthen
1381.5Schristos	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
1391.4Schristos	exit 1
1401.4Schristosfi
1411.4Schristos
1421.56Sjmcneillresize=false
1431.56Sjmcneill
1441.5Schristos. "${DIR}/conf/${h}.conf"
1451.45Schristosrelease="/usr/obj/${MACHINE}/release"
1461.4Schristos
1471.17Schristosselected_sets="$sets"
1481.50Sskrlldsets_p=false
1491.50Sskrllxsets_p=false
1501.39Schristosminwrites=false
1511.47Schristosrootdev=ld
1521.60Smartinendian=
1531.17Schristos
1541.4SchristosOPTIND=1
1551.4Schristoswhile getopts "$OPTS" f
1561.4Schristosdo
1571.4Schristos	case $f in
1581.60Smartin	B)	endian="-B $OPTARG";;
1591.18Schristos	D)	release="$OPTARG";;
1601.18Schristos	K)	kernel="$OPTARG";;
1611.32Sjmcneill	S)	;;
1621.41Schristos	b)	bootonly=true;;
1631.50Sskrll	d)	dsets_p=true
1641.39Schristos		selected_sets="$selected_sets debug"
1651.50Sskrll		if $xsets_p; then
1661.39Schristos			selected_sets="$selected_sets xdebug"
1671.39Schristos		fi
1681.39Schristos		;;
1691.4Schristos	c)	custom="$OPTARG";;
1701.4Schristos	h)	;;
1711.39Schristos	m)	minwrites=true;;
1721.47Schristos	r)	rootdev="$OPTARG";;
1731.4Schristos	s)	size="$OPTARG";;
1741.50Sskrll	x)	xsets_p=true
1751.39Schristos		selected_sets="$selected_sets $xsets"
1761.50Sskrll		if $dsets_p; then
1771.39Schristos		    selected_sets="$selected_sets xdebug"
1781.39Schristos		fi
1791.39Schristos		;;
1801.6Schristos	*)	usage;;
1811.1Sagc	esac
1821.1Sagcdone
1831.1Sagc
1841.20Sjmcneillshift $(( $OPTIND - 1 ))
1851.4Schristosif [ -n "$1" ]; then
1861.1Sagc	# take the next argument as being the image name
1871.1Sagc	image="$1"
1881.1Sagc	shift
1891.1Sagcfi
1901.1Sagc
1911.22Schristoscase "$image" in
1921.22Schristos*.gz)	compress=true; image="${image%.gz}";;
1931.22Schristos*)	compress=false;;
1941.22Schristosesac
1951.22Schristos
1961.45Schristosif [ -z "${bootonly}" ]; then
1971.36Sgarbled	echo ${bar} configuring sets ${bar}
1981.51Sskrll	(cat "${release}/etc/mtree/NetBSD.dist"
1991.36Sgarbled	for i in $selected_sets; do
2001.36Sgarbled		s="${release}/etc/mtree/set.$i"
2011.36Sgarbled		if [ -f "$s" ]; then
2021.36Sgarbled			cat "$s"
2031.36Sgarbled		fi
2041.36Sgarbled	done) > "$tmp/selected_sets"
2051.36Sgarbledfi
2061.1Sagc
2071.4Schristosmake_fstab
2081.17Schristoscustomize
2091.17Schristospopulate
2101.1Sagc
2111.45Schristosif [ -n "${msdosid}" ]; then
2121.17Schristos	echo ${bar} Populating msdos filesystem ${bar}
2131.63Sjmcneill	${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
2141.63Sjmcneill	    -O $((${init} / 2))m -s $((${boot} / 2))m \
2151.41Schristos	    ${image} ${mnt}/boot
2161.1Sagcfi
2171.1Sagc
2181.45Schristosif [ -z "${bootonly}" ]; then
2191.36Sgarbled	echo ${bar} Populating ffs filesystem ${bar}
2201.60Smartin	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
2211.46Schristos	    -O ${ffsoffset} \
2221.61Sjmcneill	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
2231.36Sgarbled	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
2241.36Sgarbledfi
2251.1Sagc
2261.23Schristosif [ "${size}" = 0 ]; then
2271.23Schristos	size="$(getsize "${image}")"
2281.17Schristosfi
2291.23Schristosnewsize=$((${size} / 2 / 1024))
2301.49Schristoscompare=$((${newsize} * 2 * 1024))
2311.49Schristoswhile [ "${compare}" != "${size}" ]
2321.49Schristosdo    
2331.49Schristos	size="$((size + size - compare))"  
2341.49Schristos	newsize="$((${size} / 2 / 1024))"
2351.49Schristos	compare="$((${newsize} * 2 * 1024))"
2361.49Schristosdone                      
2371.1Sagc
2381.45Schristosif [ -n "${msdosid}" ]; then
2391.17Schristos	echo ${bar} Running fdisk ${bar}
2401.30Sjmcneill	initsecs=$((${init} * 1024))
2411.30Sjmcneill	bootsecs=$((${boot} * 1024))
2421.63Sjmcneill	${FDISK} -f -i ${image}
2431.62Sskrll	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
2441.63Sjmcneill	if [ -z "${bootonly}" ]; then
2451.63Sjmcneill		ffsstart="$(getsectors ${ffsoffset})"
2461.63Sjmcneill		imagesize="$(getsize "${image}")"
2471.63Sjmcneill		imagesecs="$(getsectors ${imagesize})"
2481.63Sjmcneill		ffssize="$(expr ${imagesecs} - ${ffsstart})"
2491.63Sjmcneill		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
2501.63Sjmcneill	fi
2511.64Sjmcneill
2521.64Sjmcneill	echo ${bar} Adding label ${bar}
2531.64Sjmcneill	make_label > ${tmp}/label
2541.64Sjmcneill	${DISKLABEL} -R -F ${image} ${tmp}/label
2551.45Schristoselif [ -n "${netbsdid}" ]; then
2561.64Sjmcneill	echo ${bar} Adding label ${bar}
2571.64Sjmcneill	make_label > ${tmp}/label
2581.64Sjmcneill	${DISKLABEL} -R -F ${image} ${tmp}/label
2591.64Sjmcneill
2601.45Schristos	echo ${bar} Running fdisk ${bar}
2611.45Schristos	${FDISK} -f -i ${image}
2621.49Schristos	${FDISK} -f -a -u -0 -s 169/${init} ${image}
2631.45Schristos	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
2641.2Sagcfi
2651.22Schristos
2661.22Schristosif $compress; then
2671.22Schristos	echo ${bar} Compressing image ${bar}
2681.23Schristos	rm -f "${image}.gz"
2691.44Sast	${GZIP_CMD} -9 ${image}
2701.22Schristos	image="${image}.gz"
2711.22Schristosfi
2721.22Schristos
2731.17Schristosecho ${bar} Image is ${image} ${bar}
274