#!/bin/sh # $NetBSD: mkimage,v 1.18 2013/02/08 19:14:14 christos Exp $ # # Copyright (c) 2013 The NetBSD Foundation, Inc. # All rights reserved. # # This code is derived from software contributed to The NetBSD Foundation # by Christos Zoulas. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of The NetBSD Foundation nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # DIR="$(cd "$(dirname "$0")" && pwd)" PROG="$(basename "$0")" src="/usr/src" release="/usr/obj/evbarm/release" sets="base comp etc games man misc modules text" xsets="xbase xcomp xetc xfont xserver" minfree="10%" bar="===" tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")" mnt="${tmp}/mnt" mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot" trap "cleanup" 0 1 2 3 15 cleanup() { case "$tmp" in /tmp/$PROG.*) rm -fr "$tmp";; esac } getsize() { set -- $(ls -l $1) echo $5 } usage() { cat << EOF 1>&2 Usage: $PROG -h [-K ] [-S ] [-R ] [-c ] [-s ] [] EOF exit 1 } # First pass for options to get the host OPTS="K:D:S:c:h:s:x" while getopts "$OPTS" f do case $f in h) h="$OPTARG";; *) ;; esac done if [ -z "$h" ] then usage fi if [ ! -f "${DIR}/conf/${h}.conf" ] then echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 exit 1 fi . "${DIR}/conf/${h}.conf" selected_sets="$sets" OPTIND=1 while getopts "$OPTS" f do case $f in D) release="$OPTARG";; K) kernel="$OPTARG";; S) src="$OPTARG";; c) custom="$OPTARG";; h) ;; s) size="$OPTARG";; x) selected_sets="$sets $xsets";; *) usage;; esac done trap cleanup 0 1 2 3 15 shift $(( "$OPTIND" - 1 )) if [ -n "$1" ]; then # take the next argument as being the image name image="$1" shift fi . "$DIR/conf/rpi.conf" echo ${bar} configuring sets ${bar} (echo '/set type=dir uname=root gname=wheel mode=0755' for i in $selected_sets; do s="${release}/etc/mtree/set.$i" [ -f "$s" ] && cat "$s" done) > "$tmp/selected_sets" make_fstab customize populate (cd ${mnt}; mtree -c -k all | mtree -C -k all) >> "$tmp/selected_sets" if [ -n ${msdosid} ]; then echo ${bar} Populating msdos filesystem ${bar} makefs -t msdos -O $((${init} / 2))m -s $((${boot} / 2))m \ ${image} ${mnt}/boot fi echo ${bar} Populating ffs filesystem ${bar} makefs -t ffs -rx -O $(((${init} + ${boot} + ${swap}) / 2))m \ -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" if [ -z "$size" ]; then size=$(getsize ${image}) fi newsize=$((size / 2 / 1024)) echo ${bar} Adding label ${bar} make_label > ${tmp}/label disklabel -R -F ${image} ${tmp}/label if [ -n ${msdosid} ]; then echo ${bar} Running fdisk ${bar} fdisk -f -u -0 -s ${msdosid}/${init}/${boot} -F ${image} fi echo ${bar} Image is ${image} ${bar}