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