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