mkimage revision 1.45
11.31Sjmcneill#!/bin/sh 21.45Schristos# $NetBSD: mkimage,v 1.45 2014/03/31 16:20:48 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.17Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot" 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.39Schristos-d Add the debug sets 761.43Schristos-m Optimize the OS installation to mimimize disk writes for SSDs 771.39Schristos-x Load the x sets too, not just the base ones 781.12SchristosEOF 791.13Schristos exit 1 801.12Schristos} 811.12Schristos 821.32Sjmcneill# First pass for options to get the host and src directories 831.39SchristosOPTS="K:D:S:bc:dh:ms:x" 841.4Schristoswhile getopts "$OPTS" f 851.4Schristosdo 861.4Schristos case $f in 871.4Schristos h) h="$OPTARG";; 881.32Sjmcneill S) src="$OPTARG";; 891.4Schristos *) ;; 901.4Schristos esac 911.4Schristosdone 921.4Schristos 931.4Schristosif [ -z "$h" ] 941.4Schristosthen 951.4Schristos usage 961.4Schristosfi 971.4Schristos 981.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ] 991.4Schristosthen 1001.5Schristos echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 1011.4Schristos exit 1 1021.4Schristosfi 1031.4Schristos 1041.5Schristos. "${DIR}/conf/${h}.conf" 1051.45Schristosrelease="/usr/obj/${MACHINE}/release" 1061.4Schristos 1071.17Schristosselected_sets="$sets" 1081.39Schristosdsets=false 1091.39Schristosxsets=false 1101.39Schristosminwrites=false 1111.17Schristos 1121.4SchristosOPTIND=1 1131.4Schristoswhile getopts "$OPTS" f 1141.4Schristosdo 1151.4Schristos case $f in 1161.18Schristos D) release="$OPTARG";; 1171.18Schristos K) kernel="$OPTARG";; 1181.32Sjmcneill S) ;; 1191.41Schristos b) bootonly=true;; 1201.39Schristos d) dsets=true 1211.39Schristos selected_sets="$selected_sets debug" 1221.39Schristos if $xsets; then 1231.39Schristos selected_sets="$selected_sets xdebug" 1241.39Schristos fi 1251.39Schristos ;; 1261.4Schristos c) custom="$OPTARG";; 1271.4Schristos h) ;; 1281.39Schristos m) minwrites=true;; 1291.4Schristos s) size="$OPTARG";; 1301.39Schristos x) xsets=true 1311.39Schristos selected_sets="$selected_sets $xsets" 1321.39Schristos if $dsets; then 1331.39Schristos selected_sets="$selected_sets xdebug" 1341.39Schristos fi 1351.39Schristos ;; 1361.6Schristos *) usage;; 1371.1Sagc esac 1381.1Sagcdone 1391.1Sagc 1401.20Sjmcneillshift $(( $OPTIND - 1 )) 1411.4Schristosif [ -n "$1" ]; then 1421.1Sagc # take the next argument as being the image name 1431.1Sagc image="$1" 1441.1Sagc shift 1451.1Sagcfi 1461.1Sagc 1471.22Schristoscase "$image" in 1481.22Schristos*.gz) compress=true; image="${image%.gz}";; 1491.22Schristos*) compress=false;; 1501.22Schristosesac 1511.22Schristos 1521.45Schristosif [ -z "${bootonly}" ]; then 1531.36Sgarbled echo ${bar} configuring sets ${bar} 1541.36Sgarbled (echo '/set type=dir uname=root gname=wheel mode=0755' 1551.36Sgarbled for i in $selected_sets; do 1561.36Sgarbled s="${release}/etc/mtree/set.$i" 1571.36Sgarbled if [ -f "$s" ]; then 1581.36Sgarbled cat "$s" 1591.36Sgarbled fi 1601.36Sgarbled done) > "$tmp/selected_sets" 1611.36Sgarbledfi 1621.1Sagc 1631.4Schristosmake_fstab 1641.17Schristoscustomize 1651.17Schristospopulate 1661.1Sagc 1671.45Schristosif [ -z "${bootonly}" ]; then 1681.36Sgarbled (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 1691.36Sgarbled ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets" 1701.36Sgarbledfi 1711.45Schristosif [ -n "${msdosid}" ]; then 1721.17Schristos echo ${bar} Populating msdos filesystem ${bar} 1731.27Schristos ${MAKEFS} -N ${release}/etc -t msdos \ 1741.41Schristos -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 1751.41Schristos ${image} ${mnt}/boot 1761.1Sagcfi 1771.1Sagc 1781.45Schristosif [ -z "${bootonly}" ]; then 1791.36Sgarbled echo ${bar} Populating ffs filesystem ${bar} 1801.36Sgarbled ${MAKEFS} -N ${release}/etc -t ffs -rx \ 1811.36Sgarbled -O $(((${init} + ${boot} + ${swap}) / 2))m \ 1821.41Schristos -o d=4096 -b $((${extra}))m \ 1831.36Sgarbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 1841.36Sgarbledfi 1851.1Sagc 1861.23Schristosif [ "${size}" = 0 ]; then 1871.23Schristos size="$(getsize "${image}")" 1881.17Schristosfi 1891.23Schristosnewsize=$((${size} / 2 / 1024)) 1901.1Sagc 1911.17Schristosecho ${bar} Adding label ${bar} 1921.17Schristosmake_label > ${tmp}/label 1931.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label 1941.45Schristosif [ -n "${msdosid}" ]; then 1951.17Schristos echo ${bar} Running fdisk ${bar} 1961.30Sjmcneill initsecs=$((${init} * 1024)) 1971.30Sjmcneill bootsecs=$((${boot} * 1024)) 1981.30Sjmcneill ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 1991.45Schristoselif [ -n "${netbsdid}" ]; then 2001.45Schristos echo ${bar} Running fdisk ${bar} 2011.45Schristos ${FDISK} -f -i ${image} 2021.45Schristos ${FDISK} -f -a -u -0 -s 169/-1/-1/ ${image} 2031.45Schristos ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 2041.2Sagcfi 2051.22Schristos 2061.22Schristosif $compress; then 2071.22Schristos echo ${bar} Compressing image ${bar} 2081.23Schristos rm -f "${image}.gz" 2091.44Sast ${GZIP_CMD} -9 ${image} 2101.22Schristos image="${image}.gz" 2111.22Schristosfi 2121.22Schristos 2131.17Schristosecho ${bar} Image is ${image} ${bar} 214