mkimage revision 1.44
11.31Sjmcneill#!/bin/sh 21.44Sast# $NetBSD: mkimage,v 1.44 2013/09/14 12:51:27 ast Exp $ 31.13Schristos# 41.17Schristos# Copyright (c) 2013 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.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 451.21Schristos 461.17Schristossrc="/usr/src" 471.17Schristosrelease="/usr/obj/evbarm/release" 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.4Schristos 1061.17Schristosselected_sets="$sets" 1071.39Schristosdsets=false 1081.39Schristosxsets=false 1091.39Schristosminwrites=false 1101.17Schristos 1111.4SchristosOPTIND=1 1121.4Schristoswhile getopts "$OPTS" f 1131.4Schristosdo 1141.4Schristos case $f in 1151.18Schristos D) release="$OPTARG";; 1161.18Schristos K) kernel="$OPTARG";; 1171.32Sjmcneill S) ;; 1181.41Schristos b) bootonly=true;; 1191.39Schristos d) dsets=true 1201.39Schristos selected_sets="$selected_sets debug" 1211.39Schristos if $xsets; then 1221.39Schristos selected_sets="$selected_sets xdebug" 1231.39Schristos fi 1241.39Schristos ;; 1251.4Schristos c) custom="$OPTARG";; 1261.4Schristos h) ;; 1271.39Schristos m) minwrites=true;; 1281.4Schristos s) size="$OPTARG";; 1291.39Schristos x) xsets=true 1301.39Schristos selected_sets="$selected_sets $xsets" 1311.39Schristos if $dsets; then 1321.39Schristos selected_sets="$selected_sets xdebug" 1331.39Schristos fi 1341.39Schristos ;; 1351.6Schristos *) usage;; 1361.1Sagc esac 1371.1Sagcdone 1381.1Sagc 1391.20Sjmcneillshift $(( $OPTIND - 1 )) 1401.4Schristosif [ -n "$1" ]; then 1411.1Sagc # take the next argument as being the image name 1421.1Sagc image="$1" 1431.1Sagc shift 1441.1Sagcfi 1451.1Sagc 1461.22Schristoscase "$image" in 1471.22Schristos*.gz) compress=true; image="${image%.gz}";; 1481.22Schristos*) compress=false;; 1491.22Schristosesac 1501.22Schristos 1511.36Sgarbledif [ -z "$bootonly" ]; then 1521.36Sgarbled echo ${bar} configuring sets ${bar} 1531.36Sgarbled (echo '/set type=dir uname=root gname=wheel mode=0755' 1541.36Sgarbled for i in $selected_sets; do 1551.36Sgarbled s="${release}/etc/mtree/set.$i" 1561.36Sgarbled if [ -f "$s" ]; then 1571.36Sgarbled cat "$s" 1581.36Sgarbled fi 1591.36Sgarbled done) > "$tmp/selected_sets" 1601.36Sgarbledfi 1611.1Sagc 1621.4Schristosmake_fstab 1631.17Schristoscustomize 1641.17Schristospopulate 1651.1Sagc 1661.36Sgarbledif [ -z "$bootonly" ]; then 1671.36Sgarbled (cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 1681.36Sgarbled ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets" 1691.36Sgarbledfi 1701.17Schristosif [ -n ${msdosid} ]; then 1711.17Schristos echo ${bar} Populating msdos filesystem ${bar} 1721.27Schristos ${MAKEFS} -N ${release}/etc -t msdos \ 1731.41Schristos -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 1741.41Schristos ${image} ${mnt}/boot 1751.1Sagcfi 1761.1Sagc 1771.36Sgarbledif [ -z "$bootonly" ]; then 1781.36Sgarbled echo ${bar} Populating ffs filesystem ${bar} 1791.36Sgarbled ${MAKEFS} -N ${release}/etc -t ffs -rx \ 1801.36Sgarbled -O $(((${init} + ${boot} + ${swap}) / 2))m \ 1811.41Schristos -o d=4096 -b $((${extra}))m \ 1821.36Sgarbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 1831.36Sgarbledfi 1841.1Sagc 1851.23Schristosif [ "${size}" = 0 ]; then 1861.23Schristos size="$(getsize "${image}")" 1871.17Schristosfi 1881.23Schristosnewsize=$((${size} / 2 / 1024)) 1891.1Sagc 1901.17Schristosecho ${bar} Adding label ${bar} 1911.17Schristosmake_label > ${tmp}/label 1921.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label 1931.17Schristosif [ -n ${msdosid} ]; then 1941.17Schristos echo ${bar} Running fdisk ${bar} 1951.30Sjmcneill initsecs=$((${init} * 1024)) 1961.30Sjmcneill bootsecs=$((${boot} * 1024)) 1971.30Sjmcneill ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 1981.2Sagcfi 1991.22Schristos 2001.22Schristosif $compress; then 2011.22Schristos echo ${bar} Compressing image ${bar} 2021.23Schristos rm -f "${image}.gz" 2031.44Sast ${GZIP_CMD} -9 ${image} 2041.22Schristos image="${image}.gz" 2051.22Schristosfi 2061.22Schristos 2071.17Schristosecho ${bar} Image is ${image} ${bar} 208