mkimage revision 1.67
1#!/bin/sh 2# $NetBSD: mkimage,v 1.67 2017/11/28 00:14:30 kre 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 35# 36# Makes a bootable image for the host architecture given. 37# The host specific functions are pulled in from a /bin/sh script in the 38# "conf" directory, and is expected to provide the following shell 39# functions, which are called in the following order: 40# 41# - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}. 42# If -m is given, a number of directories are put on a tmpfs RAM disk 43# - customize: After unpacking the sets, this gets the system to 44# a working state, e. g. by setting up /etc/rc.conf and /dev 45# - populate: Add common goods like kernel and bootloader 46# - make_label: Prints disklabel to stdout 47# 48 49set -e 50 51DIR="$(cd "$(dirname "$0")" && pwd)" 52PROG="$(basename "$0")" 53 54MAKE=${TOOL_MAKE:-make} 55DISKLABEL=${TOOL_DISKLABEL:-disklabel} 56FDISK=${TOOL_FDISK:-fdisk} 57MAKEFS=${TOOL_MAKEFS:-makefs} 58MTREE=${TOOL_MTREE:-mtree} 59INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 60MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 61GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 62 63src="/usr/src" 64sets="base comp etc games man misc modules tests text" 65xsets="xbase xcomp xetc xfont xserver" 66minfree="10%" 67bar="===" 68 69tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")" 70mnt="${tmp}/mnt" 71mkdir -p "${mnt}/etc" "${mnt}/dev" 72 73trap "cleanup" 0 1 2 3 15 74 75cleanup() { 76 case "$tmp" in 77 /tmp/$PROG.*) rm -fr "$tmp";; 78 esac 79} 80 81getsize() { 82 set -- $(ls -l $1) 83 echo $5 84} 85 86getsectors() { 87 case "$1" in 88 *g) 89 m=1073741824 90 v=${1%g} 91 ;; 92 *m) 93 m=1048576 94 v=${1%m} 95 ;; 96 *k) 97 m=1024 98 v=${1%k} 99 ;; 100 *[0-9b]) 101 m=1 102 v=${1%b} 103 ;; 104 esac 105 echo $((m * v / 512)) 106} 107 108usage() { 109 cat << EOF 1>&2 110Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 111 112-b Boot only, no sets loaded 113-r root device kind (sd, wd, ld) 114-d Add the debug sets 115-m Optimize the OS installation to mimimize disk writes for SSDs 116-x Load the X sets too, not just the base ones 117EOF 118 exit 1 119} 120 121# First pass for options to get the host and src directories 122OPTS="B:D:K:S:bc:dh:mr:s:x" 123while getopts "$OPTS" f 124do 125 case $f in 126 h) h="$OPTARG";; 127 S) src="$OPTARG";; 128 *) ;; 129 esac 130done 131 132if [ -z "$h" ] 133then 134 usage 135fi 136 137if [ ! -f "${DIR}/conf/${h}.conf" ] 138then 139 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 140 exit 1 141fi 142 143resize=false 144 145. "${DIR}/conf/${h}.conf" 146release="/usr/obj/${MACHINE}/release" 147 148selected_sets="$sets" 149dsets_p=false 150xsets_p=false 151minwrites=false 152rootdev=ld 153endian= 154 155OPTIND=1 156while getopts "$OPTS" f 157do 158 case $f in 159 B) endian="-B $OPTARG";; 160 D) release="$OPTARG";; 161 K) kernel="$OPTARG";; 162 S) ;; 163 b) bootonly=true;; 164 d) dsets_p=true 165 selected_sets="$selected_sets debug" 166 if $xsets_p; then 167 selected_sets="$selected_sets xdebug" 168 fi 169 ;; 170 c) custom="$OPTARG";; 171 h) ;; 172 m) minwrites=true;; 173 r) rootdev="$OPTARG";; 174 s) size="$OPTARG";; 175 x) xsets_p=true 176 selected_sets="$selected_sets $xsets" 177 if $dsets_p; then 178 selected_sets="$selected_sets xdebug" 179 fi 180 ;; 181 *) usage;; 182 esac 183done 184 185shift $(( $OPTIND - 1 )) 186if [ -n "$1" ]; then 187 # take the next argument as being the image name 188 image="$1" 189 shift 190fi 191 192case "$image" in 193*.gz) compress=true; image="${image%.gz}";; 194*) compress=false;; 195esac 196 197if [ -z "${bootonly}" ]; then 198 echo ${bar} configuring sets ${bar} 199 (cat "${release}/etc/mtree/NetBSD.dist" 200 for i in $selected_sets; do 201 s="${release}/etc/mtree/set.$i" 202 if [ -f "$s" ]; then 203 cat "$s" 204 fi 205 done) > "$tmp/selected_sets" 206fi 207 208make_fstab 209customize 210populate 211 212if [ -n "${msdosid}" ]; then 213 echo ${bar} Populating msdos filesystem ${bar} 214 ${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \ 215 -O $((${init} / 2))m -s $((${boot} / 2))m \ 216 ${image} ${mnt}/boot 217fi 218 219if [ -z "${bootonly}" ]; then 220 echo ${bar} Populating ffs filesystem ${bar} 221 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \ 222 -O ${ffsoffset} \ 223 -o d=4096,f=8192,b=65536 -b $((${extra}))m \ 224 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 225fi 226 227if [ "${size}" = 0 ]; then 228 size="$(getsize "${image}")" 229fi 230newsize=$((${size} / 2 / 1024)) 231compare=$((${newsize} * 2 * 1024)) 232while [ "${compare}" != "${size}" ] 233do 234 size="$((size + size - compare))" 235 newsize="$((${size} / 2 / 1024))" 236 compare="$((${newsize} * 2 * 1024))" 237done 238 239if [ -n "${msdosid}" ]; then 240 echo ${bar} Running fdisk ${bar} 241 initsecs=$((${init} * 1024)) 242 bootsecs=$((${boot} * 1024)) 243 ${FDISK} -f -i ${image} 244 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 245 if [ -z "${bootonly}" ]; then 246 ffsstart="$(getsectors ${ffsoffset})" 247 imagesize="$(getsize "${image}")" 248 imagesecs="$(getsectors ${imagesize})" 249 ffssize="$(expr ${imagesecs} - ${ffsstart})" 250 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image} 251 fi 252 253 echo ${bar} Adding label ${bar} 254 make_label > ${tmp}/label 255 ${DISKLABEL} -R -F ${image} ${tmp}/label 256elif [ -n "${netbsdid}" ]; then 257 echo ${bar} Adding label ${bar} 258 make_label > ${tmp}/label 259 ${DISKLABEL} -R -F ${image} ${tmp}/label 260 261 echo ${bar} Running fdisk ${bar} 262 ${FDISK} -f -i ${image} 263 ${FDISK} -f -a -u -0 -s 169/${init} ${image} 264 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 265fi 266 267if $compress; then 268 echo ${bar} Compressing image ${bar} 269 rm -f "${image}.gz" 270 ${GZIP_CMD} -9 ${image} 271 image="${image}.gz" 272fi 273 274echo ${bar} Image is ${image} ${bar} 275