1#!/bin/sh 2# $NetBSD: mkimage,v 1.61 2015/08/01 10:05:51 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 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 54DISKLABEL=${TOOL_DISKLABEL:-disklabel} 55FDISK=${TOOL_FDISK:-fdisk} 56MAKEFS=${TOOL_MAKEFS:-makefs} 57MTREE=${TOOL_MTREE:-mtree} 58INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 59MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 60GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 61 62src="/usr/src" 63sets="base comp etc games man misc modules text" 64xsets="xbase xcomp xetc xfont xserver" 65minfree="10%" 66bar="===" 67 68tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")" 69mnt="${tmp}/mnt" 70mkdir -p "${mnt}/etc" "${mnt}/dev" 71 72trap "cleanup" 0 1 2 3 15 73 74cleanup() { 75 case "$tmp" in 76 /tmp/$PROG.*) rm -fr "$tmp";; 77 esac 78} 79 80getsize() { 81 set -- $(ls -l $1) 82 echo $5 83} 84 85usage() { 86 cat << EOF 1>&2 87Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 88 89-b Boot only, no sets loaded 90-r root device kind (sd, wd, ld) 91-d Add the debug sets 92-m Optimize the OS installation to mimimize disk writes for SSDs 93-x Load the X sets too, not just the base ones 94EOF 95 exit 1 96} 97 98# First pass for options to get the host and src directories 99OPTS="B:D:K:S:bc:dh:mr:s:x" 100while getopts "$OPTS" f 101do 102 case $f in 103 h) h="$OPTARG";; 104 S) src="$OPTARG";; 105 *) ;; 106 esac 107done 108 109if [ -z "$h" ] 110then 111 usage 112fi 113 114if [ ! -f "${DIR}/conf/${h}.conf" ] 115then 116 echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 117 exit 1 118fi 119 120resize=false 121 122. "${DIR}/conf/${h}.conf" 123release="/usr/obj/${MACHINE}/release" 124 125selected_sets="$sets" 126dsets_p=false 127xsets_p=false 128minwrites=false 129rootdev=ld 130endian= 131 132OPTIND=1 133while getopts "$OPTS" f 134do 135 case $f in 136 B) endian="-B $OPTARG";; 137 D) release="$OPTARG";; 138 K) kernel="$OPTARG";; 139 S) ;; 140 b) bootonly=true;; 141 d) dsets_p=true 142 selected_sets="$selected_sets debug" 143 if $xsets_p; then 144 selected_sets="$selected_sets xdebug" 145 fi 146 ;; 147 c) custom="$OPTARG";; 148 h) ;; 149 m) minwrites=true;; 150 r) rootdev="$OPTARG";; 151 s) size="$OPTARG";; 152 x) xsets_p=true 153 selected_sets="$selected_sets $xsets" 154 if $dsets_p; then 155 selected_sets="$selected_sets xdebug" 156 fi 157 ;; 158 *) usage;; 159 esac 160done 161 162shift $(( $OPTIND - 1 )) 163if [ -n "$1" ]; then 164 # take the next argument as being the image name 165 image="$1" 166 shift 167fi 168 169case "$image" in 170*.gz) compress=true; image="${image%.gz}";; 171*) compress=false;; 172esac 173 174if [ -z "${bootonly}" ]; then 175 echo ${bar} configuring sets ${bar} 176 (cat "${release}/etc/mtree/NetBSD.dist" 177 for i in $selected_sets; do 178 s="${release}/etc/mtree/set.$i" 179 if [ -f "$s" ]; then 180 cat "$s" 181 fi 182 done) > "$tmp/selected_sets" 183fi 184 185make_fstab 186customize 187populate 188 189if [ -n "${msdosid}" ]; then 190 echo ${bar} Populating msdos filesystem ${bar} 191 ${MAKEFS} -N ${release}/etc -t msdos \ 192 -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 193 ${image} ${mnt}/boot 194fi 195 196if [ -z "${bootonly}" ]; then 197 echo ${bar} Populating ffs filesystem ${bar} 198 ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \ 199 -O ${ffsoffset} \ 200 -o d=4096,f=8192,b=65536 -b $((${extra}))m \ 201 -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 202fi 203 204if [ "${size}" = 0 ]; then 205 size="$(getsize "${image}")" 206fi 207newsize=$((${size} / 2 / 1024)) 208compare=$((${newsize} * 2 * 1024)) 209while [ "${compare}" != "${size}" ] 210do 211 size="$((size + size - compare))" 212 newsize="$((${size} / 2 / 1024))" 213 compare="$((${newsize} * 2 * 1024))" 214done 215 216echo ${bar} Adding label ${bar} 217make_label > ${tmp}/label 218${DISKLABEL} -R -F ${image} ${tmp}/label 219if [ -n "${msdosid}" ]; then 220 echo ${bar} Running fdisk ${bar} 221 initsecs=$((${init} * 1024)) 222 bootsecs=$((${boot} * 1024)) 223 ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 224elif [ -n "${netbsdid}" ]; then 225 echo ${bar} Running fdisk ${bar} 226 ${FDISK} -f -i ${image} 227 ${FDISK} -f -a -u -0 -s 169/${init} ${image} 228 ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 229fi 230 231if $compress; then 232 echo ${bar} Compressing image ${bar} 233 rm -f "${image}.gz" 234 ${GZIP_CMD} -9 ${image} 235 image="${image}.gz" 236fi 237 238echo ${bar} Image is ${image} ${bar} 239