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