Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.22
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.22 2013/02/10 01:28:42 christos Exp $
      3 #
      4 # Copyright (c) 2013 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 DIR="$(cd "$(dirname "$0")" && pwd)"
     36 PROG="$(basename "$0")"
     37 
     38 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     39 FDISK=${TOOL_DISKLABEL:-fdisk}
     40 MAKEFS=${TOOL_MAKEFS:-makefs}
     41 
     42 src="/usr/src"
     43 release="/usr/obj/evbarm/release"
     44 sets="base comp etc games man misc modules text"
     45 xsets="xbase xcomp xetc xfont xserver" 
     46 minfree="10%"
     47 bar="==="
     48 
     49 tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     50 mnt="${tmp}/mnt"
     51 mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
     52 trap "cleanup" 0 1 2 3 15
     53 
     54 cleanup() {
     55 	case "$tmp" in
     56 	/tmp/$PROG.*)	rm -fr "$tmp";;
     57 	esac
     58 }
     59 
     60 getsize() {
     61 	set -- $(ls -l $1)
     62 	echo $5
     63 }
     64 
     65 usage() {
     66 	cat << EOF 1>&2
     67 Usage: $PROG -h <host-arch> [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
     68 EOF
     69 	exit 1
     70 }
     71 
     72 # First pass for options to get the host
     73 OPTS="K:D:S:c:h:s:x"
     74 while getopts "$OPTS" f
     75 do
     76 	case $f in
     77 	h)	h="$OPTARG";;
     78 	*)	;;
     79 	esac
     80 done
     81 
     82 if [ -z "$h" ]
     83 then
     84 	usage
     85 fi
     86 
     87 if [ ! -f "${DIR}/conf/${h}.conf" ]
     88 then
     89 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
     90 	exit 1
     91 fi
     92 
     93 . "${DIR}/conf/${h}.conf"
     94 
     95 selected_sets="$sets"
     96 
     97 OPTIND=1
     98 while getopts "$OPTS" f
     99 do
    100 	case $f in
    101 	D)	release="$OPTARG";;
    102 	K)	kernel="$OPTARG";;
    103 	S)	src="$OPTARG";;
    104 	c)	custom="$OPTARG";;
    105 	h)	;;
    106 	s)	size="$OPTARG";;
    107 	x)	selected_sets="$sets $xsets";;
    108 	*)	usage;;
    109 	esac
    110 done
    111 
    112 trap cleanup 0 1 2 3 15
    113 
    114 shift $(( $OPTIND - 1 ))
    115 if [ -n "$1" ]; then
    116 	# take the next argument as being the image name
    117 	image="$1"
    118 	shift
    119 fi
    120 
    121 case "$image" in
    122 *.gz)	compress=true; image="${image%.gz}";;
    123 *)	compress=false;;
    124 esac
    125 
    126 echo ${bar} configuring sets ${bar}
    127 (echo '/set type=dir uname=root gname=wheel mode=0755'
    128 for i in $selected_sets; do
    129 	s="${release}/etc/mtree/set.$i"
    130 	[ -f "$s" ] && cat "$s"
    131 done) > "$tmp/selected_sets"
    132 
    133 make_fstab
    134 customize
    135 populate
    136 
    137 (cd ${mnt}; mtree -c -k all | mtree -C -k all) >> "$tmp/selected_sets"
    138 if [ -n ${msdosid} ]; then
    139 	echo ${bar} Populating msdos filesystem ${bar}
    140 	${MAKEFS} -t msdos -O $((${init} / 2))m -s $((${boot} / 2))m \
    141 	    ${image} ${mnt}/boot
    142 fi
    143 
    144 echo ${bar} Populating ffs filesystem ${bar}
    145 ${MAKEFS} -t ffs -rx -O $(((${init} + ${boot} + ${swap}) / 2))m \
    146     -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    147 
    148 if [ -z "$size" ]; then
    149 	size=$(getsize ${image})
    150 fi
    151 newsize=$((size / 2 / 1024))
    152 
    153 echo ${bar} Adding label ${bar}
    154 make_label > ${tmp}/label
    155 ${DISKLABEL} -R -F ${image} ${tmp}/label
    156 if [ -n ${msdosid} ]; then
    157 	echo ${bar} Running fdisk ${bar}
    158 	${FDISK} -f -u -0 -s ${msdosid}/${init}/${boot} -F ${image}
    159 fi
    160 
    161 if $compress; then
    162 	echo ${bar} Compressing image ${bar}
    163 	gzip -9 ${image}
    164 	image="${image}.gz"
    165 fi
    166 
    167 echo ${bar} Image is ${image} ${bar}
    168