mkimage revision 1.69
11.31Sjmcneill#!/bin/sh 21.69Skre# $NetBSD: mkimage,v 1.69 2017/11/28 02:56:44 kre 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.58Shubertf# 361.58Shubertf# Makes a bootable image for the host architecture given. 371.58Shubertf# The host specific functions are pulled in from a /bin/sh script in the 381.58Shubertf# "conf" directory, and is expected to provide the following shell 391.58Shubertf# functions, which are called in the following order: 401.58Shubertf# 411.58Shubertf# - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}. 421.58Shubertf# If -m is given, a number of directories are put on a tmpfs RAM disk 431.58Shubertf# - customize: After unpacking the sets, this gets the system to 441.58Shubertf# a working state, e. g. by setting up /etc/rc.conf and /dev 451.58Shubertf# - populate: Add common goods like kernel and bootloader 461.58Shubertf# - make_label: Prints disklabel to stdout 471.58Shubertf# 481.58Shubertf 491.33Sjmcneillset -e 501.33Sjmcneill 511.17SchristosDIR="$(cd "$(dirname "$0")" && pwd)" 521.17SchristosPROG="$(basename "$0")" 531.17Schristos 541.66SjmcneillMAKE=${TOOL_MAKE:-make} 551.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel} 561.24SchristosFDISK=${TOOL_FDISK:-fdisk} 571.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs} 581.25SjmcneillMTREE=${TOOL_MTREE:-mtree} 591.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 601.59SchristosMKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 611.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 621.21Schristos 631.17Schristossrc="/usr/src" 641.65Schristossets="base comp etc games man misc modules tests text" 651.17Schristosxsets="xbase xcomp xetc xfont xserver" 661.17Schristosminfree="10%" 671.17Schristosbar="===" 681.17Schristos 691.67Skretmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")" 701.17Schristosmnt="${tmp}/mnt" 711.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" 721.23Schristos 731.17Schristostrap "cleanup" 0 1 2 3 15 741.17Schristos 751.17Schristoscleanup() { 761.17Schristos case "$tmp" in 771.68Skre "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";; 781.17Schristos esac 791.17Schristos} 801.1Sagc 811.69Skrefail() { 821.69Skre IFS=' ' 831.69Skre echo >&2 "${PROG}: $*" 841.69Skre exit 1 851.69Skre} 861.69Skre 871.17Schristosgetsize() { 881.17Schristos set -- $(ls -l $1) 891.17Schristos echo $5 901.1Sagc} 911.1Sagc 921.63Sjmcneillgetsectors() { 931.63Sjmcneill case "$1" in 941.63Sjmcneill *g) 951.63Sjmcneill m=1073741824 961.63Sjmcneill v=${1%g} 971.63Sjmcneill ;; 981.63Sjmcneill *m) 991.63Sjmcneill m=1048576 1001.63Sjmcneill v=${1%m} 1011.63Sjmcneill ;; 1021.63Sjmcneill *k) 1031.63Sjmcneill m=1024 1041.63Sjmcneill v=${1%k} 1051.63Sjmcneill ;; 1061.63Sjmcneill *[0-9b]) 1071.63Sjmcneill m=1 1081.63Sjmcneill v=${1%b} 1091.63Sjmcneill ;; 1101.63Sjmcneill esac 1111.63Sjmcneill echo $((m * v / 512)) 1121.63Sjmcneill} 1131.63Sjmcneill 1141.12Schristosusage() { 1151.12Schristos cat << EOF 1>&2 1161.60SmartinUsage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 1171.39Schristos 1181.39Schristos-b Boot only, no sets loaded 1191.47Schristos-r root device kind (sd, wd, ld) 1201.39Schristos-d Add the debug sets 1211.43Schristos-m Optimize the OS installation to mimimize disk writes for SSDs 1221.57Shubertf-x Load the X sets too, not just the base ones 1231.12SchristosEOF 1241.13Schristos exit 1 1251.12Schristos} 1261.12Schristos 1271.32Sjmcneill# First pass for options to get the host and src directories 1281.60SmartinOPTS="B:D:K:S:bc:dh:mr:s:x" 1291.4Schristoswhile getopts "$OPTS" f 1301.4Schristosdo 1311.4Schristos case $f in 1321.4Schristos h) h="$OPTARG";; 1331.32Sjmcneill S) src="$OPTARG";; 1341.4Schristos *) ;; 1351.4Schristos esac 1361.4Schristosdone 1371.4Schristos 1381.4Schristosif [ -z "$h" ] 1391.4Schristosthen 1401.4Schristos usage 1411.4Schristosfi 1421.4Schristos 1431.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ] 1441.4Schristosthen 1451.5Schristos echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 1461.4Schristos exit 1 1471.4Schristosfi 1481.4Schristos 1491.56Sjmcneillresize=false 1501.56Sjmcneill 1511.5Schristos. "${DIR}/conf/${h}.conf" 1521.45Schristosrelease="/usr/obj/${MACHINE}/release" 1531.4Schristos 1541.17Schristosselected_sets="$sets" 1551.50Sskrlldsets_p=false 1561.50Sskrllxsets_p=false 1571.39Schristosminwrites=false 1581.47Schristosrootdev=ld 1591.60Smartinendian= 1601.17Schristos 1611.4SchristosOPTIND=1 1621.4Schristoswhile getopts "$OPTS" f 1631.4Schristosdo 1641.4Schristos case $f in 1651.60Smartin B) endian="-B $OPTARG";; 1661.18Schristos D) release="$OPTARG";; 1671.18Schristos K) kernel="$OPTARG";; 1681.32Sjmcneill S) ;; 1691.41Schristos b) bootonly=true;; 1701.50Sskrll d) dsets_p=true 1711.39Schristos selected_sets="$selected_sets debug" 1721.50Sskrll if $xsets_p; then 1731.39Schristos selected_sets="$selected_sets xdebug" 1741.39Schristos fi 1751.39Schristos ;; 1761.4Schristos c) custom="$OPTARG";; 1771.4Schristos h) ;; 1781.39Schristos m) minwrites=true;; 1791.47Schristos r) rootdev="$OPTARG";; 1801.4Schristos s) size="$OPTARG";; 1811.50Sskrll x) xsets_p=true 1821.39Schristos selected_sets="$selected_sets $xsets" 1831.50Sskrll if $dsets_p; then 1841.39Schristos selected_sets="$selected_sets xdebug" 1851.39Schristos fi 1861.39Schristos ;; 1871.6Schristos *) usage;; 1881.1Sagc esac 1891.1Sagcdone 1901.1Sagc 1911.20Sjmcneillshift $(( $OPTIND - 1 )) 1921.4Schristosif [ -n "$1" ]; then 1931.1Sagc # take the next argument as being the image name 1941.1Sagc image="$1" 1951.1Sagc shift 1961.1Sagcfi 1971.1Sagc 1981.22Schristoscase "$image" in 1991.22Schristos*.gz) compress=true; image="${image%.gz}";; 2001.22Schristos*) compress=false;; 2011.22Schristosesac 2021.22Schristos 2031.45Schristosif [ -z "${bootonly}" ]; then 2041.36Sgarbled echo ${bar} configuring sets ${bar} 2051.51Sskrll (cat "${release}/etc/mtree/NetBSD.dist" 2061.36Sgarbled for i in $selected_sets; do 2071.36Sgarbled s="${release}/etc/mtree/set.$i" 2081.36Sgarbled if [ -f "$s" ]; then 2091.36Sgarbled cat "$s" 2101.36Sgarbled fi 2111.36Sgarbled done) > "$tmp/selected_sets" 2121.36Sgarbledfi 2131.1Sagc 2141.4Schristosmake_fstab 2151.17Schristoscustomize 2161.17Schristospopulate 2171.1Sagc 2181.45Schristosif [ -n "${msdosid}" ]; then 2191.17Schristos echo ${bar} Populating msdos filesystem ${bar} 2201.63Sjmcneill ${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \ 2211.63Sjmcneill -O $((${init} / 2))m -s $((${boot} / 2))m \ 2221.41Schristos ${image} ${mnt}/boot 2231.1Sagcfi 2241.1Sagc 2251.45Schristosif [ -z "${bootonly}" ]; then 2261.36Sgarbled echo ${bar} Populating ffs filesystem ${bar} 2271.60Smartin ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \ 2281.46Schristos -O ${ffsoffset} \ 2291.61Sjmcneill -o d=4096,f=8192,b=65536 -b $((${extra}))m \ 2301.36Sgarbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 2311.36Sgarbledfi 2321.1Sagc 2331.23Schristosif [ "${size}" = 0 ]; then 2341.23Schristos size="$(getsize "${image}")" 2351.17Schristosfi 2361.23Schristosnewsize=$((${size} / 2 / 1024)) 2371.49Schristoscompare=$((${newsize} * 2 * 1024)) 2381.49Schristoswhile [ "${compare}" != "${size}" ] 2391.49Schristosdo 2401.49Schristos size="$((size + size - compare))" 2411.49Schristos newsize="$((${size} / 2 / 1024))" 2421.49Schristos compare="$((${newsize} * 2 * 1024))" 2431.49Schristosdone 2441.1Sagc 2451.45Schristosif [ -n "${msdosid}" ]; then 2461.17Schristos echo ${bar} Running fdisk ${bar} 2471.30Sjmcneill initsecs=$((${init} * 1024)) 2481.30Sjmcneill bootsecs=$((${boot} * 1024)) 2491.63Sjmcneill ${FDISK} -f -i ${image} 2501.62Sskrll ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 2511.63Sjmcneill if [ -z "${bootonly}" ]; then 2521.63Sjmcneill ffsstart="$(getsectors ${ffsoffset})" 2531.63Sjmcneill imagesize="$(getsize "${image}")" 2541.63Sjmcneill imagesecs="$(getsectors ${imagesize})" 2551.63Sjmcneill ffssize="$(expr ${imagesecs} - ${ffsstart})" 2561.63Sjmcneill ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image} 2571.63Sjmcneill fi 2581.64Sjmcneill 2591.64Sjmcneill echo ${bar} Adding label ${bar} 2601.64Sjmcneill make_label > ${tmp}/label 2611.64Sjmcneill ${DISKLABEL} -R -F ${image} ${tmp}/label 2621.45Schristoselif [ -n "${netbsdid}" ]; then 2631.64Sjmcneill echo ${bar} Adding label ${bar} 2641.64Sjmcneill make_label > ${tmp}/label 2651.64Sjmcneill ${DISKLABEL} -R -F ${image} ${tmp}/label 2661.64Sjmcneill 2671.45Schristos echo ${bar} Running fdisk ${bar} 2681.45Schristos ${FDISK} -f -i ${image} 2691.49Schristos ${FDISK} -f -a -u -0 -s 169/${init} ${image} 2701.45Schristos ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 2711.2Sagcfi 2721.22Schristos 2731.22Schristosif $compress; then 2741.22Schristos echo ${bar} Compressing image ${bar} 2751.23Schristos rm -f "${image}.gz" 2761.44Sast ${GZIP_CMD} -9 ${image} 2771.22Schristos image="${image}.gz" 2781.22Schristosfi 2791.22Schristos 2801.17Schristosecho ${bar} Image is ${image} ${bar} 281