mkimage revision 1.72
11.8Sthorpej#!/bin/sh 21.1Scgd# $NetBSD: mkimage,v 1.72 2020/05/18 21:19:34 jmcneill Exp $ 31.1Scgd# 41.1Scgd# Copyright (c) 2013, 2014 The NetBSD Foundation, Inc. 51.1Scgd# All rights reserved. 61.1Scgd# 71.1Scgd# This code is derived from software contributed to The NetBSD Foundation 81.1Scgd# by Christos Zoulas. 91.1Scgd# 101.1Scgd# Redistribution and use in source and binary forms, with or without 111.1Scgd# modification, are permitted provided that the following conditions 121.1Scgd# are met: 131.1Scgd# 1. Redistributions of source code must retain the above copyright 141.1Scgd# notice, this list of conditions and the following disclaimer. 151.1Scgd# 2. Redistributions in binary form must reproduce the above copyright 161.1Scgd# notice, this list of conditions and the following disclaimer in the 171.1Scgd# documentation and/or other materials provided with the distribution. 181.1Scgd# 3. Neither the name of The NetBSD Foundation nor the names of its 191.1Scgd# contributors may be used to endorse or promote products derived 201.1Scgd# from this software without specific prior written permission. 211.1Scgd# 221.1Scgd# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 231.1Scgd# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 241.1Scgd# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 251.1Scgd# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 261.1Scgd# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 271.1Scgd# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 281.1Scgd# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 291.1Scgd# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 301.1Scgd# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 311.1Scgd# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 321.1Scgd# POSSIBILITY OF SUCH DAMAGE. 331.1Scgd# 341.1Scgd 351.1Scgd# 361.1Scgd# Makes a bootable image for the host architecture given. 371.1Scgd# The host specific functions are pulled in from a /bin/sh script in the 381.1Scgd# "conf" directory, and is expected to provide the following shell 391.2Scgd# functions, which are called in the following order: 401.1Scgd# 411.1Scgd# - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}. 421.1Scgd# If -m is given, a number of directories are put on a tmpfs RAM disk 431.8Sthorpej# - customize: After unpacking the sets, this gets the system to 441.1Scgd# a working state, e. g. by setting up /etc/rc.conf and /dev 451.1Scgd# - populate: Add common goods like kernel and bootloader 461.1Scgd# - make_label: Prints disklabel to stdout 471.1Scgd# 481.1Scgd 491.1Scgdset -e 501.3Scgd 511.3ScgdDIR="$(cd "$(dirname "$0")" && pwd)" 521.3ScgdPROG="$(basename "$0")" 531.3Scgd 541.3ScgdMAKE=${TOOL_MAKE:-make} 551.3ScgdDISKLABEL=${TOOL_DISKLABEL:-disklabel} 561.3ScgdFDISK=${TOOL_FDISK:-fdisk} 571.3ScgdMAKEFS=${TOOL_MAKEFS:-makefs} 581.1ScgdMTREE=${TOOL_MTREE:-mtree} 591.2ScgdINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 601.3ScgdMKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 611.2ScgdGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 621.1Scgd 631.3Scgdsrc="/usr/src" 641.3Scgdsets="base comp etc games man misc modules rescue tests text" 651.1Scgdxsets="xbase xcomp xetc xfont xserver" 661.1Scgdminfree="10%" 671.3Scgdbar="===" 681.6Sthorpej 691.6Sthorpejtmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")" 701.6Sthorpejmnt="${tmp}/mnt" 711.6Sthorpejmkdir -p "${mnt}/etc" "${mnt}/dev" 721.1Scgd 731.1Scgdtrap "cleanup" 0 1 2 3 15 741.4Sthorpej 751.3Scgdcleanup() { 761.3Scgd case "$tmp" in 771.7Sthorpej "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";; 781.7Sthorpej esac 791.7Sthorpej} 80 81fail() { 82 IFS=' ' 83 echo >&2 "${PROG}: $*" 84 exit 1 85} 86 87getsize() { 88 set -- $(ls -l $1) 89 echo $5 90} 91 92getsectors() { 93 case "$1" in 94 *g) 95 m=1073741824 96 v=${1%g} 97 ;; 98 *m) 99 m=1048576 100 v=${1%m} 101 ;; 102 *k) 103 m=1024 104 v=${1%k} 105 ;; 106 *[0-9b]) 107 m=1 108 v=${1%b} 109 ;; 110 esac 111 echo $((m * v / 512)) 112} 113 114usage() { 115 cat << EOF 1>&2 116Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 117 118-b Boot only, no sets loaded 119-r root device kind (sd, wd, ld) 120-d Add the debug sets 121-m Optimize the OS installation to mimimize disk writes for SSDs 122-x Load the X sets too, not just the base ones 123EOF 124 exit 1 125} 126 127# First pass for options to get the host and src directories 128OPTS="B:D:K:S:bc:dh:mr:s:x" 129while getopts "$OPTS" f 130do 131 case $f in 132 h) h="$OPTARG";; 133 S) src="$OPTARG";; 134 *) ;; 135 esac 136done 137 138if [ -z "$h" ] 139then 140 usage 141fi 142 143if [ ! -f "${DIR}/conf/${h}.conf" ] 144then 145 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 146 exit 1 147fi 148 149resize=false 150 151. "${DIR}/conf/${h}.conf" 152release="/usr/obj/${MACHINE}/release" 153 154selected_sets="$sets" 155dsets_p=false 156xsets_p=false 157minwrites=false 158rootdev=ld 159endian= 160 161OPTIND=1 162while getopts "$OPTS" f 163do 164 case $f in 165 B) endian="-B $OPTARG";; 166 D) release="$OPTARG";; 167 K) kernel="$OPTARG";; 168 S) ;; 169 b) bootonly=true;; 170 d) dsets_p=true 171 selected_sets="$selected_sets debug" 172 if $xsets_p; then 173 selected_sets="$selected_sets xdebug" 174 fi 175 ;; 176 c) custom="$OPTARG";; 177 h) ;; 178 m) minwrites=true;; 179 r) rootdev="$OPTARG";; 180 s) size="$OPTARG";; 181 x) xsets_p=true 182 selected_sets="$selected_sets $xsets" 183 if $dsets_p; then 184 selected_sets="$selected_sets xdebug" 185 fi 186 ;; 187 *) usage;; 188 esac 189done 190 191shift $(( $OPTIND - 1 )) 192if [ -n "$1" ]; then 193 # take the next argument as being the image name 194 image="$1" 195 shift 196fi 197 198case "$image" in 199*.gz) compress=true; image="${image%.gz}";; 200*) compress=false;; 201esac 202 203if [ -z "${bootonly}" ]; then 204 echo ${bar} configuring sets ${bar} 205 (cat "${release}/etc/mtree/NetBSD.dist" 206 for i in $selected_sets; do 207 s="${release}/etc/mtree/set.$i" 208 if [ -f "$s" ]; then 209 cat "$s" 210 fi 211 done) > "$tmp/selected_sets" 212fi 213 214make_fstab 215customize 216populate 217 218if [ ! "${MKDTB}" = "no" ]; then 219 # 220 # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and 221 # the rest on FFS. Split it up here. 222 # 223 echo ${bar} Installing devicetree blobs ${bar} 224 mkdir -p "${mnt}/boot" 225 cp -r "${release}/boot/dtb" "${mnt}/boot/dtb" 226 227 mkdir -p "${mnt}/etc/mtree" 228 cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb" 229 echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets" 230 231 mkdir -p "${mnt}/var/db/obsolete" 232 cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb" 233 echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets" 234fi 235 236if [ -n "${msdosid}" ]; then 237 echo ${bar} Populating msdos filesystem ${bar} 238 239 case $(( ${msdosid} )) in 240 1) fat_opt=",fat_type=12";; 241 4|6|14) fat_opt=",fat_type=16";; 242 11|12) fat_opt=",fat_type=32";; 243 *) fat_opt=;; 244 esac 245 ${MAKEFS} -N ${release}/etc -t msdos \ 246 -o "volume_label=NETBSD${fat_opt}" \ 247 -O $((${init} / 2))m -s $((${boot} / 2))m \ 248 ${image} ${mnt}/boot 249fi 250 251if [ -z "${bootonly}" ]; then 252 echo ${bar} Populating ffs filesystem ${bar} 253 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \ 254 -O ${ffsoffset} \ 255 -o d=4096,f=8192,b=65536 -b $((${extra}))m \ 256 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 257fi 258 259if [ "${size}" = 0 ]; then 260 size="$(getsize "${image}")" 261fi 262newsize=$((${size} / 2 / 1024)) 263compare=$((${newsize} * 2 * 1024)) 264while [ "${compare}" != "${size}" ] 265do 266 size="$((size + size - compare))" 267 newsize="$((${size} / 2 / 1024))" 268 compare="$((${newsize} * 2 * 1024))" 269done 270 271if [ -n "${msdosid}" ]; then 272 echo ${bar} Running fdisk ${bar} 273 initsecs=$((${init} * 1024)) 274 bootsecs=$((${boot} * 1024)) 275 ${FDISK} -f -i ${image} 276 ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 277 if [ -z "${bootonly}" ]; then 278 ffsstart="$(getsectors ${ffsoffset})" 279 imagesize="$(getsize "${image}")" 280 imagesecs="$(getsectors ${imagesize})" 281 ffssize="$(expr ${imagesecs} - ${ffsstart})" 282 ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image} 283 fi 284 285 echo ${bar} Adding label ${bar} 286 make_label > ${tmp}/label 287 ${DISKLABEL} -R -F ${image} ${tmp}/label 288elif [ -n "${netbsdid}" ]; then 289 echo ${bar} Adding label ${bar} 290 make_label > ${tmp}/label 291 ${DISKLABEL} -R -F ${image} ${tmp}/label 292 293 echo ${bar} Running fdisk ${bar} 294 ${FDISK} -f -i ${image} 295 ${FDISK} -f -a -u -0 -s 169/${init} ${image} 296 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 297fi 298 299if $compress; then 300 echo ${bar} Compressing image ${bar} 301 rm -f "${image}.gz" 302 ${GZIP_CMD} -9 ${image} 303 image="${image}.gz" 304fi 305 306echo ${bar} Image is ${image} ${bar} 307