mkimage revision 1.59
11.31Sjmcneill#!/bin/sh 21.59Schristos# $NetBSD: mkimage,v 1.59 2015/05/24 17:08:21 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.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.21SchristosDISKLABEL=${TOOL_DISKLABEL:-disklabel} 551.24SchristosFDISK=${TOOL_FDISK:-fdisk} 561.21SchristosMAKEFS=${TOOL_MAKEFS:-makefs} 571.25SjmcneillMTREE=${TOOL_MTREE:-mtree} 581.45SchristosINSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 591.59SchristosMKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 601.44SastGZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 611.21Schristos 621.17Schristossrc="/usr/src" 631.17Schristossets="base comp etc games man misc modules text" 641.17Schristosxsets="xbase xcomp xetc xfont xserver" 651.17Schristosminfree="10%" 661.17Schristosbar="===" 671.17Schristos 681.17Schristostmp="$(mktemp -d "/tmp/$PROG.XXXXXX")" 691.17Schristosmnt="${tmp}/mnt" 701.46Schristosmkdir -p "${mnt}/etc" "${mnt}/dev" 711.23Schristos 721.17Schristostrap "cleanup" 0 1 2 3 15 731.17Schristos 741.17Schristoscleanup() { 751.17Schristos case "$tmp" in 761.17Schristos /tmp/$PROG.*) rm -fr "$tmp";; 771.17Schristos esac 781.17Schristos} 791.1Sagc 801.17Schristosgetsize() { 811.17Schristos set -- $(ls -l $1) 821.17Schristos echo $5 831.1Sagc} 841.1Sagc 851.12Schristosusage() { 861.12Schristos cat << EOF 1>&2 871.41SchristosUsage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 881.39Schristos 891.39Schristos-b Boot only, no sets loaded 901.47Schristos-r root device kind (sd, wd, ld) 911.39Schristos-d Add the debug sets 921.43Schristos-m Optimize the OS installation to mimimize disk writes for SSDs 931.57Shubertf-x Load the X sets too, not just the base ones 941.12SchristosEOF 951.13Schristos exit 1 961.12Schristos} 971.12Schristos 981.32Sjmcneill# First pass for options to get the host and src directories 991.47SchristosOPTS="K:D:S:bc:dh:mr:s:x" 1001.4Schristoswhile getopts "$OPTS" f 1011.4Schristosdo 1021.4Schristos case $f in 1031.4Schristos h) h="$OPTARG";; 1041.32Sjmcneill S) src="$OPTARG";; 1051.4Schristos *) ;; 1061.4Schristos esac 1071.4Schristosdone 1081.4Schristos 1091.4Schristosif [ -z "$h" ] 1101.4Schristosthen 1111.4Schristos usage 1121.4Schristosfi 1131.4Schristos 1141.5Schristosif [ ! -f "${DIR}/conf/${h}.conf" ] 1151.4Schristosthen 1161.5Schristos echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2 1171.4Schristos exit 1 1181.4Schristosfi 1191.4Schristos 1201.56Sjmcneillresize=false 1211.56Sjmcneill 1221.5Schristos. "${DIR}/conf/${h}.conf" 1231.45Schristosrelease="/usr/obj/${MACHINE}/release" 1241.4Schristos 1251.17Schristosselected_sets="$sets" 1261.50Sskrlldsets_p=false 1271.50Sskrllxsets_p=false 1281.39Schristosminwrites=false 1291.47Schristosrootdev=ld 1301.17Schristos 1311.4SchristosOPTIND=1 1321.4Schristoswhile getopts "$OPTS" f 1331.4Schristosdo 1341.4Schristos case $f in 1351.18Schristos D) release="$OPTARG";; 1361.18Schristos K) kernel="$OPTARG";; 1371.32Sjmcneill S) ;; 1381.41Schristos b) bootonly=true;; 1391.50Sskrll d) dsets_p=true 1401.39Schristos selected_sets="$selected_sets debug" 1411.50Sskrll if $xsets_p; then 1421.39Schristos selected_sets="$selected_sets xdebug" 1431.39Schristos fi 1441.39Schristos ;; 1451.4Schristos c) custom="$OPTARG";; 1461.4Schristos h) ;; 1471.39Schristos m) minwrites=true;; 1481.47Schristos r) rootdev="$OPTARG";; 1491.4Schristos s) size="$OPTARG";; 1501.50Sskrll x) xsets_p=true 1511.39Schristos selected_sets="$selected_sets $xsets" 1521.50Sskrll if $dsets_p; then 1531.39Schristos selected_sets="$selected_sets xdebug" 1541.39Schristos fi 1551.39Schristos ;; 1561.6Schristos *) usage;; 1571.1Sagc esac 1581.1Sagcdone 1591.1Sagc 1601.20Sjmcneillshift $(( $OPTIND - 1 )) 1611.4Schristosif [ -n "$1" ]; then 1621.1Sagc # take the next argument as being the image name 1631.1Sagc image="$1" 1641.1Sagc shift 1651.1Sagcfi 1661.1Sagc 1671.22Schristoscase "$image" in 1681.22Schristos*.gz) compress=true; image="${image%.gz}";; 1691.22Schristos*) compress=false;; 1701.22Schristosesac 1711.22Schristos 1721.45Schristosif [ -z "${bootonly}" ]; then 1731.36Sgarbled echo ${bar} configuring sets ${bar} 1741.51Sskrll (cat "${release}/etc/mtree/NetBSD.dist" 1751.36Sgarbled for i in $selected_sets; do 1761.36Sgarbled s="${release}/etc/mtree/set.$i" 1771.36Sgarbled if [ -f "$s" ]; then 1781.36Sgarbled cat "$s" 1791.36Sgarbled fi 1801.36Sgarbled done) > "$tmp/selected_sets" 1811.36Sgarbledfi 1821.1Sagc 1831.4Schristosmake_fstab 1841.17Schristoscustomize 1851.17Schristospopulate 1861.1Sagc 1871.45Schristosif [ -n "${msdosid}" ]; then 1881.17Schristos echo ${bar} Populating msdos filesystem ${bar} 1891.27Schristos ${MAKEFS} -N ${release}/etc -t msdos \ 1901.41Schristos -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \ 1911.41Schristos ${image} ${mnt}/boot 1921.1Sagcfi 1931.1Sagc 1941.45Schristosif [ -z "${bootonly}" ]; then 1951.36Sgarbled echo ${bar} Populating ffs filesystem ${bar} 1961.52Sskrll ${MAKEFS} -r -N ${release}/etc -t ffs -rx \ 1971.46Schristos -O ${ffsoffset} \ 1981.54Sjmcneill -o d=4096,f=2048,b=16384 -b $((${extra}))m \ 1991.36Sgarbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 2001.36Sgarbledfi 2011.1Sagc 2021.23Schristosif [ "${size}" = 0 ]; then 2031.23Schristos size="$(getsize "${image}")" 2041.17Schristosfi 2051.23Schristosnewsize=$((${size} / 2 / 1024)) 2061.49Schristoscompare=$((${newsize} * 2 * 1024)) 2071.49Schristoswhile [ "${compare}" != "${size}" ] 2081.49Schristosdo 2091.49Schristos size="$((size + size - compare))" 2101.49Schristos newsize="$((${size} / 2 / 1024))" 2111.49Schristos compare="$((${newsize} * 2 * 1024))" 2121.49Schristosdone 2131.1Sagc 2141.17Schristosecho ${bar} Adding label ${bar} 2151.17Schristosmake_label > ${tmp}/label 2161.21Schristos${DISKLABEL} -R -F ${image} ${tmp}/label 2171.45Schristosif [ -n "${msdosid}" ]; then 2181.17Schristos echo ${bar} Running fdisk ${bar} 2191.30Sjmcneill initsecs=$((${init} * 1024)) 2201.30Sjmcneill bootsecs=$((${boot} * 1024)) 2211.30Sjmcneill ${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 2221.45Schristoselif [ -n "${netbsdid}" ]; then 2231.45Schristos echo ${bar} Running fdisk ${bar} 2241.45Schristos ${FDISK} -f -i ${image} 2251.49Schristos ${FDISK} -f -a -u -0 -s 169/${init} ${image} 2261.45Schristos ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 2271.2Sagcfi 2281.22Schristos 2291.22Schristosif $compress; then 2301.22Schristos echo ${bar} Compressing image ${bar} 2311.23Schristos rm -f "${image}.gz" 2321.44Sast ${GZIP_CMD} -9 ${image} 2331.22Schristos image="${image}.gz" 2341.22Schristosfi 2351.22Schristos 2361.17Schristosecho ${bar} Image is ${image} ${bar} 237