xref: /freebsd/tools/tools/nanobsd/defaults.sh (revision 1934c7aa0a3412c404499a8bf57714a01bba7d92)
16b8c8deeSWill Andrews#!/bin/sh
26b8c8deeSWill Andrews#
36b8c8deeSWill Andrews# Copyright (c) 2005 Poul-Henning Kamp.
46b8c8deeSWill Andrews# All rights reserved.
56b8c8deeSWill Andrews#
66b8c8deeSWill Andrews# Redistribution and use in source and binary forms, with or without
76b8c8deeSWill Andrews# modification, are permitted provided that the following conditions
86b8c8deeSWill Andrews# are met:
96b8c8deeSWill Andrews# 1. Redistributions of source code must retain the above copyright
106b8c8deeSWill Andrews#    notice, this list of conditions and the following disclaimer.
116b8c8deeSWill Andrews# 2. Redistributions in binary form must reproduce the above copyright
126b8c8deeSWill Andrews#    notice, this list of conditions and the following disclaimer in the
136b8c8deeSWill Andrews#    documentation and/or other materials provided with the distribution.
146b8c8deeSWill Andrews#
156b8c8deeSWill Andrews# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
166b8c8deeSWill Andrews# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176b8c8deeSWill Andrews# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
186b8c8deeSWill Andrews# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
196b8c8deeSWill Andrews# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206b8c8deeSWill Andrews# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
216b8c8deeSWill Andrews# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226b8c8deeSWill Andrews# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236b8c8deeSWill Andrews# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246b8c8deeSWill Andrews# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256b8c8deeSWill Andrews# SUCH DAMAGE.
266b8c8deeSWill Andrews#
276b8c8deeSWill Andrews# $FreeBSD$
286b8c8deeSWill Andrews#
296b8c8deeSWill Andrews
306b8c8deeSWill Andrewsset -e
316b8c8deeSWill Andrews
326b8c8deeSWill Andrews#######################################################################
336b8c8deeSWill Andrews#
346b8c8deeSWill Andrews# Setup default values for all controlling variables.
356b8c8deeSWill Andrews# These values can be overridden from the config file(s)
366b8c8deeSWill Andrews#
376b8c8deeSWill Andrews#######################################################################
386b8c8deeSWill Andrews
396b8c8deeSWill Andrews# Name of this NanoBSD build.  (Used to construct workdir names)
406b8c8deeSWill AndrewsNANO_NAME=full
416b8c8deeSWill Andrews
426b8c8deeSWill Andrews# Source tree directory
436b8c8deeSWill AndrewsNANO_SRC=/usr/src
446b8c8deeSWill Andrews
456b8c8deeSWill Andrews# Where nanobsd additional files live under the source tree
466b8c8deeSWill AndrewsNANO_TOOLS=tools/tools/nanobsd
476b8c8deeSWill Andrews
4818803951SWarner Losh# Where cust_pkgng() finds packages to install
496b8c8deeSWill AndrewsNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
506b8c8deeSWill AndrewsNANO_PACKAGE_LIST="*"
516b8c8deeSWill Andrews
526b8c8deeSWill Andrews# where package metadata gets placed
536b8c8deeSWill AndrewsNANO_PKG_META_BASE=/var/db
546b8c8deeSWill Andrews
556b8c8deeSWill Andrews# Object tree directory
566b8c8deeSWill Andrews# default is subdir of /usr/obj
576b8c8deeSWill Andrews#NANO_OBJ=""
586b8c8deeSWill Andrews
596b8c8deeSWill Andrews# The directory to put the final images
606b8c8deeSWill Andrews# default is ${NANO_OBJ}
616b8c8deeSWill Andrews#NANO_DISKIMGDIR=""
626b8c8deeSWill Andrews
636b8c8deeSWill Andrews# Make & parallel Make
646b8c8deeSWill AndrewsNANO_MAKE="make"
656b8c8deeSWill AndrewsNANO_PMAKE="make -j 3"
666b8c8deeSWill Andrews
676b8c8deeSWill Andrews# The default name for any image we create.
686b8c8deeSWill AndrewsNANO_IMGNAME="_.disk.full"
696b8c8deeSWill Andrews
706b8c8deeSWill Andrews# Options to put in make.conf during buildworld only
716b8c8deeSWill AndrewsCONF_BUILD=' '
726b8c8deeSWill Andrews
736b8c8deeSWill Andrews# Options to put in make.conf during installworld only
746b8c8deeSWill AndrewsCONF_INSTALL=' '
756b8c8deeSWill Andrews
766b8c8deeSWill Andrews# Options to put in make.conf during both build- & installworld.
776b8c8deeSWill AndrewsCONF_WORLD=' '
786b8c8deeSWill Andrews
796b8c8deeSWill Andrews# Kernel config file to use
806b8c8deeSWill AndrewsNANO_KERNEL=GENERIC
816b8c8deeSWill Andrews
826b8c8deeSWill Andrews# Kernel modules to install. If empty, no modules are installed.
836b8c8deeSWill Andrews# Use "default" to install all built modules.
846b8c8deeSWill AndrewsNANO_MODULES=
856b8c8deeSWill Andrews
866b8c8deeSWill Andrews# Customize commands.
876b8c8deeSWill AndrewsNANO_CUSTOMIZE=""
886b8c8deeSWill Andrews
896b8c8deeSWill Andrews# Late customize commands.
906b8c8deeSWill AndrewsNANO_LATE_CUSTOMIZE=""
916b8c8deeSWill Andrews
926b8c8deeSWill Andrews# Newfs paramters to use
936b8c8deeSWill AndrewsNANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
946b8c8deeSWill Andrews
956b8c8deeSWill Andrews# The drive name of the media at runtime
966b8c8deeSWill AndrewsNANO_DRIVE=ad0
976b8c8deeSWill Andrews
986b8c8deeSWill Andrews# Target media size in 512 bytes sectors
996b8c8deeSWill AndrewsNANO_MEDIASIZE=2000000
1006b8c8deeSWill Andrews
1016b8c8deeSWill Andrews# Number of code images on media (1 or 2)
1026b8c8deeSWill AndrewsNANO_IMAGES=2
1036b8c8deeSWill Andrews
1046b8c8deeSWill Andrews# 0 -> Leave second image all zeroes so it compresses better.
1056b8c8deeSWill Andrews# 1 -> Initialize second image with a copy of the first
1066b8c8deeSWill AndrewsNANO_INIT_IMG2=1
1076b8c8deeSWill Andrews
1086b8c8deeSWill Andrews# Size of code file system in 512 bytes sectors
1096b8c8deeSWill Andrews# If zero, size will be as large as possible.
1106b8c8deeSWill AndrewsNANO_CODESIZE=0
1116b8c8deeSWill Andrews
1126b8c8deeSWill Andrews# Size of configuration file system in 512 bytes sectors
1136b8c8deeSWill Andrews# Cannot be zero.
1146b8c8deeSWill AndrewsNANO_CONFSIZE=2048
1156b8c8deeSWill Andrews
1166b8c8deeSWill Andrews# Size of data file system in 512 bytes sectors
1176b8c8deeSWill Andrews# If zero: no partition configured.
1186b8c8deeSWill Andrews# If negative: max size possible
1196b8c8deeSWill AndrewsNANO_DATASIZE=0
1206b8c8deeSWill Andrews
1216b8c8deeSWill Andrews# Size of the /etc ramdisk in 512 bytes sectors
1226b8c8deeSWill AndrewsNANO_RAM_ETCSIZE=10240
1236b8c8deeSWill Andrews
1246b8c8deeSWill Andrews# Size of the /tmp+/var ramdisk in 512 bytes sectors
1256b8c8deeSWill AndrewsNANO_RAM_TMPVARSIZE=10240
1266b8c8deeSWill Andrews
1276b8c8deeSWill Andrews# Media geometry, only relevant if bios doesn't understand LBA.
1286b8c8deeSWill AndrewsNANO_SECTS=63
1296b8c8deeSWill AndrewsNANO_HEADS=16
1306b8c8deeSWill Andrews
1316b8c8deeSWill Andrews# boot0 flags/options and configuration
1326b8c8deeSWill AndrewsNANO_BOOT0CFG="-o packet -s 1 -m 3"
1336b8c8deeSWill AndrewsNANO_BOOTLOADER="boot/boot0sio"
1346b8c8deeSWill Andrews
1356b8c8deeSWill Andrews# boot2 flags/options
1366b8c8deeSWill Andrews# default force serial console
1376b8c8deeSWill AndrewsNANO_BOOT2CFG="-h"
1386b8c8deeSWill Andrews
1396b8c8deeSWill Andrews# Backing type of md(4) device
1406b8c8deeSWill Andrews# Can be "file" or "swap"
1416b8c8deeSWill AndrewsNANO_MD_BACKING="file"
1426b8c8deeSWill Andrews
1436b8c8deeSWill Andrews# for swap type md(4) backing, write out the mbr only
1446b8c8deeSWill AndrewsNANO_IMAGE_MBRONLY=true
1456b8c8deeSWill Andrews
1466b8c8deeSWill Andrews# Progress Print level
1476b8c8deeSWill AndrewsPPLEVEL=3
1486b8c8deeSWill Andrews
1496b8c8deeSWill Andrews# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
1506b8c8deeSWill Andrews# in preference to /dev/${NANO_DRIVE}
1516b8c8deeSWill Andrews# Root partition will be ${NANO_LABEL}s{1,2}
1526b8c8deeSWill Andrews# /cfg partition will be ${NANO_LABEL}s3
1536b8c8deeSWill Andrews# /data partition will be ${NANO_LABEL}s4
1546b8c8deeSWill AndrewsNANO_LABEL=""
1557b77d3eaSWarner LoshNANO_SLICE_ROOT=s1
1567b77d3eaSWarner LoshNANO_SLICE_ALTROOT=s2
1577b77d3eaSWarner LoshNANO_SLICE_CFG=s3
1587b77d3eaSWarner LoshNANO_SLICE_DATA=s4
1597b77d3eaSWarner Losh
1606b8c8deeSWill Andrews
1616b8c8deeSWill Andrews#######################################################################
1626b8c8deeSWill Andrews# Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
1636b8c8deeSWill Andrews# Unfortunately, there's no way to set TARGET at this time, and it
164d07833f9SWarner Losh# conflates the two, so architectures where TARGET != TARGET_ARCH and
165d07833f9SWarner Losh# TARGET can't be guessed from TARGET_ARCH do not work.  This defaults
166d07833f9SWarner Losh# to the arch of the current machine.
1676b8c8deeSWill Andrews
1686b8c8deeSWill AndrewsNANO_ARCH=`uname -p`
1696b8c8deeSWill Andrews
170d07833f9SWarner Losh# CPUTYPE defaults to "" which is the default when CPUTYPE isn't
171d07833f9SWarner Losh# defined.
172d07833f9SWarner LoshNANO_CPUTYPE=""
173d07833f9SWarner Losh
1746b8c8deeSWill Andrews# Directory to populate /cfg from
1756b8c8deeSWill AndrewsNANO_CFGDIR=""
1766b8c8deeSWill Andrews
1776b8c8deeSWill Andrews# Directory to populate /data from
1786b8c8deeSWill AndrewsNANO_DATADIR=""
1796b8c8deeSWill Andrews
1806b8c8deeSWill Andrews# src.conf to use when building the image. Defaults to /dev/null for the sake
1816b8c8deeSWill Andrews# of determinism.
1826b8c8deeSWill AndrewsSRCCONF=${SRCCONF:=/dev/null}
1836b8c8deeSWill Andrews
1846b8c8deeSWill Andrews#######################################################################
1856b8c8deeSWill Andrews#
1866b8c8deeSWill Andrews# The functions which do the real work.
1876b8c8deeSWill Andrews# Can be overridden from the config file(s)
1886b8c8deeSWill Andrews#
1896b8c8deeSWill Andrews#######################################################################
1906b8c8deeSWill Andrews
191*1934c7aaSWarner Losh# Export values into the shell. Must use { } instead of ( ) like
192*1934c7aaSWarner Losh# other functions to avoid a subshell.
193*1934c7aaSWarner Losh# We set __MAKE_CONF as a global since it is easier to get quoting
194*1934c7aaSWarner Losh# right for paths with spaces in them.
195*1934c7aaSWarner Loshmake_export ( ) {
196*1934c7aaSWarner Losh	# Similar to export_var, except puts the data out to stdout
197*1934c7aaSWarner Losh	var=$1
198*1934c7aaSWarner Losh	eval val=\$$var
199*1934c7aaSWarner Losh	echo "Setting variable: $var=\"$val\""
200*1934c7aaSWarner Losh	export $1
201*1934c7aaSWarner Losh}
202*1934c7aaSWarner Losh
203*1934c7aaSWarner Loshnano_make_build_env ( ) {
204*1934c7aaSWarner Losh	__MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
205*1934c7aaSWarner Losh	make_export __MAKE_CONF
206*1934c7aaSWarner Losh}
207*1934c7aaSWarner Losh
208*1934c7aaSWarner Loshnano_make_install_env ( ) {
209*1934c7aaSWarner Losh	__MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
210*1934c7aaSWarner Losh	make_export __MAKE_CONF
211*1934c7aaSWarner Losh}
212*1934c7aaSWarner Losh
213*1934c7aaSWarner Losh# Extra environment variables for kernel builds
214*1934c7aaSWarner Loshnano_make_kernel_env ( ) {
215*1934c7aaSWarner Losh	if [ -f ${NANO_KERNEL} ] ; then
216*1934c7aaSWarner Losh		KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
217*1934c7aaSWarner Losh		KERNCONF="$(basename ${NANO_KERNEL})"
218*1934c7aaSWarner Losh		make_export KERNCONFDIR
219*1934c7aaSWarner Losh		make_export KERNCONF
220*1934c7aaSWarner Losh	else
221*1934c7aaSWarner Losh		export KERNCONF="${NANO_KERNEL}"
222*1934c7aaSWarner Losh		make_export KERNCONF
223*1934c7aaSWarner Losh	fi
224*1934c7aaSWarner Losh}
225*1934c7aaSWarner Losh
226*1934c7aaSWarner Loshnano_global_make_env ( ) (
227*1934c7aaSWarner Losh	[ ! -z "${NANO_ARCH}" ] && echo TARGET_ARCH="${NANO_ARCH}"
228*1934c7aaSWarner Losh	[ ! -z "${NANO_CPUTYPE}" ] && echo TARGET_CPUTYPE="${NANO_CPUTYPE}"
229*1934c7aaSWarner Losh	echo SRCCONF=${SRCCONF}
230*1934c7aaSWarner Losh)
231*1934c7aaSWarner Losh
2326b8c8deeSWill Andrews# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
2336b8c8deeSWill Andrews# hosts for now.
2346b8c8deeSWill Andrewsnano_rm ( ) {
2356b8c8deeSWill Andrews	case $(uname -r) in
2366b8c8deeSWill Andrews	7*|8*|9*) rm $* ;;
2376b8c8deeSWill Andrews	*) rm -x $* ;;
2386b8c8deeSWill Andrews	esac
2396b8c8deeSWill Andrews}
2406b8c8deeSWill Andrews
2416b8c8deeSWill Andrews# run in the world chroot, errors fatal
242*1934c7aaSWarner LoshCR ( ) {
243*1934c7aaSWarner Losh	chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
2446b8c8deeSWill Andrews}
2456b8c8deeSWill Andrews
2466b8c8deeSWill Andrews# run in the world chroot, errors not fatal
247*1934c7aaSWarner LoshCR0 ( ) {
248*1934c7aaSWarner Losh	chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
2496b8c8deeSWill Andrews}
2506b8c8deeSWill Andrews
2516b8c8deeSWill Andrewsnano_cleanup ( ) (
2526b8c8deeSWill Andrews	if [ $? -ne 0 ]; then
2536b8c8deeSWill Andrews		echo "Error encountered.  Check for errors in last log file." 1>&2
2546b8c8deeSWill Andrews	fi
2556b8c8deeSWill Andrews	exit $?
2566b8c8deeSWill Andrews)
2576b8c8deeSWill Andrews
2586b8c8deeSWill Andrewsclean_build ( ) (
2596b8c8deeSWill Andrews	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
2606b8c8deeSWill Andrews
2616b8c8deeSWill Andrews	if ! nano_rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
2626b8c8deeSWill Andrews		chflags -R noschg ${MAKEOBJDIRPREFIX}/
2636b8c8deeSWill Andrews		nano_rm -r ${MAKEOBJDIRPREFIX}/
2646b8c8deeSWill Andrews	fi
2656b8c8deeSWill Andrews)
2666b8c8deeSWill Andrews
2676b8c8deeSWill Andrewsmake_conf_build ( ) (
2686b8c8deeSWill Andrews	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
2696b8c8deeSWill Andrews
2706b8c8deeSWill Andrews	mkdir -p ${MAKEOBJDIRPREFIX}
2716b8c8deeSWill Andrews	printenv > ${MAKEOBJDIRPREFIX}/_.env
2726b8c8deeSWill Andrews
273*1934c7aaSWarner Losh	# Make sure we get all the global settings that NanoBSD wants
274*1934c7aaSWarner Losh	# in addition to the user's global settings
275*1934c7aaSWarner Losh	(
276*1934c7aaSWarner Losh	nano_global_make_env
277*1934c7aaSWarner Losh	echo "${CONF_WORLD}"
278*1934c7aaSWarner Losh	echo "${CONF_BUILD}"
279*1934c7aaSWarner Losh	) > ${NANO_MAKE_CONF_BUILD}
2806b8c8deeSWill Andrews)
2816b8c8deeSWill Andrews
2826b8c8deeSWill Andrewsbuild_world ( ) (
2836b8c8deeSWill Andrews	pprint 2 "run buildworld"
2846b8c8deeSWill Andrews	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
2856b8c8deeSWill Andrews
286*1934c7aaSWarner Losh	(
287*1934c7aaSWarner Losh	nano_make_build_env
288*1934c7aaSWarner Losh	set -o xtrace
289*1934c7aaSWarner Losh	cd "${NANO_SRC}"
290*1934c7aaSWarner Losh	${NANO_PMAKE} buildworld
291*1934c7aaSWarner Losh	) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
2926b8c8deeSWill Andrews)
2936b8c8deeSWill Andrews
2946b8c8deeSWill Andrewsbuild_kernel ( ) (
2956b8c8deeSWill Andrews	local extra
2966b8c8deeSWill Andrews
2976b8c8deeSWill Andrews	pprint 2 "build kernel ($NANO_KERNEL)"
2986b8c8deeSWill Andrews	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
2996b8c8deeSWill Andrews
3006b8c8deeSWill Andrews	(
301*1934c7aaSWarner Losh	nano_make_build_env
302*1934c7aaSWarner Losh	nano_make_kernel_env
3036b8c8deeSWill Andrews
3046b8c8deeSWill Andrews	# Note: We intentionally build all modules, not only the ones in
3056b8c8deeSWill Andrews	# NANO_MODULES so the built world can be reused by multiple images.
306*1934c7aaSWarner Losh	# Although MODULES_OVERRIDE can be defined in the kenrel config
307*1934c7aaSWarner Losh	# file to override this behavior. Just set NANO_MODULES=default.
308*1934c7aaSWarner Losh	set -o xtrace
309*1934c7aaSWarner Losh	cd "${NANO_SRC}"
310*1934c7aaSWarner Losh	${NANO_PMAKE} buildkernel
3116b8c8deeSWill Andrews	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
3126b8c8deeSWill Andrews)
3136b8c8deeSWill Andrews
3146b8c8deeSWill Andrewsclean_world ( ) (
3156b8c8deeSWill Andrews	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
3166b8c8deeSWill Andrews		pprint 2 "Clean and create object directory (${NANO_OBJ})"
3176b8c8deeSWill Andrews		if ! nano_rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
3186b8c8deeSWill Andrews			chflags -R noschg ${NANO_OBJ}
3196b8c8deeSWill Andrews			nano_rm -r ${NANO_OBJ}/
3206b8c8deeSWill Andrews		fi
321*1934c7aaSWarner Losh		mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
3226b8c8deeSWill Andrews		printenv > ${NANO_OBJ}/_.env
3236b8c8deeSWill Andrews	else
3246b8c8deeSWill Andrews		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
325*1934c7aaSWarner Losh		if ! nano_rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
326*1934c7aaSWarner Losh			chflags -R noschg "${NANO_WORLDDIR}"
327*1934c7aaSWarner Losh			nano_rm -rf "${NANO_WORLDDIR}/"
3286b8c8deeSWill Andrews		fi
329*1934c7aaSWarner Losh		mkdir -p "${NANO_WORLDDIR}"
3306b8c8deeSWill Andrews	fi
3316b8c8deeSWill Andrews)
3326b8c8deeSWill Andrews
3336b8c8deeSWill Andrewsmake_conf_install ( ) (
3346b8c8deeSWill Andrews	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
3356b8c8deeSWill Andrews
336*1934c7aaSWarner Losh	# Make sure we get all the global settings that NanoBSD wants
337*1934c7aaSWarner Losh	# in addition to the user's global settings
338*1934c7aaSWarner Losh	(
339*1934c7aaSWarner Losh	nano_global_make_env
340*1934c7aaSWarner Losh	echo "${CONF_WORLD}"
341*1934c7aaSWarner Losh	echo "${CONF_INSTALL}"
342*1934c7aaSWarner Losh	) >  ${NANO_MAKE_CONF_INSTALL}
3436b8c8deeSWill Andrews)
3446b8c8deeSWill Andrews
3456b8c8deeSWill Andrewsinstall_world ( ) (
3466b8c8deeSWill Andrews	pprint 2 "installworld"
3476b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.iw"
3486b8c8deeSWill Andrews
349*1934c7aaSWarner Losh	(
350*1934c7aaSWarner Losh	nano_make_install_env
351*1934c7aaSWarner Losh	set -o xtrace
352*1934c7aaSWarner Losh	cd "${NANO_SRC}"
353*1934c7aaSWarner Losh	${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}"
354*1934c7aaSWarner Losh	chflags -R noschg "${NANO_WORLDDIR}"
355*1934c7aaSWarner Losh	) > ${NANO_OBJ}/_.iw 2>&1
3566b8c8deeSWill Andrews)
3576b8c8deeSWill Andrews
3586b8c8deeSWill Andrewsinstall_etc ( ) (
3596b8c8deeSWill Andrews
3606b8c8deeSWill Andrews	pprint 2 "install /etc"
3616b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.etc"
3626b8c8deeSWill Andrews
363*1934c7aaSWarner Losh	(
364*1934c7aaSWarner Losh	nano_make_install_env
365*1934c7aaSWarner Losh	set -o xtrace
366*1934c7aaSWarner Losh	cd "${NANO_SRC}"
367*1934c7aaSWarner Losh	${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}"
3686b8c8deeSWill Andrews	# make.conf doesn't get created by default, but some ports need it
3696b8c8deeSWill Andrews	# so they can spam it.
370*1934c7aaSWarner Losh	cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
371*1934c7aaSWarner Losh	) > ${NANO_OBJ}/_.etc 2>&1
3726b8c8deeSWill Andrews)
3736b8c8deeSWill Andrews
3746b8c8deeSWill Andrewsinstall_kernel ( ) (
3756b8c8deeSWill Andrews	local extra
3766b8c8deeSWill Andrews
3776b8c8deeSWill Andrews	pprint 2 "install kernel ($NANO_KERNEL)"
3786b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.ik"
3796b8c8deeSWill Andrews
3806b8c8deeSWill Andrews	(
3816b8c8deeSWill Andrews
382*1934c7aaSWarner Losh	nano_make_install_env
383*1934c7aaSWarner Losh	nano_make_kernel_env
384*1934c7aaSWarner Losh
3856b8c8deeSWill Andrews	if [ "${NANO_MODULES}" != "default" ]; then
386*1934c7aaSWarner Losh		MODULES_OVERRIDE="${NANO_MODULES}"
387*1934c7aaSWarner Losh		make_export MODULES_OVERRIDE
3886b8c8deeSWill Andrews	fi
3896b8c8deeSWill Andrews
390*1934c7aaSWarner Losh	set -o xtrace
391*1934c7aaSWarner Losh	cd "${NANO_SRC}"
392*1934c7aaSWarner Losh	${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}"
393*1934c7aaSWarner Losh
3946b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.ik 2>&1
3956b8c8deeSWill Andrews)
3966b8c8deeSWill Andrews
3976b8c8deeSWill Andrewsnative_xtools ( ) (
3986b8c8deeSWill Andrews	print 2 "Installing the optimized native build tools for cross env"
3996b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
4006b8c8deeSWill Andrews
401*1934c7aaSWarner Losh	(
402*1934c7aaSWarner Losh
403*1934c7aaSWarner Losh	nano_make_install_env
404*1934c7aaSWarner Losh	set -o xtrace
405*1934c7aaSWarner Losh	cd "${NANO_SRC}"
406*1934c7aaSWarner Losh	${NANO_MAKE} native-xtools DESTDIR="${NANO_WORLDDIR}"
407*1934c7aaSWarner Losh
408*1934c7aaSWarner Losh	) > ${NANO_OBJ}/_.native_xtools 2>&1
4096b8c8deeSWill Andrews)
4106b8c8deeSWill Andrews
4116b8c8deeSWill Andrewsrun_customize ( ) (
4126b8c8deeSWill Andrews
4136b8c8deeSWill Andrews	pprint 2 "run customize scripts"
4146b8c8deeSWill Andrews	for c in $NANO_CUSTOMIZE
4156b8c8deeSWill Andrews	do
4166b8c8deeSWill Andrews		pprint 2 "customize \"$c\""
4176b8c8deeSWill Andrews		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
4186b8c8deeSWill Andrews		pprint 4 "`type $c`"
4196b8c8deeSWill Andrews		( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
4206b8c8deeSWill Andrews	done
4216b8c8deeSWill Andrews)
4226b8c8deeSWill Andrews
4236b8c8deeSWill Andrewsrun_late_customize ( ) (
4246b8c8deeSWill Andrews
4256b8c8deeSWill Andrews	pprint 2 "run late customize scripts"
4266b8c8deeSWill Andrews	for c in $NANO_LATE_CUSTOMIZE
4276b8c8deeSWill Andrews	do
4286b8c8deeSWill Andrews		pprint 2 "late customize \"$c\""
4296b8c8deeSWill Andrews		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
4306b8c8deeSWill Andrews		pprint 4 "`type $c`"
4316b8c8deeSWill Andrews		( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
4326b8c8deeSWill Andrews	done
4336b8c8deeSWill Andrews)
4346b8c8deeSWill Andrews
4356b8c8deeSWill Andrewssetup_nanobsd ( ) (
4366b8c8deeSWill Andrews	pprint 2 "configure nanobsd setup"
4376b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.dl"
4386b8c8deeSWill Andrews
4396b8c8deeSWill Andrews	(
440*1934c7aaSWarner Losh	cd "${NANO_WORLDDIR}"
4416b8c8deeSWill Andrews
4426b8c8deeSWill Andrews	# Move /usr/local/etc to /etc/local so that the /cfg stuff
4436b8c8deeSWill Andrews	# can stomp on it.  Otherwise packages like ipsec-tools which
4446b8c8deeSWill Andrews	# have hardcoded paths under ${prefix}/etc are not tweakable.
4456b8c8deeSWill Andrews	if [ -d usr/local/etc ] ; then
4466b8c8deeSWill Andrews		(
4476b8c8deeSWill Andrews		mkdir -p etc/local
4486b8c8deeSWill Andrews		cd usr/local/etc
4496b8c8deeSWill Andrews		find . -print | cpio -dumpl ../../../etc/local
4506b8c8deeSWill Andrews		cd ..
4516b8c8deeSWill Andrews		nano_rm -rf etc
4526b8c8deeSWill Andrews		ln -s ../../etc/local etc
4536b8c8deeSWill Andrews		)
4546b8c8deeSWill Andrews	fi
4556b8c8deeSWill Andrews
4566b8c8deeSWill Andrews	for d in var etc
4576b8c8deeSWill Andrews	do
4586b8c8deeSWill Andrews		# link /$d under /conf
4596b8c8deeSWill Andrews		# we use hard links so we have them both places.
4606b8c8deeSWill Andrews		# the files in /$d will be hidden by the mount.
4616b8c8deeSWill Andrews		mkdir -p conf/base/$d conf/default/$d
4626b8c8deeSWill Andrews		find $d -print | cpio -dumpl conf/base/
4636b8c8deeSWill Andrews	done
4646b8c8deeSWill Andrews
4656b8c8deeSWill Andrews	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
4666b8c8deeSWill Andrews	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
4676b8c8deeSWill Andrews
4686b8c8deeSWill Andrews	# pick up config files from the special partition
4697b77d3eaSWarner Losh	echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount
4706b8c8deeSWill Andrews
4716b8c8deeSWill Andrews	# Put /tmp on the /var ramdisk (could be symlink already)
4726b8c8deeSWill Andrews	nano_rm -rf tmp
4736b8c8deeSWill Andrews	ln -s var/tmp tmp
4746b8c8deeSWill Andrews
4756b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.dl 2>&1
4766b8c8deeSWill Andrews)
4776b8c8deeSWill Andrews
4786b8c8deeSWill Andrewssetup_nanobsd_etc ( ) (
4796b8c8deeSWill Andrews	pprint 2 "configure nanobsd /etc"
4806b8c8deeSWill Andrews
4816b8c8deeSWill Andrews	(
482*1934c7aaSWarner Losh	cd "${NANO_WORLDDIR}"
4836b8c8deeSWill Andrews
4846b8c8deeSWill Andrews	# create diskless marker file
4856b8c8deeSWill Andrews	touch etc/diskless
4866b8c8deeSWill Andrews
4876b8c8deeSWill Andrews	# Make root filesystem R/O by default
4886b8c8deeSWill Andrews	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
4896b8c8deeSWill Andrews
4906b8c8deeSWill Andrews	# save config file for scripts
4916b8c8deeSWill Andrews	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
4926b8c8deeSWill Andrews
4937b77d3eaSWarner Losh	echo "/dev/${NANO_DRIVE}${NANO_SLICE_ROOT}a / ufs ro 1 1" > etc/fstab
4947b77d3eaSWarner Losh	echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab
4956b8c8deeSWill Andrews	mkdir -p cfg
4966b8c8deeSWill Andrews	)
4976b8c8deeSWill Andrews)
4986b8c8deeSWill Andrews
4996b8c8deeSWill Andrewsprune_usr ( ) (
5006b8c8deeSWill Andrews
5016b8c8deeSWill Andrews	# Remove all empty directories in /usr
502*1934c7aaSWarner Losh	find "${NANO_WORLDDIR}"/usr -type d -depth -print |
5036b8c8deeSWill Andrews		while read d
5046b8c8deeSWill Andrews		do
5056b8c8deeSWill Andrews			rmdir $d > /dev/null 2>&1 || true
5066b8c8deeSWill Andrews		done
5076b8c8deeSWill Andrews)
5086b8c8deeSWill Andrews
5096b8c8deeSWill Andrewsnewfs_part ( ) (
5106b8c8deeSWill Andrews	local dev mnt lbl
5116b8c8deeSWill Andrews	dev=$1
5126b8c8deeSWill Andrews	mnt=$2
5136b8c8deeSWill Andrews	lbl=$3
5146b8c8deeSWill Andrews	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
5156b8c8deeSWill Andrews	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
5166b8c8deeSWill Andrews	mount -o async ${dev} ${mnt}
5176b8c8deeSWill Andrews)
5186b8c8deeSWill Andrews
5196b8c8deeSWill Andrews# Convenient spot to work around any umount issues that your build environment
5206b8c8deeSWill Andrews# hits by overriding this method.
5216b8c8deeSWill Andrewsnano_umount ( ) (
5226b8c8deeSWill Andrews	umount ${1}
5236b8c8deeSWill Andrews)
5246b8c8deeSWill Andrews
5256b8c8deeSWill Andrewspopulate_slice ( ) (
5266b8c8deeSWill Andrews	local dev dir mnt lbl
5276b8c8deeSWill Andrews	dev=$1
5286b8c8deeSWill Andrews	dir=$2
5296b8c8deeSWill Andrews	mnt=$3
5306b8c8deeSWill Andrews	lbl=$4
5316b8c8deeSWill Andrews	echo "Creating ${dev} (mounting on ${mnt})"
5326b8c8deeSWill Andrews	newfs_part ${dev} ${mnt} ${lbl}
5336b8c8deeSWill Andrews	if [ -n "${dir}" -a -d "${dir}" ]; then
5346b8c8deeSWill Andrews		echo "Populating ${lbl} from ${dir}"
535*1934c7aaSWarner Losh		cd "${dir}"
5366b8c8deeSWill Andrews		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
5376b8c8deeSWill Andrews	fi
5386b8c8deeSWill Andrews	df -i ${mnt}
5396b8c8deeSWill Andrews	nano_umount ${mnt}
5406b8c8deeSWill Andrews)
5416b8c8deeSWill Andrews
5426b8c8deeSWill Andrewspopulate_cfg_slice ( ) (
5436b8c8deeSWill Andrews	populate_slice "$1" "$2" "$3" "$4"
5446b8c8deeSWill Andrews)
5456b8c8deeSWill Andrews
5466b8c8deeSWill Andrewspopulate_data_slice ( ) (
5476b8c8deeSWill Andrews	populate_slice "$1" "$2" "$3" "$4"
5486b8c8deeSWill Andrews)
5496b8c8deeSWill Andrews
5506b8c8deeSWill Andrewscreate_diskimage ( ) (
5516b8c8deeSWill Andrews	pprint 2 "build diskimage"
5526b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.di"
5536b8c8deeSWill Andrews
5546b8c8deeSWill Andrews	(
5556b8c8deeSWill Andrews	echo $NANO_MEDIASIZE $NANO_IMAGES \
5566b8c8deeSWill Andrews		$NANO_SECTS $NANO_HEADS \
5576b8c8deeSWill Andrews		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
5586b8c8deeSWill Andrews	awk '
5596b8c8deeSWill Andrews	{
5606b8c8deeSWill Andrews		printf "# %s\n", $0
5616b8c8deeSWill Andrews
5626b8c8deeSWill Andrews		# size of cylinder in sectors
5636b8c8deeSWill Andrews		cs = $3 * $4
5646b8c8deeSWill Andrews
5656b8c8deeSWill Andrews		# number of full cylinders on media
5666b8c8deeSWill Andrews		cyl = int ($1 / cs)
5676b8c8deeSWill Andrews
5686b8c8deeSWill Andrews		# output fdisk geometry spec, truncate cyls to 1023
5696b8c8deeSWill Andrews		if (cyl <= 1023)
5706b8c8deeSWill Andrews			print "g c" cyl " h" $4 " s" $3
5716b8c8deeSWill Andrews		else
5726b8c8deeSWill Andrews			print "g c" 1023 " h" $4 " s" $3
5736b8c8deeSWill Andrews
5746b8c8deeSWill Andrews		if ($7 > 0) {
5756b8c8deeSWill Andrews			# size of data partition in full cylinders
5766b8c8deeSWill Andrews			dsl = int (($7 + cs - 1) / cs)
5776b8c8deeSWill Andrews		} else {
5786b8c8deeSWill Andrews			dsl = 0;
5796b8c8deeSWill Andrews		}
5806b8c8deeSWill Andrews
5816b8c8deeSWill Andrews		# size of config partition in full cylinders
5826b8c8deeSWill Andrews		csl = int (($6 + cs - 1) / cs)
5836b8c8deeSWill Andrews
5846b8c8deeSWill Andrews		if ($5 == 0) {
5856b8c8deeSWill Andrews			# size of image partition(s) in full cylinders
5866b8c8deeSWill Andrews			isl = int ((cyl - dsl - csl) / $2)
5876b8c8deeSWill Andrews		} else {
5886b8c8deeSWill Andrews			isl = int (($5 + cs - 1) / cs)
5896b8c8deeSWill Andrews		}
5906b8c8deeSWill Andrews
5916b8c8deeSWill Andrews		# First image partition start at second track
5926b8c8deeSWill Andrews		print "p 1 165 " $3, isl * cs - $3
5936b8c8deeSWill Andrews		c = isl * cs;
5946b8c8deeSWill Andrews
5956b8c8deeSWill Andrews		# Second image partition (if any) also starts offset one
5966b8c8deeSWill Andrews		# track to keep them identical.
5976b8c8deeSWill Andrews		if ($2 > 1) {
5986b8c8deeSWill Andrews			print "p 2 165 " $3 + c, isl * cs - $3
5996b8c8deeSWill Andrews			c += isl * cs;
6006b8c8deeSWill Andrews		}
6016b8c8deeSWill Andrews
6026b8c8deeSWill Andrews		# Config partition starts at cylinder boundary.
6036b8c8deeSWill Andrews		print "p 3 165 " c, csl * cs
6046b8c8deeSWill Andrews		c += csl * cs
6056b8c8deeSWill Andrews
6066b8c8deeSWill Andrews		# Data partition (if any) starts at cylinder boundary.
6076b8c8deeSWill Andrews		if ($7 > 0) {
6086b8c8deeSWill Andrews			print "p 4 165 " c, dsl * cs
6096b8c8deeSWill Andrews		} else if ($7 < 0 && $1 > c) {
6106b8c8deeSWill Andrews			print "p 4 165 " c, $1 - c
6116b8c8deeSWill Andrews		} else if ($1 < c) {
6126b8c8deeSWill Andrews			print "Disk space overcommitted by", \
6136b8c8deeSWill Andrews			    c - $1, "sectors" > "/dev/stderr"
6146b8c8deeSWill Andrews			exit 2
6156b8c8deeSWill Andrews		}
6166b8c8deeSWill Andrews
6176b8c8deeSWill Andrews		# Force slice 1 to be marked active. This is necessary
6186b8c8deeSWill Andrews		# for booting the image from a USB device to work.
6196b8c8deeSWill Andrews		print "a 1"
6206b8c8deeSWill Andrews	}
6216b8c8deeSWill Andrews	' > ${NANO_OBJ}/_.fdisk
6226b8c8deeSWill Andrews
6236b8c8deeSWill Andrews	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
6246b8c8deeSWill Andrews	MNT=${NANO_OBJ}/_.mnt
6256b8c8deeSWill Andrews	mkdir -p ${MNT}
6266b8c8deeSWill Andrews
6276b8c8deeSWill Andrews	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
6286b8c8deeSWill Andrews		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
6296b8c8deeSWill Andrews			-y ${NANO_HEADS}`
6306b8c8deeSWill Andrews	else
6316b8c8deeSWill Andrews		echo "Creating md backing file..."
6326b8c8deeSWill Andrews		nano_rm -f ${IMG}
6336b8c8deeSWill Andrews		dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
6346b8c8deeSWill Andrews		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
6356b8c8deeSWill Andrews			-y ${NANO_HEADS}`
6366b8c8deeSWill Andrews	fi
6376b8c8deeSWill Andrews
6386b8c8deeSWill Andrews	trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
6396b8c8deeSWill Andrews
6406b8c8deeSWill Andrews	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
6416b8c8deeSWill Andrews	fdisk ${MD}
6426b8c8deeSWill Andrews	# XXX: params
6436b8c8deeSWill Andrews	# XXX: pick up cached boot* files, they may not be in image anymore.
6446b8c8deeSWill Andrews	if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
6456b8c8deeSWill Andrews		boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
6466b8c8deeSWill Andrews	fi
6476b8c8deeSWill Andrews	if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
6487b77d3eaSWarner Losh		bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT}
6496b8c8deeSWill Andrews	else
6507b77d3eaSWarner Losh		bsdlabel -w ${MD}${NANO_SLICE_ROOT}
6516b8c8deeSWill Andrews	fi
6527b77d3eaSWarner Losh	bsdlabel ${MD}${NANO_SLICE_ROOT}
6536b8c8deeSWill Andrews
6546b8c8deeSWill Andrews	# Create first image
6557b77d3eaSWarner Losh	populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a"
6567b77d3eaSWarner Losh	mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT}
6576b8c8deeSWill Andrews	echo "Generating mtree..."
658*1934c7aaSWarner Losh	( cd "${MNT}" && mtree -c ) > ${NANO_OBJ}/_.mtree
659*1934c7aaSWarner Losh	( cd "${MNT}" && du -k ) > ${NANO_OBJ}/_.du
660*1934c7aaSWarner Losh	nano_umount "${MNT}"
6616b8c8deeSWill Andrews
6626b8c8deeSWill Andrews	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
6636b8c8deeSWill Andrews		# Duplicate to second image (if present)
6646b8c8deeSWill Andrews		echo "Duplicating to second image..."
6657b77d3eaSWarner Losh		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
6667b77d3eaSWarner Losh		mount /dev/${MD}${NANO_SLICE_ALTROOT}a ${MNT}
6676b8c8deeSWill Andrews		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
6686b8c8deeSWill Andrews		do
6697b77d3eaSWarner Losh			sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
6706b8c8deeSWill Andrews		done
6716b8c8deeSWill Andrews		nano_umount ${MNT}
6726b8c8deeSWill Andrews		# Override the label from the first partition so we
6736b8c8deeSWill Andrews		# don't confuse glabel with duplicates.
6746b8c8deeSWill Andrews		if [ ! -z ${NANO_LABEL} ]; then
6757b77d3eaSWarner Losh			tunefs -L ${NANO_LABEL}"${NANO_SLICE_ALTROOT}a" /dev/${MD}${NANO_SLICE_ALTROOT}a
6766b8c8deeSWill Andrews		fi
6776b8c8deeSWill Andrews	fi
6786b8c8deeSWill Andrews
6796b8c8deeSWill Andrews	# Create Config slice
6807b77d3eaSWarner Losh	populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
6816b8c8deeSWill Andrews
6826b8c8deeSWill Andrews	# Create Data slice, if any.
6837b77d3eaSWarner Losh	if [ ! -z $NANO_SLICE_DATA -a $NANO_SLICE_CFG = $NANO_SLICE_DATA -a \
6847b77d3eaSWarner Losh	   $NANO_DATASIZE -ne 0 ]; then
6857b77d3eaSWarner Losh		pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
6867b77d3eaSWarner Losh		exit 2
6877b77d3eaSWarner Losh	fi
6887b77d3eaSWarner Losh	if [ $NANO_DATASIZE -ne 0 -a ! -z $NANO_SLICE_DATA ] ; then
6897b77d3eaSWarner Losh		populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
6906b8c8deeSWill Andrews	fi
6916b8c8deeSWill Andrews
6926b8c8deeSWill Andrews	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
6936b8c8deeSWill Andrews		if [ ${NANO_IMAGE_MBRONLY} ]; then
6946b8c8deeSWill Andrews			echo "Writing out _.disk.mbr..."
6956b8c8deeSWill Andrews			dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
6966b8c8deeSWill Andrews		else
6976b8c8deeSWill Andrews			echo "Writing out ${NANO_IMGNAME}..."
6986b8c8deeSWill Andrews			dd if=/dev/${MD} of=${IMG} bs=64k
6996b8c8deeSWill Andrews		fi
7006b8c8deeSWill Andrews
7016b8c8deeSWill Andrews		echo "Writing out ${NANO_IMGNAME}..."
7026b8c8deeSWill Andrews		dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
7036b8c8deeSWill Andrews	fi
7046b8c8deeSWill Andrews
7056b8c8deeSWill Andrews	if ${do_copyout_partition} ; then
7066b8c8deeSWill Andrews		echo "Writing out _.disk.image..."
7077b77d3eaSWarner Losh		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
7086b8c8deeSWill Andrews	fi
7096b8c8deeSWill Andrews	mdconfig -d -u $MD
7106b8c8deeSWill Andrews
7116b8c8deeSWill Andrews	trap - 1 2 15
7126b8c8deeSWill Andrews	trap nano_cleanup EXIT
7136b8c8deeSWill Andrews
7146b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.di 2>&1
7156b8c8deeSWill Andrews)
7166b8c8deeSWill Andrews
7176b8c8deeSWill Andrewslast_orders ( ) (
7186b8c8deeSWill Andrews	# Redefine this function with any last orders you may have
7196b8c8deeSWill Andrews	# after the build completed, for instance to copy the finished
7206b8c8deeSWill Andrews	# image to a more convenient place:
7216b8c8deeSWill Andrews	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
7226b8c8deeSWill Andrews	true
7236b8c8deeSWill Andrews)
7246b8c8deeSWill Andrews
7256b8c8deeSWill Andrews#######################################################################
7266b8c8deeSWill Andrews#
7276b8c8deeSWill Andrews# Optional convenience functions.
7286b8c8deeSWill Andrews#
7296b8c8deeSWill Andrews#######################################################################
7306b8c8deeSWill Andrews
7316b8c8deeSWill Andrews#######################################################################
7326b8c8deeSWill Andrews# Common Flash device geometries
7336b8c8deeSWill Andrews#
7346b8c8deeSWill Andrews
7356b8c8deeSWill AndrewsFlashDevice ( ) {
7366b8c8deeSWill Andrews	if [ -d ${NANO_TOOLS} ] ; then
7376b8c8deeSWill Andrews		. ${NANO_TOOLS}/FlashDevice.sub
7386b8c8deeSWill Andrews	else
7396b8c8deeSWill Andrews		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
7406b8c8deeSWill Andrews	fi
7416b8c8deeSWill Andrews	sub_FlashDevice $1 $2
7426b8c8deeSWill Andrews}
7436b8c8deeSWill Andrews
7446b8c8deeSWill Andrews#######################################################################
7456b8c8deeSWill Andrews# USB device geometries
7466b8c8deeSWill Andrews#
7476b8c8deeSWill Andrews# Usage:
7486b8c8deeSWill Andrews#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
7496b8c8deeSWill Andrews#
7506b8c8deeSWill Andrews# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
7516b8c8deeSWill Andrews#
7526b8c8deeSWill Andrews# Note that the capacity of a flash key is usually advertised in MB or
7536b8c8deeSWill Andrews# GB, *not* MiB/GiB. As such, the precise number of cylinders available
7546b8c8deeSWill Andrews# for C/H/S geometry may vary depending on the actual flash geometry.
7556b8c8deeSWill Andrews#
7566b8c8deeSWill Andrews# The following generic device layouts are understood:
7576b8c8deeSWill Andrews#  generic           An alias for generic-hdd.
7586b8c8deeSWill Andrews#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
7596b8c8deeSWill Andrews#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
7606b8c8deeSWill Andrews#
7616b8c8deeSWill Andrews# The generic-hdd device is preferred for flash devices larger than 1GB.
7626b8c8deeSWill Andrews#
7636b8c8deeSWill Andrews
7646b8c8deeSWill AndrewsUsbDevice ( ) {
7656b8c8deeSWill Andrews	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
7666b8c8deeSWill Andrews	case $a1 in
7676b8c8deeSWill Andrews	generic-fdd)
7686b8c8deeSWill Andrews		NANO_HEADS=64
7696b8c8deeSWill Andrews		NANO_SECTS=32
7706b8c8deeSWill Andrews		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
7716b8c8deeSWill Andrews		;;
7726b8c8deeSWill Andrews	generic|generic-hdd)
7736b8c8deeSWill Andrews		NANO_HEADS=255
7746b8c8deeSWill Andrews		NANO_SECTS=63
7756b8c8deeSWill Andrews		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
7766b8c8deeSWill Andrews		;;
7776b8c8deeSWill Andrews	*)
7786b8c8deeSWill Andrews		echo "Unknown USB flash device"
7796b8c8deeSWill Andrews		exit 2
7806b8c8deeSWill Andrews		;;
7816b8c8deeSWill Andrews	esac
7826b8c8deeSWill Andrews}
7836b8c8deeSWill Andrews
7846b8c8deeSWill Andrews#######################################################################
7856b8c8deeSWill Andrews# Setup serial console
7866b8c8deeSWill Andrews
7876b8c8deeSWill Andrewscust_comconsole ( ) (
7886b8c8deeSWill Andrews	# Enable getty on console
7896b8c8deeSWill Andrews	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
7906b8c8deeSWill Andrews
7916b8c8deeSWill Andrews	# Disable getty on syscons devices
7926b8c8deeSWill Andrews	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
7936b8c8deeSWill Andrews
7946b8c8deeSWill Andrews	# Tell loader to use serial console early.
7956b8c8deeSWill Andrews	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
7966b8c8deeSWill Andrews)
7976b8c8deeSWill Andrews
7986b8c8deeSWill Andrews#######################################################################
7996b8c8deeSWill Andrews# Allow root login via ssh
8006b8c8deeSWill Andrews
8016b8c8deeSWill Andrewscust_allow_ssh_root ( ) (
8026b8c8deeSWill Andrews	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
8036b8c8deeSWill Andrews	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
8046b8c8deeSWill Andrews)
8056b8c8deeSWill Andrews
8066b8c8deeSWill Andrews#######################################################################
8076b8c8deeSWill Andrews# Install the stuff under ./Files
8086b8c8deeSWill Andrews
8096b8c8deeSWill Andrewscust_install_files ( ) (
810*1934c7aaSWarner Losh	cd "${NANO_TOOLS}/Files"
8116b8c8deeSWill Andrews	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
8126b8c8deeSWill Andrews)
8136b8c8deeSWill Andrews
8146b8c8deeSWill Andrews#######################################################################
8156b8c8deeSWill Andrews# Install packages from ${NANO_PACKAGE_DIR}
8166b8c8deeSWill Andrews
8176b8c8deeSWill Andrewscust_pkgng ( ) (
8186b8c8deeSWill Andrews
8196b8c8deeSWill Andrews	# If the package directory doesn't exist, we're done.
8206b8c8deeSWill Andrews	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
8216b8c8deeSWill Andrews		echo "DONE 0 packages"
8226b8c8deeSWill Andrews		return 0
8236b8c8deeSWill Andrews	fi
8246b8c8deeSWill Andrews
8256b8c8deeSWill Andrews	# Find a pkg-* package
8266b8c8deeSWill Andrews	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
8276b8c8deeSWill Andrews		_NANO_PKG_PACKAGE=`basename "$x"`
8286b8c8deeSWill Andrews	done
8296b8c8deeSWill Andrews	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
8306b8c8deeSWill Andrews		echo "FAILED: need a pkg/ package for bootstrapping"
8316b8c8deeSWill Andrews		exit 2
8326b8c8deeSWill Andrews	fi
8336b8c8deeSWill Andrews
8346b8c8deeSWill Andrews	# Copy packages into chroot
8356b8c8deeSWill Andrews	mkdir -p ${NANO_WORLDDIR}/Pkg
8366b8c8deeSWill Andrews	(
837*1934c7aaSWarner Losh		cd "${NANO_PACKAGE_DIR}"
838*1934c7aaSWarner Losh		find "${NANO_PACKAGE_LIST}" -print |
8396b8c8deeSWill Andrews		cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
8406b8c8deeSWill Andrews	)
8416b8c8deeSWill Andrews
8426b8c8deeSWill Andrews	#Bootstrap pkg
8436b8c8deeSWill Andrews	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
8446b8c8deeSWill Andrews	CR pkg -N >/dev/null 2>&1
8456b8c8deeSWill Andrews	if [ "$?" -ne "0" ]; then
8466b8c8deeSWill Andrews		echo "FAILED: pkg bootstrapping faied"
8476b8c8deeSWill Andrews		exit 2
8486b8c8deeSWill Andrews	fi
8496b8c8deeSWill Andrews	nano_rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
8506b8c8deeSWill Andrews
8516b8c8deeSWill Andrews	# Count & report how many we have to install
8526b8c8deeSWill Andrews	todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
8536b8c8deeSWill Andrews	todo=$(expr $todo + 1) # add one for pkg since it is installed already
8546b8c8deeSWill Andrews	echo "=== TODO: $todo"
8556b8c8deeSWill Andrews	ls ${NANO_WORLDDIR}/Pkg
8566b8c8deeSWill Andrews	echo "==="
8576b8c8deeSWill Andrews	while true
8586b8c8deeSWill Andrews	do
8596b8c8deeSWill Andrews		# Record how many we have now
8606b8c8deeSWill Andrews 		have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
8616b8c8deeSWill Andrews
8626b8c8deeSWill Andrews		# Attempt to install more packages
8636b8c8deeSWill Andrews		CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
8646b8c8deeSWill Andrews
8656b8c8deeSWill Andrews		# See what that got us
8666b8c8deeSWill Andrews 		now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
8676b8c8deeSWill Andrews		echo "=== NOW $now"
8686b8c8deeSWill Andrews		CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
8696b8c8deeSWill Andrews		echo "==="
8706b8c8deeSWill Andrews		if [ $now -eq $todo ] ; then
8716b8c8deeSWill Andrews			echo "DONE $now packages"
8726b8c8deeSWill Andrews			break
8736b8c8deeSWill Andrews		elif [ $now -eq $have ] ; then
8746b8c8deeSWill Andrews			echo "FAILED: Nothing happened on this pass"
8756b8c8deeSWill Andrews			exit 2
8766b8c8deeSWill Andrews		fi
8776b8c8deeSWill Andrews	done
8786b8c8deeSWill Andrews	nano_rm -rf ${NANO_WORLDDIR}/Pkg
8796b8c8deeSWill Andrews)
8806b8c8deeSWill Andrews
8816b8c8deeSWill Andrews#######################################################################
8826b8c8deeSWill Andrews# Convenience function:
8836b8c8deeSWill Andrews# 	Register all args as customize function.
8846b8c8deeSWill Andrews
8856b8c8deeSWill Andrewscustomize_cmd ( ) {
8866b8c8deeSWill Andrews	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
8876b8c8deeSWill Andrews}
8886b8c8deeSWill Andrews
8896b8c8deeSWill Andrews#######################################################################
8906b8c8deeSWill Andrews# Convenience function:
8916b8c8deeSWill Andrews# 	Register all args as late customize function to run just before
8926b8c8deeSWill Andrews#	image creation.
8936b8c8deeSWill Andrews
8946b8c8deeSWill Andrewslate_customize_cmd ( ) {
8956b8c8deeSWill Andrews	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
8966b8c8deeSWill Andrews}
8976b8c8deeSWill Andrews
8986b8c8deeSWill Andrews#######################################################################
8996b8c8deeSWill Andrews#
9006b8c8deeSWill Andrews# All set up to go...
9016b8c8deeSWill Andrews#
9026b8c8deeSWill Andrews#######################################################################
9036b8c8deeSWill Andrews
9046b8c8deeSWill Andrews# Progress Print
9056b8c8deeSWill Andrews#	Print $2 at level $1.
9066b8c8deeSWill Andrewspprint ( ) (
9076b8c8deeSWill Andrews    if [ "$1" -le $PPLEVEL ]; then
9086b8c8deeSWill Andrews	runtime=$(( `date +%s` - $NANO_STARTTIME ))
9096b8c8deeSWill Andrews	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
9106b8c8deeSWill Andrews    fi
9116b8c8deeSWill Andrews)
9126b8c8deeSWill Andrews
9136b8c8deeSWill Andrewsusage ( ) {
9146b8c8deeSWill Andrews	(
9156b8c8deeSWill Andrews	echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
9166b8c8deeSWill Andrews	echo "	-b	suppress builds (both kernel and world)"
9176b8c8deeSWill Andrews	echo "	-c	specify config file"
9186b8c8deeSWill Andrews	echo "	-f	suppress code slice extraction"
9196b8c8deeSWill Andrews	echo "	-i	suppress disk image build"
9206b8c8deeSWill Andrews	echo "	-K	suppress installkernel"
9216b8c8deeSWill Andrews	echo "	-k	suppress buildkernel"
9226b8c8deeSWill Andrews	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
9236b8c8deeSWill Andrews	echo "	-q	make output more quiet"
9246b8c8deeSWill Andrews	echo "	-v	make output more verbose"
9256b8c8deeSWill Andrews	echo "	-w	suppress buildworld"
9266b8c8deeSWill Andrews	) 1>&2
9276b8c8deeSWill Andrews	exit 2
9286b8c8deeSWill Andrews}
9296b8c8deeSWill Andrews
9306b8c8deeSWill Andrews#######################################################################
9316b8c8deeSWill Andrews# Setup and Export Internal variables
9326b8c8deeSWill Andrews#
9336b8c8deeSWill Andrews
934*1934c7aaSWarner Loshexport_var ( ) {		# Don't wawnt a subshell
9356b8c8deeSWill Andrews	var=$1
9366b8c8deeSWill Andrews	# Lookup value of the variable.
9376b8c8deeSWill Andrews	eval val=\$$var
9386b8c8deeSWill Andrews	pprint 3 "Setting variable: $var=\"$val\""
9396b8c8deeSWill Andrews	export $1
9406b8c8deeSWill Andrews}
9416b8c8deeSWill Andrews
9426b8c8deeSWill Andrews# Call this function to set defaults _after_ parsing options.
943*1934c7aaSWarner Losh# dont want a subshell otherwise variable setting is thrown away.
9446b8c8deeSWill Andrewsset_defaults_and_export ( ) {
9456b8c8deeSWill Andrews	test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
9466b8c8deeSWill Andrews	test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
9476b8c8deeSWill Andrews	test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
9486b8c8deeSWill Andrews	NANO_WORLDDIR=${NANO_OBJ}/_.w
9496b8c8deeSWill Andrews	NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
9506b8c8deeSWill Andrews	NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
9516b8c8deeSWill Andrews
9526b8c8deeSWill Andrews	# Override user's NANO_DRIVE if they specified a NANO_LABEL
9536b8c8deeSWill Andrews	[ ! -z "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}"
9546b8c8deeSWill Andrews
9556b8c8deeSWill Andrews	# Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
9566b8c8deeSWill Andrews	[ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
9576b8c8deeSWill Andrews		NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}"
9586b8c8deeSWill Andrews
9596b8c8deeSWill Andrews	NANO_STARTTIME=`date +%s`
9606b8c8deeSWill Andrews	pprint 3 "Exporting NanoBSD variables"
9616b8c8deeSWill Andrews	export_var MAKEOBJDIRPREFIX
9626b8c8deeSWill Andrews	export_var NANO_ARCH
9636b8c8deeSWill Andrews	export_var NANO_CODESIZE
9646b8c8deeSWill Andrews	export_var NANO_CONFSIZE
9656b8c8deeSWill Andrews	export_var NANO_CUSTOMIZE
9666b8c8deeSWill Andrews	export_var NANO_DATASIZE
9676b8c8deeSWill Andrews	export_var NANO_DRIVE
9686b8c8deeSWill Andrews	export_var NANO_HEADS
9696b8c8deeSWill Andrews	export_var NANO_IMAGES
9706b8c8deeSWill Andrews	export_var NANO_IMGNAME
9716b8c8deeSWill Andrews	export_var NANO_MAKE
9726b8c8deeSWill Andrews	export_var NANO_MAKE_CONF_BUILD
9736b8c8deeSWill Andrews	export_var NANO_MAKE_CONF_INSTALL
9746b8c8deeSWill Andrews	export_var NANO_MEDIASIZE
9756b8c8deeSWill Andrews	export_var NANO_NAME
9766b8c8deeSWill Andrews	export_var NANO_NEWFS
9776b8c8deeSWill Andrews	export_var NANO_OBJ
9786b8c8deeSWill Andrews	export_var NANO_PMAKE
9796b8c8deeSWill Andrews	export_var NANO_SECTS
9806b8c8deeSWill Andrews	export_var NANO_SRC
9816b8c8deeSWill Andrews	export_var NANO_TOOLS
9826b8c8deeSWill Andrews	export_var NANO_WORLDDIR
9836b8c8deeSWill Andrews	export_var NANO_BOOT0CFG
9846b8c8deeSWill Andrews	export_var NANO_BOOTLOADER
9856b8c8deeSWill Andrews	export_var NANO_LABEL
9866b8c8deeSWill Andrews	export_var NANO_MODULES
987*1934c7aaSWarner Losh	export_var SRCCONF
9886b8c8deeSWill Andrews}
989