mkimage revision 1.40
1#!/bin/sh 2# $NetBSD: mkimage,v 1.40 2013/06/16 13:47:42 jakllsch 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 35set -e 36 37DIR="$(cd "$(dirname "$0")" && pwd)" 38PROG="$(basename "$0")" 39 40DISKLABEL=${TOOL_DISKLABEL:-disklabel} 41FDISK=${TOOL_FDISK:-fdisk} 42GZIP=${TOOL_GZIP:-gzip} 43MAKEFS=${TOOL_MAKEFS:-makefs} 44MTREE=${TOOL_MTREE:-mtree} 45 46src="/usr/src" 47release="/usr/obj/evbarm/release" 48sets="base comp etc games man misc modules text" 49xsets="xbase xcomp xetc xfont xserver" 50minfree="10%" 51bar="===" 52 53tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")" 54mnt="${tmp}/mnt" 55mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot" 56 57trap "cleanup" 0 1 2 3 15 58 59cleanup() { 60 case "$tmp" in 61 /tmp/$PROG.*) rm -fr "$tmp";; 62 esac 63} 64 65getsize() { 66 set -- $(ls -l $1) 67 echo $5 68} 69 70usage() { 71 cat << EOF 1>&2 72Usage: $PROG -h <host-arch> [-bmx] [-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 Mimimize disk writes for sd cards 77-x Load the x sets too, not just the base ones 78EOF 79 exit 1 80} 81 82# First pass for options to get the host and src directories 83OPTS="K:D:S:bc:dh:ms:x" 84while getopts "$OPTS" f 85do 86 case $f in 87 h) h="$OPTARG";; 88 S) src="$OPTARG";; 89 *) ;; 90 esac 91done 92 93if [ -z "$h" ] 94then 95 usage 96fi 97 98if [ ! -f "${DIR}/conf/${h}.conf" ] 99then 100 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 101 exit 1 102fi 103 104. "${DIR}/conf/${h}.conf" 105 106selected_sets="$sets" 107dsets=false 108xsets=false 109minwrites=false 110 111OPTIND=1 112while getopts "$OPTS" f 113do 114 case $f in 115 D) release="$OPTARG";; 116 K) kernel="$OPTARG";; 117 S) ;; 118 b) bootonly="true";; 119 d) dsets=true 120 selected_sets="$selected_sets debug" 121 if $xsets; then 122 selected_sets="$selected_sets xdebug" 123 fi 124 ;; 125 c) custom="$OPTARG";; 126 h) ;; 127 m) minwrites=true;; 128 s) size="$OPTARG";; 129 x) xsets=true 130 selected_sets="$selected_sets $xsets" 131 if $dsets; then 132 selected_sets="$selected_sets xdebug" 133 fi 134 ;; 135 *) usage;; 136 esac 137done 138 139shift $(( $OPTIND - 1 )) 140if [ -n "$1" ]; then 141 # take the next argument as being the image name 142 image="$1" 143 shift 144fi 145 146case "$image" in 147*.gz) compress=true; image="${image%.gz}";; 148*) compress=false;; 149esac 150 151if [ -z "$bootonly" ]; then 152 echo ${bar} configuring sets ${bar} 153 (echo '/set type=dir uname=root gname=wheel mode=0755' 154 for i in $selected_sets; do 155 s="${release}/etc/mtree/set.$i" 156 if [ -f "$s" ]; then 157 cat "$s" 158 fi 159 done) > "$tmp/selected_sets" 160fi 161 162make_fstab 163customize 164populate 165 166if [ -z "$bootonly" ]; then 167 (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 168 ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets" 169fi 170if [ -n ${msdosid} ]; then 171 echo ${bar} Populating msdos filesystem ${bar} 172 ${MAKEFS} -N ${release}/etc -t msdos \ 173 -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m ${image} ${mnt}/boot 174fi 175 176if [ -z "$bootonly" ]; then 177 echo ${bar} Populating ffs filesystem ${bar} 178 ${MAKEFS} -N ${release}/etc -t ffs -rx \ 179 -O $(((${init} + ${boot} + ${swap}) / 2))m \ 180 -o d=4096 \ 181 -b $((${extra}))m \ 182 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 183fi 184 185if [ "${size}" = 0 ]; then 186 size="$(getsize "${image}")" 187fi 188newsize=$((${size} / 2 / 1024)) 189 190echo ${bar} Adding label ${bar} 191make_label > ${tmp}/label 192${DISKLABEL} -R -F ${image} ${tmp}/label 193if [ -n ${msdosid} ]; then 194 echo ${bar} Running fdisk ${bar} 195 initsecs=$((${init} * 1024)) 196 bootsecs=$((${boot} * 1024)) 197 ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 198fi 199 200if $compress; then 201 echo ${bar} Compressing image ${bar} 202 rm -f "${image}.gz" 203 ${GZIP} -9 ${image} 204 image="${image}.gz" 205fi 206 207echo ${bar} Image is ${image} ${bar} 208