defaults.sh revision 291863
1#!/bin/sh
2#
3# Copyright (c) 2005 Poul-Henning Kamp.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/tools/tools/nanobsd/defaults.sh 291863 2015-12-05 17:40:11Z imp $
28#
29
30set -e
31
32#######################################################################
33#
34# Setup default values for all controlling variables.
35# These values can be overridden from the config file(s)
36#
37#######################################################################
38
39# Name of this NanoBSD build.  (Used to construct workdir names)
40NANO_NAME=full
41
42# Source tree directory
43NANO_SRC=/usr/src
44
45# Where nanobsd additional files live under the source tree
46NANO_TOOLS=tools/tools/nanobsd
47
48# Where cust_pkgng() finds packages to install
49NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
50NANO_PACKAGE_LIST="*"
51
52# where package metadata gets placed
53NANO_PKG_META_BASE=/var/db
54
55# Object tree directory
56# default is subdir of /usr/obj
57#NANO_OBJ=""
58
59# The directory to put the final images
60# default is ${NANO_OBJ}
61#NANO_DISKIMGDIR=""
62
63# Make & parallel Make
64NANO_MAKE="make"
65NANO_PMAKE="make -j 3"
66
67# The default name for any image we create.
68NANO_IMGNAME="_.disk.full"
69
70# Options to put in make.conf during buildworld only
71CONF_BUILD=' '
72
73# Options to put in make.conf during installworld only
74CONF_INSTALL=' '
75
76# Options to put in make.conf during both build- & installworld.
77CONF_WORLD=' '
78
79# Kernel config file to use
80NANO_KERNEL=GENERIC
81
82# Kernel modules to install. If empty, no modules are installed.
83# Use "default" to install all built modules.
84NANO_MODULES=
85
86# Customize commands.
87NANO_CUSTOMIZE=""
88
89# Late customize commands.
90NANO_LATE_CUSTOMIZE=""
91
92# Newfs paramters to use
93NANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
94
95# The drive name of the media at runtime
96NANO_DRIVE=ad0
97
98# Target media size in 512 bytes sectors
99NANO_MEDIASIZE=2000000
100
101# Number of code images on media (1 or 2)
102NANO_IMAGES=2
103
104# 0 -> Leave second image all zeroes so it compresses better.
105# 1 -> Initialize second image with a copy of the first
106NANO_INIT_IMG2=1
107
108# Size of code file system in 512 bytes sectors
109# If zero, size will be as large as possible.
110NANO_CODESIZE=0
111
112# Size of configuration file system in 512 bytes sectors
113# Cannot be zero.
114NANO_CONFSIZE=2048
115
116# Size of data file system in 512 bytes sectors
117# If zero: no partition configured.
118# If negative: max size possible
119NANO_DATASIZE=0
120
121# Size of the /etc ramdisk in 512 bytes sectors
122NANO_RAM_ETCSIZE=10240
123
124# Size of the /tmp+/var ramdisk in 512 bytes sectors
125NANO_RAM_TMPVARSIZE=10240
126
127# Media geometry, only relevant if bios doesn't understand LBA.
128NANO_SECTS=63
129NANO_HEADS=16
130
131# boot0 flags/options and configuration
132NANO_BOOT0CFG="-o packet -s 1 -m 3"
133NANO_BOOTLOADER="boot/boot0sio"
134
135# boot2 flags/options
136# default force serial console
137NANO_BOOT2CFG="-h"
138
139# Backing type of md(4) device
140# Can be "file" or "swap"
141NANO_MD_BACKING="file"
142
143# for swap type md(4) backing, write out the mbr only
144NANO_IMAGE_MBRONLY=true
145
146# Progress Print level
147PPLEVEL=3
148
149# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
150# in preference to /dev/${NANO_DRIVE}
151# Root partition will be ${NANO_LABEL}s{1,2}
152# /cfg partition will be ${NANO_LABEL}s3
153# /data partition will be ${NANO_LABEL}s4
154NANO_LABEL=""
155NANO_SLICE_ROOT=s1
156NANO_SLICE_ALTROOT=s2
157NANO_SLICE_CFG=s3
158NANO_SLICE_DATA=s4
159
160
161#######################################################################
162# Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
163# Unfortunately, there's no way to set TARGET at this time, and it
164# conflates the two, so architectures where TARGET != TARGET_ARCH and
165# TARGET can't be guessed from TARGET_ARCH do not work.  This defaults
166# to the arch of the current machine.
167NANO_ARCH=`uname -p`
168
169# CPUTYPE defaults to "" which is the default when CPUTYPE isn't
170# defined.
171NANO_CPUTYPE=""
172
173# Directory to populate /cfg from
174NANO_CFGDIR=""
175
176# Directory to populate /data from
177NANO_DATADIR=""
178
179# We don't need SRCCONF or SRC_ENV_CONF. NanoBSD puts everything we
180# need for the build in files included with __MAKE_CONF. Override in your
181# config file if you really must. We set them unconditionally here, though
182# in case they are stray in the build environment
183SRCCONF=/dev/null
184SRC_ENV_CONF=/dev/null
185 
186#######################################################################
187#
188# The functions which do the real work.
189# Can be overridden from the config file(s)
190#
191#######################################################################
192
193# Export values into the shell. Must use { } instead of ( ) like
194# other functions to avoid a subshell.
195# We set __MAKE_CONF as a global since it is easier to get quoting
196# right for paths with spaces in them.
197make_export ( ) {
198	# Similar to export_var, except puts the data out to stdout
199	var=$1
200	eval val=\$$var
201	echo "Setting variable: $var=\"$val\""
202	export $1
203}
204
205nano_make_build_env ( ) {
206	__MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
207	make_export __MAKE_CONF
208}
209
210nano_make_install_env ( ) {
211	__MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
212	make_export __MAKE_CONF
213}
214
215# Extra environment variables for kernel builds
216nano_make_kernel_env ( ) {
217	if [ -f ${NANO_KERNEL} ] ; then
218		KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
219		KERNCONF="$(basename ${NANO_KERNEL})"
220		make_export KERNCONFDIR
221		make_export KERNCONF
222	else
223		export KERNCONF="${NANO_KERNEL}"
224		make_export KERNCONF
225	fi
226}
227
228nano_global_make_env ( ) (
229	[ ! -z "${NANO_ARCH}" ] && echo TARGET_ARCH="${NANO_ARCH}"  || true
230	[ ! -z "${NANO_CPUTYPE}" ] && echo TARGET_CPUTYPE="${NANO_CPUTYPE}" || true
231)
232
233# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
234# hosts for now.
235nano_rm ( ) {
236	case $(uname -r) in
237	7*|8*|9*) rm $* ;;
238	*) rm -x $* ;;
239	esac
240}
241
242# run in the world chroot, errors fatal
243CR ( ) {
244	chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
245}
246
247# run in the world chroot, errors not fatal
248CR0 ( ) {
249	chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
250}
251
252nano_cleanup ( ) (
253	if [ $? -ne 0 ]; then
254		echo "Error encountered.  Check for errors in last log file." 1>&2
255	fi
256	exit $?
257)
258
259clean_build ( ) (
260	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
261
262	if ! nano_rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
263		chflags -R noschg ${MAKEOBJDIRPREFIX}/
264		nano_rm -r ${MAKEOBJDIRPREFIX}/
265	fi
266)
267
268make_conf_build ( ) (
269	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
270
271	mkdir -p ${MAKEOBJDIRPREFIX}
272	printenv > ${MAKEOBJDIRPREFIX}/_.env
273
274	# Make sure we get all the global settings that NanoBSD wants
275	# in addition to the user's global settings
276	(
277	nano_global_make_env
278	echo "${CONF_WORLD}" 
279	echo "${CONF_BUILD}"
280	) > ${NANO_MAKE_CONF_BUILD}
281)
282
283build_world ( ) (
284	pprint 2 "run buildworld"
285	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
286
287	(
288	nano_make_build_env
289	set -o xtrace
290	cd "${NANO_SRC}"
291	${NANO_PMAKE} buildworld
292	) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
293)
294
295build_kernel ( ) (
296	local extra
297
298	pprint 2 "build kernel ($NANO_KERNEL)"
299	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
300
301	(
302	nano_make_build_env
303	nano_make_kernel_env
304
305	# Note: We intentionally build all modules, not only the ones in
306	# NANO_MODULES so the built world can be reused by multiple images.
307	# Although MODULES_OVERRIDE can be defined in the kenrel config
308	# file to override this behavior. Just set NANO_MODULES=default.
309	set -o xtrace
310	cd "${NANO_SRC}"
311	${NANO_PMAKE} buildkernel
312	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
313)
314
315clean_world ( ) (
316	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
317		pprint 2 "Clean and create object directory (${NANO_OBJ})"
318		if ! nano_rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
319			chflags -R noschg ${NANO_OBJ}
320			nano_rm -r ${NANO_OBJ}/
321		fi
322		mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
323		printenv > ${NANO_OBJ}/_.env
324	else
325		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
326		if ! nano_rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
327			chflags -R noschg "${NANO_WORLDDIR}"
328			nano_rm -rf "${NANO_WORLDDIR}/"
329		fi
330		mkdir -p "${NANO_WORLDDIR}"
331	fi
332)
333
334make_conf_install ( ) (
335	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
336
337	# Make sure we get all the global settings that NanoBSD wants
338	# in addition to the user's global settings
339	(
340	nano_global_make_env
341	echo "${CONF_WORLD}"
342	echo "${CONF_INSTALL}"
343	if [ ! -z "${NANO_NOPRIV_BUILD}" ]; then
344	    echo NO_ROOT=t
345	    echo METALOG=${NANO_METALOG}
346	fi
347	) >  ${NANO_MAKE_CONF_INSTALL}
348)
349
350install_world ( ) (
351	pprint 2 "installworld"
352	pprint 3 "log: ${NANO_OBJ}/_.iw"
353
354	(
355	nano_make_install_env
356	set -o xtrace
357	cd "${NANO_SRC}"
358	${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}"
359	chflags -R noschg "${NANO_WORLDDIR}"
360	) > ${NANO_OBJ}/_.iw 2>&1
361)
362
363install_etc ( ) (
364
365	pprint 2 "install /etc"
366	pprint 3 "log: ${NANO_OBJ}/_.etc"
367
368	(
369	nano_make_install_env
370	set -o xtrace
371	cd "${NANO_SRC}"
372	${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}"
373	# make.conf doesn't get created by default, but some ports need it
374	# so they can spam it.
375	cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
376	) > ${NANO_OBJ}/_.etc 2>&1
377)
378
379install_kernel ( ) (
380	local extra
381
382	pprint 2 "install kernel ($NANO_KERNEL)"
383	pprint 3 "log: ${NANO_OBJ}/_.ik"
384
385	(
386
387	nano_make_install_env
388	nano_make_kernel_env    
389
390	if [ "${NANO_MODULES}" != "default" ]; then
391		MODULES_OVERRIDE="${NANO_MODULES}"
392		make_export MODULES_OVERRIDE
393	fi
394
395	set -o xtrace
396	cd "${NANO_SRC}"
397	${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}"
398
399	) > ${NANO_OBJ}/_.ik 2>&1
400)
401
402native_xtools ( ) (
403	print 2 "Installing the optimized native build tools for cross env"
404	pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
405
406	(
407
408	nano_make_install_env
409	set -o xtrace
410	cd "${NANO_SRC}"
411	${NANO_MAKE} native-xtools DESTDIR="${NANO_WORLDDIR}"
412
413	) > ${NANO_OBJ}/_.native_xtools 2>&1
414)
415
416run_customize ( ) (
417
418	pprint 2 "run customize scripts"
419	for c in $NANO_CUSTOMIZE
420	do
421		pprint 2 "customize \"$c\""
422		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
423		pprint 4 "`type $c`"
424		( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
425	done
426)
427
428run_late_customize ( ) (
429
430	pprint 2 "run late customize scripts"
431	for c in $NANO_LATE_CUSTOMIZE
432	do
433		pprint 2 "late customize \"$c\""
434		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
435		pprint 4 "`type $c`"
436		( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
437	done
438)
439
440setup_nanobsd ( ) (
441	pprint 2 "configure nanobsd setup"
442	pprint 3 "log: ${NANO_OBJ}/_.dl"
443
444	(
445	cd "${NANO_WORLDDIR}"
446
447	# Move /usr/local/etc to /etc/local so that the /cfg stuff
448	# can stomp on it.  Otherwise packages like ipsec-tools which
449	# have hardcoded paths under ${prefix}/etc are not tweakable.
450	if [ -d usr/local/etc ] ; then
451		(
452		mkdir -p etc/local
453		cd usr/local/etc
454		find . -print | cpio -dumpl ../../../etc/local
455		cd ..
456		nano_rm -rf etc
457		ln -s ../../etc/local etc
458		)
459	fi
460
461	for d in var etc
462	do
463		# link /$d under /conf
464		# we use hard links so we have them both places.
465		# the files in /$d will be hidden by the mount.
466		mkdir -p conf/base/$d conf/default/$d
467		find $d -print | cpio -dumpl conf/base/
468	done
469
470	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
471	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
472
473	# pick up config files from the special partition
474	echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount
475
476	# Put /tmp on the /var ramdisk (could be symlink already)
477	nano_rm -rf tmp
478	ln -s var/tmp tmp
479
480	) > ${NANO_OBJ}/_.dl 2>&1
481)
482
483setup_nanobsd_etc ( ) (
484	pprint 2 "configure nanobsd /etc"
485
486	(
487	cd "${NANO_WORLDDIR}"
488
489	# create diskless marker file
490	touch etc/diskless
491
492	# Make root filesystem R/O by default
493	[ ! -z "${NANO_NOPRIV_BUILD}" ] && chmod 666 etc/defaults/rc.conf
494	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
495	[ ! -z "${NANO_NOPRIV_BUILD}" ] && chmod 444 etc/defaults/rc.conf
496
497	# save config file for scripts
498	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
499
500	echo "/dev/${NANO_DRIVE}${NANO_SLICE_ROOT}a / ufs ro 1 1" > etc/fstab
501	echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab
502	mkdir -p cfg
503	)
504)
505
506prune_usr ( ) (
507
508	# Remove all empty directories in /usr 
509	find "${NANO_WORLDDIR}"/usr -type d -depth -print |
510		while read d
511		do
512			rmdir $d > /dev/null 2>&1 || true 
513		done
514)
515
516newfs_part ( ) (
517	local dev mnt lbl
518	dev=$1
519	mnt=$2
520	lbl=$3
521	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
522	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
523	mount -o async ${dev} ${mnt}
524)
525
526# Convenient spot to work around any umount issues that your build environment
527# hits by overriding this method.
528nano_umount ( ) (
529	umount ${1}
530)
531
532populate_slice ( ) (
533	local dev dir mnt lbl
534	dev=$1
535	dir=$2
536	mnt=$3
537	lbl=$4
538	echo "Creating ${dev} (mounting on ${mnt})"
539	newfs_part ${dev} ${mnt} ${lbl}
540	if [ -n "${dir}" -a -d "${dir}" ]; then
541		echo "Populating ${lbl} from ${dir}"
542		cd "${dir}"
543		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
544	fi
545	df -i ${mnt}
546	nano_umount ${mnt}
547)
548
549populate_cfg_slice ( ) (
550	populate_slice "$1" "$2" "$3" "$4"
551)
552
553populate_data_slice ( ) (
554	populate_slice "$1" "$2" "$3" "$4"
555)
556
557create_diskimage ( ) (
558	pprint 2 "build diskimage"
559	pprint 3 "log: ${NANO_OBJ}/_.di"
560
561	(
562	echo $NANO_MEDIASIZE $NANO_IMAGES \
563		$NANO_SECTS $NANO_HEADS \
564		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
565	awk '
566	{
567		printf "# %s\n", $0
568
569		# size of cylinder in sectors
570		cs = $3 * $4
571
572		# number of full cylinders on media
573		cyl = int ($1 / cs)
574
575		# output fdisk geometry spec, truncate cyls to 1023
576		if (cyl <= 1023)
577			print "g c" cyl " h" $4 " s" $3
578		else
579			print "g c" 1023 " h" $4 " s" $3
580
581		if ($7 > 0) { 
582			# size of data partition in full cylinders
583			dsl = int (($7 + cs - 1) / cs)
584		} else {
585			dsl = 0;
586		}
587
588		# size of config partition in full cylinders
589		csl = int (($6 + cs - 1) / cs)
590
591		if ($5 == 0) {
592			# size of image partition(s) in full cylinders
593			isl = int ((cyl - dsl - csl) / $2)
594		} else {
595			isl = int (($5 + cs - 1) / cs)
596		}
597
598		# First image partition start at second track
599		print "p 1 165 " $3, isl * cs - $3
600		c = isl * cs;
601
602		# Second image partition (if any) also starts offset one 
603		# track to keep them identical.
604		if ($2 > 1) {
605			print "p 2 165 " $3 + c, isl * cs - $3
606			c += isl * cs;
607		}
608
609		# Config partition starts at cylinder boundary.
610		print "p 3 165 " c, csl * cs
611		c += csl * cs
612
613		# Data partition (if any) starts at cylinder boundary.
614		if ($7 > 0) {
615			print "p 4 165 " c, dsl * cs
616		} else if ($7 < 0 && $1 > c) {
617			print "p 4 165 " c, $1 - c
618		} else if ($1 < c) {
619			print "Disk space overcommitted by", \
620			    c - $1, "sectors" > "/dev/stderr"
621			exit 2
622		}
623
624		# Force slice 1 to be marked active. This is necessary
625		# for booting the image from a USB device to work.
626		print "a 1"
627	}
628	' > ${NANO_OBJ}/_.fdisk
629
630	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
631	MNT=${NANO_OBJ}/_.mnt
632	mkdir -p ${MNT}
633
634	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
635		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
636			-y ${NANO_HEADS}`
637	else
638		echo "Creating md backing file..."
639		nano_rm -f ${IMG}
640		dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
641		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
642			-y ${NANO_HEADS}`
643	fi
644
645	trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
646
647	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
648	fdisk ${MD}
649	# XXX: params
650	# XXX: pick up cached boot* files, they may not be in image anymore.
651	if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
652		boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
653	fi
654	if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
655		bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT}
656	else
657		bsdlabel -w ${MD}${NANO_SLICE_ROOT}
658	fi
659	bsdlabel ${MD}${NANO_SLICE_ROOT}
660
661	# Create first image
662	populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a"
663	mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT}
664	echo "Generating mtree..."
665	( cd "${MNT}" && mtree -c ) > ${NANO_OBJ}/_.mtree
666	( cd "${MNT}" && du -k ) > ${NANO_OBJ}/_.du
667	nano_umount "${MNT}"
668
669	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
670		# Duplicate to second image (if present)
671		echo "Duplicating to second image..."
672		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
673		mount /dev/${MD}${NANO_SLICE_ALTROOT}a ${MNT}
674		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
675		do
676			sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
677		done
678		nano_umount ${MNT}
679		# Override the label from the first partition so we
680		# don't confuse glabel with duplicates.
681		if [ ! -z ${NANO_LABEL} ]; then
682			tunefs -L ${NANO_LABEL}"${NANO_SLICE_ALTROOT}a" /dev/${MD}${NANO_SLICE_ALTROOT}a
683		fi
684	fi
685	
686	# Create Config slice
687	populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
688
689	# Create Data slice, if any.
690	if [ ! -z $NANO_SLICE_DATA -a $NANO_SLICE_CFG = $NANO_SLICE_DATA -a \
691	   $NANO_DATASIZE -ne 0 ]; then
692		pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
693		exit 2
694	fi
695	if [ $NANO_DATASIZE -ne 0 -a ! -z $NANO_SLICE_DATA ] ; then
696		populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
697	fi
698
699	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
700		if [ ${NANO_IMAGE_MBRONLY} ]; then
701			echo "Writing out _.disk.mbr..."
702			dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
703		else
704			echo "Writing out ${NANO_IMGNAME}..."
705			dd if=/dev/${MD} of=${IMG} bs=64k
706		fi
707
708		echo "Writing out ${NANO_IMGNAME}..."
709		dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
710	fi
711
712	if ${do_copyout_partition} ; then
713		echo "Writing out _.disk.image..."
714		dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
715	fi
716	mdconfig -d -u $MD
717
718	trap - 1 2 15
719	trap nano_cleanup EXIT
720
721	) > ${NANO_OBJ}/_.di 2>&1
722)
723
724last_orders ( ) (
725	# Redefine this function with any last orders you may have
726	# after the build completed, for instance to copy the finished
727	# image to a more convenient place:
728	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
729	true
730)
731
732#######################################################################
733#
734# Optional convenience functions.
735#
736#######################################################################
737
738#######################################################################
739# Common Flash device geometries
740#
741
742FlashDevice ( ) {
743	if [ -d ${NANO_TOOLS} ] ; then
744		. ${NANO_TOOLS}/FlashDevice.sub
745	else
746		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
747	fi
748	sub_FlashDevice $1 $2
749}
750
751#######################################################################
752# USB device geometries
753#
754# Usage:
755#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
756#
757# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
758#
759# Note that the capacity of a flash key is usually advertised in MB or
760# GB, *not* MiB/GiB. As such, the precise number of cylinders available
761# for C/H/S geometry may vary depending on the actual flash geometry.
762#
763# The following generic device layouts are understood:
764#  generic           An alias for generic-hdd.
765#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
766#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
767#
768# The generic-hdd device is preferred for flash devices larger than 1GB.
769#
770
771UsbDevice ( ) {
772	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
773	case $a1 in
774	generic-fdd)
775		NANO_HEADS=64
776		NANO_SECTS=32
777		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
778		;;
779	generic|generic-hdd)
780		NANO_HEADS=255
781		NANO_SECTS=63
782		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
783		;;
784	*)
785		echo "Unknown USB flash device"
786		exit 2
787		;;
788	esac
789}
790
791#######################################################################
792# Setup serial console
793
794cust_comconsole ( ) (
795	# Enable getty on console
796	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
797
798	# Disable getty on syscons devices
799	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
800
801	# Tell loader to use serial console early.
802	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
803)
804
805#######################################################################
806# Allow root login via ssh
807
808cust_allow_ssh_root ( ) (
809	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
810	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
811)
812
813#######################################################################
814# Install the stuff under ./Files
815
816cust_install_files ( ) (
817	cd "${NANO_TOOLS}/Files"
818	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
819)
820
821#######################################################################
822# Install packages from ${NANO_PACKAGE_DIR}
823
824cust_pkgng ( ) (
825
826	# If the package directory doesn't exist, we're done.
827	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
828		echo "DONE 0 packages"
829		return 0
830	fi
831
832	# Find a pkg-* package
833	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
834		_NANO_PKG_PACKAGE=`basename "$x"`
835	done
836	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
837		echo "FAILED: need a pkg/ package for bootstrapping"
838		exit 2
839	fi
840
841	# Copy packages into chroot
842	mkdir -p ${NANO_WORLDDIR}/Pkg
843	(
844		cd "${NANO_PACKAGE_DIR}"
845		find "${NANO_PACKAGE_LIST}" -print |
846		cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
847	)
848
849	#Bootstrap pkg
850	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
851	CR pkg -N >/dev/null 2>&1
852	if [ "$?" -ne "0" ]; then
853		echo "FAILED: pkg bootstrapping faied"
854		exit 2
855	fi
856	nano_rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
857
858	# Count & report how many we have to install
859	todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
860	todo=$(expr $todo + 1) # add one for pkg since it is installed already
861	echo "=== TODO: $todo"
862	ls ${NANO_WORLDDIR}/Pkg
863	echo "==="
864	while true
865	do
866		# Record how many we have now
867 		have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
868
869		# Attempt to install more packages
870		CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
871
872		# See what that got us
873 		now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
874		echo "=== NOW $now"
875		CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
876		echo "==="
877		if [ $now -eq $todo ] ; then
878			echo "DONE $now packages"
879			break
880		elif [ $now -eq $have ] ; then
881			echo "FAILED: Nothing happened on this pass"
882			exit 2
883		fi
884	done
885	nano_rm -rf ${NANO_WORLDDIR}/Pkg
886)
887
888#######################################################################
889# Convenience function:
890# 	Register all args as customize function.
891
892customize_cmd ( ) {
893	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
894}
895
896#######################################################################
897# Convenience function:
898# 	Register all args as late customize function to run just before
899#	image creation.
900
901late_customize_cmd ( ) {
902	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
903}
904
905#######################################################################
906#
907# All set up to go...
908#
909#######################################################################
910
911# Progress Print
912#	Print $2 at level $1.
913pprint ( ) (
914    if [ "$1" -le $PPLEVEL ]; then
915	runtime=$(( `date +%s` - $NANO_STARTTIME ))
916	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
917    fi
918)
919
920usage ( ) {
921	(
922	echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
923	echo "	-b	suppress builds (both kernel and world)"
924	echo "	-c	specify config file"
925	echo "	-f	suppress code slice extraction"
926	echo "	-i	suppress disk image build"
927	echo "	-K	suppress installkernel"
928	echo "	-k	suppress buildkernel"
929	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
930	echo "	-q	make output more quiet"
931	echo "	-v	make output more verbose"
932	echo "	-w	suppress buildworld"
933	) 1>&2
934	exit 2
935}
936
937#######################################################################
938# Setup and Export Internal variables
939#
940
941export_var ( ) {		# Don't wawnt a subshell
942	var=$1
943	# Lookup value of the variable.
944	eval val=\$$var
945	pprint 3 "Setting variable: $var=\"$val\""
946	export $1
947}
948
949# Call this function to set defaults _after_ parsing options.
950# dont want a subshell otherwise variable setting is thrown away.
951set_defaults_and_export ( ) {
952	test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
953	test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
954	test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
955	NANO_WORLDDIR=${NANO_OBJ}/_.w
956	NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
957	NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
958
959	# Override user's NANO_DRIVE if they specified a NANO_LABEL
960	[ ! -z "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true
961
962	# Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
963	[ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
964		NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true
965
966	[ ! -z "${NANO_NOPRIV_BUILD}" ] && [ -z "${NANO_METALOG}" ] && \
967		NANO_METALOG=${NANO_OBJ}/_.metalog || true
968
969	NANO_STARTTIME=`date +%s`
970	pprint 3 "Exporting NanoBSD variables"
971	export_var MAKEOBJDIRPREFIX
972	export_var NANO_ARCH
973	export_var NANO_CODESIZE
974	export_var NANO_CONFSIZE
975	export_var NANO_CUSTOMIZE
976	export_var NANO_DATASIZE
977	export_var NANO_DRIVE
978	export_var NANO_HEADS
979	export_var NANO_IMAGES
980	export_var NANO_IMGNAME
981	export_var NANO_MAKE
982	export_var NANO_MAKE_CONF_BUILD
983	export_var NANO_MAKE_CONF_INSTALL
984	export_var NANO_MEDIASIZE
985	export_var NANO_NAME
986	export_var NANO_NEWFS
987	export_var NANO_OBJ
988	export_var NANO_PMAKE
989	export_var NANO_SECTS
990	export_var NANO_SRC
991	export_var NANO_TOOLS
992	export_var NANO_WORLDDIR
993	export_var NANO_BOOT0CFG
994	export_var NANO_BOOTLOADER
995	export_var NANO_LABEL
996	export_var NANO_MODULES
997	export_var NANO_NOPRIV_BUILD
998	export_var NANO_METALOG
999	export_var SRCCONF
1000	export_var SRC_ENV_CONF
1001}
1002