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