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
181265166Simp# run in the world chroot, errors fatal
182265166SimpCR()
183265166Simp{
184265166Simp	chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
185265166Simp}
186265166Simp
187265166Simp# run in the world chroot, errors not fatal
188265166SimpCR0()
189265166Simp{
190265166Simp	chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
191265166Simp}
192265166Simp
193265166Simpnano_cleanup ( ) (
194265166Simp	if [ $? -ne 0 ]; then
195265166Simp		echo "Error encountered.  Check for errors in last log file." 1>&2
196265166Simp	fi
197265166Simp	exit $?
198265166Simp)
199265166Simp
200149120Sphkclean_build ( ) (
201183314Ssimon	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
202148422Sphk
203265166Simp	if ! rm -xrf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
204265166Simp		chflags -R noschg ${MAKEOBJDIRPREFIX}/
205265166Simp		rm -xr ${MAKEOBJDIRPREFIX}/
206148422Sphk	fi
207148422Sphk	mkdir -p ${MAKEOBJDIRPREFIX}
208150343Sphk	printenv > ${MAKEOBJDIRPREFIX}/_.env
209148422Sphk)
210148422Sphk
211148422Sphkmake_conf_build ( ) (
212194431Sn_hibma	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
213148422Sphk
214194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
215194431Sn_hibma	echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
216148422Sphk)
217148422Sphk
218148422Sphkbuild_world ( ) (
219183314Ssimon	pprint 2 "run buildworld"
220183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
221148422Sphk
222148422Sphk	cd ${NANO_SRC}
223183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
224265166Simp		SRCCONF=${SRCCONF} \
225194431Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
226148422Sphk		> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
227148422Sphk)
228148422Sphk
229148422Sphkbuild_kernel ( ) (
230265166Simp	local extra
231265166Simp
232183314Ssimon	pprint 2 "build kernel ($NANO_KERNEL)"
233183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
234148422Sphk
235214955Sn_hibma	(
236148422Sphk	if [ -f ${NANO_KERNEL} ] ; then
237265894Sthomas		kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
238214955Sn_hibma		kernconf=$(basename ${NANO_KERNEL})
239214955Sn_hibma	else
240214955Sn_hibma		kernconf=${NANO_KERNEL}
241148422Sphk	fi
242148422Sphk
243214955Sn_hibma	cd ${NANO_SRC};
244181083Ssam	# unset these just in case to avoid compiler complaints
245181083Ssam	# when cross-building
246181083Ssam	unset TARGET_CPUTYPE
247214955Sn_hibma	# Note: We intentionally build all modules, not only the ones in
248214955Sn_hibma	# NANO_MODULES so the built world can be reused by multiple images.
249265894Sthomas	eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
250265894Sthomas		SRCCONF='${SRCCONF}' \
251265894Sthomas		__MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \
252265894Sthomas		${kernconfdir_arg} KERNCONF=${kernconf}"
253214955Sn_hibma	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
254148422Sphk)
255148422Sphk
256148422Sphkclean_world ( ) (
257194431Sn_hibma	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
258194431Sn_hibma		pprint 2 "Clean and create object directory (${NANO_OBJ})"
259265166Simp		if ! rm -rxf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
260194431Sn_hibma			chflags -R noschg ${NANO_OBJ}
261265166Simp			rm -xr ${NANO_OBJ}/
262194431Sn_hibma		fi
263194431Sn_hibma		mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
264194431Sn_hibma		printenv > ${NANO_OBJ}/_.env
265148422Sphk	else
266194431Sn_hibma		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
267265166Simp		if ! rm -rxf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
268194431Sn_hibma			chflags -R noschg ${NANO_WORLDDIR}
269265166Simp			rm -rxf ${NANO_WORLDDIR}/
270194431Sn_hibma		fi
271194431Sn_hibma		mkdir -p ${NANO_WORLDDIR}
272148422Sphk	fi
273148422Sphk)
274148422Sphk
275148422Sphkmake_conf_install ( ) (
276194431Sn_hibma	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
277148422Sphk
278194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
279194431Sn_hibma	echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
280148422Sphk)
281148422Sphk
282148422Sphkinstall_world ( ) (
283183314Ssimon	pprint 2 "installworld"
284194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.iw"
285148422Sphk
286148422Sphk	cd ${NANO_SRC}
287183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
288265166Simp	${NANO_MAKE} SRCCONF=${SRCCONF} \
289265166Simp		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
290148422Sphk		DESTDIR=${NANO_WORLDDIR} \
291194431Sn_hibma		> ${NANO_OBJ}/_.iw 2>&1
292148422Sphk	chflags -R noschg ${NANO_WORLDDIR}
293148422Sphk)
294148422Sphk
295148422Sphkinstall_etc ( ) (
296148422Sphk
297183314Ssimon	pprint 2 "install /etc"
298194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.etc"
299148422Sphk
300160783Sthomas	cd ${NANO_SRC}
301183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
302265166Simp	${NANO_MAKE} SRCCONF=${SRCCONF} \
303265166Simp		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
304148422Sphk		DESTDIR=${NANO_WORLDDIR} \
305194431Sn_hibma		> ${NANO_OBJ}/_.etc 2>&1
306209209Simp	# make.conf doesn't get created by default, but some ports need it
307209209Simp	# so they can spam it.
308209209Simp	cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
309148422Sphk)
310148422Sphk
311148422Sphkinstall_kernel ( ) (
312265166Simp	local extra
313265166Simp
314214955Sn_hibma	pprint 2 "install kernel ($NANO_KERNEL)"
315194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.ik"
316148422Sphk
317214955Sn_hibma	(
318214955Sn_hibma	if [ -f ${NANO_KERNEL} ] ; then
319265894Sthomas		kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
320214955Sn_hibma		kernconf=$(basename ${NANO_KERNEL})
321214955Sn_hibma	else
322214955Sn_hibma		kernconf=${NANO_KERNEL}
323214955Sn_hibma	fi
324214955Sn_hibma
325266441Sthomas	# Install all built modules if NANO_MODULES=default,
326266441Sthomas	# else install only listed modules (none if NANO_MODULES is empty).
327266441Sthomas	if [ "${NANO_MODULES}" != "default" ]; then
328266441Sthomas		modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'"
329266441Sthomas	fi
330266441Sthomas
331148422Sphk	cd ${NANO_SRC}
332265894Sthomas	eval "TARGET_ARCH=${NANO_ARCH} ${NANO_MAKE} installkernel \
333265894Sthomas		DESTDIR='${NANO_WORLDDIR}' \
334265894Sthomas		SRCCONF='${SRCCONF}' \
335265894Sthomas		__MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
336265894Sthomas		${kernconfdir_arg} KERNCONF=${kernconf} \
337266441Sthomas		${modules_override_arg}"
338214955Sn_hibma	) > ${NANO_OBJ}/_.ik 2>&1
339148422Sphk)
340148422Sphk
341150366Sphkrun_customize() (
342150366Sphk
343183314Ssimon	pprint 2 "run customize scripts"
344150366Sphk	for c in $NANO_CUSTOMIZE
345150366Sphk	do
346183314Ssimon		pprint 2 "customize \"$c\""
347194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
348183314Ssimon		pprint 4 "`type $c`"
349214955Sn_hibma		( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
350150366Sphk	done
351150366Sphk)
352150366Sphk
353181461Sphkrun_late_customize() (
354181461Sphk
355183314Ssimon	pprint 2 "run late customize scripts"
356181461Sphk	for c in $NANO_LATE_CUSTOMIZE
357181461Sphk	do
358183314Ssimon		pprint 2 "late customize \"$c\""
359194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
360183314Ssimon		pprint 4 "`type $c`"
361214955Sn_hibma		( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
362181461Sphk	done
363181461Sphk)
364181461Sphk
365150366Sphksetup_nanobsd ( ) (
366183314Ssimon	pprint 2 "configure nanobsd setup"
367194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.dl"
368148422Sphk
369149103Sphk	(
370148422Sphk	cd ${NANO_WORLDDIR}
371148422Sphk
372170607Sphk	# Move /usr/local/etc to /etc/local so that the /cfg stuff
373170607Sphk	# can stomp on it.  Otherwise packages like ipsec-tools which
374170607Sphk	# have hardcoded paths under ${prefix}/etc are not tweakable.
375170607Sphk	if [ -d usr/local/etc ] ; then
376170607Sphk		(
377183727Sn_hibma		mkdir -p etc/local
378170607Sphk		cd usr/local/etc
379170607Sphk		find . -print | cpio -dumpl ../../../etc/local
380170607Sphk		cd ..
381170607Sphk		rm -rf etc
382170607Sphk		ln -s ../../etc/local etc
383170607Sphk		)
384170607Sphk	fi
385170607Sphk
386149082Sphk	for d in var etc
387149082Sphk	do
388149082Sphk		# link /$d under /conf
389149082Sphk		# we use hard links so we have them both places.
390149082Sphk		# the files in /$d will be hidden by the mount.
391149082Sphk		# XXX: configure /$d ramdisk size
392149082Sphk		mkdir -p conf/base/$d conf/default/$d
393149082Sphk		find $d -print | cpio -dumpl conf/base/
394149082Sphk	done
395148422Sphk
396154466Sphk	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
397154466Sphk	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
398154466Sphk
399148422Sphk	# pick up config files from the special partition
400149082Sphk	echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
401148422Sphk
402149082Sphk	# Put /tmp on the /var ramdisk (could be symlink already)
403250036Sn_hibma	test -d tmp && rmdir tmp || rm -f tmp
404148422Sphk	ln -s var/tmp tmp
405149082Sphk
406194431Sn_hibma	) > ${NANO_OBJ}/_.dl 2>&1
407148422Sphk)
408148422Sphk
409161341Ssimonsetup_nanobsd_etc ( ) (
410183314Ssimon	pprint 2 "configure nanobsd /etc"
411161341Ssimon
412161341Ssimon	(
413161341Ssimon	cd ${NANO_WORLDDIR}
414161341Ssimon
415161341Ssimon	# create diskless marker file
416161341Ssimon	touch etc/diskless
417161341Ssimon
418170324Sphk	# Make root filesystem R/O by default
419170324Sphk	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
420170324Sphk
421161341Ssimon	# save config file for scripts
422161341Ssimon	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
423161341Ssimon
424161341Ssimon	echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
425161341Ssimon	echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
426161341Ssimon	mkdir -p cfg
427161341Ssimon	)
428161341Ssimon)
429161341Ssimon
430149014Sphkprune_usr() (
431149014Sphk
432149014Sphk	# Remove all empty directories in /usr 
433149014Sphk	find ${NANO_WORLDDIR}/usr -type d -depth -print |
434149014Sphk		while read d
435149014Sphk		do
436149014Sphk			rmdir $d > /dev/null 2>&1 || true 
437149014Sphk		done
438149014Sphk)
439149014Sphk
440212938Simpnewfs_part ( ) (
441212938Simp	local dev mnt lbl
442212938Simp	dev=$1
443212938Simp	mnt=$2
444212938Simp	lbl=$3
445212938Simp	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
446212938Simp	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
447216145Simp	mount -o async ${dev} ${mnt}
448212938Simp)
449212938Simp
450265166Simp# Convenient spot to work around any umount issues that your build environment
451265166Simp# hits by overriding this method.
452265166Simpnano_umount () (
453265166Simp	umount ${1}
454265166Simp)
455265166Simp
456209210Simppopulate_slice ( ) (
457212938Simp	local dev dir mnt lbl
458209210Simp	dev=$1
459209210Simp	dir=$2
460209210Simp	mnt=$3
461212938Simp	lbl=$4
462250034Sn_hibma	echo "Creating ${dev} (mounting on ${mnt})"
463250034Sn_hibma	newfs_part ${dev} ${mnt} ${lbl}
464250034Sn_hibma	if [ -n "${dir}" -a -d "${dir}" ]; then
465250034Sn_hibma		echo "Populating ${lbl} from ${dir}"
466250034Sn_hibma		cd ${dir}
467265166Simp		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
468250034Sn_hibma	fi
469209210Simp	df -i ${mnt}
470265166Simp	nano_umount ${mnt}
471209210Simp)
472209210Simp
473209210Simppopulate_cfg_slice ( ) (
474212938Simp	populate_slice "$1" "$2" "$3" "$4"
475209210Simp)
476209210Simp
477209210Simppopulate_data_slice ( ) (
478212938Simp	populate_slice "$1" "$2" "$3" "$4"
479209210Simp)
480209210Simp
481148422Sphkcreate_i386_diskimage ( ) (
482183314Ssimon	pprint 2 "build diskimage"
483194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.di"
484148422Sphk
485149103Sphk	(
486149120Sphk	echo $NANO_MEDIASIZE $NANO_IMAGES \
487149120Sphk		$NANO_SECTS $NANO_HEADS \
488149120Sphk		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
489148422Sphk	awk '
490150717Sphk	{
491149120Sphk		printf "# %s\n", $0
492149120Sphk
493148422Sphk		# size of cylinder in sectors
494149120Sphk		cs = $3 * $4
495148422Sphk
496148422Sphk		# number of full cylinders on media
497148422Sphk		cyl = int ($1 / cs)
498148422Sphk
499149120Sphk		# output fdisk geometry spec, truncate cyls to 1023
500148422Sphk		if (cyl <= 1023)
501149120Sphk			print "g c" cyl " h" $4 " s" $3
502148422Sphk		else
503149120Sphk			print "g c" 1023 " h" $4 " s" $3
504148422Sphk
505149120Sphk		if ($7 > 0) { 
506149120Sphk			# size of data partition in full cylinders
507149120Sphk			dsl = int (($7 + cs - 1) / cs)
508149120Sphk		} else {
509149120Sphk			dsl = 0;
510149120Sphk		}
511148422Sphk
512148422Sphk		# size of config partition in full cylinders
513149120Sphk		csl = int (($6 + cs - 1) / cs)
514148422Sphk
515150717Sphk		if ($5 == 0) {
516149120Sphk			# size of image partition(s) in full cylinders
517149120Sphk			isl = int ((cyl - dsl - csl) / $2)
518149120Sphk		} else {
519149120Sphk			isl = int (($5 + cs - 1) / cs)
520149120Sphk		}
521148422Sphk
522148422Sphk		# First image partition start at second track
523149120Sphk		print "p 1 165 " $3, isl * cs - $3
524148422Sphk		c = isl * cs;
525148422Sphk
526148422Sphk		# Second image partition (if any) also starts offset one 
527148422Sphk		# track to keep them identical.
528148422Sphk		if ($2 > 1) {
529149120Sphk			print "p 2 165 " $3 + c, isl * cs - $3
530148422Sphk			c += isl * cs;
531148422Sphk		}
532148422Sphk
533148422Sphk		# Config partition starts at cylinder boundary.
534148422Sphk		print "p 3 165 " c, csl * cs
535148422Sphk		c += csl * cs
536148422Sphk
537148422Sphk		# Data partition (if any) starts at cylinder boundary.
538149120Sphk		if ($7 > 0) {
539148422Sphk			print "p 4 165 " c, dsl * cs
540174936Ssimon		} else if ($7 < 0 && $1 > c) {
541174936Ssimon			print "p 4 165 " c, $1 - c
542150717Sphk		} else if ($1 < c) {
543150717Sphk			print "Disk space overcommitted by", \
544150717Sphk			    c - $1, "sectors" > "/dev/stderr"
545150717Sphk			exit 2
546148422Sphk		}
547183279Sbms
548183279Sbms		# Force slice 1 to be marked active. This is necessary
549183279Sbms		# for booting the image from a USB device to work.
550183279Sbms		print "a 1"
551150717Sphk	}
552194431Sn_hibma	' > ${NANO_OBJ}/_.fdisk
553148422Sphk
554183284Sbms	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
555194431Sn_hibma	MNT=${NANO_OBJ}/_.mnt
556148422Sphk	mkdir -p ${MNT}
557148422Sphk
558175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
559175258Ssimon		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
560175258Ssimon			-y ${NANO_HEADS}`
561175258Ssimon	else
562175258Ssimon		echo "Creating md backing file..."
563216145Simp		rm -f ${IMG}
564216145Simp		dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
565175258Ssimon		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
566175258Ssimon			-y ${NANO_HEADS}`
567175258Ssimon	fi
568148422Sphk
569265166Simp	trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
570148422Sphk
571194431Sn_hibma	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
572149120Sphk	fdisk ${MD}
573148422Sphk	# XXX: params
574148422Sphk	# XXX: pick up cached boot* files, they may not be in image anymore.
575168894Sadrian	boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
576160388Sthomas	bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
577149120Sphk	bsdlabel ${MD}s1
578148422Sphk
579148422Sphk	# Create first image
580212990Simp	populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a"
581194437Sn_hibma	mount /dev/${MD}s1a ${MNT}
582175258Ssimon	echo "Generating mtree..."
583194431Sn_hibma	( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
584194431Sn_hibma	( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
585265166Simp	nano_umount ${MNT}
586148422Sphk
587171986Sphk	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
588148422Sphk		# Duplicate to second image (if present)
589175258Ssimon		echo "Duplicating to second image..."
590250036Sn_hibma		dd conv=sparse if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
591170322Sphk		mount /dev/${MD}s2a ${MNT}
592170322Sphk		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
593170322Sphk		do
594212169Simp			sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
595170322Sphk		done
596265166Simp		nano_umount ${MNT}
597212990Simp		# Override the label from the first partition so we
598212990Simp		# don't confuse glabel with duplicates.
599212938Simp		if [ ! -z ${NANO_LABEL} ]; then
600212990Simp			tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a
601212938Simp		fi
602148422Sphk	fi
603148422Sphk	
604148422Sphk	# Create Config slice
605212938Simp	populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3"
606148422Sphk
607148422Sphk	# Create Data slice, if any.
608194437Sn_hibma	if [ $NANO_DATASIZE -ne 0 ] ; then
609212938Simp		populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4"
610148422Sphk	fi
611148422Sphk
612175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
613265166Simp		if [ ${NANO_IMAGE_MBRONLY} ]; then
614265166Simp			echo "Writing out _.disk.mbr..."
615265166Simp			dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
616265166Simp		else
617265166Simp			echo "Writing out ${NANO_IMGNAME}..."
618265166Simp			dd if=/dev/${MD} of=${IMG} bs=64k
619265166Simp		fi
620265166Simp
621194440Sn_hibma		echo "Writing out ${NANO_IMGNAME}..."
622250036Sn_hibma		dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
623175258Ssimon	fi
624175258Ssimon
625221852Simp	if ${do_copyout_partition} ; then
626221851Simp		echo "Writing out _.disk.image..."
627250036Sn_hibma		dd conv=sparse if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
628221851Simp	fi
629148422Sphk	mdconfig -d -u $MD
630197542Ssimon
631265166Simp	trap - 1 2 15
632265166Simp	trap nano_cleanup EXIT
633197542Ssimon
634194431Sn_hibma	) > ${NANO_OBJ}/_.di 2>&1
635148422Sphk)
636148422Sphk
637205980Simp# i386 and amd64 are identical for disk images
638205980Simpcreate_amd64_diskimage ( ) (
639205980Simp	create_i386_diskimage
640205980Simp)
641205980Simp
642154466Sphklast_orders () (
643154466Sphk	# Redefine this function with any last orders you may have
644154466Sphk	# after the build completed, for instance to copy the finished
645154466Sphk	# image to a more convenient place:
646194431Sn_hibma	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
647250036Sn_hibma	true
648154466Sphk)
649154466Sphk
650148422Sphk#######################################################################
651148422Sphk#
652150366Sphk# Optional convenience functions.
653148422Sphk#
654148422Sphk#######################################################################
655148422Sphk
656149014Sphk#######################################################################
657150367Sphk# Common Flash device geometries
658150367Sphk#
659150367Sphk
660150367SphkFlashDevice () {
661150717Sphk	if [ -d ${NANO_TOOLS} ] ; then
662150717Sphk		. ${NANO_TOOLS}/FlashDevice.sub
663150717Sphk	else
664150717Sphk		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
665150717Sphk	fi
666150367Sphk	sub_FlashDevice $1 $2
667150367Sphk}
668150367Sphk
669183284Sbms#######################################################################
670183284Sbms# USB device geometries
671183284Sbms#
672183284Sbms# Usage:
673183284Sbms#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
674183284Sbms#
675183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
676183284Sbms#
677183284Sbms# Note that the capacity of a flash key is usually advertised in MB or
678183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available
679183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry.
680183284Sbms#
681183284Sbms# The following generic device layouts are understood:
682183284Sbms#  generic           An alias for generic-hdd.
683183284Sbms#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
684183284Sbms#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
685183284Sbms#
686183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB.
687183284Sbms#
688150367Sphk
689183284SbmsUsbDevice () {
690183284Sbms	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
691183284Sbms	case $a1 in
692183284Sbms	generic-fdd)
693183284Sbms		NANO_HEADS=64
694183284Sbms		NANO_SECTS=32
695183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
696183284Sbms		;;
697183284Sbms	generic|generic-hdd)
698183284Sbms		NANO_HEADS=255
699183284Sbms		NANO_SECTS=63
700183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
701183284Sbms		;;
702183284Sbms	*)
703183284Sbms		echo "Unknown USB flash device"
704183284Sbms		exit 2
705183284Sbms		;;
706183284Sbms	esac
707183284Sbms}
708183284Sbms
709150367Sphk#######################################################################
710150366Sphk# Setup serial console
711150343Sphk
712150366Sphkcust_comconsole () (
713150366Sphk	# Enable getty on console
714181751Sphk	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
715150366Sphk
716150366Sphk	# Disable getty on syscons devices
717150366Sphk	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
718150366Sphk
719150366Sphk	# Tell loader to use serial console early.
720221877Simp	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
721150366Sphk)
722150366Sphk
723150366Sphk#######################################################################
724150366Sphk# Allow root login via ssh
725150366Sphk
726150366Sphkcust_allow_ssh_root () (
727150366Sphk	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
728150366Sphk	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
729150366Sphk)
730150366Sphk
731150366Sphk#######################################################################
732150366Sphk# Install the stuff under ./Files
733150366Sphk
734150366Sphkcust_install_files () (
735150511Ssimon	cd ${NANO_TOOLS}/Files
736265166Simp	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
737150366Sphk)
738150366Sphk
739150366Sphk#######################################################################
740170607Sphk# Install packages from ${NANO_PACKAGE_DIR}
741170607Sphk
742170607Sphkcust_pkg () (
743170607Sphk
744221856Simp	# If the package directory doesn't exist, we're done.
745221856Simp	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
746221856Simp		echo "DONE 0 packages"
747221856Simp		return 0
748221856Simp	fi
749221856Simp
750170607Sphk	# Copy packages into chroot
751265166Simp	mkdir -p ${NANO_WORLDDIR}/Pkg ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
752186181Sphk	(
753186181Sphk		cd ${NANO_PACKAGE_DIR}
754186181Sphk		find ${NANO_PACKAGE_LIST} -print |
755221850Simp		    cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
756186181Sphk	)
757170607Sphk
758170607Sphk	# Count & report how many we have to install
759170607Sphk	todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
760170607Sphk	echo "=== TODO: $todo"
761170607Sphk	ls ${NANO_WORLDDIR}/Pkg
762170607Sphk	echo "==="
763170607Sphk	while true
764170607Sphk	do
765182690Sthomas		# Record how many we have now
766265166Simp		have=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
767170607Sphk
768171986Sphk		# Attempt to install more packages
769171986Sphk		# ...but no more than 200 at a time due to pkg_add's internal
770171986Sphk		# limitations.
771265166Simp		CR0 'ls Pkg/*tbz | xargs -n 200 env PKG_DBDIR='${NANO_PKG_META_BASE}'/pkg pkg_add -v -F'
772170607Sphk
773170607Sphk		# See what that got us
774265166Simp		now=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
775170607Sphk		echo "=== NOW $now"
776265166Simp		ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
777170607Sphk		echo "==="
778170607Sphk
779170607Sphk
780170607Sphk		if [ $now -eq $todo ] ; then
781170607Sphk			echo "DONE $now packages"
782170607Sphk			break
783170607Sphk		elif [ $now -eq $have ] ; then
784170607Sphk			echo "FAILED: Nothing happened on this pass"
785170607Sphk			exit 2
786170607Sphk		fi
787170607Sphk	done
788265166Simp	rm -rxf ${NANO_WORLDDIR}/Pkg
789170607Sphk)
790170607Sphk
791265166Simpcust_pkgng () (
792265166Simp
793265166Simp	# If the package directory doesn't exist, we're done.
794265166Simp	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
795265166Simp		echo "DONE 0 packages"
796265166Simp		return 0
797265166Simp	fi
798265166Simp
799265166Simp	# Find a pkg-* package
800265166Simp	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
801265166Simp		_NANO_PKG_PACKAGE=`basename "$x"`
802265166Simp	done
803265166Simp	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
804265166Simp		echo "FAILED: need a pkg/ package for bootstrapping"
805265166Simp		exit 2
806265166Simp	fi
807265166Simp
808265166Simp	# Copy packages into chroot
809265166Simp	mkdir -p ${NANO_WORLDDIR}/Pkg
810265166Simp	(
811265166Simp		cd ${NANO_PACKAGE_DIR}
812265166Simp		find ${NANO_PACKAGE_LIST} -print |
813265166Simp		cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
814265166Simp	)
815265166Simp
816265166Simp	#Bootstrap pkg
817265166Simp	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
818265166Simp	CR pkg -N >/dev/null 2>&1
819265166Simp	if [ "$?" -ne "0" ]; then
820265166Simp		echo "FAILED: pkg bootstrapping faied"
821265166Simp		exit 2
822265166Simp	fi
823265166Simp	rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
824265166Simp
825265166Simp	# Count & report how many we have to install
826265166Simp	todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
827265166Simp	todo=$(expr $todo + 1) # add one for pkg since it is installed already
828265166Simp	echo "=== TODO: $todo"
829265166Simp	ls ${NANO_WORLDDIR}/Pkg
830265166Simp	echo "==="
831265166Simp	while true
832265166Simp	do
833265166Simp		# Record how many we have now
834265166Simp 		have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
835265166Simp
836265166Simp		# Attempt to install more packages
837265166Simp		CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
838265166Simp
839265166Simp		# See what that got us
840265166Simp 		now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
841265166Simp		echo "=== NOW $now"
842265166Simp		CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
843265166Simp		echo "==="
844265166Simp		if [ $now -eq $todo ] ; then
845265166Simp			echo "DONE $now packages"
846265166Simp			break
847265166Simp		elif [ $now -eq $have ] ; then
848265166Simp			echo "FAILED: Nothing happened on this pass"
849265166Simp			exit 2
850265166Simp		fi
851265166Simp	done
852265166Simp	rm -rxf ${NANO_WORLDDIR}/Pkg
853265166Simp)
854265166Simp
855170607Sphk#######################################################################
856150366Sphk# Convenience function:
857209207Simp# 	Register all args as customize function.
858150366Sphk
859150343Sphkcustomize_cmd () {
860209207Simp	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
861150343Sphk}
862150343Sphk
863150343Sphk#######################################################################
864181461Sphk# Convenience function:
865209207Simp# 	Register all args as late customize function to run just before
866181461Sphk#	image creation.
867181461Sphk
868181461Sphklate_customize_cmd () {
869209207Simp	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
870181461Sphk}
871181461Sphk
872181461Sphk#######################################################################
873150366Sphk#
874150366Sphk# All set up to go...
875150366Sphk#
876150366Sphk#######################################################################
877150366Sphk
878183314Ssimon# Progress Print
879183314Ssimon#	Print $2 at level $1.
880265166Simppprint() (
881183314Ssimon    if [ "$1" -le $PPLEVEL ]; then
882194958Sn_hibma	runtime=$(( `date +%s` - $NANO_STARTTIME ))
883194958Sn_hibma	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
884183314Ssimon    fi
885265166Simp)
886183314Ssimon
887156496Sphkusage () {
888156496Sphk	(
889221851Simp	echo "Usage: $0 [-bfiknqvw] [-c config_file]"
890156496Sphk	echo "	-b	suppress builds (both kernel and world)"
891221851Simp	echo "	-f	suppress code slice extraction"
892183314Ssimon	echo "	-i	suppress disk image build"
893156496Sphk	echo "	-k	suppress buildkernel"
894184048Ssam	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
895207436Sphilip	echo "	-q	make output more quiet"
896183314Ssimon	echo "	-v	make output more verbose"
897156496Sphk	echo "	-w	suppress buildworld"
898156496Sphk	echo "	-c	specify config file"
899156496Sphk	) 1>&2
900156496Sphk	exit 2
901156496Sphk}
902156496Sphk
903150366Sphk#######################################################################
904149014Sphk# Parse arguments
905148422Sphk
906184048Ssamdo_clean=true
907156496Sphkdo_kernel=true
908156496Sphkdo_world=true
909181460Sphkdo_image=true
910221851Simpdo_copyout_partition=true
911150366Sphk
912156496Sphkset +e
913221851Simpargs=`getopt bc:fhiknqvw $*`
914149014Sphkif [ $? -ne 0 ] ; then
915156496Sphk	usage
916149014Sphk	exit 2
917149014Sphkfi
918156496Sphkset -e
919149014Sphk
920149014Sphkset -- $args
921149014Sphkfor i
922149014Sphkdo
923149014Sphk	case "$i" 
924149014Sphk	in
925150366Sphk	-b)
926156496Sphk		do_world=false
927156496Sphk		do_kernel=false
928183314Ssimon		shift
929150366Sphk		;;
930156496Sphk	-k)
931156496Sphk		do_kernel=false
932183314Ssimon		shift
933156496Sphk		;;
934149014Sphk	-c)
935266441Sthomas		# Make config file path available to the config file
936266441Sthomas		# itself so that it can access additional files relative
937266441Sthomas		# to its own location.
938266441Sthomas		NANO_CONFIG=$2
939149014Sphk		. "$2"
940183314Ssimon		shift
941183314Ssimon		shift
942149014Sphk		;;
943221851Simp	-f)
944221851Simp		do_copyout_partition=false
945221851Simp		shift
946221851Simp		;;
947150366Sphk	-h)
948156496Sphk		usage
949150366Sphk		;;
950181460Sphk	-i)
951181460Sphk		do_image=false
952183314Ssimon		shift
953181481Ssam		;;
954184048Ssam	-n)
955184048Ssam		do_clean=false
956184048Ssam		shift
957184048Ssam		;;
958183314Ssimon	-q)
959183314Ssimon		PPLEVEL=$(($PPLEVEL - 1))
960183314Ssimon		shift
961183314Ssimon		;;
962183314Ssimon	-v)
963183314Ssimon		PPLEVEL=$(($PPLEVEL + 1))
964183314Ssimon		shift
965183314Ssimon		;;
966156496Sphk	-w)
967156496Sphk		do_world=false
968183314Ssimon		shift
969156496Sphk		;;
970149014Sphk	--)
971183314Ssimon		shift
972183314Ssimon		break
973149014Sphk	esac
974149014Sphkdone
975149014Sphk
976150366Sphkif [ $# -gt 0 ] ; then
977156496Sphk	echo "$0: Extraneous arguments supplied"
978156496Sphk	usage
979150366Sphkfi
980150366Sphk
981265166Simptrap nano_cleanup EXIT
982265166Simp
983148422Sphk#######################################################################
984150366Sphk# Setup and Export Internal variables
985150366Sphk#
986194431Sn_hibmatest -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
987194431Sn_hibmatest -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
988194431Sn_hibmatest -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
989148422Sphk
990194431Sn_hibmaNANO_WORLDDIR=${NANO_OBJ}/_.w
991194431Sn_hibmaNANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
992194431Sn_hibmaNANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
993175258Ssimon
994150511Ssimonif [ -d ${NANO_TOOLS} ] ; then
995150511Ssimon	true
996150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
997150511Ssimon	NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
998150511Ssimonelse
999150511Ssimon	echo "NANO_TOOLS directory does not exist" 1>&2
1000150511Ssimon	exit 1
1001150511Ssimonfi
1002150511Ssimon
1003184048Ssamif $do_clean ; then
1004184048Ssam	true
1005184048Ssamelse
1006265166Simp	NANO_MAKE="${NANO_MAKE} -DNO_CLEAN"
1007184048Ssam	NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
1008184048Ssamfi
1009184048Ssam
1010212938Simp# Override user's NANO_DRIVE if they specified a NANO_LABEL
1011212938Simpif [ ! -z "${NANO_LABEL}" ]; then
1012212938Simp	NANO_DRIVE=ufs/${NANO_LABEL}
1013212938Simpfi
1014212938Simp
1015148422Sphkexport MAKEOBJDIRPREFIX
1016148422Sphk
1017148422Sphkexport NANO_ARCH
1018149120Sphkexport NANO_CODESIZE
1019148422Sphkexport NANO_CONFSIZE
1020149073Sphkexport NANO_CUSTOMIZE
1021148422Sphkexport NANO_DATASIZE
1022149082Sphkexport NANO_DRIVE
1023148422Sphkexport NANO_HEADS
1024148648Sphkexport NANO_IMAGES
1025183284Sbmsexport NANO_IMGNAME
1026265166Simpexport NANO_MAKE
1027194431Sn_hibmaexport NANO_MAKE_CONF_BUILD
1028194431Sn_hibmaexport NANO_MAKE_CONF_INSTALL
1029149120Sphkexport NANO_MEDIASIZE
1030148648Sphkexport NANO_NAME
1031148648Sphkexport NANO_NEWFS
1032148648Sphkexport NANO_OBJ
1033148422Sphkexport NANO_PMAKE
1034148648Sphkexport NANO_SECTS
1035148422Sphkexport NANO_SRC
1036150366Sphkexport NANO_TOOLS
1037148648Sphkexport NANO_WORLDDIR
1038168894Sadrianexport NANO_BOOT0CFG
1039168894Sadrianexport NANO_BOOTLOADER
1040212938Simpexport NANO_LABEL
1041148422Sphk
1042148422Sphk#######################################################################
1043150366Sphk# And then it is as simple as that...
1044148422Sphk
1045194958Sn_hibma# File descriptor 3 is used for logging output, see pprint
1046194958Sn_hibmaexec 3>&1
1047194958Sn_hibma
1048194958Sn_hibmaNANO_STARTTIME=`date +%s`
1049183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting"
1050183314Ssimon
1051156496Sphkif $do_world ; then
1052184048Ssam	if $do_clean ; then
1053184048Ssam		clean_build
1054184048Ssam	else
1055184048Ssam		pprint 2 "Using existing build tree (as instructed)"
1056184048Ssam	fi
1057150366Sphk	make_conf_build
1058150366Sphk	build_world
1059156496Sphkelse
1060183314Ssimon	pprint 2 "Skipping buildworld (as instructed)"
1061156496Sphkfi
1062156496Sphk
1063156496Sphkif $do_kernel ; then
1064215070Simp	if ! $do_world ; then
1065215070Simp		make_conf_build
1066215070Simp	fi
1067150366Sphk	build_kernel
1068150366Sphkelse
1069183314Ssimon	pprint 2 "Skipping buildkernel (as instructed)"
1070150366Sphkfi
1071149120Sphk
1072149120Sphkclean_world
1073149082Sphkmake_conf_install
1074148422Sphkinstall_world
1075148422Sphkinstall_etc
1076161341Ssimonsetup_nanobsd_etc
1077148422Sphkinstall_kernel
1078149120Sphk
1079149082Sphkrun_customize
1080150366Sphksetup_nanobsd
1081149014Sphkprune_usr
1082181461Sphkrun_late_customize
1083181460Sphkif $do_image ; then
1084181460Sphk	create_${NANO_ARCH}_diskimage
1085181460Sphkelse
1086183314Ssimon	pprint 2 "Skipping image build (as instructed)"
1087181460Sphkfi
1088154466Sphklast_orders
1089149120Sphk
1090183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed"
1091