xref: /freebsd/tools/tools/nanobsd/defaults.sh (revision 2a58b312)
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$
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# Path to mtree file to apply to anything copied by cust_install_files().
56# If you specify this, the mtree file *must* have an entry for every file and
57# directory located in Files.
58#NANO_CUST_FILES_MTREE=""
59
60# Object tree directory
61# default is subdir of /usr/obj
62#NANO_OBJ=""
63
64# The directory to put the final images
65# default is ${NANO_OBJ}
66#NANO_DISKIMGDIR=""
67
68# Make & parallel Make
69NANO_MAKE="make"
70NANO_NCPU=$(sysctl -n hw.ncpu)
71NANO_PMAKE="make -j $NANO_NCPU"
72
73# The default name for any image we create.
74NANO_IMGNAME="_.disk.full"
75NANO_IMG1NAME="_.disk.image"
76
77# Options to put in make.conf during buildworld only
78CONF_BUILD=' '
79
80# Options to put in make.conf during installworld only
81CONF_INSTALL=' '
82
83# Options to put in make.conf during both build- & installworld.
84CONF_WORLD=' '
85
86# Kernel config file to use
87NANO_KERNEL=GENERIC
88
89# Kernel modules to install. If empty, no modules are installed.
90# Use "default" to install all built modules.
91NANO_MODULES=
92
93# Early customize commands.
94NANO_EARLY_CUSTOMIZE=""
95
96# Customize commands.
97NANO_CUSTOMIZE=""
98
99# Late customize commands.
100NANO_LATE_CUSTOMIZE=""
101
102# Newfs parameters to use
103NANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
104
105# The drive name of the media at runtime
106NANO_DRIVE=ada0
107
108# Target media size in 512 bytes sectors
109NANO_MEDIASIZE=2000000
110
111# Number of code images on media (1 or 2)
112NANO_IMAGES=2
113
114# 0 -> Leave second image all zeroes so it compresses better.
115# 1 -> Initialize second image with a copy of the first
116NANO_INIT_IMG2=1
117
118# Size of code file system in 512 bytes sectors
119# If zero, size will be as large as possible.
120NANO_CODESIZE=0
121
122# Size of configuration file system in 512 bytes sectors
123# Cannot be zero.
124NANO_CONFSIZE=2048
125
126# Size of data file system in 512 bytes sectors
127# If zero: no partition configured.
128# If negative: max size possible
129NANO_DATASIZE=0
130
131# Size of the /etc ramdisk in 512 bytes sectors
132NANO_RAM_ETCSIZE=10240
133
134# Size of the /tmp+/var ramdisk in 512 bytes sectors
135NANO_RAM_TMPVARSIZE=10240
136
137# boot0 flags/options and configuration
138NANO_BOOT0CFG="-o packet -s 1 -m 3"
139NANO_BOOTLOADER="boot/boot0sio"
140
141# boot2 flags/options
142# default force serial console
143NANO_BOOT2CFG="-h -S115200"
144
145# Backing type of md(4) device
146# Can be "file" or "swap"
147NANO_MD_BACKING="file"
148
149# for swap type md(4) backing, write out the mbr only
150NANO_IMAGE_MBRONLY=true
151
152# Progress Print level
153PPLEVEL=3
154
155# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
156# in preference to /dev/${NANO_DRIVE}
157# Root partition will be ${NANO_LABEL}s{1,2}
158# /cfg partition will be ${NANO_LABEL}s3
159# /data partition will be ${NANO_LABEL}s4
160NANO_LABEL=""
161NANO_SLICE_ROOT=s1
162NANO_SLICE_ALTROOT=s2
163NANO_SLICE_CFG=s3
164NANO_SLICE_DATA=s4
165NANO_PARTITION_ROOT=a
166NANO_PARTITION_ALTROOT=a
167NANO_ROOT=s1a
168NANO_ALTROOT=s2a
169
170# Default ownwership for nopriv build
171NANO_DEF_UNAME=root
172NANO_DEF_GNAME=wheel
173
174#######################################################################
175# Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
176# Unfortunately, there's no way to set TARGET at this time, and it
177# conflates the two, so architectures where TARGET != TARGET_ARCH and
178# TARGET can't be guessed from TARGET_ARCH do not work.  This defaults
179# to the arch of the current machine.
180NANO_ARCH=`uname -p`
181
182# CPUTYPE defaults to "" which is the default when CPUTYPE isn't
183# defined.
184NANO_CPUTYPE=""
185
186# Directory to populate /cfg from
187NANO_CFGDIR=""
188
189# Directory to populate /data from
190NANO_DATADIR=""
191
192# We don't need SRCCONF or SRC_ENV_CONF. NanoBSD puts everything we
193# need for the build in files included with __MAKE_CONF. Override in your
194# config file if you really must. We set them unconditionally here, though
195# in case they are stray in the build environment
196SRCCONF=/dev/null
197SRC_ENV_CONF=/dev/null
198
199# Comment this out if /usr/obj is a symlink
200# CPIO_SYMLINK=--insecure
201
202#######################################################################
203#
204# The functions which do the real work.
205# Can be overridden from the config file(s)
206#
207#######################################################################
208
209# Export values into the shell. Must use { } instead of ( ) like
210# other functions to avoid a subshell.
211# We set __MAKE_CONF as a global since it is easier to get quoting
212# right for paths with spaces in them.
213make_export ( ) {
214	# Similar to export_var, except puts the data out to stdout
215	var=$1
216	eval val=\$$var
217	echo "Setting variable: $var=\"$val\""
218	export $1
219}
220
221nano_make_build_env ( ) {
222	__MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
223	make_export __MAKE_CONF
224}
225
226nano_make_install_env ( ) {
227	__MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
228	make_export __MAKE_CONF
229}
230
231# Extra environment variables for kernel builds
232nano_make_kernel_env ( ) {
233	if [ -f "${NANO_KERNEL}" ] ; then
234		KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
235		KERNCONF="$(basename ${NANO_KERNEL})"
236		make_export KERNCONFDIR
237		make_export KERNCONF
238	else
239		export KERNCONF="${NANO_KERNEL}"
240		make_export KERNCONF
241	fi
242}
243
244nano_global_make_env ( ) (
245	# global settings for the make.conf file, if set
246	[ -z "${NANO_ARCH}" ] || echo TARGET_ARCH="${NANO_ARCH}"
247	[ -z "${NANO_CPUTYPE}" ] || echo TARGET_CPUTYPE="${NANO_CPUTYPE}"
248)
249
250#
251# Create empty files in the target tree, and record the fact.  All paths
252# are relative to NANO_WORLDDIR.
253#
254tgt_touch ( ) (
255	cd "${NANO_WORLDDIR}"
256	for i; do
257		touch $i
258		echo "./${i} type=file" >> ${NANO_METALOG}
259	done
260)
261
262#
263# Convert a directory into a symlink. Takes two arguments, the
264# current directory and what it should become a symlink to. The
265# directory is removed and a symlink is created. If we're doing
266# a nopriv build, then append this fact to the metalog
267#
268tgt_dir2symlink ( ) (
269	dir=$1
270	symlink=$2
271
272	cd "${NANO_WORLDDIR}"
273	rm -xrf "$dir"
274	ln -s "$symlink" "$dir"
275	if [ -n "$NANO_METALOG" ]; then
276		echo "./${dir} type=link mode=0777 link=${symlink}" >> ${NANO_METALOG}
277	fi
278)
279
280# run in the world chroot, errors fatal
281CR ( ) {
282	chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
283}
284
285# run in the world chroot, errors not fatal
286CR0 ( ) {
287	chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
288}
289
290clean_build ( ) (
291	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
292
293	if ! rm -xrf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
294		chflags -R noschg ${MAKEOBJDIRPREFIX}/
295		rm -xr ${MAKEOBJDIRPREFIX}/
296	fi
297)
298
299make_conf_build ( ) (
300	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
301
302	mkdir -p ${MAKEOBJDIRPREFIX}
303	printenv > ${MAKEOBJDIRPREFIX}/_.env
304
305	# Make sure we get all the global settings that NanoBSD wants
306	# in addition to the user's global settings
307	(
308	nano_global_make_env
309	echo "${CONF_WORLD}"
310	echo "${CONF_BUILD}"
311	) > ${NANO_MAKE_CONF_BUILD}
312)
313
314build_world ( ) (
315	pprint 2 "run buildworld"
316	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
317
318	(
319	nano_make_build_env
320	set -o xtrace
321	cd "${NANO_SRC}"
322	${NANO_PMAKE} buildworld
323	) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
324)
325
326build_kernel ( ) (
327	pprint 2 "build kernel ($NANO_KERNEL)"
328	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
329
330	(
331	nano_make_build_env
332	nano_make_kernel_env
333
334	# Note: We intentionally build all modules, not only the ones in
335	# NANO_MODULES so the built world can be reused by multiple images.
336	# Although MODULES_OVERRIDE can be defined in the kernel config
337	# file to override this behavior. Just set NANO_MODULES=default.
338	set -o xtrace
339	cd "${NANO_SRC}"
340	${NANO_PMAKE} buildkernel
341	) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
342)
343
344clean_world ( ) (
345	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
346		pprint 2 "Clean and create object directory (${NANO_OBJ})"
347		if ! rm -xrf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
348			chflags -R noschg ${NANO_OBJ}
349			rm -xr ${NANO_OBJ}/
350		fi
351		mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
352		printenv > ${NANO_LOG}/_.env
353	else
354		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
355		if ! rm -xrf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
356			chflags -R noschg "${NANO_WORLDDIR}"
357			rm -xrf "${NANO_WORLDDIR}/"
358		fi
359		mkdir -p "${NANO_WORLDDIR}"
360	fi
361)
362
363make_conf_install ( ) (
364	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
365
366	# Make sure we get all the global settings that NanoBSD wants
367	# in addition to the user's global settings
368	(
369	nano_global_make_env
370	echo "${CONF_WORLD}"
371	echo "${CONF_INSTALL}"
372	if [ -n "${NANO_NOPRIV_BUILD}" ]; then
373	    echo NO_ROOT=t
374	    echo METALOG=${NANO_METALOG}
375	fi
376	) >  ${NANO_MAKE_CONF_INSTALL}
377)
378
379install_world ( ) (
380	pprint 2 "installworld"
381	pprint 3 "log: ${NANO_LOG}/_.iw"
382
383	(
384	nano_make_install_env
385	set -o xtrace
386	cd "${NANO_SRC}"
387	${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
388	chflags -R noschg "${NANO_WORLDDIR}"
389	) > ${NANO_LOG}/_.iw 2>&1
390)
391
392install_etc ( ) (
393	pprint 2 "install /etc"
394	pprint 3 "log: ${NANO_LOG}/_.etc"
395
396	(
397	nano_make_install_env
398	set -o xtrace
399	cd "${NANO_SRC}"
400	${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
401	# make.conf doesn't get created by default, but some ports need it
402	# so they can spam it.
403	cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
404	) > ${NANO_LOG}/_.etc 2>&1
405)
406
407install_kernel ( ) (
408	pprint 2 "install kernel ($NANO_KERNEL)"
409	pprint 3 "log: ${NANO_LOG}/_.ik"
410
411	(
412
413	nano_make_install_env
414	nano_make_kernel_env
415
416	if [ "${NANO_MODULES}" != "default" ]; then
417		MODULES_OVERRIDE="${NANO_MODULES}"
418		make_export MODULES_OVERRIDE
419	fi
420
421	set -o xtrace
422	cd "${NANO_SRC}"
423	${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
424
425	) > ${NANO_LOG}/_.ik 2>&1
426)
427
428native_xtools ( ) (
429	print 2 "Installing the optimized native build tools for cross env"
430	pprint 3 "log: ${NANO_LOG}/_.native_xtools"
431
432	(
433
434	nano_make_install_env
435	set -o xtrace
436	cd "${NANO_SRC}"
437	${NANO_MAKE} native-xtools
438	${NANO_MAKE} native-xtools-install DESTDIR="${NANO_WORLDDIR}"
439
440	) > ${NANO_LOG}/_.native_xtools 2>&1
441)
442
443#
444# Run the requested set of early customization scripts, run before
445# buildworld.
446#
447run_early_customize ( ) {
448	pprint 2 "run early customize scripts"
449	for c in $NANO_EARLY_CUSTOMIZE
450	do
451		pprint 2 "early customize \"$c\""
452		pprint 3 "log: ${NANO_LOG}/_.early_cust.$c"
453		pprint 4 "`type $c`"
454		{ t=$(set -o | awk '$1 == "xtrace" && $2 == "off" { print "set +o xtrace"}');
455		  set -o xtrace ;
456		  $c ;
457		  eval $t
458		} >${NANO_LOG}/_.early_cust.$c 2>&1
459	done
460}
461
462#
463# Run the requested set of customization scripts, run after we've
464# done an installworld, installed the etc files, installed the kernel
465# and tweaked them in the standard way.
466#
467run_customize ( ) (
468
469	pprint 2 "run customize scripts"
470	for c in $NANO_CUSTOMIZE
471	do
472		pprint 2 "customize \"$c\""
473		pprint 3 "log: ${NANO_LOG}/_.cust.$c"
474		pprint 4 "`type $c`"
475		( set -o xtrace ; $c ) > ${NANO_LOG}/_.cust.$c 2>&1
476	done
477)
478
479#
480# Run any last-minute customization commands after we've had a chance to
481# setup nanobsd, prune empty dirs from /usr, etc
482#
483run_late_customize ( ) (
484	pprint 2 "run late customize scripts"
485	for c in $NANO_LATE_CUSTOMIZE
486	do
487		pprint 2 "late customize \"$c\""
488		pprint 3 "log: ${NANO_LOG}/_.late_cust.$c"
489		pprint 4 "`type $c`"
490		( set -o xtrace ; $c ) > ${NANO_LOG}/_.late_cust.$c 2>&1
491	done
492)
493
494#
495# Hook called after we run all the late customize commands, but
496# before we invoke the disk imager. The nopriv build uses it to
497# read in the meta log, apply the changes other parts of nanobsd
498# have been recording their actions. It's not anticipated that
499# a user's cfg file would override this.
500#
501fixup_before_diskimage ( ) (
502	# Run the deduplication script that takes the matalog journal and
503	# combines multiple entries for the same file (see source for
504	# details). We take the extra step of removing the size keywords. This
505	# script, and many of the user scripts, copies, appeneds and otherwise
506	# modifies files in the build, changing their sizes.  These actions are
507	# impossible to trap, so go ahead remove the size= keyword. For this
508	# narrow use, it doesn't buy us any protection and just gets in the way.
509	# The dedup tool's output must be sorted due to limitations in awk.
510	if [ -n "${NANO_METALOG}" ]; then
511		pprint 2 "Fixing metalog"
512		cp ${NANO_METALOG} ${NANO_METALOG}.pre
513		echo "/set uname=${NANO_DEF_UNAME} gname=${NANO_DEF_GNAME}" > ${NANO_METALOG}
514		cat ${NANO_METALOG}.pre | ${NANO_TOOLS}/mtree-dedup.awk | \
515		    sed -e 's/ size=[0-9][0-9]*//' | sort >> ${NANO_METALOG}
516	fi
517)
518
519setup_nanobsd ( ) (
520	pprint 2 "configure nanobsd setup"
521	pprint 3 "log: ${NANO_LOG}/_.dl"
522
523	(
524	cd "${NANO_WORLDDIR}"
525
526	# Move /usr/local/etc to /etc/local so that the /cfg stuff
527	# can stomp on it.  Otherwise packages like ipsec-tools which
528	# have hardcoded paths under ${prefix}/etc are not tweakable.
529	if [ -d usr/local/etc ] ; then
530		(
531		cd usr/local/etc
532		find . -print | cpio ${CPIO_SYMLINK} -dumpl ../../../etc/local
533		cd ..
534		rm -xrf etc
535		)
536	fi
537
538	# Always setup the usr/local/etc -> etc/local symlink.
539	# usr/local/etc gets created by packages, but if no packages
540	# are installed by this point, but are later in the process,
541	# the symlink not being here causes problems. It never hurts
542	# to have the symlink in error though.
543	ln -s ../../etc/local usr/local/etc
544
545	for d in var etc
546	do
547		# link /$d under /conf
548		# we use hard links so we have them both places.
549		# the files in /$d will be hidden by the mount.
550		mkdir -p conf/base/$d conf/default/$d
551		find $d -print | cpio ${CPIO_SYMLINK} -dumpl conf/base/
552	done
553
554	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
555	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
556
557	# pick up config files from the special partition
558	echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount
559
560	# Put /tmp on the /var ramdisk (could be symlink already)
561	tgt_dir2symlink tmp var/tmp
562
563	) > ${NANO_LOG}/_.dl 2>&1
564)
565
566setup_nanobsd_etc ( ) (
567	pprint 2 "configure nanobsd /etc"
568
569	(
570	cd "${NANO_WORLDDIR}"
571
572	# create diskless marker file
573	touch etc/diskless
574
575	[ -n "${NANO_NOPRIV_BUILD}" ] && chmod 666 etc/defaults/rc.conf
576
577	# Make root filesystem R/O by default
578	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
579	# Disable entropy file, since / is read-only /var/db/entropy should be enough?
580	echo "entropy_file=NO" >> etc/defaults/rc.conf
581
582	[ -n "${NANO_NOPRIV_BUILD}" ] && chmod 444 etc/defaults/rc.conf
583
584	# save config file for scripts
585	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
586
587	echo "/dev/${NANO_DRIVE}${NANO_ROOT} / ufs ro 1 1" > etc/fstab
588	echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab
589	mkdir -p cfg
590
591	# Create directory for eventual /usr/local/etc contents
592	mkdir -p etc/local
593	)
594)
595
596prune_usr ( ) (
597	# Remove all empty directories in /usr
598	find "${NANO_WORLDDIR}"/usr -type d -depth -print |
599		while read d
600		do
601			rmdir $d > /dev/null 2>&1 || true
602		done
603)
604
605newfs_part ( ) (
606	local dev mnt lbl
607	dev=$1
608	mnt=$2
609	lbl=$3
610	echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
611	newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
612	mount -o async ${dev} ${mnt}
613)
614
615# Convenient spot to work around any umount issues that your build environment
616# hits by overriding this method.
617nano_umount ( ) (
618	umount ${1}
619)
620
621populate_slice ( ) (
622	local dev dir mnt lbl
623	dev=$1
624	dir=$2
625	mnt=$3
626	lbl=$4
627	echo "Creating ${dev} (mounting on ${mnt})"
628	newfs_part ${dev} ${mnt} ${lbl}
629	if [ -n "${dir}" -a -d "${dir}" ]; then
630		echo "Populating ${lbl} from ${dir}"
631		cd "${dir}"
632		find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)/' | cpio ${CPIO_SYMLINK} -dumpv ${mnt}
633	fi
634	df -i ${mnt}
635	nano_umount ${mnt}
636)
637
638populate_cfg_slice ( ) (
639	populate_slice "$1" "$2" "$3" "$4"
640)
641
642populate_data_slice ( ) (
643	populate_slice "$1" "$2" "$3" "$4"
644)
645
646last_orders ( ) (
647	# Redefine this function with any last orders you may have
648	# after the build completed, for instance to copy the finished
649	# image to a more convenient place:
650	# cp ${NANO_DISKIMGDIR}/${NANO_IMG1NAME} /home/ftp/pub/nanobsd.disk
651	true
652)
653
654#######################################################################
655#
656# Optional convenience functions.
657#
658#######################################################################
659
660#######################################################################
661# Common Flash device geometries
662#
663
664FlashDevice ( ) {
665	if [ -d ${NANO_TOOLS} ] ; then
666		. ${NANO_TOOLS}/FlashDevice.sub
667	else
668		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
669	fi
670	sub_FlashDevice $1 $2
671}
672
673#######################################################################
674# USB device geometries
675#
676# Usage:
677#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
678#
679# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
680#
681# Note that the capacity of a flash key is usually advertised in MB or
682# GB, *not* MiB/GiB. As such, the precise number of cylinders available
683# for C/H/S geometry may vary depending on the actual flash geometry.
684#
685# The following generic device layouts are understood:
686#  generic           An alias for generic-hdd.
687#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
688#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
689#
690# The generic-hdd device is preferred for flash devices larger than 1GB.
691#
692
693UsbDevice ( ) {
694	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
695	case $a1 in
696	generic-fdd)
697		NANO_HEADS=64
698		NANO_SECTS=32
699		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
700		;;
701	generic|generic-hdd)
702		NANO_HEADS=255
703		NANO_SECTS=63
704		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
705		;;
706	*)
707		echo "Unknown USB flash device"
708		exit 2
709		;;
710	esac
711}
712
713#######################################################################
714# Setup serial console
715
716cust_comconsole ( ) (
717	# Enable getty on console
718	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
719
720	# Disable getty on syscons devices
721	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
722
723	# Tell loader to use serial console early.
724	echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
725)
726
727#######################################################################
728# Allow root login via ssh
729
730cust_allow_ssh_root ( ) (
731	sed -i "" -E 's/^#?PermitRootLogin.*/PermitRootLogin yes/' \
732	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
733)
734
735#######################################################################
736# Install the stuff under ./Files
737
738cust_install_files ( ) (
739	cd "${NANO_TOOLS}/Files"
740	find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)/' | cpio ${CPIO_SYMLINK} -Ldumpv ${NANO_WORLDDIR}
741
742	if [ -n "${NANO_CUST_FILES_MTREE}" -a -f ${NANO_CUST_FILES_MTREE} ]; then
743		CR "mtree -eiU -p /" <${NANO_CUST_FILES_MTREE}
744	fi
745)
746
747#######################################################################
748# Install packages from ${NANO_PACKAGE_DIR}
749
750cust_pkgng ( ) (
751	mkdir -p ${NANO_WORLDDIR}/usr/local/etc
752	local PKG_CONF="${NANO_WORLDDIR}/usr/local/etc/pkg.conf"
753	local PKGCMD="env BATCH=YES ASSUME_ALWAYS_YES=YES PKG_DBDIR=${NANO_PKG_META_BASE}/pkg SIGNATURE_TYPE=none /usr/sbin/pkg"
754
755	# Ensure pkg.conf points pkg to where the package meta data lives.
756	touch ${PKG_CONF}
757	if grep -Eiq '^PKG_DBDIR:.*' ${PKG_CONF}; then
758		sed -i -e "\|^PKG_DBDIR:.*|Is||PKG_DBDIR: "\"${NANO_PKG_META_BASE}/pkg\""|" ${PKG_CONF}
759	else
760		echo "PKG_DBDIR: \"${NANO_PKG_META_BASE}/pkg\"" >> ${PKG_CONF}
761	fi
762
763	# If the package directory doesn't exist, we're done.
764	if [ ! -d ${NANO_PACKAGE_DIR} ]; then
765		echo "DONE 0 packages"
766		return 0
767	fi
768
769	# Find a pkg-* package
770	for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
771		_NANO_PKG_PACKAGE=`basename "$x"`
772	done
773	if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
774		echo "FAILED: need a pkg/ package for bootstrapping"
775		exit 2
776	fi
777
778	# Mount packages into chroot
779	mkdir -p ${NANO_WORLDDIR}/_.p
780	mount -t nullfs -o noatime -o ro ${NANO_PACKAGE_DIR} ${NANO_WORLDDIR}/_.p
781	mount -t devfs devfs ${NANO_WORLDDIR}/dev
782
783	trap "umount ${NANO_WORLDDIR}/dev; umount ${NANO_WORLDDIR}/_.p ; rm -xrf ${NANO_WORLDDIR}/_.p" 1 2 15 EXIT
784
785	# Install pkg-* package
786	CR "${PKGCMD} add /_.p/${_NANO_PKG_PACKAGE}"
787
788	(
789		# Expand any glob characters in pacakge list
790		cd "${NANO_PACKAGE_DIR}"
791		_PKGS=`find ${NANO_PACKAGE_LIST} -not -name "${_NANO_PKG_PACKAGE}" -print | sort | uniq`
792
793		# Show todo
794		todo=`echo "$_PKGS" | wc -l`
795		echo "=== TODO: $todo"
796		echo "$_PKGS"
797		echo "==="
798
799		# Install packages
800		for _PKG in $_PKGS; do
801			CR "${PKGCMD} add /_.p/${_PKG}"
802		done
803	)
804
805	CR0 "${PKGCMD} info"
806
807	trap - 1 2 15 EXIT
808	umount ${NANO_WORLDDIR}/dev
809	umount ${NANO_WORLDDIR}/_.p
810	rm -xrf ${NANO_WORLDDIR}/_.p
811)
812
813#######################################################################
814# Convenience function:
815#	Register all args as early customize function to run just before
816#	build commences.
817
818early_customize_cmd ( ) {
819	NANO_EARLY_CUSTOMIZE="$NANO_EARLY_CUSTOMIZE $*"
820}
821
822#######################################################################
823# Convenience function:
824# 	Register all args as customize function.
825
826customize_cmd ( ) {
827	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
828}
829
830#######################################################################
831# Convenience function:
832# 	Register all args as late customize function to run just before
833#	image creation.
834
835late_customize_cmd ( ) {
836	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
837}
838
839#######################################################################
840#
841# All set up to go...
842#
843#######################################################################
844
845# Progress Print
846#	Print $2 at level $1.
847pprint ( ) (
848    if [ "$1" -le $PPLEVEL ]; then
849	runtime=$(( `date +%s` - $NANO_STARTTIME ))
850	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
851    fi
852)
853
854usage ( ) {
855	(
856	echo "Usage: $0 [-bfhiKknqvwX] [-c config_file]"
857	echo "	-b	suppress builds (both kernel and world)"
858	echo "	-c	specify config file"
859	echo "	-f	suppress code slice extraction (implies -i)"
860	echo "	-h	print this help summary page"
861	echo "	-i	suppress disk image build"
862	echo "	-K	suppress installkernel"
863	echo "	-k	suppress buildkernel"
864	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
865	echo "	-q	make output more quiet"
866	echo "	-v	make output more verbose"
867	echo "	-w	suppress buildworld"
868	echo "	-X	make native-xtools"
869	) 1>&2
870	exit 2
871}
872
873#######################################################################
874# Setup and Export Internal variables
875#
876
877export_var ( ) {		# Don't wawnt a subshell
878	var=$1
879	# Lookup value of the variable.
880	eval val=\$$var
881	pprint 3 "Setting variable: $var=\"$val\""
882	export $1
883}
884
885# Call this function to set defaults _after_ parsing options.
886# dont want a subshell otherwise variable setting is thrown away.
887set_defaults_and_export ( ) {
888	: ${NANO_OBJ:=/usr/obj/nanobsd.${NANO_NAME}${NANO_LAYOUT}:+.${NANO_LAYOUT}}
889	: ${MAKEOBJDIRPREFIX:=${NANO_OBJ}}
890	: ${NANO_DISKIMGDIR:=${NANO_OBJ}}
891	: ${NANO_WORLDDIR:=${NANO_OBJ}/_.w}
892	: ${NANO_LOG:=${NANO_OBJ}}
893	NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
894	NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
895
896	# Override user's NANO_DRIVE if they specified a NANO_LABEL
897	[ -n "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true
898
899	# Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
900	[ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
901		NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true
902
903	[ -n "${NANO_NOPRIV_BUILD}" ] && [ -z "${NANO_METALOG}" ] && \
904		NANO_METALOG=${NANO_OBJ}/_.metalog || true
905
906	NANO_STARTTIME=`date +%s`
907	pprint 3 "Exporting NanoBSD variables"
908	export_var MAKEOBJDIRPREFIX
909	export_var NANO_ARCH
910	export_var NANO_CODESIZE
911	export_var NANO_CONFSIZE
912	export_var NANO_CUSTOMIZE
913	export_var NANO_DATASIZE
914	export_var NANO_DRIVE
915	export_var NANO_HEADS
916	export_var NANO_IMAGES
917	export_var NANO_IMGNAME
918	export_var NANO_IMG1NAME
919	export_var NANO_MAKE
920	export_var NANO_MAKE_CONF_BUILD
921	export_var NANO_MAKE_CONF_INSTALL
922	export_var NANO_MEDIASIZE
923	export_var NANO_NAME
924	export_var NANO_NCPU
925	export_var NANO_NEWFS
926	export_var NANO_OBJ
927	export_var NANO_PMAKE
928	export_var NANO_SECTS
929	export_var NANO_SRC
930	export_var NANO_TOOLS
931	export_var NANO_WORLDDIR
932	export_var NANO_BOOT0CFG
933	export_var NANO_BOOTLOADER
934	export_var NANO_LABEL
935	export_var NANO_MODULES
936	export_var NANO_NOPRIV_BUILD
937	export_var NANO_METALOG
938	export_var NANO_LOG
939	export_var SRCCONF
940	export_var SRC_ENV_CONF
941}
942