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