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