1148422Sphk#!/bin/sh
2150366Sphk#
3148422Sphk# Copyright (c) 2005 Poul-Henning Kamp.
4150366Sphk# All rights reserved.
5148422Sphk#
6150366Sphk# Redistribution and use in source and binary forms, with or without
7150366Sphk# modification, are permitted provided that the following conditions
8150366Sphk# are met:
9150366Sphk# 1. Redistributions of source code must retain the above copyright
10150366Sphk#    notice, this list of conditions and the following disclaimer.
11150366Sphk# 2. Redistributions in binary form must reproduce the above copyright
12150366Sphk#    notice, this list of conditions and the following disclaimer in the
13150366Sphk#    documentation and/or other materials provided with the distribution.
14148422Sphk#
15150366Sphk# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16150366Sphk# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17150366Sphk# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18150366Sphk# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19150366Sphk# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20150366Sphk# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21150366Sphk# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22150366Sphk# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23150366Sphk# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24150366Sphk# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25150366Sphk# SUCH DAMAGE.
26150366Sphk#
27148422Sphk# $FreeBSD$
28148422Sphk#
29148422Sphk
30148422Sphkset -e
31148422Sphk
32150366Sphk#######################################################################
33150366Sphk#
34150366Sphk# Setup default values for all controlling variables.
35150366Sphk# These values can be overridden from the config file(s)
36150366Sphk#
37150366Sphk#######################################################################
38150366Sphk
39148422Sphk# Name of this NanoBSD build.  (Used to construct workdir names)
40148422SphkNANO_NAME=full
41148422Sphk
42148422Sphk# Source tree directory
43148422SphkNANO_SRC=/usr/src
44148422Sphk
45150366Sphk# Where nanobsd additional files live under the source tree
46150366SphkNANO_TOOLS=tools/tools/nanobsd
47150366Sphk
48170607Sphk# Where cust_pkg() finds packages to install
49170607SphkNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
50186181SphkNANO_PACKAGE_LIST="*"
51170607Sphk
52265166Simp# where package metadata gets placed
53265166SimpNANO_PKG_META_BASE=/var/db
54265166Simp
55148422Sphk# Object tree directory
56148422Sphk# default is subdir of /usr/obj
57148648Sphk#NANO_OBJ=""
58148422Sphk
59175258Ssimon# The directory to put the final images
60175258Ssimon# default is ${NANO_OBJ}
61175258Ssimon#NANO_DISKIMGDIR=""
62175258Ssimon
63265166Simp# Make & parallel Make
64265166SimpNANO_MAKE="make"
65148422SphkNANO_PMAKE="make -j 3"
66148422Sphk
67183284Sbms# The default name for any image we create.
68183284SbmsNANO_IMGNAME="_.disk.full"
69183284Sbms
70148422Sphk# Options to put in make.conf during buildworld only
71148422SphkCONF_BUILD=' '
72148422Sphk
73148422Sphk# Options to put in make.conf during installworld only
74148422SphkCONF_INSTALL=' '
75148422Sphk
76148422Sphk# Options to put in make.conf during both build- & installworld.
77148422SphkCONF_WORLD=' '
78148422Sphk
79148422Sphk# Kernel config file to use
80148422SphkNANO_KERNEL=GENERIC
81148422Sphk
82266441Sthomas# Kernel modules to install. If empty, no modules are installed.
83266441Sthomas# Use "default" to install all built modules.
84214955Sn_hibmaNANO_MODULES=
85214955Sn_hibma
86149073Sphk# Customize commands.
87149073SphkNANO_CUSTOMIZE=""
88149073Sphk
89181461Sphk# Late customize commands.
90181461SphkNANO_LATE_CUSTOMIZE=""
91181461Sphk
92148648Sphk# Newfs paramters to use
93265166SimpNANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
94148648Sphk
95149082Sphk# The drive name of the media at runtime
96149082SphkNANO_DRIVE=ad0
97149082Sphk
98148648Sphk# Target media size in 512 bytes sectors
99265166SimpNANO_MEDIASIZE=2000000
100148648Sphk
101148648Sphk# Number of code images on media (1 or 2)
102148422SphkNANO_IMAGES=2
103148648Sphk
104171986Sphk# 0 -> Leave second image all zeroes so it compresses better.
105171986Sphk# 1 -> Initialize second image with a copy of the first
106171986SphkNANO_INIT_IMG2=1
107171986Sphk
108149535Ssimon# Size of code file system in 512 bytes sectors
109149120Sphk# If zero, size will be as large as possible.
110149120SphkNANO_CODESIZE=0
111149120Sphk
112149120Sphk# Size of configuration file system in 512 bytes sectors
113149120Sphk# Cannot be zero.
114148422SphkNANO_CONFSIZE=2048
115148648Sphk
116148648Sphk# Size of data file system in 512 bytes sectors
117149120Sphk# If zero: no partition configured.
118149120Sphk# If negative: max size possible
119149120SphkNANO_DATASIZE=0
120148422Sphk
121154466Sphk# Size of the /etc ramdisk in 512 bytes sectors
122154466SphkNANO_RAM_ETCSIZE=10240
123154466Sphk
124154466Sphk# Size of the /tmp+/var ramdisk in 512 bytes sectors
125154466SphkNANO_RAM_TMPVARSIZE=10240
126154466Sphk
127148422Sphk# Media geometry, only relevant if bios doesn't understand LBA.
128170607SphkNANO_SECTS=63
129148422SphkNANO_HEADS=16
130148422Sphk
131168894Sadrian# boot0 flags/options and configuration
132168894SadrianNANO_BOOT0CFG="-o packet -s 1 -m 3"
133168894SadrianNANO_BOOTLOADER="boot/boot0sio"
134168894Sadrian
135209225Simp# boot2 flags/options
136209225Simp# default force serial console
137209225SimpNANO_BOOT2CFG="-h"
138209225Simp
139175258Ssimon# Backing type of md(4) device
140175258Ssimon# Can be "file" or "swap"
141175258SsimonNANO_MD_BACKING="file"
142175258Ssimon
143265166Simp# for swap type md(4) backing, write out the mbr only
144265166SimpNANO_IMAGE_MBRONLY=true
145265166Simp
146183314Ssimon# Progress Print level
147183314SsimonPPLEVEL=3
148183314Ssimon
149212938Simp# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
150212938Simp# in preference to /dev/${NANO_DRIVE}
151212938Simp# Root partition will be ${NANO_LABEL}s{1,2}
152212938Simp# /cfg partition will be ${NANO_LABEL}s3
153212938Simp# /data partition will be ${NANO_LABEL}s4
154212938SimpNANO_LABEL=""
155212938Simp
156148422Sphk#######################################################################
157206276Simp# Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
158212938Simp# Unfortunately, there's no way to set TARGET at this time, and it
159206276Simp# conflates the two, so architectures where TARGET != TARGET_ARCH do
160206276Simp# not work.  This defaults to the arch of the current machine.
161148648Sphk
162205980SimpNANO_ARCH=`uname -p`
163148648Sphk
164215069Simp# Directory to populate /cfg from
165215069SimpNANO_CFGDIR=""
166215069Simp
167215069Simp# Directory to populate /data from
168215069SimpNANO_DATADIR=""
169215069Simp
170265166Simp# src.conf to use when building the image. Defaults to /dev/null for the sake
171265166Simp# of determinism.
172265166SimpSRCCONF=${SRCCONF:=/dev/null}
173265166Simp 
174148648Sphk#######################################################################
175150366Sphk#
176150366Sphk# The functions which do the real work.
177150366Sphk# Can be overridden from the config file(s)
178150366Sphk#
179150366Sphk#######################################################################
180148422Sphk
181274561Simp# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
182274561Simp# hosts for now.
183274561Simpnano_rm ( ) {
184274561Simp	case $(uname -r) in
185274561Simp	7*|8*|9*) rm $* ;;
186274561Simp	*) rm -x $* ;;
187274561Simp	esac
188274561Simp}
189274561Simp
190265166Simp# run in the world chroot, errors fatal
191265166SimpCR()
192265166Simp{
193265166Simp	chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
194265166Simp}
195265166Simp
196265166Simp# run in the world chroot, errors not fatal
197265166SimpCR0()
198265166Simp{
199265166Simp	chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
200265166Simp}
201265166Simp
202265166Simpnano_cleanup ( ) (
203265166Simp	if [ $? -ne 0 ]; then
204265166Simp		echo "Error encountered.  Check for errors in last log file." 1>&2
205265166Simp	fi
206265166Simp	exit $?
207265166Simp)
208265166Simp
209149120Sphkclean_build ( ) (
210183314Ssimon	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
211148422Sphk
212274561Simp	if ! nano_rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
213265166Simp		chflags -R noschg ${MAKEOBJDIRPREFIX}/
214274561Simp		nano_rm -r ${MAKEOBJDIRPREFIX}/
215148422Sphk	fi
216148422Sphk	mkdir -p ${MAKEOBJDIRPREFIX}
217150343Sphk	printenv > ${MAKEOBJDIRPREFIX}/_.env
218148422Sphk)
219148422Sphk
220148422Sphkmake_conf_build ( ) (
221194431Sn_hibma	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
222148422Sphk
223194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
224194431Sn_hibma	echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
225148422Sphk)
226148422Sphk
227148422Sphkbuild_world ( ) (
228183314Ssimon	pprint 2 "run buildworld"
229183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
230148422Sphk
231148422Sphk	cd ${NANO_SRC}
232183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
233265166Simp		SRCCONF=${SRCCONF} \
234194431Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
235148422Sphk		> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
236148422Sphk)
237148422Sphk
238148422Sphkbuild_kernel ( ) (
239265166Simp	local extra
240265166Simp
241183314Ssimon	pprint 2 "build kernel ($NANO_KERNEL)"
242183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
243148422Sphk
244214955Sn_hibma	(
245148422Sphk	if [ -f ${NANO_KERNEL} ] ; then
246265894Sthomas		kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
247214955Sn_hibma		kernconf=$(basename ${NANO_KERNEL})
248214955Sn_hibma	else
249214955Sn_hibma		kernconf=${NANO_KERNEL}
250148422Sphk	fi
251148422Sphk
252214955Sn_hibma	cd ${NANO_SRC};
253181083Ssam	# unset these just in case to avoid compiler complaints
254181083Ssam	# when cross-building
255181083Ssam	unset TARGET_CPUTYPE
256214955Sn_hibma	# Note: We intentionally build all modules, not only the ones in
257214955Sn_hibma	# NANO_MODULES so the built world can be reused by multiple images.
258265894Sthomas	eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
259265894Sthomas		SRCCONF='${SRCCONF}' \
260265894Sthomas		__MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \
261265894Sthomas		${kernconfdir_arg} KERNCONF=${kernconf}"
262214955Sn_hibma	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
263148422Sphk)
264148422Sphk
265148422Sphkclean_world ( ) (
266194431Sn_hibma	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
267194431Sn_hibma		pprint 2 "Clean and create object directory (${NANO_OBJ})"
268274561Simp		if ! nano_rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
269194431Sn_hibma			chflags -R noschg ${NANO_OBJ}
270274561Simp			nano_rm -r ${NANO_OBJ}/
271194431Sn_hibma		fi
272194431Sn_hibma		mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
273194431Sn_hibma		printenv > ${NANO_OBJ}/_.env
274148422Sphk	else
275194431Sn_hibma		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
276274561Simp		if ! nano_rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
277194431Sn_hibma			chflags -R noschg ${NANO_WORLDDIR}
278274561Simp			nano_rm -rf ${NANO_WORLDDIR}/
279194431Sn_hibma		fi
280194431Sn_hibma		mkdir -p ${NANO_WORLDDIR}
281148422Sphk	fi
282148422Sphk)
283148422Sphk
284148422Sphkmake_conf_install ( ) (
285194431Sn_hibma	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
286148422Sphk
287194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
288194431Sn_hibma	echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
289148422Sphk)
290148422Sphk
291148422Sphkinstall_world ( ) (
292183314Ssimon	pprint 2 "installworld"
293194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.iw"
294148422Sphk
295148422Sphk	cd ${NANO_SRC}
296183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
297265166Simp	${NANO_MAKE} SRCCONF=${SRCCONF} \
298265166Simp		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
299148422Sphk		DESTDIR=${NANO_WORLDDIR} \
300194431Sn_hibma		> ${NANO_OBJ}/_.iw 2>&1
301148422Sphk	chflags -R noschg ${NANO_WORLDDIR}
302148422Sphk)
303148422Sphk
304148422Sphkinstall_etc ( ) (
305148422Sphk
306183314Ssimon	pprint 2 "install /etc"
307194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.etc"
308148422Sphk
309160783Sthomas	cd ${NANO_SRC}
310183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
311265166Simp	${NANO_MAKE} SRCCONF=${SRCCONF} \
312265166Simp		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
313148422Sphk		DESTDIR=${NANO_WORLDDIR} \
314194431Sn_hibma		> ${NANO_OBJ}/_.etc 2>&1
315209209Simp	# make.conf doesn't get created by default, but some ports need it
316209209Simp	# so they can spam it.
317209209Simp	cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
318148422Sphk)
319148422Sphk
320148422Sphkinstall_kernel ( ) (
321265166Simp	local extra
322265166Simp
323214955Sn_hibma	pprint 2 "install kernel ($NANO_KERNEL)"
324194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.ik"
325148422Sphk
326214955Sn_hibma	(
327214955Sn_hibma	if [ -f ${NANO_KERNEL} ] ; then
328265894Sthomas		kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
329214955Sn_hibma		kernconf=$(basename ${NANO_KERNEL})
330214955Sn_hibma	else
331214955Sn_hibma		kernconf=${NANO_KERNEL}
332214955Sn_hibma	fi
333214955Sn_hibma
334266441Sthomas	# Install all built modules if NANO_MODULES=default,
335266441Sthomas	# else install only listed modules (none if NANO_MODULES is empty).
336266441Sthomas	if [ "${NANO_MODULES}" != "default" ]; then
337266441Sthomas		modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'"
338266441Sthomas	fi
339266441Sthomas
340148422Sphk	cd ${NANO_SRC}
341265894Sthomas	eval "TARGET_ARCH=${NANO_ARCH} ${NANO_MAKE} installkernel \
342265894Sthomas		DESTDIR='${NANO_WORLDDIR}' \
343265894Sthomas		SRCCONF='${SRCCONF}' \
344265894Sthomas		__MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
345265894Sthomas		${kernconfdir_arg} KERNCONF=${kernconf} \
346266441Sthomas		${modules_override_arg}"
347214955Sn_hibma	) > ${NANO_OBJ}/_.ik 2>&1
348148422Sphk)
349148422Sphk
350274561Simpnative_xtools ( ) (
351274561Simp	print 2 "Installing the optimized native build tools for cross env"
352274561Simp	pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
353274561Simp
354274561Simp	cd ${NANO_SRC}
355274561Simp	env TARGET_ARCH=${NANO_ARCH} \
356274561Simp	${NANO_MAKE} SRCCONF=${SRCCONF} \
357274561Simp		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} native-xtools \
358274561Simp		DESTDIR=${NANO_WORLDDIR} \
359274561Simp		> ${NANO_OBJ}/_.native_xtools 2>&1
360274561Simp)
361274561Simp
362150366Sphkrun_customize() (
363150366Sphk
364183314Ssimon	pprint 2 "run customize scripts"
365150366Sphk	for c in $NANO_CUSTOMIZE
366150366Sphk	do
367183314Ssimon		pprint 2 "customize \"$c\""
368194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
369183314Ssimon		pprint 4 "`type $c`"
370214955Sn_hibma		( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
371150366Sphk	done
372150366Sphk)
373150366Sphk
374181461Sphkrun_late_customize() (
375181461Sphk
376183314Ssimon	pprint 2 "run late customize scripts"
377181461Sphk	for c in $NANO_LATE_CUSTOMIZE
378181461Sphk	do
379183314Ssimon		pprint 2 "late customize \"$c\""
380194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
381183314Ssimon		pprint 4 "`type $c`"
382214955Sn_hibma		( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
383181461Sphk	done
384181461Sphk)
385181461Sphk
386150366Sphksetup_nanobsd ( ) (
387183314Ssimon	pprint 2 "configure nanobsd setup"
388194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.dl"
389148422Sphk
390149103Sphk	(
391148422Sphk	cd ${NANO_WORLDDIR}
392148422Sphk
393170607Sphk	# Move /usr/local/etc to /etc/local so that the /cfg stuff
394170607Sphk	# can stomp on it.  Otherwise packages like ipsec-tools which
395170607Sphk	# have hardcoded paths under ${prefix}/etc are not tweakable.
396170607Sphk	if [ -d usr/local/etc ] ; then
397170607Sphk		(
398183727Sn_hibma		mkdir -p etc/local
399170607Sphk		cd usr/local/etc
400170607Sphk		find . -print | cpio -dumpl ../../../etc/local
401170607Sphk		cd ..
402274561Simp		nano_rm -rf etc
403170607Sphk		ln -s ../../etc/local etc
404170607Sphk		)
405170607Sphk	fi
406170607Sphk
407149082Sphk	for d in var etc
408149082Sphk	do
409149082Sphk		# link /$d under /conf
410149082Sphk		# we use hard links so we have them both places.
411149082Sphk		# the files in /$d will be hidden by the mount.
412149082Sphk		# XXX: configure /$d ramdisk size
413149082Sphk		mkdir -p conf/base/$d conf/default/$d
414149082Sphk		find $d -print | cpio -dumpl conf/base/
415149082Sphk	done
416148422Sphk
417154466Sphk	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
418154466Sphk	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
419154466Sphk
420148422Sphk	# pick up config files from the special partition
421149082Sphk	echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
422148422Sphk
423149082Sphk	# Put /tmp on the /var ramdisk (could be symlink already)
424274892Sn_hibma	nano_rm -rf tmp
425148422Sphk	ln -s var/tmp tmp
426149082Sphk
427194431Sn_hibma	) > ${NANO_OBJ}/_.dl 2>&1
428148422Sphk)
429148422Sphk
430161341Ssimonsetup_nanobsd_etc ( ) (
431183314Ssimon	pprint 2 "configure nanobsd /etc"
432161341Ssimon
433161341Ssimon	(
434161341Ssimon	cd ${NANO_WORLDDIR}
435161341Ssimon
436161341Ssimon	# create diskless marker file
437161341Ssimon	touch etc/diskless
438161341Ssimon
439170324Sphk	# Make root filesystem R/O by default
440170324Sphk	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
441170324Sphk
442161341Ssimon	# save config file for scripts
443161341Ssimon	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
444161341Ssimon
445161341Ssimon	echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
446161341Ssimon	echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
447161341Ssimon	mkdir -p cfg
448161341Ssimon	)
449161341Ssimon)
450161341Ssimon
451149014Sphkprune_usr() (
452149014Sphk
453149014Sphk	# Remove all empty directories in /usr 
454149014Sphk	find ${NANO_WORLDDIR}/usr -type d -depth -print |
455149014Sphk		while read d
456149014Sphk		do
457149014Sphk			rmdir $d > /dev/null 2>&1 || true 
458149014Sphk		done
459149014Sphk)
460149014Sphk
461212938Simpnewfs_part ( ) (
462212938Simp	local dev mnt lbl
463212938Simp	dev=$1
464212938Simp	mnt=$2
465212938Simp	lbl=$3
466212938Simp	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
467212938Simp	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
468216145Simp	mount -o async ${dev} ${mnt}
469212938Simp)
470212938Simp
471265166Simp# Convenient spot to work around any umount issues that your build environment
472265166Simp# hits by overriding this method.
473265166Simpnano_umount () (
474265166Simp	umount ${1}
475265166Simp)
476265166Simp
477209210Simppopulate_slice ( ) (
478212938Simp	local dev dir mnt lbl
479209210Simp	dev=$1
480209210Simp	dir=$2
481209210Simp	mnt=$3
482212938Simp	lbl=$4
483250034Sn_hibma	echo "Creating ${dev} (mounting on ${mnt})"
484250034Sn_hibma	newfs_part ${dev} ${mnt} ${lbl}
485250034Sn_hibma	if [ -n "${dir}" -a -d "${dir}" ]; then
486250034Sn_hibma		echo "Populating ${lbl} from ${dir}"
487250034Sn_hibma		cd ${dir}
488265166Simp		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
489250034Sn_hibma	fi
490209210Simp	df -i ${mnt}
491265166Simp	nano_umount ${mnt}
492209210Simp)
493209210Simp
494209210Simppopulate_cfg_slice ( ) (
495212938Simp	populate_slice "$1" "$2" "$3" "$4"
496209210Simp)
497209210Simp
498209210Simppopulate_data_slice ( ) (
499212938Simp	populate_slice "$1" "$2" "$3" "$4"
500209210Simp)
501209210Simp
502274561Simpcreate_diskimage ( ) (
503183314Ssimon	pprint 2 "build diskimage"
504194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.di"
505148422Sphk
506149103Sphk	(
507149120Sphk	echo $NANO_MEDIASIZE $NANO_IMAGES \
508149120Sphk		$NANO_SECTS $NANO_HEADS \
509149120Sphk		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
510148422Sphk	awk '
511150717Sphk	{
512149120Sphk		printf "# %s\n", $0
513149120Sphk
514148422Sphk		# size of cylinder in sectors
515149120Sphk		cs = $3 * $4
516148422Sphk
517148422Sphk		# number of full cylinders on media
518148422Sphk		cyl = int ($1 / cs)
519148422Sphk
520149120Sphk		# output fdisk geometry spec, truncate cyls to 1023
521148422Sphk		if (cyl <= 1023)
522149120Sphk			print "g c" cyl " h" $4 " s" $3
523148422Sphk		else
524149120Sphk			print "g c" 1023 " h" $4 " s" $3
525148422Sphk
526149120Sphk		if ($7 > 0) { 
527149120Sphk			# size of data partition in full cylinders
528149120Sphk			dsl = int (($7 + cs - 1) / cs)
529149120Sphk		} else {
530149120Sphk			dsl = 0;
531149120Sphk		}
532148422Sphk
533148422Sphk		# size of config partition in full cylinders
534149120Sphk		csl = int (($6 + cs - 1) / cs)
535148422Sphk
536150717Sphk		if ($5 == 0) {
537149120Sphk			# size of image partition(s) in full cylinders
538149120Sphk			isl = int ((cyl - dsl - csl) / $2)
539149120Sphk		} else {
540149120Sphk			isl = int (($5 + cs - 1) / cs)
541149120Sphk		}
542148422Sphk
543148422Sphk		# First image partition start at second track
544149120Sphk		print "p 1 165 " $3, isl * cs - $3
545148422Sphk		c = isl * cs;
546148422Sphk
547148422Sphk		# Second image partition (if any) also starts offset one 
548148422Sphk		# track to keep them identical.
549148422Sphk		if ($2 > 1) {
550149120Sphk			print "p 2 165 " $3 + c, isl * cs - $3
551148422Sphk			c += isl * cs;
552148422Sphk		}
553148422Sphk
554148422Sphk		# Config partition starts at cylinder boundary.
555148422Sphk		print "p 3 165 " c, csl * cs
556148422Sphk		c += csl * cs
557148422Sphk
558148422Sphk		# Data partition (if any) starts at cylinder boundary.
559149120Sphk		if ($7 > 0) {
560148422Sphk			print "p 4 165 " c, dsl * cs
561174936Ssimon		} else if ($7 < 0 && $1 > c) {
562174936Ssimon			print "p 4 165 " c, $1 - c
563150717Sphk		} else if ($1 < c) {
564150717Sphk			print "Disk space overcommitted by", \
565150717Sphk			    c - $1, "sectors" > "/dev/stderr"
566150717Sphk			exit 2
567148422Sphk		}
568183279Sbms
569183279Sbms		# Force slice 1 to be marked active. This is necessary
570183279Sbms		# for booting the image from a USB device to work.
571183279Sbms		print "a 1"
572150717Sphk	}
573194431Sn_hibma	' > ${NANO_OBJ}/_.fdisk
574148422Sphk
575183284Sbms	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
576194431Sn_hibma	MNT=${NANO_OBJ}/_.mnt
577148422Sphk	mkdir -p ${MNT}
578148422Sphk
579175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
580175258Ssimon		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
581175258Ssimon			-y ${NANO_HEADS}`
582175258Ssimon	else
583175258Ssimon		echo "Creating md backing file..."
584274561Simp		nano_rm -f ${IMG}
585216145Simp		dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
586175258Ssimon		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
587175258Ssimon			-y ${NANO_HEADS}`
588175258Ssimon	fi
589148422Sphk
590265166Simp	trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
591148422Sphk
592194431Sn_hibma	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
593149120Sphk	fdisk ${MD}
594148422Sphk	# XXX: params
595148422Sphk	# XXX: pick up cached boot* files, they may not be in image anymore.
596274561Simp	if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
597274561Simp		boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
598274561Simp	fi
599274561Simp	if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
600274561Simp		bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
601274561Simp	else
602274561Simp		bsdlabel -w ${MD}s1
603274561Simp	fi
604149120Sphk	bsdlabel ${MD}s1
605148422Sphk
606148422Sphk	# Create first image
607212990Simp	populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a"
608194437Sn_hibma	mount /dev/${MD}s1a ${MNT}
609175258Ssimon	echo "Generating mtree..."
610194431Sn_hibma	( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
611194431Sn_hibma	( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
612265166Simp	nano_umount ${MNT}
613148422Sphk
614171986Sphk	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
615148422Sphk		# Duplicate to second image (if present)
616175258Ssimon		echo "Duplicating to second image..."
617250036Sn_hibma		dd conv=sparse if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
618170322Sphk		mount /dev/${MD}s2a ${MNT}
619170322Sphk		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
620170322Sphk		do
621212169Simp			sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
622170322Sphk		done
623265166Simp		nano_umount ${MNT}
624212990Simp		# Override the label from the first partition so we
625212990Simp		# don't confuse glabel with duplicates.
626212938Simp		if [ ! -z ${NANO_LABEL} ]; then
627212990Simp			tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a
628212938Simp		fi
629148422Sphk	fi
630148422Sphk	
631148422Sphk	# Create Config slice
632212938Simp	populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3"
633148422Sphk
634148422Sphk	# Create Data slice, if any.
635194437Sn_hibma	if [ $NANO_DATASIZE -ne 0 ] ; then
636212938Simp		populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4"
637148422Sphk	fi
638148422Sphk
639175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
640265166Simp		if [ ${NANO_IMAGE_MBRONLY} ]; then
641265166Simp			echo "Writing out _.disk.mbr..."
642265166Simp			dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
643265166Simp		else
644265166Simp			echo "Writing out ${NANO_IMGNAME}..."
645265166Simp			dd if=/dev/${MD} of=${IMG} bs=64k
646265166Simp		fi
647265166Simp
648194440Sn_hibma		echo "Writing out ${NANO_IMGNAME}..."
649250036Sn_hibma		dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
650175258Ssimon	fi
651175258Ssimon
652221852Simp	if ${do_copyout_partition} ; then
653221851Simp		echo "Writing out _.disk.image..."
654250036Sn_hibma		dd conv=sparse if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
655221851Simp	fi
656148422Sphk	mdconfig -d -u $MD
657197542Ssimon
658265166Simp	trap - 1 2 15
659265166Simp	trap nano_cleanup EXIT
660197542Ssimon
661194431Sn_hibma	) > ${NANO_OBJ}/_.di 2>&1
662148422Sphk)
663148422Sphk
664154466Sphklast_orders () (
665154466Sphk	# Redefine this function with any last orders you may have
666154466Sphk	# after the build completed, for instance to copy the finished
667154466Sphk	# image to a more convenient place:
668194431Sn_hibma	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
669250036Sn_hibma	true
670154466Sphk)
671154466Sphk
672148422Sphk#######################################################################
673148422Sphk#
674150366Sphk# Optional convenience functions.
675148422Sphk#
676148422Sphk#######################################################################
677148422Sphk
678149014Sphk#######################################################################
679150367Sphk# Common Flash device geometries
680150367Sphk#
681150367Sphk
682150367SphkFlashDevice () {
683150717Sphk	if [ -d ${NANO_TOOLS} ] ; then
684150717Sphk		. ${NANO_TOOLS}/FlashDevice.sub
685150717Sphk	else
686150717Sphk		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
687150717Sphk	fi
688150367Sphk	sub_FlashDevice $1 $2
689150367Sphk}
690150367Sphk
691183284Sbms#######################################################################
692183284Sbms# USB device geometries
693183284Sbms#
694183284Sbms# Usage:
695183284Sbms#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
696183284Sbms#
697183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
698183284Sbms#
699183284Sbms# Note that the capacity of a flash key is usually advertised in MB or
700183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available
701183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry.
702183284Sbms#
703183284Sbms# The following generic device layouts are understood:
704183284Sbms#  generic           An alias for generic-hdd.
705183284Sbms#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
706183284Sbms#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
707183284Sbms#
708183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB.
709183284Sbms#
710150367Sphk
711183284SbmsUsbDevice () {
712183284Sbms	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
713183284Sbms	case $a1 in
714183284Sbms	generic-fdd)
715183284Sbms		NANO_HEADS=64
716183284Sbms		NANO_SECTS=32
717183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
718183284Sbms		;;
719183284Sbms	generic|generic-hdd)
720183284Sbms		NANO_HEADS=255
721183284Sbms		NANO_SECTS=63
722183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
723183284Sbms		;;
724183284Sbms	*)
725183284Sbms		echo "Unknown USB flash device"
726183284Sbms		exit 2
727183284Sbms		;;
728183284Sbms	esac
729183284Sbms}
730183284Sbms
731150367Sphk#######################################################################
732150366Sphk# Setup serial console
733150343Sphk
734150366Sphkcust_comconsole () (
735150366Sphk	# Enable getty on console
736181751Sphk	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
737150366Sphk
738150366Sphk	# Disable getty on syscons devices
739150366Sphk	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
740150366Sphk
741150366Sphk	# Tell loader to use serial console early.
742221877Simp	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
743150366Sphk)
744150366Sphk
745150366Sphk#######################################################################
746150366Sphk# Allow root login via ssh
747150366Sphk
748150366Sphkcust_allow_ssh_root () (
749150366Sphk	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
750150366Sphk	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
751150366Sphk)
752150366Sphk
753150366Sphk#######################################################################
754150366Sphk# Install the stuff under ./Files
755150366Sphk
756150366Sphkcust_install_files () (
757150511Ssimon	cd ${NANO_TOOLS}/Files
758265166Simp	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
759150366Sphk)
760150366Sphk
761150366Sphk#######################################################################
762170607Sphk# Install packages from ${NANO_PACKAGE_DIR}
763170607Sphk
764170607Sphkcust_pkg () (
765170607Sphk
766221856Simp	# If the package directory doesn't exist, we're done.
767221856Simp	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
768221856Simp		echo "DONE 0 packages"
769221856Simp		return 0
770221856Simp	fi
771221856Simp
772170607Sphk	# Copy packages into chroot
773265166Simp	mkdir -p ${NANO_WORLDDIR}/Pkg ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
774186181Sphk	(
775186181Sphk		cd ${NANO_PACKAGE_DIR}
776186181Sphk		find ${NANO_PACKAGE_LIST} -print |
777221850Simp		    cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
778186181Sphk	)
779170607Sphk
780170607Sphk	# Count & report how many we have to install
781170607Sphk	todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
782170607Sphk	echo "=== TODO: $todo"
783170607Sphk	ls ${NANO_WORLDDIR}/Pkg
784170607Sphk	echo "==="
785170607Sphk	while true
786170607Sphk	do
787182690Sthomas		# Record how many we have now
788265166Simp		have=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
789170607Sphk
790171986Sphk		# Attempt to install more packages
791171986Sphk		# ...but no more than 200 at a time due to pkg_add's internal
792171986Sphk		# limitations.
793265166Simp		CR0 'ls Pkg/*tbz | xargs -n 200 env PKG_DBDIR='${NANO_PKG_META_BASE}'/pkg pkg_add -v -F'
794170607Sphk
795170607Sphk		# See what that got us
796265166Simp		now=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
797170607Sphk		echo "=== NOW $now"
798265166Simp		ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
799170607Sphk		echo "==="
800170607Sphk
801170607Sphk
802170607Sphk		if [ $now -eq $todo ] ; then
803170607Sphk			echo "DONE $now packages"
804170607Sphk			break
805170607Sphk		elif [ $now -eq $have ] ; then
806170607Sphk			echo "FAILED: Nothing happened on this pass"
807170607Sphk			exit 2
808170607Sphk		fi
809170607Sphk	done
810274561Simp	nano_rm -rf ${NANO_WORLDDIR}/Pkg
811170607Sphk)
812170607Sphk
813265166Simpcust_pkgng () (
814265166Simp
815265166Simp	# If the package directory doesn't exist, we're done.
816265166Simp	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
817265166Simp		echo "DONE 0 packages"
818265166Simp		return 0
819265166Simp	fi
820265166Simp
821265166Simp	# Find a pkg-* package
822265166Simp	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
823265166Simp		_NANO_PKG_PACKAGE=`basename "$x"`
824265166Simp	done
825265166Simp	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
826265166Simp		echo "FAILED: need a pkg/ package for bootstrapping"
827265166Simp		exit 2
828265166Simp	fi
829265166Simp
830265166Simp	# Copy packages into chroot
831265166Simp	mkdir -p ${NANO_WORLDDIR}/Pkg
832265166Simp	(
833265166Simp		cd ${NANO_PACKAGE_DIR}
834265166Simp		find ${NANO_PACKAGE_LIST} -print |
835265166Simp		cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
836265166Simp	)
837265166Simp
838265166Simp	#Bootstrap pkg
839265166Simp	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
840265166Simp	CR pkg -N >/dev/null 2>&1
841265166Simp	if [ "$?" -ne "0" ]; then
842265166Simp		echo "FAILED: pkg bootstrapping faied"
843265166Simp		exit 2
844265166Simp	fi
845274561Simp	nano_rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
846265166Simp
847265166Simp	# Count & report how many we have to install
848265166Simp	todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
849265166Simp	todo=$(expr $todo + 1) # add one for pkg since it is installed already
850265166Simp	echo "=== TODO: $todo"
851265166Simp	ls ${NANO_WORLDDIR}/Pkg
852265166Simp	echo "==="
853265166Simp	while true
854265166Simp	do
855265166Simp		# Record how many we have now
856265166Simp 		have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
857265166Simp
858265166Simp		# Attempt to install more packages
859265166Simp		CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
860265166Simp
861265166Simp		# See what that got us
862265166Simp 		now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
863265166Simp		echo "=== NOW $now"
864265166Simp		CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
865265166Simp		echo "==="
866265166Simp		if [ $now -eq $todo ] ; then
867265166Simp			echo "DONE $now packages"
868265166Simp			break
869265166Simp		elif [ $now -eq $have ] ; then
870265166Simp			echo "FAILED: Nothing happened on this pass"
871265166Simp			exit 2
872265166Simp		fi
873265166Simp	done
874274561Simp	nano_rm -rf ${NANO_WORLDDIR}/Pkg
875265166Simp)
876265166Simp
877170607Sphk#######################################################################
878150366Sphk# Convenience function:
879209207Simp# 	Register all args as customize function.
880150366Sphk
881150343Sphkcustomize_cmd () {
882209207Simp	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
883150343Sphk}
884150343Sphk
885150343Sphk#######################################################################
886181461Sphk# Convenience function:
887209207Simp# 	Register all args as late customize function to run just before
888181461Sphk#	image creation.
889181461Sphk
890181461Sphklate_customize_cmd () {
891209207Simp	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
892181461Sphk}
893181461Sphk
894181461Sphk#######################################################################
895150366Sphk#
896150366Sphk# All set up to go...
897150366Sphk#
898150366Sphk#######################################################################
899150366Sphk
900183314Ssimon# Progress Print
901183314Ssimon#	Print $2 at level $1.
902265166Simppprint() (
903183314Ssimon    if [ "$1" -le $PPLEVEL ]; then
904194958Sn_hibma	runtime=$(( `date +%s` - $NANO_STARTTIME ))
905194958Sn_hibma	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
906183314Ssimon    fi
907265166Simp)
908183314Ssimon
909156496Sphkusage () {
910156496Sphk	(
911274561Simp	echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
912274561Simp	echo "	-K	suppress installkernel"
913156496Sphk	echo "	-b	suppress builds (both kernel and world)"
914274561Simp	echo "	-c	specify config file"
915221851Simp	echo "	-f	suppress code slice extraction"
916183314Ssimon	echo "	-i	suppress disk image build"
917156496Sphk	echo "	-k	suppress buildkernel"
918184048Ssam	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
919207436Sphilip	echo "	-q	make output more quiet"
920183314Ssimon	echo "	-v	make output more verbose"
921156496Sphk	echo "	-w	suppress buildworld"
922156496Sphk	) 1>&2
923156496Sphk	exit 2
924156496Sphk}
925156496Sphk
926150366Sphk#######################################################################
927149014Sphk# Parse arguments
928148422Sphk
929184048Ssamdo_clean=true
930156496Sphkdo_kernel=true
931274561Simpdo_installkernel=true
932156496Sphkdo_world=true
933181460Sphkdo_image=true
934221851Simpdo_copyout_partition=true
935274561Simpdo_native_xtools=false
936150366Sphk
937156496Sphkset +e
938274561Simpargs=`getopt KXbc:fhiknqvw $*`
939149014Sphkif [ $? -ne 0 ] ; then
940156496Sphk	usage
941149014Sphk	exit 2
942149014Sphkfi
943156496Sphkset -e
944149014Sphk
945149014Sphkset -- $args
946149014Sphkfor i
947149014Sphkdo
948149014Sphk	case "$i" 
949149014Sphk	in
950274561Simp	-K)
951274561Simp		do_installkernel=false
952274561Simp		shift
953274561Simp		;;
954274561Simp	-X)
955274561Simp		do_native_xtools=true
956274561Simp		shift
957274561Simp		;;
958150366Sphk	-b)
959156496Sphk		do_world=false
960156496Sphk		do_kernel=false
961183314Ssimon		shift
962150366Sphk		;;
963149014Sphk	-c)
964266441Sthomas		# Make config file path available to the config file
965266441Sthomas		# itself so that it can access additional files relative
966266441Sthomas		# to its own location.
967266441Sthomas		NANO_CONFIG=$2
968149014Sphk		. "$2"
969183314Ssimon		shift
970183314Ssimon		shift
971149014Sphk		;;
972221851Simp	-f)
973221851Simp		do_copyout_partition=false
974221851Simp		shift
975221851Simp		;;
976150366Sphk	-h)
977156496Sphk		usage
978150366Sphk		;;
979181460Sphk	-i)
980181460Sphk		do_image=false
981183314Ssimon		shift
982181481Ssam		;;
983274561Simp	-k)
984274561Simp		do_kernel=false
985274561Simp		shift
986274561Simp		;;
987184048Ssam	-n)
988184048Ssam		do_clean=false
989184048Ssam		shift
990184048Ssam		;;
991183314Ssimon	-q)
992183314Ssimon		PPLEVEL=$(($PPLEVEL - 1))
993183314Ssimon		shift
994183314Ssimon		;;
995183314Ssimon	-v)
996183314Ssimon		PPLEVEL=$(($PPLEVEL + 1))
997183314Ssimon		shift
998183314Ssimon		;;
999156496Sphk	-w)
1000156496Sphk		do_world=false
1001183314Ssimon		shift
1002156496Sphk		;;
1003149014Sphk	--)
1004183314Ssimon		shift
1005183314Ssimon		break
1006149014Sphk	esac
1007149014Sphkdone
1008149014Sphk
1009150366Sphkif [ $# -gt 0 ] ; then
1010156496Sphk	echo "$0: Extraneous arguments supplied"
1011156496Sphk	usage
1012150366Sphkfi
1013150366Sphk
1014265166Simptrap nano_cleanup EXIT
1015265166Simp
1016148422Sphk#######################################################################
1017150366Sphk# Setup and Export Internal variables
1018150366Sphk#
1019274561Simptest -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
1020194431Sn_hibmatest -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
1021194431Sn_hibmatest -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
1022148422Sphk
1023194431Sn_hibmaNANO_WORLDDIR=${NANO_OBJ}/_.w
1024194431Sn_hibmaNANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
1025194431Sn_hibmaNANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
1026175258Ssimon
1027150511Ssimonif [ -d ${NANO_TOOLS} ] ; then
1028150511Ssimon	true
1029150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
1030150511Ssimon	NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
1031150511Ssimonelse
1032150511Ssimon	echo "NANO_TOOLS directory does not exist" 1>&2
1033150511Ssimon	exit 1
1034150511Ssimonfi
1035150511Ssimon
1036184048Ssamif $do_clean ; then
1037184048Ssam	true
1038184048Ssamelse
1039265166Simp	NANO_MAKE="${NANO_MAKE} -DNO_CLEAN"
1040184048Ssam	NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
1041184048Ssamfi
1042184048Ssam
1043212938Simp# Override user's NANO_DRIVE if they specified a NANO_LABEL
1044212938Simpif [ ! -z "${NANO_LABEL}" ]; then
1045212938Simp	NANO_DRIVE=ufs/${NANO_LABEL}
1046212938Simpfi
1047212938Simp
1048148422Sphkexport MAKEOBJDIRPREFIX
1049148422Sphk
1050148422Sphkexport NANO_ARCH
1051149120Sphkexport NANO_CODESIZE
1052148422Sphkexport NANO_CONFSIZE
1053149073Sphkexport NANO_CUSTOMIZE
1054148422Sphkexport NANO_DATASIZE
1055149082Sphkexport NANO_DRIVE
1056148422Sphkexport NANO_HEADS
1057148648Sphkexport NANO_IMAGES
1058183284Sbmsexport NANO_IMGNAME
1059265166Simpexport NANO_MAKE
1060194431Sn_hibmaexport NANO_MAKE_CONF_BUILD
1061194431Sn_hibmaexport NANO_MAKE_CONF_INSTALL
1062149120Sphkexport NANO_MEDIASIZE
1063148648Sphkexport NANO_NAME
1064148648Sphkexport NANO_NEWFS
1065148648Sphkexport NANO_OBJ
1066148422Sphkexport NANO_PMAKE
1067148648Sphkexport NANO_SECTS
1068148422Sphkexport NANO_SRC
1069150366Sphkexport NANO_TOOLS
1070148648Sphkexport NANO_WORLDDIR
1071168894Sadrianexport NANO_BOOT0CFG
1072168894Sadrianexport NANO_BOOTLOADER
1073212938Simpexport NANO_LABEL
1074148422Sphk
1075148422Sphk#######################################################################
1076150366Sphk# And then it is as simple as that...
1077148422Sphk
1078194958Sn_hibma# File descriptor 3 is used for logging output, see pprint
1079194958Sn_hibmaexec 3>&1
1080194958Sn_hibma
1081194958Sn_hibmaNANO_STARTTIME=`date +%s`
1082183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting"
1083183314Ssimon
1084156496Sphkif $do_world ; then
1085184048Ssam	if $do_clean ; then
1086184048Ssam		clean_build
1087184048Ssam	else
1088184048Ssam		pprint 2 "Using existing build tree (as instructed)"
1089184048Ssam	fi
1090150366Sphk	make_conf_build
1091150366Sphk	build_world
1092156496Sphkelse
1093183314Ssimon	pprint 2 "Skipping buildworld (as instructed)"
1094156496Sphkfi
1095156496Sphk
1096156496Sphkif $do_kernel ; then
1097215070Simp	if ! $do_world ; then
1098215070Simp		make_conf_build
1099215070Simp	fi
1100150366Sphk	build_kernel
1101150366Sphkelse
1102183314Ssimon	pprint 2 "Skipping buildkernel (as instructed)"
1103150366Sphkfi
1104149120Sphk
1105149120Sphkclean_world
1106149082Sphkmake_conf_install
1107148422Sphkinstall_world
1108148422Sphkinstall_etc
1109274561Simpif $do_native_xtools ; then
1110274561Simp	native_xtools
1111274561Simpfi
1112161341Ssimonsetup_nanobsd_etc
1113274561Simpif $do_installkernel ; then
1114274561Simp	install_kernel
1115274561Simpelse
1116274561Simp	pprint 2 "Skipping installkernel (as instructed)"
1117274561Simpfi
1118149120Sphk
1119149082Sphkrun_customize
1120150366Sphksetup_nanobsd
1121149014Sphkprune_usr
1122181461Sphkrun_late_customize
1123181460Sphkif $do_image ; then
1124274561Simp	create_diskimage
1125181460Sphkelse
1126183314Ssimon	pprint 2 "Skipping image build (as instructed)"
1127181460Sphkfi
1128154466Sphklast_orders
1129149120Sphk
1130183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed"
1131