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