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