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