Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.11
      1   1.1       agc #! /bin/sh
      2   1.1       agc 
      3  1.11  christos # $NetBSD: mkimage,v 1.11 2013/01/15 21:04:41 christos Exp $
      4   1.1       agc 
      5   1.1       agc # Copyright (c) 2012 Alistair Crooks <agc (at] NetBSD.org>
      6   1.1       agc # All rights reserved.
      7   1.1       agc #
      8   1.1       agc # Redistribution and use in source and binary forms, with or without
      9   1.1       agc # modification, are permitted provided that the following conditions
     10   1.1       agc # are met:
     11   1.1       agc # 1. Redistributions of source code must retain the above copyright
     12   1.1       agc #    notice, this list of conditions and the following disclaimer.
     13   1.1       agc # 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       agc #    notice, this list of conditions and the following disclaimer in the
     15   1.1       agc #    documentation and/or other materials provided with the distribution.
     16   1.1       agc #
     17   1.1       agc # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1       agc # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1       agc # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1       agc # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1       agc # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1       agc # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1       agc # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1       agc # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1       agc # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26   1.1       agc # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1       agc #
     28   1.1       agc 
     29   1.1       agc # find next available vnd, from kre
     30   1.1       agc next_avail ()
     31   1.1       agc {
     32   1.1       agc 	local dev="$1"
     33   1.1       agc 	local N=$(( ${#dev} + 1 ))
     34   1.1       agc 	local unit units
     35   1.1       agc 
     36   1.1       agc 	units=$(
     37   1.1       agc 		sysctl -n hw.disknames		|
     38   1.1       agc 			tr ' ' '\012'		|
     39   1.1       agc 			grep '^'"${dev}"'[0-9]'	|
     40   1.1       agc 			sort -n -k 1.$N			)
     41   1.1       agc 
     42   1.1       agc 	test -z "${units}" && {
     43   1.1       agc 		test -e "/dev/${dev}0a" || {
     44   1.1       agc 			echo >&2 "No ${dev}s available!"
     45   1.1       agc 			return 1
     46   1.1       agc 		}
     47   1.1       agc 		echo "${dev}0"
     48   1.1       agc 		return
     49   1.1       agc 	}
     50   1.1       agc 
     51   1.1       agc 	N=0
     52   1.1       agc 	for unit in ${units}
     53   1.1       agc 	do
     54   1.1       agc 		if [ "${unit}" = "${dev}${N}" ]
     55   1.1       agc 		then
     56   1.1       agc 			N=$(( N + 1 ))
     57   1.1       agc 		else
     58   1.1       agc 			echo "${dev}${N}"
     59   1.1       agc 			return
     60   1.1       agc 		fi
     61   1.1       agc 	done
     62   1.1       agc 
     63   1.1       agc 	test -e /dev/"${dev}${N}a" || {
     64   1.1       agc 		echo >&2 "All ${dev}s in use"
     65   1.1       agc 		return 1
     66   1.1       agc 	}
     67   1.1       agc 
     68   1.1       agc 	echo "${dev}${N}"
     69   1.1       agc }
     70   1.1       agc 
     71   1.1       agc # find the size of the gzipped files in a .tgz archive
     72  1.11  christos # Directories appear as 0, so count them as one block
     73  1.11  christos # and round up files to a fragment.
     74   1.1       agc sizeone() {
     75   1.4  christos 	if [ ! -f "$1" ]
     76   1.4  christos 	then
     77   1.4  christos 		echo "$PROG: Missing set $1" 1>&2
     78   1.4  christos 		echo 0
     79   1.4  christos 		return;
     80   1.4  christos 	fi
     81   1.1       agc         case "$1" in 
     82  1.11  christos         *.tgz|*.tar.gz|*.tbz|*.tar.bz2|*.txz|*.tar.xz)
     83  1.11  christos                 tar tvzf "$1" |
     84  1.11  christos 		awk -v fsize=${fsize} -v bsize=${bsize} '
     85  1.11  christos 		{
     86  1.11  christos 			if ($5 == 0)
     87  1.11  christos 				tot += bsize;
     88  1.11  christos 			else
     89  1.11  christos 				tot += ((int)(($5 + fsize - 1) / fsize)) * fsize;
     90  1.11  christos 		}
     91  1.11  christos 		END {
     92  1.11  christos 			printf("%d\n", tot);
     93  1.11  christos 		}'
     94   1.1       agc                 ;;
     95   1.1       agc         *)
     96   1.1       agc                 echo 0
     97   1.1       agc                 ;; 
     98   1.1       agc         esac
     99   1.1       agc }
    100   1.1       agc 
    101   1.4  christos usage() {
    102   1.4  christos 	cat << EOF 1>&2
    103   1.4  christos Usage: $PROG [-S <setsdir>] [-c <custom-files-dir>] [-h <host-arch>] [-s <size>]
    104   1.4  christos EOF
    105   1.4  christos exit 1
    106   1.4  christos }
    107   1.4  christos 
    108  1.11  christos # Return the usable filesystem size in bytes, given the total size in bytes,
    109  1.11  christos # and optionally block and fragment sizes
    110  1.11  christos getffssize() {
    111  1.11  christos 	local bytes="$1"
    112  1.11  christos 	local barg
    113  1.11  christos 	local farg
    114  1.11  christos 	local overhead
    115  1.11  christos 
    116  1.11  christos 	if [ -n "$2" ]
    117  1.11  christos 	then
    118  1.11  christos 		barg="-b $2"
    119  1.11  christos 		if [ -n "$3" ]
    120  1.11  christos 		then
    121  1.11  christos 			farg="-f $3"
    122  1.11  christos 		fi
    123  1.11  christos 	fi
    124  1.11  christos 
    125  1.11  christos 	overhead=$(newfs -N ${farg} ${barg} -s "${bytes}b" -F /dev/null |
    126  1.11  christos 	    awk '/using/ {
    127  1.11  christos 		printf("%d\n", substr($6, 1, length($6) - 3) * 1024 * 1024);
    128  1.11  christos 	    }'
    129  1.11  christos 	)
    130  1.11  christos 	echo $(( ${bytes} - ${overhead} ))
    131  1.11  christos }
    132  1.11  christos 
    133  1.11  christos # Compute the size of an ffs filesystem that can fit x bytes.
    134  1.11  christos # Instead of duplicating the newfs calculations here we let
    135  1.11  christos # it do the job, using binary search.
    136  1.11  christos makeffssize() {
    137  1.11  christos 	local bytes=$1
    138  1.11  christos 	local bsize=$2
    139  1.11  christos 	local fsize=$3
    140  1.11  christos 	local max=$(( 2 * ${bytes} ))
    141  1.11  christos 	local min="${bytes}"
    142  1.11  christos 	local cur
    143  1.11  christos 	local res
    144  1.11  christos 	while true; do
    145  1.11  christos 		cur="$(( ( ${max} + ${min} ) / 2 ))"
    146  1.11  christos 		res="$(getffssize "${cur}" ${bsize} ${fsize})"
    147  1.11  christos #		echo ${min} ${cur} ${max} ${res} ${bytes} 1>&2
    148  1.11  christos 		if [ "${res}" -eq "${bytes}" ]
    149  1.11  christos 		then
    150  1.11  christos 		    break
    151  1.11  christos 		elif [ "$(( ${min} + 1 ))" -ge "${max}" ]
    152  1.11  christos 		then
    153  1.11  christos 		    break
    154  1.11  christos 		elif [ "${res}" -lt "${bytes}" ]
    155  1.11  christos 		then
    156  1.11  christos 		    min="${cur}"
    157  1.11  christos 		elif [ "${res}" -gt "${bytes}" ]
    158  1.11  christos 		then
    159  1.11  christos 		    max="${cur}"
    160  1.11  christos 		fi
    161  1.11  christos 	done
    162  1.11  christos 	echo "${cur}"
    163  1.11  christos }
    164  1.11  christos 
    165   1.4  christos finish() {
    166   1.4  christos     cleanup
    167   1.4  christos     ${sudo} umount ${mnt}
    168   1.4  christos     ${sudo} vnconfig -u ${vnddev}
    169   1.4  christos }
    170   1.4  christos 
    171  1.11  christos 
    172   1.4  christos DIR="$(dirname "$0")"
    173   1.4  christos PROG="$(basename "$0")"
    174   1.2       agc bar="==="
    175   1.4  christos sudo=
    176   1.7  christos mnt="${TMPDIR:-/tmp}/image.$$"
    177   1.4  christos src="/usr/src"
    178   1.4  christos obj="/usr/obj"
    179   1.4  christos 
    180  1.11  christos 
    181  1.11  christos # Presumable block and fragment size.
    182  1.11  christos bsize=16384
    183  1.11  christos fsize=2048
    184  1.11  christos 
    185   1.4  christos # First pass for options to get the host
    186   1.4  christos OPTS="S:c:h:s:x"
    187   1.4  christos while getopts "$OPTS" f
    188   1.4  christos do
    189   1.4  christos 	case $f in
    190   1.4  christos 	h)	h="$OPTARG";;
    191   1.4  christos 	*)	;;
    192   1.4  christos 	esac
    193   1.4  christos done
    194   1.4  christos 
    195   1.4  christos if [ -z "$h" ]
    196   1.4  christos then
    197   1.4  christos 	usage
    198   1.4  christos fi
    199   1.4  christos 
    200   1.5  christos if [ ! -f "${DIR}/conf/${h}.conf" ]
    201   1.4  christos then
    202   1.5  christos 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    203   1.4  christos 	exit 1
    204   1.4  christos fi
    205   1.4  christos 
    206   1.5  christos . "${DIR}/conf/${h}.conf"
    207   1.4  christos 
    208   1.4  christos OPTIND=1
    209   1.4  christos while getopts "$OPTS" f
    210   1.4  christos do
    211   1.4  christos 	case $f in
    212   1.4  christos 	S)	setsdir="$OPTARG";;
    213   1.4  christos 	c)	custom="$OPTARG";;
    214   1.4  christos 	h)	;;
    215   1.4  christos 	s)	size="$OPTARG";;
    216   1.4  christos 	x)	set -x;;
    217   1.6  christos 	*)	usage;;
    218   1.1       agc 	esac
    219   1.1       agc done
    220   1.1       agc 
    221   1.8  jmcneill trap finish 0 1 2 3 15
    222   1.8  jmcneill 
    223   1.4  christos shift $(( "$OPTIND" - 1 ))
    224   1.4  christos if [ -n "$1" ]; then
    225   1.1       agc 	# take the next argument as being the image name
    226   1.1       agc 	image="$1"
    227   1.1       agc 	shift
    228   1.1       agc fi
    229   1.1       agc 
    230  1.11  christos # calculate the set bytes
    231  1.11  christos setbytes=0
    232  1.11  christos echo -n "${bar} computing set sizes ("
    233   1.1       agc for s in ${sets}; do
    234   1.4  christos 	one="$(sizeone ${setsdir}/${s}.tgz)"
    235  1.11  christos 	echo -n " $s=$(( ${one} / 1024 / 1024 ))MB"
    236  1.11  christos 	setbytes=$(( ${setbytes} +  ${one} ))
    237   1.1       agc done
    238  1.11  christos echo "): $(( ${setbytes} / 1024 / 1024 ))MB ${bar}"
    239  1.11  christos 
    240   1.2       agc # calculate size of custom files
    241  1.11  christos custbytes=0
    242   1.2       agc if [ -d "${custom}" ]; then
    243  1.11  christos 	custbytes=$(ls -lR "${custom}" | 
    244  1.11  christos 	    awk 'NF == 9 { tot += $5 } END { print tot }')
    245   1.2       agc fi
    246  1.11  christos echo "${bar} computing custom sizes: $(( ${custbytes} / 1024 / 1024 ))MB ${bar}"
    247  1.11  christos 
    248  1.11  christos # how many bytes
    249  1.11  christos rawbytes="$(( ${setbytes} + ${custbytes} ))"
    250  1.11  christos echo -n "${bar} computing ffs filesystem size for $(( ${rawbytes} / 1024 / 1024 ))MB: "
    251  1.11  christos ffsbytes="$(makeffssize "${rawbytes}")"
    252  1.11  christos ffsmb=$(( ${ffsbytes} / 1024 / 1024 ))
    253  1.11  christos echo " ${ffsmb}MB ${bar}"
    254  1.11  christos 
    255  1.11  christos # total in MB
    256  1.11  christos total=$(( ${ffsmb} + ${overhead} ))
    257  1.11  christos echo "${bar} overhead: ${overhead}MB ${bar}"
    258  1.11  christos 
    259   1.1       agc if [ $size -eq 0 ]; then
    260   1.1       agc         # auto-size the pkgs fs
    261  1.10  christos         newsize=${total}
    262   1.1       agc else
    263   1.1       agc         # check that we've been given enough space
    264   1.1       agc         if [ ${total} -gt ${size} ]; then
    265   1.4  christos                 echo "$PROG: Given size is ${size} MB, but we need ${total} MB" >&2
    266   1.1       agc                 exit 1
    267   1.1       agc         fi
    268  1.10  christos 	newsize=${size}
    269   1.1       agc fi
    270   1.1       agc 
    271  1.10  christos echo "${bar} making a new ${newsize} MB image in ${image} ${bar}"
    272  1.10  christos dd if=/dev/zero of=${image} bs=1m count=${newsize} conv=sparse
    273   1.1       agc 
    274   1.1       agc vnddev=$(next_avail vnd)
    275   1.1       agc echo "${bar} mounting image via vnd ${vnddev} ${bar}"
    276   1.2       agc ${sudo} vnconfig ${vnddev} ${image}
    277   1.4  christos ${sudo} mkdir -p ${mnt}
    278   1.4  christos make_filesystems
    279   1.4  christos 
    280   1.4  christos ${sudo} mkdir -p ${mnt}/etc ${mnt}/dev
    281   1.1       agc 
    282  1.10  christos echo -n "${bar} installing sets:"
    283   1.4  christos (cd ${mnt} &&
    284   1.1       agc 	for s in ${sets}; do
    285  1.10  christos 		ss="${setsdir}/${s}.tgz"
    286  1.10  christos 		if [ -f "${ss}" ]; then
    287  1.10  christos 			echo -n " ${s}"
    288  1.10  christos 			${sudo} tar xpzf "${ss}"
    289   1.4  christos 		fi
    290   1.1       agc 	done
    291   1.1       agc )
    292  1.10  christos echo " ${bar}"
    293   1.1       agc 
    294   1.1       agc echo "${bar} performing customisations ${bar}"
    295   1.1       agc 
    296   1.4  christos make_fstab
    297   1.1       agc 
    298   1.4  christos ${sudo} cat > ${mnt}/etc/rc.conf << EOF
    299   1.1       agc #
    300   1.1       agc # see rc.conf(5) for more information.
    301   1.1       agc #
    302   1.1       agc # Use program=YES to enable program, NO to disable it. program_flags are
    303   1.1       agc # passed to the program on the command line.
    304   1.1       agc #
    305   1.1       agc 
    306   1.1       agc # Load the defaults in from /etc/defaults/rc.conf (if it's readable).
    307   1.1       agc # These can be overridden below.
    308   1.1       agc #
    309   1.1       agc if [ -r /etc/defaults/rc.conf ]; then
    310   1.1       agc         . /etc/defaults/rc.conf
    311   1.1       agc fi
    312   1.1       agc 
    313   1.1       agc # If this is not set to YES, the system will drop into single-user mode.
    314   1.1       agc #
    315   1.1       agc rc_configured=YES
    316   1.1       agc 
    317   1.4  christos hostname=${h}
    318   1.1       agc 
    319   1.4  christos EOF
    320   1.1       agc 
    321   1.4  christos customize
    322   1.1       agc 
    323   1.1       agc for d in ${specialdirs}; do
    324   1.4  christos 	${sudo} mkdir -p ${mnt}/${d}
    325   1.1       agc done
    326   1.1       agc 
    327   1.4  christos if [ \( -n "${custom}" \) -a \( -d "${custom}" \) ]; then
    328   1.2       agc 	echo "${bar} user customisations from files in ${custom} ${bar}"
    329   1.4  christos 	(cd ${custom} && ${sudo} pax -rwpe . ${mnt})
    330   1.2       agc fi
    331   1.1       agc 
    332   1.1       agc exit 0
    333