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