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