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