mkimage revision 1.55
1#!/bin/sh 2# $NetBSD: mkimage,v 1.55 2015/04/06 20:19:28 jmcneill Exp $ 3# 4# Copyright (c) 2013, 2014 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} 44INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 45GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 46 47src="/usr/src" 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" 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> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 73 74-b Boot only, no sets loaded 75-r root device kind (sd, wd, ld) 76-d Add the debug sets 77-m Optimize the OS installation to mimimize disk writes for SSDs 78-x Load the x sets too, not just the base ones 79EOF 80 exit 1 81} 82 83# First pass for options to get the host and src directories 84OPTS="K:D:S:bc:dh:mr:s:x" 85while getopts "$OPTS" f 86do 87 case $f in 88 h) h="$OPTARG";; 89 S) src="$OPTARG";; 90 *) ;; 91 esac 92done 93 94if [ -z "$h" ] 95then 96 usage 97fi 98 99if [ ! -f "${DIR}/conf/${h}.conf" ] 100then 101 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 102 exit 1 103fi 104 105. "${DIR}/conf/${h}.conf" 106release="/usr/obj/${MACHINE}/release" 107 108selected_sets="$sets" 109dsets_p=false 110xsets_p=false 111minwrites=false 112resize=false 113rootdev=ld 114 115OPTIND=1 116while getopts "$OPTS" f 117do 118 case $f in 119 D) release="$OPTARG";; 120 K) kernel="$OPTARG";; 121 S) ;; 122 b) bootonly=true;; 123 d) dsets_p=true 124 selected_sets="$selected_sets debug" 125 if $xsets_p; then 126 selected_sets="$selected_sets xdebug" 127 fi 128 ;; 129 c) custom="$OPTARG";; 130 h) ;; 131 m) minwrites=true;; 132 r) rootdev="$OPTARG";; 133 s) size="$OPTARG";; 134 x) xsets_p=true 135 selected_sets="$selected_sets $xsets" 136 if $dsets_p; then 137 selected_sets="$selected_sets xdebug" 138 fi 139 ;; 140 *) usage;; 141 esac 142done 143 144shift $(( $OPTIND - 1 )) 145if [ -n "$1" ]; then 146 # take the next argument as being the image name 147 image="$1" 148 shift 149fi 150 151case "$image" in 152*.gz) compress=true; image="${image%.gz}";; 153*) compress=false;; 154esac 155 156if [ -z "${bootonly}" ]; then 157 echo ${bar} configuring sets ${bar} 158 (cat "${release}/etc/mtree/NetBSD.dist" 159 for i in $selected_sets; do 160 s="${release}/etc/mtree/set.$i" 161 if [ -f "$s" ]; then 162 cat "$s" 163 fi 164 done) > "$tmp/selected_sets" 165fi 166 167make_fstab 168customize 169populate 170 171if [ -n "${msdosid}" ]; then 172 echo ${bar} Populating msdos filesystem ${bar} 173 ${MAKEFS} -N ${release}/etc -t msdos \ 174 -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 175 ${image} ${mnt}/boot 176fi 177 178if [ -z "${bootonly}" ]; then 179 echo ${bar} Populating ffs filesystem ${bar} 180 ${MAKEFS} -r -N ${release}/etc -t ffs -rx \ 181 -O ${ffsoffset} \ 182 -o d=4096,f=2048,b=16384 -b $((${extra}))m \ 183 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 184fi 185 186if [ "${size}" = 0 ]; then 187 size="$(getsize "${image}")" 188fi 189newsize=$((${size} / 2 / 1024)) 190compare=$((${newsize} * 2 * 1024)) 191while [ "${compare}" != "${size}" ] 192do 193 size="$((size + size - compare))" 194 newsize="$((${size} / 2 / 1024))" 195 compare="$((${newsize} * 2 * 1024))" 196done 197 198echo ${bar} Adding label ${bar} 199make_label > ${tmp}/label 200${DISKLABEL} -R -F ${image} ${tmp}/label 201if [ -n "${msdosid}" ]; then 202 echo ${bar} Running fdisk ${bar} 203 initsecs=$((${init} * 1024)) 204 bootsecs=$((${boot} * 1024)) 205 ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 206elif [ -n "${netbsdid}" ]; then 207 echo ${bar} Running fdisk ${bar} 208 ${FDISK} -f -i ${image} 209 ${FDISK} -f -a -u -0 -s 169/${init} ${image} 210 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 211fi 212 213if $compress; then 214 echo ${bar} Compressing image ${bar} 215 rm -f "${image}.gz" 216 ${GZIP_CMD} -9 ${image} 217 image="${image}.gz" 218fi 219 220echo ${bar} Image is ${image} ${bar} 221