mkimage revision 1.49
11.31Sjmcneill#!/bin/sh 21.49Schristos# $NetBSD: mkimage,v 1.49 2014/12/14 00:28:46 christos Exp $ 31.13Schristos# 41.45Schristos# Copyright (c) 2013, 2014 The NetBSD Foundation, Inc. 51.1Sagc# All rights reserved. 61.1Sagc# 71.17Schristos# This code is derived from software contributed to The NetBSD Foundation 81.17Schristos# by Christos Zoulas. 91.17Schristos# 101.1Sagc# Redistribution and use in source and binary forms, with or without 111.1Sagc# modification, are permitted provided that the following conditions 121.1Sagc# are met: 131.1Sagc# 1. Redistributions of source code must retain the above copyright 141.1Sagc# notice, this list of conditions and the following disclaimer. 151.1Sagc# 2. Redistributions in binary form must reproduce the above copyright 161.1Sagc# notice, this list of conditions and the following disclaimer in the 171.1Sagc# documentation and/or other materials provided with the distribution. 181.17Schristos# 3. Neither the name of The NetBSD Foundation nor the names of its 191.17Schristos# contributors may be used to endorse or promote products derived 201.17Schristos# from this software without specific prior written permission. 211.17Schristos# 221.17Schristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 231.17Schristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 241.17Schristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 251.17Schristos# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 261.17Schristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 271.17Schristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 281.17Schristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 291.17Schristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 301.17Schristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 311.17Schristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 321.17Schristos# POSSIBILITY OF SUCH DAMAGE. 331.1Sagc# 341.1Sagc 351.33Sjmcneillset -e 361.33Sjmcneill 371.17SchristosDIR="$(cd "$(dirname "$0")" && pwd)" 381.17SchristosPROG="$(basename "$0")" 391.17Schristos 401.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel} 411.24SchristosFDISK=${TOOL_FDISK:-fdisk} 421.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs} 431.25SjmcneillMTREE=${TOOL_MTREE:-mtree} 441.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 451.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 461.21Schristos 471.17Schristossrc="/usr/src" 481.17Schristossets="base comp etc games man misc modules text" 491.17Schristosxsets="xbase xcomp xetc xfont xserver" 501.17Schristosminfree="10%" 511.17Schristosbar="===" 521.17Schristos 531.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")" 541.17Schristosmnt="${tmp}/mnt" 551.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" 561.23Schristos 571.17Schristostrap "cleanup" 0 1 2 3 15 581.17Schristos 591.17Schristoscleanup() { 601.17Schristos case "$tmp" in 611.17Schristos /tmp/$PROG.*) rm -fr "$tmp";; 621.17Schristos esac 631.17Schristos} 641.1Sagc 651.17Schristosgetsize() { 661.17Schristos set -- $(ls -l $1) 671.17Schristos echo $5 681.1Sagc} 691.1Sagc 701.12Schristosusage() { 711.12Schristos cat << EOF 1>&2 721.41SchristosUsage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 731.39Schristos 741.39Schristos-b Boot only, no sets loaded 751.47Schristos-r root device kind (sd, wd, ld) 761.39Schristos-d Add the debug sets 771.43Schristos-m Optimize the OS installation to mimimize disk writes for SSDs 781.39Schristos-x Load the x sets too, not just the base ones 791.12SchristosEOF 801.13Schristos exit 1 811.12Schristos} 821.12Schristos 831.32Sjmcneill# First pass for options to get the host and src directories 841.47SchristosOPTS="K:D:S:bc:dh:mr:s:x" 851.4Schristoswhile getopts "$OPTS" f 861.4Schristosdo 871.4Schristos case $f in 881.4Schristos h) h="$OPTARG";; 891.32Sjmcneill S) src="$OPTARG";; 901.4Schristos *) ;; 911.4Schristos esac 921.4Schristosdone 931.4Schristos 941.4Schristosif [ -z "$h" ] 951.4Schristosthen 961.4Schristos usage 971.4Schristosfi 981.4Schristos 991.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ] 1001.4Schristosthen 1011.5Schristos echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 1021.4Schristos exit 1 1031.4Schristosfi 1041.4Schristos 1051.5Schristos. "${DIR}/conf/${h}.conf" 1061.45Schristosrelease="/usr/obj/${MACHINE}/release" 1071.4Schristos 1081.17Schristosselected_sets="$sets" 1091.39Schristosdsets=false 1101.39Schristosxsets=false 1111.39Schristosminwrites=false 1121.47Schristosrootdev=ld 1131.17Schristos 1141.4SchristosOPTIND=1 1151.4Schristoswhile getopts "$OPTS" f 1161.4Schristosdo 1171.4Schristos case $f in 1181.18Schristos D) release="$OPTARG";; 1191.18Schristos K) kernel="$OPTARG";; 1201.32Sjmcneill S) ;; 1211.41Schristos b) bootonly=true;; 1221.39Schristos d) dsets=true 1231.39Schristos selected_sets="$selected_sets debug" 1241.39Schristos if $xsets; then 1251.39Schristos selected_sets="$selected_sets xdebug" 1261.39Schristos fi 1271.39Schristos ;; 1281.4Schristos c) custom="$OPTARG";; 1291.4Schristos h) ;; 1301.39Schristos m) minwrites=true;; 1311.47Schristos r) rootdev="$OPTARG";; 1321.4Schristos s) size="$OPTARG";; 1331.39Schristos x) xsets=true 1341.39Schristos selected_sets="$selected_sets $xsets" 1351.39Schristos if $dsets; then 1361.39Schristos selected_sets="$selected_sets xdebug" 1371.39Schristos fi 1381.39Schristos ;; 1391.6Schristos *) usage;; 1401.1Sagc esac 1411.1Sagcdone 1421.1Sagc 1431.20Sjmcneillshift $(( $OPTIND - 1 )) 1441.4Schristosif [ -n "$1" ]; then 1451.1Sagc # take the next argument as being the image name 1461.1Sagc image="$1" 1471.1Sagc shift 1481.1Sagcfi 1491.1Sagc 1501.22Schristoscase "$image" in 1511.22Schristos*.gz) compress=true; image="${image%.gz}";; 1521.22Schristos*) compress=false;; 1531.22Schristosesac 1541.22Schristos 1551.45Schristosif [ -z "${bootonly}" ]; then 1561.36Sgarbled echo ${bar} configuring sets ${bar} 1571.36Sgarbled (echo '/set type=dir uname=root gname=wheel mode=0755' 1581.36Sgarbled for i in $selected_sets; do 1591.36Sgarbled s="${release}/etc/mtree/set.$i" 1601.36Sgarbled if [ -f "$s" ]; then 1611.36Sgarbled cat "$s" 1621.36Sgarbled fi 1631.36Sgarbled done) > "$tmp/selected_sets" 1641.36Sgarbledfi 1651.1Sagc 1661.4Schristosmake_fstab 1671.17Schristoscustomize 1681.17Schristospopulate 1691.1Sagc 1701.45Schristosif [ -z "${bootonly}" ]; then 1711.36Sgarbled (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 1721.36Sgarbled ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets" 1731.36Sgarbledfi 1741.45Schristosif [ -n "${msdosid}" ]; then 1751.17Schristos echo ${bar} Populating msdos filesystem ${bar} 1761.27Schristos ${MAKEFS} -N ${release}/etc -t msdos \ 1771.41Schristos -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 1781.41Schristos ${image} ${mnt}/boot 1791.1Sagcfi 1801.1Sagc 1811.45Schristosif [ -z "${bootonly}" ]; then 1821.36Sgarbled echo ${bar} Populating ffs filesystem ${bar} 1831.36Sgarbled ${MAKEFS} -N ${release}/etc -t ffs -rx \ 1841.46Schristos -O ${ffsoffset} \ 1851.41Schristos -o d=4096 -b $((${extra}))m \ 1861.36Sgarbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 1871.36Sgarbledfi 1881.1Sagc 1891.23Schristosif [ "${size}" = 0 ]; then 1901.23Schristos size="$(getsize "${image}")" 1911.17Schristosfi 1921.23Schristosnewsize=$((${size} / 2 / 1024)) 1931.49Schristoscompare=$((${newsize} * 2 * 1024)) 1941.49Schristoswhile [ "${compare}" != "${size}" ] 1951.49Schristosdo 1961.49Schristos size="$((size + size - compare))" 1971.49Schristos newsize="$((${size} / 2 / 1024))" 1981.49Schristos compare="$((${newsize} * 2 * 1024))" 1991.49Schristosdone 2001.1Sagc 2011.17Schristosecho ${bar} Adding label ${bar} 2021.17Schristosmake_label > ${tmp}/label 2031.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label 2041.45Schristosif [ -n "${msdosid}" ]; then 2051.17Schristos echo ${bar} Running fdisk ${bar} 2061.30Sjmcneill initsecs=$((${init} * 1024)) 2071.30Sjmcneill bootsecs=$((${boot} * 1024)) 2081.30Sjmcneill ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 2091.45Schristoselif [ -n "${netbsdid}" ]; then 2101.45Schristos echo ${bar} Running fdisk ${bar} 2111.45Schristos ${FDISK} -f -i ${image} 2121.49Schristos ${FDISK} -f -a -u -0 -s 169/${init} ${image} 2131.45Schristos ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 2141.2Sagcfi 2151.22Schristos 2161.22Schristosif $compress; then 2171.22Schristos echo ${bar} Compressing image ${bar} 2181.23Schristos rm -f "${image}.gz" 2191.44Sast ${GZIP_CMD} -9 ${image} 2201.22Schristos image="${image}.gz" 2211.22Schristosfi 2221.22Schristos 2231.17Schristosecho ${bar} Image is ${image} ${bar} 224