xref: /freebsd/tools/tools/nanobsd/defaults.sh (revision c275d9ec22d91fe34fb620fe97e67a69c097ca39)
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 AndrewsNANO_ARCH=`uname -p`
1686b8c8deeSWill Andrews
169d07833f9SWarner Losh# CPUTYPE defaults to "" which is the default when CPUTYPE isn't
170d07833f9SWarner Losh# defined.
171d07833f9SWarner LoshNANO_CPUTYPE=""
172d07833f9SWarner Losh
1736b8c8deeSWill Andrews# Directory to populate /cfg from
1746b8c8deeSWill AndrewsNANO_CFGDIR=""
1756b8c8deeSWill Andrews
1766b8c8deeSWill Andrews# Directory to populate /data from
1776b8c8deeSWill AndrewsNANO_DATADIR=""
1786b8c8deeSWill Andrews
179a9d9c49fSWarner Losh# We don't need SRCCONF or SRC_ENV_CONF. NanoBSD puts everything we
180a9d9c49fSWarner Losh# need for the build in files included with __MAKE_CONF. Override in your
181a9d9c49fSWarner Losh# config file if you really must. We set them unconditionally here, though
182a9d9c49fSWarner Losh# in case they are stray in the build environment
183a9d9c49fSWarner LoshSRCCONF=/dev/null
184a9d9c49fSWarner LoshSRC_ENV_CONF=/dev/null
1856b8c8deeSWill Andrews
1866b8c8deeSWill Andrews#######################################################################
1876b8c8deeSWill Andrews#
1886b8c8deeSWill Andrews# The functions which do the real work.
1896b8c8deeSWill Andrews# Can be overridden from the config file(s)
1906b8c8deeSWill Andrews#
1916b8c8deeSWill Andrews#######################################################################
1926b8c8deeSWill Andrews
1931934c7aaSWarner Losh# Export values into the shell. Must use { } instead of ( ) like
1941934c7aaSWarner Losh# other functions to avoid a subshell.
1951934c7aaSWarner Losh# We set __MAKE_CONF as a global since it is easier to get quoting
1961934c7aaSWarner Losh# right for paths with spaces in them.
1971934c7aaSWarner Loshmake_export ( ) {
1981934c7aaSWarner Losh	# Similar to export_var, except puts the data out to stdout
1991934c7aaSWarner Losh	var=$1
2001934c7aaSWarner Losh	eval val=\$$var
2011934c7aaSWarner Losh	echo "Setting variable: $var=\"$val\""
2021934c7aaSWarner Losh	export $1
2031934c7aaSWarner Losh}
2041934c7aaSWarner Losh
2051934c7aaSWarner Loshnano_make_build_env ( ) {
2061934c7aaSWarner Losh	__MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
2071934c7aaSWarner Losh	make_export __MAKE_CONF
2081934c7aaSWarner Losh}
2091934c7aaSWarner Losh
2101934c7aaSWarner Loshnano_make_install_env ( ) {
2111934c7aaSWarner Losh	__MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
2121934c7aaSWarner Losh	make_export __MAKE_CONF
2131934c7aaSWarner Losh}
2141934c7aaSWarner Losh
2151934c7aaSWarner Losh# Extra environment variables for kernel builds
2161934c7aaSWarner Loshnano_make_kernel_env ( ) {
2171934c7aaSWarner Losh	if [ -f ${NANO_KERNEL} ] ; then
2181934c7aaSWarner Losh		KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
2191934c7aaSWarner Losh		KERNCONF="$(basename ${NANO_KERNEL})"
2201934c7aaSWarner Losh		make_export KERNCONFDIR
2211934c7aaSWarner Losh		make_export KERNCONF
2221934c7aaSWarner Losh	else
2231934c7aaSWarner Losh		export KERNCONF="${NANO_KERNEL}"
2241934c7aaSWarner Losh		make_export KERNCONF
2251934c7aaSWarner Losh	fi
2261934c7aaSWarner Losh}
2271934c7aaSWarner Losh
2281934c7aaSWarner Loshnano_global_make_env ( ) (
229a9d9c49fSWarner Losh	[ ! -z "${NANO_ARCH}" ] && echo TARGET_ARCH="${NANO_ARCH}"  || true
230a9d9c49fSWarner Losh	[ ! -z "${NANO_CPUTYPE}" ] && echo TARGET_CPUTYPE="${NANO_CPUTYPE}" || true
2311934c7aaSWarner Losh)
2321934c7aaSWarner Losh
2336b8c8deeSWill Andrews# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
2346b8c8deeSWill Andrews# hosts for now.
2356b8c8deeSWill Andrewsnano_rm ( ) {
2366b8c8deeSWill Andrews	case $(uname -r) in
2376b8c8deeSWill Andrews	7*|8*|9*) rm $* ;;
2386b8c8deeSWill Andrews	*) rm -x $* ;;
2396b8c8deeSWill Andrews	esac
2406b8c8deeSWill Andrews}
2416b8c8deeSWill Andrews
2426b8c8deeSWill Andrews# run in the world chroot, errors fatal
2431934c7aaSWarner LoshCR ( ) {
2441934c7aaSWarner Losh	chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
2456b8c8deeSWill Andrews}
2466b8c8deeSWill Andrews
2476b8c8deeSWill Andrews# run in the world chroot, errors not fatal
2481934c7aaSWarner LoshCR0 ( ) {
2491934c7aaSWarner Losh	chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
2506b8c8deeSWill Andrews}
2516b8c8deeSWill Andrews
2526b8c8deeSWill Andrewsnano_cleanup ( ) (
2536b8c8deeSWill Andrews	if [ $? -ne 0 ]; then
2546b8c8deeSWill Andrews		echo "Error encountered.  Check for errors in last log file." 1>&2
2556b8c8deeSWill Andrews	fi
2566b8c8deeSWill Andrews	exit $?
2576b8c8deeSWill Andrews)
2586b8c8deeSWill Andrews
2596b8c8deeSWill Andrewsclean_build ( ) (
2606b8c8deeSWill Andrews	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
2616b8c8deeSWill Andrews
2626b8c8deeSWill Andrews	if ! nano_rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
2636b8c8deeSWill Andrews		chflags -R noschg ${MAKEOBJDIRPREFIX}/
2646b8c8deeSWill Andrews		nano_rm -r ${MAKEOBJDIRPREFIX}/
2656b8c8deeSWill Andrews	fi
2666b8c8deeSWill Andrews)
2676b8c8deeSWill Andrews
2686b8c8deeSWill Andrewsmake_conf_build ( ) (
2696b8c8deeSWill Andrews	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
2706b8c8deeSWill Andrews
2716b8c8deeSWill Andrews	mkdir -p ${MAKEOBJDIRPREFIX}
2726b8c8deeSWill Andrews	printenv > ${MAKEOBJDIRPREFIX}/_.env
2736b8c8deeSWill Andrews
2741934c7aaSWarner Losh	# Make sure we get all the global settings that NanoBSD wants
2751934c7aaSWarner Losh	# in addition to the user's global settings
2761934c7aaSWarner Losh	(
2771934c7aaSWarner Losh	nano_global_make_env
2781934c7aaSWarner Losh	echo "${CONF_WORLD}"
2791934c7aaSWarner Losh	echo "${CONF_BUILD}"
2801934c7aaSWarner Losh	) > ${NANO_MAKE_CONF_BUILD}
2816b8c8deeSWill Andrews)
2826b8c8deeSWill Andrews
2836b8c8deeSWill Andrewsbuild_world ( ) (
2846b8c8deeSWill Andrews	pprint 2 "run buildworld"
2856b8c8deeSWill Andrews	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
2866b8c8deeSWill Andrews
2871934c7aaSWarner Losh	(
2881934c7aaSWarner Losh	nano_make_build_env
2891934c7aaSWarner Losh	set -o xtrace
2901934c7aaSWarner Losh	cd "${NANO_SRC}"
2911934c7aaSWarner Losh	${NANO_PMAKE} buildworld
2921934c7aaSWarner Losh	) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
2936b8c8deeSWill Andrews)
2946b8c8deeSWill Andrews
2956b8c8deeSWill Andrewsbuild_kernel ( ) (
2966b8c8deeSWill Andrews	local extra
2976b8c8deeSWill Andrews
2986b8c8deeSWill Andrews	pprint 2 "build kernel ($NANO_KERNEL)"
2996b8c8deeSWill Andrews	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
3006b8c8deeSWill Andrews
3016b8c8deeSWill Andrews	(
3021934c7aaSWarner Losh	nano_make_build_env
3031934c7aaSWarner Losh	nano_make_kernel_env
3046b8c8deeSWill Andrews
3056b8c8deeSWill Andrews	# Note: We intentionally build all modules, not only the ones in
3066b8c8deeSWill Andrews	# NANO_MODULES so the built world can be reused by multiple images.
3071934c7aaSWarner Losh	# Although MODULES_OVERRIDE can be defined in the kenrel config
3081934c7aaSWarner Losh	# file to override this behavior. Just set NANO_MODULES=default.
3091934c7aaSWarner Losh	set -o xtrace
3101934c7aaSWarner Losh	cd "${NANO_SRC}"
3111934c7aaSWarner Losh	${NANO_PMAKE} buildkernel
3126b8c8deeSWill Andrews	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
3136b8c8deeSWill Andrews)
3146b8c8deeSWill Andrews
3156b8c8deeSWill Andrewsclean_world ( ) (
3166b8c8deeSWill Andrews	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
3176b8c8deeSWill Andrews		pprint 2 "Clean and create object directory (${NANO_OBJ})"
3186b8c8deeSWill Andrews		if ! nano_rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
3196b8c8deeSWill Andrews			chflags -R noschg ${NANO_OBJ}
3206b8c8deeSWill Andrews			nano_rm -r ${NANO_OBJ}/
3216b8c8deeSWill Andrews		fi
3221934c7aaSWarner Losh		mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
3236b8c8deeSWill Andrews		printenv > ${NANO_OBJ}/_.env
3246b8c8deeSWill Andrews	else
3256b8c8deeSWill Andrews		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
3261934c7aaSWarner Losh		if ! nano_rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
3271934c7aaSWarner Losh			chflags -R noschg "${NANO_WORLDDIR}"
3281934c7aaSWarner Losh			nano_rm -rf "${NANO_WORLDDIR}/"
3296b8c8deeSWill Andrews		fi
3301934c7aaSWarner Losh		mkdir -p "${NANO_WORLDDIR}"
3316b8c8deeSWill Andrews	fi
3326b8c8deeSWill Andrews)
3336b8c8deeSWill Andrews
3346b8c8deeSWill Andrewsmake_conf_install ( ) (
3356b8c8deeSWill Andrews	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
3366b8c8deeSWill Andrews
3371934c7aaSWarner Losh	# Make sure we get all the global settings that NanoBSD wants
3381934c7aaSWarner Losh	# in addition to the user's global settings
3391934c7aaSWarner Losh	(
3401934c7aaSWarner Losh	nano_global_make_env
3411934c7aaSWarner Losh	echo "${CONF_WORLD}"
3421934c7aaSWarner Losh	echo "${CONF_INSTALL}"
343*c275d9ecSWarner Losh	if [ ! -z "${NANO_NOPRIV_BUILD}" ]; then
344*c275d9ecSWarner Losh	    echo NO_ROOT=t
345*c275d9ecSWarner Losh	    echo METALOG=${NANO_METALOG}
346*c275d9ecSWarner Losh	fi
3471934c7aaSWarner Losh	) >  ${NANO_MAKE_CONF_INSTALL}
3486b8c8deeSWill Andrews)
3496b8c8deeSWill Andrews
3506b8c8deeSWill Andrewsinstall_world ( ) (
3516b8c8deeSWill Andrews	pprint 2 "installworld"
3526b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.iw"
3536b8c8deeSWill Andrews
3541934c7aaSWarner Losh	(
3551934c7aaSWarner Losh	nano_make_install_env
3561934c7aaSWarner Losh	set -o xtrace
3571934c7aaSWarner Losh	cd "${NANO_SRC}"
3581934c7aaSWarner Losh	${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}"
3591934c7aaSWarner Losh	chflags -R noschg "${NANO_WORLDDIR}"
3601934c7aaSWarner Losh	) > ${NANO_OBJ}/_.iw 2>&1
3616b8c8deeSWill Andrews)
3626b8c8deeSWill Andrews
3636b8c8deeSWill Andrewsinstall_etc ( ) (
3646b8c8deeSWill Andrews
3656b8c8deeSWill Andrews	pprint 2 "install /etc"
3666b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.etc"
3676b8c8deeSWill Andrews
3681934c7aaSWarner Losh	(
3691934c7aaSWarner Losh	nano_make_install_env
3701934c7aaSWarner Losh	set -o xtrace
3711934c7aaSWarner Losh	cd "${NANO_SRC}"
3721934c7aaSWarner Losh	${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}"
3736b8c8deeSWill Andrews	# make.conf doesn't get created by default, but some ports need it
3746b8c8deeSWill Andrews	# so they can spam it.
3751934c7aaSWarner Losh	cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
3761934c7aaSWarner Losh	) > ${NANO_OBJ}/_.etc 2>&1
3776b8c8deeSWill Andrews)
3786b8c8deeSWill Andrews
3796b8c8deeSWill Andrewsinstall_kernel ( ) (
3806b8c8deeSWill Andrews	local extra
3816b8c8deeSWill Andrews
3826b8c8deeSWill Andrews	pprint 2 "install kernel ($NANO_KERNEL)"
3836b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.ik"
3846b8c8deeSWill Andrews
3856b8c8deeSWill Andrews	(
3866b8c8deeSWill Andrews
3871934c7aaSWarner Losh	nano_make_install_env
3881934c7aaSWarner Losh	nano_make_kernel_env
3891934c7aaSWarner Losh
3906b8c8deeSWill Andrews	if [ "${NANO_MODULES}" != "default" ]; then
3911934c7aaSWarner Losh		MODULES_OVERRIDE="${NANO_MODULES}"
3921934c7aaSWarner Losh		make_export MODULES_OVERRIDE
3936b8c8deeSWill Andrews	fi
3946b8c8deeSWill Andrews
3951934c7aaSWarner Losh	set -o xtrace
3961934c7aaSWarner Losh	cd "${NANO_SRC}"
3971934c7aaSWarner Losh	${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}"
3981934c7aaSWarner Losh
3996b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.ik 2>&1
4006b8c8deeSWill Andrews)
4016b8c8deeSWill Andrews
4026b8c8deeSWill Andrewsnative_xtools ( ) (
4036b8c8deeSWill Andrews	print 2 "Installing the optimized native build tools for cross env"
4046b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
4056b8c8deeSWill Andrews
4061934c7aaSWarner Losh	(
4071934c7aaSWarner Losh
4081934c7aaSWarner Losh	nano_make_install_env
4091934c7aaSWarner Losh	set -o xtrace
4101934c7aaSWarner Losh	cd "${NANO_SRC}"
4111934c7aaSWarner Losh	${NANO_MAKE} native-xtools DESTDIR="${NANO_WORLDDIR}"
4121934c7aaSWarner Losh
4131934c7aaSWarner Losh	) > ${NANO_OBJ}/_.native_xtools 2>&1
4146b8c8deeSWill Andrews)
4156b8c8deeSWill Andrews
4166b8c8deeSWill Andrewsrun_customize ( ) (
4176b8c8deeSWill Andrews
4186b8c8deeSWill Andrews	pprint 2 "run customize scripts"
4196b8c8deeSWill Andrews	for c in $NANO_CUSTOMIZE
4206b8c8deeSWill Andrews	do
4216b8c8deeSWill Andrews		pprint 2 "customize \"$c\""
4226b8c8deeSWill Andrews		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
4236b8c8deeSWill Andrews		pprint 4 "`type $c`"
4246b8c8deeSWill Andrews		( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
4256b8c8deeSWill Andrews	done
4266b8c8deeSWill Andrews)
4276b8c8deeSWill Andrews
4286b8c8deeSWill Andrewsrun_late_customize ( ) (
4296b8c8deeSWill Andrews
4306b8c8deeSWill Andrews	pprint 2 "run late customize scripts"
4316b8c8deeSWill Andrews	for c in $NANO_LATE_CUSTOMIZE
4326b8c8deeSWill Andrews	do
4336b8c8deeSWill Andrews		pprint 2 "late customize \"$c\""
4346b8c8deeSWill Andrews		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
4356b8c8deeSWill Andrews		pprint 4 "`type $c`"
4366b8c8deeSWill Andrews		( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
4376b8c8deeSWill Andrews	done
4386b8c8deeSWill Andrews)
4396b8c8deeSWill Andrews
4406b8c8deeSWill Andrewssetup_nanobsd ( ) (
4416b8c8deeSWill Andrews	pprint 2 "configure nanobsd setup"
4426b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.dl"
4436b8c8deeSWill Andrews
4446b8c8deeSWill Andrews	(
4451934c7aaSWarner Losh	cd "${NANO_WORLDDIR}"
4466b8c8deeSWill Andrews
4476b8c8deeSWill Andrews	# Move /usr/local/etc to /etc/local so that the /cfg stuff
4486b8c8deeSWill Andrews	# can stomp on it.  Otherwise packages like ipsec-tools which
4496b8c8deeSWill Andrews	# have hardcoded paths under ${prefix}/etc are not tweakable.
4506b8c8deeSWill Andrews	if [ -d usr/local/etc ] ; then
4516b8c8deeSWill Andrews		(
4526b8c8deeSWill Andrews		mkdir -p etc/local
4536b8c8deeSWill Andrews		cd usr/local/etc
4546b8c8deeSWill Andrews		find . -print | cpio -dumpl ../../../etc/local
4556b8c8deeSWill Andrews		cd ..
4566b8c8deeSWill Andrews		nano_rm -rf etc
4576b8c8deeSWill Andrews		ln -s ../../etc/local etc
4586b8c8deeSWill Andrews		)
4596b8c8deeSWill Andrews	fi
4606b8c8deeSWill Andrews
4616b8c8deeSWill Andrews	for d in var etc
4626b8c8deeSWill Andrews	do
4636b8c8deeSWill Andrews		# link /$d under /conf
4646b8c8deeSWill Andrews		# we use hard links so we have them both places.
4656b8c8deeSWill Andrews		# the files in /$d will be hidden by the mount.
4666b8c8deeSWill Andrews		mkdir -p conf/base/$d conf/default/$d
4676b8c8deeSWill Andrews		find $d -print | cpio -dumpl conf/base/
4686b8c8deeSWill Andrews	done
4696b8c8deeSWill Andrews
4706b8c8deeSWill Andrews	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
4716b8c8deeSWill Andrews	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
4726b8c8deeSWill Andrews
4736b8c8deeSWill Andrews	# pick up config files from the special partition
4747b77d3eaSWarner Losh	echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount
4756b8c8deeSWill Andrews
4766b8c8deeSWill Andrews	# Put /tmp on the /var ramdisk (could be symlink already)
4776b8c8deeSWill Andrews	nano_rm -rf tmp
4786b8c8deeSWill Andrews	ln -s var/tmp tmp
4796b8c8deeSWill Andrews
4806b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.dl 2>&1
4816b8c8deeSWill Andrews)
4826b8c8deeSWill Andrews
4836b8c8deeSWill Andrewssetup_nanobsd_etc ( ) (
4846b8c8deeSWill Andrews	pprint 2 "configure nanobsd /etc"
4856b8c8deeSWill Andrews
4866b8c8deeSWill Andrews	(
4871934c7aaSWarner Losh	cd "${NANO_WORLDDIR}"
4886b8c8deeSWill Andrews
4896b8c8deeSWill Andrews	# create diskless marker file
4906b8c8deeSWill Andrews	touch etc/diskless
4916b8c8deeSWill Andrews
4926b8c8deeSWill Andrews	# Make root filesystem R/O by default
4936b8c8deeSWill Andrews	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
4946b8c8deeSWill Andrews
4956b8c8deeSWill Andrews	# save config file for scripts
4966b8c8deeSWill Andrews	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
4976b8c8deeSWill Andrews
4987b77d3eaSWarner Losh	echo "/dev/${NANO_DRIVE}${NANO_SLICE_ROOT}a / ufs ro 1 1" > etc/fstab
4997b77d3eaSWarner Losh	echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab
5006b8c8deeSWill Andrews	mkdir -p cfg
5016b8c8deeSWill Andrews	)
5026b8c8deeSWill Andrews)
5036b8c8deeSWill Andrews
5046b8c8deeSWill Andrewsprune_usr ( ) (
5056b8c8deeSWill Andrews
5066b8c8deeSWill Andrews	# Remove all empty directories in /usr
5071934c7aaSWarner Losh	find "${NANO_WORLDDIR}"/usr -type d -depth -print |
5086b8c8deeSWill Andrews		while read d
5096b8c8deeSWill Andrews		do
5106b8c8deeSWill Andrews			rmdir $d > /dev/null 2>&1 || true
5116b8c8deeSWill Andrews		done
5126b8c8deeSWill Andrews)
5136b8c8deeSWill Andrews
5146b8c8deeSWill Andrewsnewfs_part ( ) (
5156b8c8deeSWill Andrews	local dev mnt lbl
5166b8c8deeSWill Andrews	dev=$1
5176b8c8deeSWill Andrews	mnt=$2
5186b8c8deeSWill Andrews	lbl=$3
5196b8c8deeSWill Andrews	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
5206b8c8deeSWill Andrews	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
5216b8c8deeSWill Andrews	mount -o async ${dev} ${mnt}
5226b8c8deeSWill Andrews)
5236b8c8deeSWill Andrews
5246b8c8deeSWill Andrews# Convenient spot to work around any umount issues that your build environment
5256b8c8deeSWill Andrews# hits by overriding this method.
5266b8c8deeSWill Andrewsnano_umount ( ) (
5276b8c8deeSWill Andrews	umount ${1}
5286b8c8deeSWill Andrews)
5296b8c8deeSWill Andrews
5306b8c8deeSWill Andrewspopulate_slice ( ) (
5316b8c8deeSWill Andrews	local dev dir mnt lbl
5326b8c8deeSWill Andrews	dev=$1
5336b8c8deeSWill Andrews	dir=$2
5346b8c8deeSWill Andrews	mnt=$3
5356b8c8deeSWill Andrews	lbl=$4
5366b8c8deeSWill Andrews	echo "Creating ${dev} (mounting on ${mnt})"
5376b8c8deeSWill Andrews	newfs_part ${dev} ${mnt} ${lbl}
5386b8c8deeSWill Andrews	if [ -n "${dir}" -a -d "${dir}" ]; then
5396b8c8deeSWill Andrews		echo "Populating ${lbl} from ${dir}"
5401934c7aaSWarner Losh		cd "${dir}"
5416b8c8deeSWill Andrews		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
5426b8c8deeSWill Andrews	fi
5436b8c8deeSWill Andrews	df -i ${mnt}
5446b8c8deeSWill Andrews	nano_umount ${mnt}
5456b8c8deeSWill Andrews)
5466b8c8deeSWill Andrews
5476b8c8deeSWill Andrewspopulate_cfg_slice ( ) (
5486b8c8deeSWill Andrews	populate_slice "$1" "$2" "$3" "$4"
5496b8c8deeSWill Andrews)
5506b8c8deeSWill Andrews
5516b8c8deeSWill Andrewspopulate_data_slice ( ) (
5526b8c8deeSWill Andrews	populate_slice "$1" "$2" "$3" "$4"
5536b8c8deeSWill Andrews)
5546b8c8deeSWill Andrews
5556b8c8deeSWill Andrewscreate_diskimage ( ) (
5566b8c8deeSWill Andrews	pprint 2 "build diskimage"
5576b8c8deeSWill Andrews	pprint 3 "log: ${NANO_OBJ}/_.di"
5586b8c8deeSWill Andrews
5596b8c8deeSWill Andrews	(
5606b8c8deeSWill Andrews	echo $NANO_MEDIASIZE $NANO_IMAGES \
5616b8c8deeSWill Andrews		$NANO_SECTS $NANO_HEADS \
5626b8c8deeSWill Andrews		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
5636b8c8deeSWill Andrews	awk '
5646b8c8deeSWill Andrews	{
5656b8c8deeSWill Andrews		printf "# %s\n", $0
5666b8c8deeSWill Andrews
5676b8c8deeSWill Andrews		# size of cylinder in sectors
5686b8c8deeSWill Andrews		cs = $3 * $4
5696b8c8deeSWill Andrews
5706b8c8deeSWill Andrews		# number of full cylinders on media
5716b8c8deeSWill Andrews		cyl = int ($1 / cs)
5726b8c8deeSWill Andrews
5736b8c8deeSWill Andrews		# output fdisk geometry spec, truncate cyls to 1023
5746b8c8deeSWill Andrews		if (cyl <= 1023)
5756b8c8deeSWill Andrews			print "g c" cyl " h" $4 " s" $3
5766b8c8deeSWill Andrews		else
5776b8c8deeSWill Andrews			print "g c" 1023 " h" $4 " s" $3
5786b8c8deeSWill Andrews
5796b8c8deeSWill Andrews		if ($7 > 0) {
5806b8c8deeSWill Andrews			# size of data partition in full cylinders
5816b8c8deeSWill Andrews			dsl = int (($7 + cs - 1) / cs)
5826b8c8deeSWill Andrews		} else {
5836b8c8deeSWill Andrews			dsl = 0;
5846b8c8deeSWill Andrews		}
5856b8c8deeSWill Andrews
5866b8c8deeSWill Andrews		# size of config partition in full cylinders
5876b8c8deeSWill Andrews		csl = int (($6 + cs - 1) / cs)
5886b8c8deeSWill Andrews
5896b8c8deeSWill Andrews		if ($5 == 0) {
5906b8c8deeSWill Andrews			# size of image partition(s) in full cylinders
5916b8c8deeSWill Andrews			isl = int ((cyl - dsl - csl) / $2)
5926b8c8deeSWill Andrews		} else {
5936b8c8deeSWill Andrews			isl = int (($5 + cs - 1) / cs)
5946b8c8deeSWill Andrews		}
5956b8c8deeSWill Andrews
5966b8c8deeSWill Andrews		# First image partition start at second track
5976b8c8deeSWill Andrews		print "p 1 165 " $3, isl * cs - $3
5986b8c8deeSWill Andrews		c = isl * cs;
5996b8c8deeSWill Andrews
6006b8c8deeSWill Andrews		# Second image partition (if any) also starts offset one
6016b8c8deeSWill Andrews		# track to keep them identical.
6026b8c8deeSWill Andrews		if ($2 > 1) {
6036b8c8deeSWill Andrews			print "p 2 165 " $3 + c, isl * cs - $3
6046b8c8deeSWill Andrews			c += isl * cs;
6056b8c8deeSWill Andrews		}
6066b8c8deeSWill Andrews
6076b8c8deeSWill Andrews		# Config partition starts at cylinder boundary.
6086b8c8deeSWill Andrews		print "p 3 165 " c, csl * cs
6096b8c8deeSWill Andrews		c += csl * cs
6106b8c8deeSWill Andrews
6116b8c8deeSWill Andrews		# Data partition (if any) starts at cylinder boundary.
6126b8c8deeSWill Andrews		if ($7 > 0) {
6136b8c8deeSWill Andrews			print "p 4 165 " c, dsl * cs
6146b8c8deeSWill Andrews		} else if ($7 < 0 && $1 > c) {
6156b8c8deeSWill Andrews			print "p 4 165 " c, $1 - c
6166b8c8deeSWill Andrews		} else if ($1 < c) {
6176b8c8deeSWill Andrews			print "Disk space overcommitted by", \
6186b8c8deeSWill Andrews			    c - $1, "sectors" > "/dev/stderr"
6196b8c8deeSWill Andrews			exit 2
6206b8c8deeSWill Andrews		}
6216b8c8deeSWill Andrews
6226b8c8deeSWill Andrews		# Force slice 1 to be marked active. This is necessary
6236b8c8deeSWill Andrews		# for booting the image from a USB device to work.
6246b8c8deeSWill Andrews		print "a 1"
6256b8c8deeSWill Andrews	}
6266b8c8deeSWill Andrews	' > ${NANO_OBJ}/_.fdisk
6276b8c8deeSWill Andrews
6286b8c8deeSWill Andrews	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
6296b8c8deeSWill Andrews	MNT=${NANO_OBJ}/_.mnt
6306b8c8deeSWill Andrews	mkdir -p ${MNT}
6316b8c8deeSWill Andrews
6326b8c8deeSWill Andrews	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
6336b8c8deeSWill Andrews		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
6346b8c8deeSWill Andrews			-y ${NANO_HEADS}`
6356b8c8deeSWill Andrews	else
6366b8c8deeSWill Andrews		echo "Creating md backing file..."
6376b8c8deeSWill Andrews		nano_rm -f ${IMG}
6386b8c8deeSWill Andrews		dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
6396b8c8deeSWill Andrews		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
6406b8c8deeSWill Andrews			-y ${NANO_HEADS}`
6416b8c8deeSWill Andrews	fi
6426b8c8deeSWill Andrews
6436b8c8deeSWill Andrews	trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
6446b8c8deeSWill Andrews
6456b8c8deeSWill Andrews	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
6466b8c8deeSWill Andrews	fdisk ${MD}
6476b8c8deeSWill Andrews	# XXX: params
6486b8c8deeSWill Andrews	# XXX: pick up cached boot* files, they may not be in image anymore.
6496b8c8deeSWill Andrews	if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
6506b8c8deeSWill Andrews		boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
6516b8c8deeSWill Andrews	fi
6526b8c8deeSWill Andrews	if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
6537b77d3eaSWarner Losh		bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT}
6546b8c8deeSWill Andrews	else
6557b77d3eaSWarner Losh		bsdlabel -w ${MD}${NANO_SLICE_ROOT}
6566b8c8deeSWill Andrews	fi
6577b77d3eaSWarner Losh	bsdlabel ${MD}${NANO_SLICE_ROOT}
6586b8c8deeSWill Andrews
6596b8c8deeSWill Andrews	# Create first image
6607b77d3eaSWarner Losh	populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a"
6617b77d3eaSWarner Losh	mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT}
6626b8c8deeSWill Andrews	echo "Generating mtree..."
6631934c7aaSWarner Losh	( cd "${MNT}" && mtree -c ) > ${NANO_OBJ}/_.mtree
6641934c7aaSWarner Losh	( cd "${MNT}" && du -k ) > ${NANO_OBJ}/_.du
6651934c7aaSWarner Losh	nano_umount "${MNT}"
6666b8c8deeSWill Andrews
6676b8c8deeSWill Andrews	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
6686b8c8deeSWill Andrews		# Duplicate to second image (if present)
6696b8c8deeSWill Andrews		echo "Duplicating to second image..."
6707b77d3eaSWarner Losh		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
6717b77d3eaSWarner Losh		mount /dev/${MD}${NANO_SLICE_ALTROOT}a ${MNT}
6726b8c8deeSWill Andrews		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
6736b8c8deeSWill Andrews		do
6747b77d3eaSWarner Losh			sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
6756b8c8deeSWill Andrews		done
6766b8c8deeSWill Andrews		nano_umount ${MNT}
6776b8c8deeSWill Andrews		# Override the label from the first partition so we
6786b8c8deeSWill Andrews		# don't confuse glabel with duplicates.
6796b8c8deeSWill Andrews		if [ ! -z ${NANO_LABEL} ]; then
6807b77d3eaSWarner Losh			tunefs -L ${NANO_LABEL}"${NANO_SLICE_ALTROOT}a" /dev/${MD}${NANO_SLICE_ALTROOT}a
6816b8c8deeSWill Andrews		fi
6826b8c8deeSWill Andrews	fi
6836b8c8deeSWill Andrews
6846b8c8deeSWill Andrews	# Create Config slice
6857b77d3eaSWarner Losh	populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
6866b8c8deeSWill Andrews
6876b8c8deeSWill Andrews	# Create Data slice, if any.
6887b77d3eaSWarner Losh	if [ ! -z $NANO_SLICE_DATA -a $NANO_SLICE_CFG = $NANO_SLICE_DATA -a \
6897b77d3eaSWarner Losh	   $NANO_DATASIZE -ne 0 ]; then
6907b77d3eaSWarner Losh		pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
6917b77d3eaSWarner Losh		exit 2
6927b77d3eaSWarner Losh	fi
6937b77d3eaSWarner Losh	if [ $NANO_DATASIZE -ne 0 -a ! -z $NANO_SLICE_DATA ] ; then
6947b77d3eaSWarner Losh		populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
6956b8c8deeSWill Andrews	fi
6966b8c8deeSWill Andrews
6976b8c8deeSWill Andrews	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
6986b8c8deeSWill Andrews		if [ ${NANO_IMAGE_MBRONLY} ]; then
6996b8c8deeSWill Andrews			echo "Writing out _.disk.mbr..."
7006b8c8deeSWill Andrews			dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
7016b8c8deeSWill Andrews		else
7026b8c8deeSWill Andrews			echo "Writing out ${NANO_IMGNAME}..."
7036b8c8deeSWill Andrews			dd if=/dev/${MD} of=${IMG} bs=64k
7046b8c8deeSWill Andrews		fi
7056b8c8deeSWill Andrews
7066b8c8deeSWill Andrews		echo "Writing out ${NANO_IMGNAME}..."
7076b8c8deeSWill Andrews		dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
7086b8c8deeSWill Andrews	fi
7096b8c8deeSWill Andrews
7106b8c8deeSWill Andrews	if ${do_copyout_partition} ; then
7116b8c8deeSWill Andrews		echo "Writing out _.disk.image..."
7127b77d3eaSWarner Losh		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
7136b8c8deeSWill Andrews	fi
7146b8c8deeSWill Andrews	mdconfig -d -u $MD
7156b8c8deeSWill Andrews
7166b8c8deeSWill Andrews	trap - 1 2 15
7176b8c8deeSWill Andrews	trap nano_cleanup EXIT
7186b8c8deeSWill Andrews
7196b8c8deeSWill Andrews	) > ${NANO_OBJ}/_.di 2>&1
7206b8c8deeSWill Andrews)
7216b8c8deeSWill Andrews
7226b8c8deeSWill Andrewslast_orders ( ) (
7236b8c8deeSWill Andrews	# Redefine this function with any last orders you may have
7246b8c8deeSWill Andrews	# after the build completed, for instance to copy the finished
7256b8c8deeSWill Andrews	# image to a more convenient place:
7266b8c8deeSWill Andrews	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
7276b8c8deeSWill Andrews	true
7286b8c8deeSWill Andrews)
7296b8c8deeSWill Andrews
7306b8c8deeSWill Andrews#######################################################################
7316b8c8deeSWill Andrews#
7326b8c8deeSWill Andrews# Optional convenience functions.
7336b8c8deeSWill Andrews#
7346b8c8deeSWill Andrews#######################################################################
7356b8c8deeSWill Andrews
7366b8c8deeSWill Andrews#######################################################################
7376b8c8deeSWill Andrews# Common Flash device geometries
7386b8c8deeSWill Andrews#
7396b8c8deeSWill Andrews
7406b8c8deeSWill AndrewsFlashDevice ( ) {
7416b8c8deeSWill Andrews	if [ -d ${NANO_TOOLS} ] ; then
7426b8c8deeSWill Andrews		. ${NANO_TOOLS}/FlashDevice.sub
7436b8c8deeSWill Andrews	else
7446b8c8deeSWill Andrews		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
7456b8c8deeSWill Andrews	fi
7466b8c8deeSWill Andrews	sub_FlashDevice $1 $2
7476b8c8deeSWill Andrews}
7486b8c8deeSWill Andrews
7496b8c8deeSWill Andrews#######################################################################
7506b8c8deeSWill Andrews# USB device geometries
7516b8c8deeSWill Andrews#
7526b8c8deeSWill Andrews# Usage:
7536b8c8deeSWill Andrews#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
7546b8c8deeSWill Andrews#
7556b8c8deeSWill Andrews# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
7566b8c8deeSWill Andrews#
7576b8c8deeSWill Andrews# Note that the capacity of a flash key is usually advertised in MB or
7586b8c8deeSWill Andrews# GB, *not* MiB/GiB. As such, the precise number of cylinders available
7596b8c8deeSWill Andrews# for C/H/S geometry may vary depending on the actual flash geometry.
7606b8c8deeSWill Andrews#
7616b8c8deeSWill Andrews# The following generic device layouts are understood:
7626b8c8deeSWill Andrews#  generic           An alias for generic-hdd.
7636b8c8deeSWill Andrews#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
7646b8c8deeSWill Andrews#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
7656b8c8deeSWill Andrews#
7666b8c8deeSWill Andrews# The generic-hdd device is preferred for flash devices larger than 1GB.
7676b8c8deeSWill Andrews#
7686b8c8deeSWill Andrews
7696b8c8deeSWill AndrewsUsbDevice ( ) {
7706b8c8deeSWill Andrews	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
7716b8c8deeSWill Andrews	case $a1 in
7726b8c8deeSWill Andrews	generic-fdd)
7736b8c8deeSWill Andrews		NANO_HEADS=64
7746b8c8deeSWill Andrews		NANO_SECTS=32
7756b8c8deeSWill Andrews		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
7766b8c8deeSWill Andrews		;;
7776b8c8deeSWill Andrews	generic|generic-hdd)
7786b8c8deeSWill Andrews		NANO_HEADS=255
7796b8c8deeSWill Andrews		NANO_SECTS=63
7806b8c8deeSWill Andrews		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
7816b8c8deeSWill Andrews		;;
7826b8c8deeSWill Andrews	*)
7836b8c8deeSWill Andrews		echo "Unknown USB flash device"
7846b8c8deeSWill Andrews		exit 2
7856b8c8deeSWill Andrews		;;
7866b8c8deeSWill Andrews	esac
7876b8c8deeSWill Andrews}
7886b8c8deeSWill Andrews
7896b8c8deeSWill Andrews#######################################################################
7906b8c8deeSWill Andrews# Setup serial console
7916b8c8deeSWill Andrews
7926b8c8deeSWill Andrewscust_comconsole ( ) (
7936b8c8deeSWill Andrews	# Enable getty on console
7946b8c8deeSWill Andrews	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
7956b8c8deeSWill Andrews
7966b8c8deeSWill Andrews	# Disable getty on syscons devices
7976b8c8deeSWill Andrews	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
7986b8c8deeSWill Andrews
7996b8c8deeSWill Andrews	# Tell loader to use serial console early.
8006b8c8deeSWill Andrews	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
8016b8c8deeSWill Andrews)
8026b8c8deeSWill Andrews
8036b8c8deeSWill Andrews#######################################################################
8046b8c8deeSWill Andrews# Allow root login via ssh
8056b8c8deeSWill Andrews
8066b8c8deeSWill Andrewscust_allow_ssh_root ( ) (
8076b8c8deeSWill Andrews	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
8086b8c8deeSWill Andrews	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
8096b8c8deeSWill Andrews)
8106b8c8deeSWill Andrews
8116b8c8deeSWill Andrews#######################################################################
8126b8c8deeSWill Andrews# Install the stuff under ./Files
8136b8c8deeSWill Andrews
8146b8c8deeSWill Andrewscust_install_files ( ) (
8151934c7aaSWarner Losh	cd "${NANO_TOOLS}/Files"
8166b8c8deeSWill Andrews	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
8176b8c8deeSWill Andrews)
8186b8c8deeSWill Andrews
8196b8c8deeSWill Andrews#######################################################################
8206b8c8deeSWill Andrews# Install packages from ${NANO_PACKAGE_DIR}
8216b8c8deeSWill Andrews
8226b8c8deeSWill Andrewscust_pkgng ( ) (
8236b8c8deeSWill Andrews
8246b8c8deeSWill Andrews	# If the package directory doesn't exist, we're done.
8256b8c8deeSWill Andrews	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
8266b8c8deeSWill Andrews		echo "DONE 0 packages"
8276b8c8deeSWill Andrews		return 0
8286b8c8deeSWill Andrews	fi
8296b8c8deeSWill Andrews
8306b8c8deeSWill Andrews	# Find a pkg-* package
8316b8c8deeSWill Andrews	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
8326b8c8deeSWill Andrews		_NANO_PKG_PACKAGE=`basename "$x"`
8336b8c8deeSWill Andrews	done
8346b8c8deeSWill Andrews	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
8356b8c8deeSWill Andrews		echo "FAILED: need a pkg/ package for bootstrapping"
8366b8c8deeSWill Andrews		exit 2
8376b8c8deeSWill Andrews	fi
8386b8c8deeSWill Andrews
8396b8c8deeSWill Andrews	# Copy packages into chroot
8406b8c8deeSWill Andrews	mkdir -p ${NANO_WORLDDIR}/Pkg
8416b8c8deeSWill Andrews	(
8421934c7aaSWarner Losh		cd "${NANO_PACKAGE_DIR}"
8431934c7aaSWarner Losh		find "${NANO_PACKAGE_LIST}" -print |
8446b8c8deeSWill Andrews		cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
8456b8c8deeSWill Andrews	)
8466b8c8deeSWill Andrews
8476b8c8deeSWill Andrews	#Bootstrap pkg
8486b8c8deeSWill Andrews	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
8496b8c8deeSWill Andrews	CR pkg -N >/dev/null 2>&1
8506b8c8deeSWill Andrews	if [ "$?" -ne "0" ]; then
8516b8c8deeSWill Andrews		echo "FAILED: pkg bootstrapping faied"
8526b8c8deeSWill Andrews		exit 2
8536b8c8deeSWill Andrews	fi
8546b8c8deeSWill Andrews	nano_rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
8556b8c8deeSWill Andrews
8566b8c8deeSWill Andrews	# Count & report how many we have to install
8576b8c8deeSWill Andrews	todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
8586b8c8deeSWill Andrews	todo=$(expr $todo + 1) # add one for pkg since it is installed already
8596b8c8deeSWill Andrews	echo "=== TODO: $todo"
8606b8c8deeSWill Andrews	ls ${NANO_WORLDDIR}/Pkg
8616b8c8deeSWill Andrews	echo "==="
8626b8c8deeSWill Andrews	while true
8636b8c8deeSWill Andrews	do
8646b8c8deeSWill Andrews		# Record how many we have now
8656b8c8deeSWill Andrews 		have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
8666b8c8deeSWill Andrews
8676b8c8deeSWill Andrews		# Attempt to install more packages
8686b8c8deeSWill Andrews		CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
8696b8c8deeSWill Andrews
8706b8c8deeSWill Andrews		# See what that got us
8716b8c8deeSWill Andrews 		now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
8726b8c8deeSWill Andrews		echo "=== NOW $now"
8736b8c8deeSWill Andrews		CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
8746b8c8deeSWill Andrews		echo "==="
8756b8c8deeSWill Andrews		if [ $now -eq $todo ] ; then
8766b8c8deeSWill Andrews			echo "DONE $now packages"
8776b8c8deeSWill Andrews			break
8786b8c8deeSWill Andrews		elif [ $now -eq $have ] ; then
8796b8c8deeSWill Andrews			echo "FAILED: Nothing happened on this pass"
8806b8c8deeSWill Andrews			exit 2
8816b8c8deeSWill Andrews		fi
8826b8c8deeSWill Andrews	done
8836b8c8deeSWill Andrews	nano_rm -rf ${NANO_WORLDDIR}/Pkg
8846b8c8deeSWill Andrews)
8856b8c8deeSWill Andrews
8866b8c8deeSWill Andrews#######################################################################
8876b8c8deeSWill Andrews# Convenience function:
8886b8c8deeSWill Andrews# 	Register all args as customize function.
8896b8c8deeSWill Andrews
8906b8c8deeSWill Andrewscustomize_cmd ( ) {
8916b8c8deeSWill Andrews	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
8926b8c8deeSWill Andrews}
8936b8c8deeSWill Andrews
8946b8c8deeSWill Andrews#######################################################################
8956b8c8deeSWill Andrews# Convenience function:
8966b8c8deeSWill Andrews# 	Register all args as late customize function to run just before
8976b8c8deeSWill Andrews#	image creation.
8986b8c8deeSWill Andrews
8996b8c8deeSWill Andrewslate_customize_cmd ( ) {
9006b8c8deeSWill Andrews	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
9016b8c8deeSWill Andrews}
9026b8c8deeSWill Andrews
9036b8c8deeSWill Andrews#######################################################################
9046b8c8deeSWill Andrews#
9056b8c8deeSWill Andrews# All set up to go...
9066b8c8deeSWill Andrews#
9076b8c8deeSWill Andrews#######################################################################
9086b8c8deeSWill Andrews
9096b8c8deeSWill Andrews# Progress Print
9106b8c8deeSWill Andrews#	Print $2 at level $1.
9116b8c8deeSWill Andrewspprint ( ) (
9126b8c8deeSWill Andrews    if [ "$1" -le $PPLEVEL ]; then
9136b8c8deeSWill Andrews	runtime=$(( `date +%s` - $NANO_STARTTIME ))
9146b8c8deeSWill Andrews	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
9156b8c8deeSWill Andrews    fi
9166b8c8deeSWill Andrews)
9176b8c8deeSWill Andrews
9186b8c8deeSWill Andrewsusage ( ) {
9196b8c8deeSWill Andrews	(
9206b8c8deeSWill Andrews	echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
9216b8c8deeSWill Andrews	echo "	-b	suppress builds (both kernel and world)"
9226b8c8deeSWill Andrews	echo "	-c	specify config file"
9236b8c8deeSWill Andrews	echo "	-f	suppress code slice extraction"
9246b8c8deeSWill Andrews	echo "	-i	suppress disk image build"
9256b8c8deeSWill Andrews	echo "	-K	suppress installkernel"
9266b8c8deeSWill Andrews	echo "	-k	suppress buildkernel"
9276b8c8deeSWill Andrews	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
9286b8c8deeSWill Andrews	echo "	-q	make output more quiet"
9296b8c8deeSWill Andrews	echo "	-v	make output more verbose"
9306b8c8deeSWill Andrews	echo "	-w	suppress buildworld"
9316b8c8deeSWill Andrews	) 1>&2
9326b8c8deeSWill Andrews	exit 2
9336b8c8deeSWill Andrews}
9346b8c8deeSWill Andrews
9356b8c8deeSWill Andrews#######################################################################
9366b8c8deeSWill Andrews# Setup and Export Internal variables
9376b8c8deeSWill Andrews#
9386b8c8deeSWill Andrews
9391934c7aaSWarner Loshexport_var ( ) {		# Don't wawnt a subshell
9406b8c8deeSWill Andrews	var=$1
9416b8c8deeSWill Andrews	# Lookup value of the variable.
9426b8c8deeSWill Andrews	eval val=\$$var
9436b8c8deeSWill Andrews	pprint 3 "Setting variable: $var=\"$val\""
9446b8c8deeSWill Andrews	export $1
9456b8c8deeSWill Andrews}
9466b8c8deeSWill Andrews
9476b8c8deeSWill Andrews# Call this function to set defaults _after_ parsing options.
9481934c7aaSWarner Losh# dont want a subshell otherwise variable setting is thrown away.
9496b8c8deeSWill Andrewsset_defaults_and_export ( ) {
9506b8c8deeSWill Andrews	test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
9516b8c8deeSWill Andrews	test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
9526b8c8deeSWill Andrews	test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
9536b8c8deeSWill Andrews	NANO_WORLDDIR=${NANO_OBJ}/_.w
9546b8c8deeSWill Andrews	NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
9556b8c8deeSWill Andrews	NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
9566b8c8deeSWill Andrews
9576b8c8deeSWill Andrews	# Override user's NANO_DRIVE if they specified a NANO_LABEL
958*c275d9ecSWarner Losh	[ ! -z "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true
9596b8c8deeSWill Andrews
9606b8c8deeSWill Andrews	# Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
9616b8c8deeSWill Andrews	[ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
962*c275d9ecSWarner Losh		NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true
963*c275d9ecSWarner Losh
964*c275d9ecSWarner Losh	[ ! -z "${NANO_NOPRIV_BUILD" ] && [ -z "${NANO_METALOG}"] && \
965*c275d9ecSWarner Losh		NANO_METALOG=${NANO_OBJ}/_.metalog || true
9666b8c8deeSWill Andrews
9676b8c8deeSWill Andrews	NANO_STARTTIME=`date +%s`
9686b8c8deeSWill Andrews	pprint 3 "Exporting NanoBSD variables"
9696b8c8deeSWill Andrews	export_var MAKEOBJDIRPREFIX
9706b8c8deeSWill Andrews	export_var NANO_ARCH
9716b8c8deeSWill Andrews	export_var NANO_CODESIZE
9726b8c8deeSWill Andrews	export_var NANO_CONFSIZE
9736b8c8deeSWill Andrews	export_var NANO_CUSTOMIZE
9746b8c8deeSWill Andrews	export_var NANO_DATASIZE
9756b8c8deeSWill Andrews	export_var NANO_DRIVE
9766b8c8deeSWill Andrews	export_var NANO_HEADS
9776b8c8deeSWill Andrews	export_var NANO_IMAGES
9786b8c8deeSWill Andrews	export_var NANO_IMGNAME
9796b8c8deeSWill Andrews	export_var NANO_MAKE
9806b8c8deeSWill Andrews	export_var NANO_MAKE_CONF_BUILD
9816b8c8deeSWill Andrews	export_var NANO_MAKE_CONF_INSTALL
9826b8c8deeSWill Andrews	export_var NANO_MEDIASIZE
9836b8c8deeSWill Andrews	export_var NANO_NAME
9846b8c8deeSWill Andrews	export_var NANO_NEWFS
9856b8c8deeSWill Andrews	export_var NANO_OBJ
9866b8c8deeSWill Andrews	export_var NANO_PMAKE
9876b8c8deeSWill Andrews	export_var NANO_SECTS
9886b8c8deeSWill Andrews	export_var NANO_SRC
9896b8c8deeSWill Andrews	export_var NANO_TOOLS
9906b8c8deeSWill Andrews	export_var NANO_WORLDDIR
9916b8c8deeSWill Andrews	export_var NANO_BOOT0CFG
9926b8c8deeSWill Andrews	export_var NANO_BOOTLOADER
9936b8c8deeSWill Andrews	export_var NANO_LABEL
9946b8c8deeSWill Andrews	export_var NANO_MODULES
995*c275d9ecSWarner Losh	export_var NANO_NOPRIV_BUILD
996*c275d9ecSWarner Losh	export_var NANO_METALOG
9971934c7aaSWarner Losh	export_var SRCCONF
998a9d9c49fSWarner Losh	export_var SRC_ENV_CONF
9996b8c8deeSWill Andrews}
1000