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