Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.49
      1  1.31  jmcneill #!/bin/sh
      2  1.49  christos # $NetBSD: mkimage,v 1.49 2014/12/14 00:28:46 christos Exp $
      3  1.13  christos #
      4  1.45  christos # Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
      5   1.1       agc # All rights reserved.
      6   1.1       agc #
      7  1.17  christos # This code is derived from software contributed to The NetBSD Foundation
      8  1.17  christos # by Christos Zoulas.
      9  1.17  christos #
     10   1.1       agc # Redistribution and use in source and binary forms, with or without
     11   1.1       agc # modification, are permitted provided that the following conditions
     12   1.1       agc # are met:
     13   1.1       agc # 1. Redistributions of source code must retain the above copyright
     14   1.1       agc #    notice, this list of conditions and the following disclaimer.
     15   1.1       agc # 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       agc #    notice, this list of conditions and the following disclaimer in the
     17   1.1       agc #    documentation and/or other materials provided with the distribution.
     18  1.17  christos # 3. Neither the name of The NetBSD Foundation nor the names of its
     19  1.17  christos #    contributors may be used to endorse or promote products derived
     20  1.17  christos #    from this software without specific prior written permission.
     21  1.17  christos #
     22  1.17  christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.17  christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.17  christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.17  christos # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.17  christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.17  christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.17  christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.17  christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.17  christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.17  christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.17  christos # POSSIBILITY OF SUCH DAMAGE.
     33   1.1       agc #
     34   1.1       agc 
     35  1.33  jmcneill set -e
     36  1.33  jmcneill 
     37  1.17  christos DIR="$(cd "$(dirname "$0")" && pwd)"
     38  1.17  christos PROG="$(basename "$0")"
     39  1.17  christos 
     40  1.21  christos DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     41  1.24  christos FDISK=${TOOL_FDISK:-fdisk}
     42  1.21  christos MAKEFS=${TOOL_MAKEFS:-makefs}
     43  1.25  jmcneill MTREE=${TOOL_MTREE:-mtree}
     44  1.45  christos INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
     45  1.44       ast GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
     46  1.21  christos 
     47  1.17  christos src="/usr/src"
     48  1.17  christos sets="base comp etc games man misc modules text"
     49  1.17  christos xsets="xbase xcomp xetc xfont xserver" 
     50  1.17  christos minfree="10%"
     51  1.17  christos bar="==="
     52  1.17  christos 
     53  1.17  christos tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     54  1.17  christos mnt="${tmp}/mnt"
     55  1.46  christos mkdir -p "${mnt}/etc" "${mnt}/dev"
     56  1.23  christos 
     57  1.17  christos trap "cleanup" 0 1 2 3 15
     58  1.17  christos 
     59  1.17  christos cleanup() {
     60  1.17  christos 	case "$tmp" in
     61  1.17  christos 	/tmp/$PROG.*)	rm -fr "$tmp";;
     62  1.17  christos 	esac
     63  1.17  christos }
     64   1.1       agc 
     65  1.17  christos getsize() {
     66  1.17  christos 	set -- $(ls -l $1)
     67  1.17  christos 	echo $5
     68   1.1       agc }
     69   1.1       agc 
     70  1.12  christos usage() {
     71  1.12  christos 	cat << EOF 1>&2
     72  1.41  christos Usage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
     73  1.39  christos 
     74  1.39  christos -b	Boot only, no sets loaded
     75  1.47  christos -r	root device kind (sd, wd, ld)
     76  1.39  christos -d	Add the debug sets
     77  1.43  christos -m	Optimize the OS installation to mimimize disk writes for SSDs
     78  1.39  christos -x	Load the x sets too, not just the base ones
     79  1.12  christos EOF
     80  1.13  christos 	exit 1
     81  1.12  christos }
     82  1.12  christos 
     83  1.32  jmcneill # First pass for options to get the host and src directories
     84  1.47  christos OPTS="K:D:S:bc:dh:mr:s:x"
     85   1.4  christos while getopts "$OPTS" f
     86   1.4  christos do
     87   1.4  christos 	case $f in
     88   1.4  christos 	h)	h="$OPTARG";;
     89  1.32  jmcneill 	S)	src="$OPTARG";;
     90   1.4  christos 	*)	;;
     91   1.4  christos 	esac
     92   1.4  christos done
     93   1.4  christos 
     94   1.4  christos if [ -z "$h" ]
     95   1.4  christos then
     96   1.4  christos 	usage
     97   1.4  christos fi
     98   1.4  christos 
     99   1.5  christos if [ ! -f "${DIR}/conf/${h}.conf" ]
    100   1.4  christos then
    101   1.5  christos 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    102   1.4  christos 	exit 1
    103   1.4  christos fi
    104   1.4  christos 
    105   1.5  christos . "${DIR}/conf/${h}.conf"
    106  1.45  christos release="/usr/obj/${MACHINE}/release"
    107   1.4  christos 
    108  1.17  christos selected_sets="$sets"
    109  1.39  christos dsets=false
    110  1.39  christos xsets=false
    111  1.39  christos minwrites=false
    112  1.47  christos rootdev=ld
    113  1.17  christos 
    114   1.4  christos OPTIND=1
    115   1.4  christos while getopts "$OPTS" f
    116   1.4  christos do
    117   1.4  christos 	case $f in
    118  1.18  christos 	D)	release="$OPTARG";;
    119  1.18  christos 	K)	kernel="$OPTARG";;
    120  1.32  jmcneill 	S)	;;
    121  1.41  christos 	b)	bootonly=true;;
    122  1.39  christos 	d)	dsets=true
    123  1.39  christos 		selected_sets="$selected_sets debug"
    124  1.39  christos 		if $xsets; then
    125  1.39  christos 			selected_sets="$selected_sets xdebug"
    126  1.39  christos 		fi
    127  1.39  christos 		;;
    128   1.4  christos 	c)	custom="$OPTARG";;
    129   1.4  christos 	h)	;;
    130  1.39  christos 	m)	minwrites=true;;
    131  1.47  christos 	r)	rootdev="$OPTARG";;
    132   1.4  christos 	s)	size="$OPTARG";;
    133  1.39  christos 	x)	xsets=true
    134  1.39  christos 		selected_sets="$selected_sets $xsets"
    135  1.39  christos 		if $dsets; then
    136  1.39  christos 		    selected_sets="$selected_sets xdebug"
    137  1.39  christos 		fi
    138  1.39  christos 		;;
    139   1.6  christos 	*)	usage;;
    140   1.1       agc 	esac
    141   1.1       agc done
    142   1.1       agc 
    143  1.20  jmcneill shift $(( $OPTIND - 1 ))
    144   1.4  christos if [ -n "$1" ]; then
    145   1.1       agc 	# take the next argument as being the image name
    146   1.1       agc 	image="$1"
    147   1.1       agc 	shift
    148   1.1       agc fi
    149   1.1       agc 
    150  1.22  christos case "$image" in
    151  1.22  christos *.gz)	compress=true; image="${image%.gz}";;
    152  1.22  christos *)	compress=false;;
    153  1.22  christos esac
    154  1.22  christos 
    155  1.45  christos if [ -z "${bootonly}" ]; then
    156  1.36   garbled 	echo ${bar} configuring sets ${bar}
    157  1.36   garbled 	(echo '/set type=dir uname=root gname=wheel mode=0755'
    158  1.36   garbled 	for i in $selected_sets; do
    159  1.36   garbled 		s="${release}/etc/mtree/set.$i"
    160  1.36   garbled 		if [ -f "$s" ]; then
    161  1.36   garbled 			cat "$s"
    162  1.36   garbled 		fi
    163  1.36   garbled 	done) > "$tmp/selected_sets"
    164  1.36   garbled fi
    165   1.1       agc 
    166   1.4  christos make_fstab
    167  1.17  christos customize
    168  1.17  christos populate
    169   1.1       agc 
    170  1.45  christos if [ -z "${bootonly}" ]; then
    171  1.36   garbled 	(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
    172  1.36   garbled 	    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
    173  1.36   garbled fi
    174  1.45  christos if [ -n "${msdosid}" ]; then
    175  1.17  christos 	echo ${bar} Populating msdos filesystem ${bar}
    176  1.27  christos 	${MAKEFS} -N ${release}/etc -t msdos \
    177  1.41  christos 	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
    178  1.41  christos 	    ${image} ${mnt}/boot
    179   1.1       agc fi
    180   1.1       agc 
    181  1.45  christos if [ -z "${bootonly}" ]; then
    182  1.36   garbled 	echo ${bar} Populating ffs filesystem ${bar}
    183  1.36   garbled 	${MAKEFS} -N ${release}/etc -t ffs -rx \
    184  1.46  christos 	    -O ${ffsoffset} \
    185  1.41  christos 	    -o d=4096 -b $((${extra}))m \
    186  1.36   garbled 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    187  1.36   garbled fi
    188   1.1       agc 
    189  1.23  christos if [ "${size}" = 0 ]; then
    190  1.23  christos 	size="$(getsize "${image}")"
    191  1.17  christos fi
    192  1.23  christos newsize=$((${size} / 2 / 1024))
    193  1.49  christos compare=$((${newsize} * 2 * 1024))
    194  1.49  christos while [ "${compare}" != "${size}" ]
    195  1.49  christos do    
    196  1.49  christos 	size="$((size + size - compare))"  
    197  1.49  christos 	newsize="$((${size} / 2 / 1024))"
    198  1.49  christos 	compare="$((${newsize} * 2 * 1024))"
    199  1.49  christos done                      
    200   1.1       agc 
    201  1.17  christos echo ${bar} Adding label ${bar}
    202  1.17  christos make_label > ${tmp}/label
    203  1.21  christos ${DISKLABEL} -R -F ${image} ${tmp}/label
    204  1.45  christos if [ -n "${msdosid}" ]; then
    205  1.17  christos 	echo ${bar} Running fdisk ${bar}
    206  1.30  jmcneill 	initsecs=$((${init} * 1024))
    207  1.30  jmcneill 	bootsecs=$((${boot} * 1024))
    208  1.30  jmcneill 	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    209  1.45  christos elif [ -n "${netbsdid}" ]; then
    210  1.45  christos 	echo ${bar} Running fdisk ${bar}
    211  1.45  christos 	${FDISK} -f -i ${image}
    212  1.49  christos 	${FDISK} -f -a -u -0 -s 169/${init} ${image}
    213  1.45  christos 	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    214   1.2       agc fi
    215  1.22  christos 
    216  1.22  christos if $compress; then
    217  1.22  christos 	echo ${bar} Compressing image ${bar}
    218  1.23  christos 	rm -f "${image}.gz"
    219  1.44       ast 	${GZIP_CMD} -9 ${image}
    220  1.22  christos 	image="${image}.gz"
    221  1.22  christos fi
    222  1.22  christos 
    223  1.17  christos echo ${bar} Image is ${image} ${bar}
    224