nanobsd.sh revision 183284
11556Srgrimes#!/bin/sh
21556Srgrimes#
31556Srgrimes# Copyright (c) 2005 Poul-Henning Kamp.
41556Srgrimes# All rights reserved.
51556Srgrimes#
61556Srgrimes# Redistribution and use in source and binary forms, with or without
71556Srgrimes# modification, are permitted provided that the following conditions
81556Srgrimes# are met:
91556Srgrimes# 1. Redistributions of source code must retain the above copyright
101556Srgrimes#    notice, this list of conditions and the following disclaimer.
111556Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
121556Srgrimes#    notice, this list of conditions and the following disclaimer in the
131556Srgrimes#    documentation and/or other materials provided with the distribution.
141556Srgrimes#
151556Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161556Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171556Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181556Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191556Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201556Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211556Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221556Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231556Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241556Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251556Srgrimes# SUCH DAMAGE.
261556Srgrimes#
271556Srgrimes# $FreeBSD: head/tools/tools/nanobsd/nanobsd.sh 183284 2008-09-22 23:56:36Z bms $
281556Srgrimes#
291556Srgrimes
301556Srgrimesset -e
311556Srgrimes
321556Srgrimes#######################################################################
331556Srgrimes#
3436150Scharnier# Setup default values for all controlling variables.
3536150Scharnier# These values can be overridden from the config file(s)
3636150Scharnier#
371556Srgrimes#######################################################################
3899110Sobrien
3999110Sobrien# Name of this NanoBSD build.  (Used to construct workdir names)
401556SrgrimesNANO_NAME=full
4117987Speter
42149017Sstefanf# Source tree directory
43209337SjillesNANO_SRC=/usr/src
4417987Speter
451556Srgrimes# Where nanobsd additional files live under the source tree
461556SrgrimesNANO_TOOLS=tools/tools/nanobsd
471556Srgrimes
481556Srgrimes# Where cust_pkg() finds packages to install
491556SrgrimesNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
501556Srgrimes
511556Srgrimes# Object tree directory
521556Srgrimes# default is subdir of /usr/obj
531556Srgrimes# XXX: MAKEOBJDIRPREFIX handling... ?
541556Srgrimes#NANO_OBJ=""
551556Srgrimes
561556Srgrimes# The directory to put the final images
571556Srgrimes# default is ${NANO_OBJ}
5817987Speter#NANO_DISKIMGDIR=""
5959436Scracauer
60214304Sjilles# Parallel Make
6117987SpeterNANO_PMAKE="make -j 3"
621556Srgrimes
6317987Speter# The default name for any image we create.
641556SrgrimesNANO_IMGNAME="_.disk.full"
651556Srgrimes
661556Srgrimes# Options to put in make.conf during buildworld only
671556SrgrimesCONF_BUILD=' '
681556Srgrimes
69142845Sobrien# Options to put in make.conf during installworld only
70142845SobrienCONF_INSTALL=' '
711556Srgrimes
72214709Sjilles# Options to put in make.conf during both build- & installworld.
73214709SjillesCONF_WORLD=' '
74214709Sjilles
75214709Sjilles# Kernel config file to use
76214709SjillesNANO_KERNEL=GENERIC
771556Srgrimes
7817987Speter# Customize commands.
791556SrgrimesNANO_CUSTOMIZE=""
801556Srgrimes
811556Srgrimes# Late customize commands.
821556SrgrimesNANO_LATE_CUSTOMIZE=""
831556Srgrimes
841556Srgrimes# Newfs paramters to use
851556SrgrimesNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
861556Srgrimes
871556Srgrimes# The drive name of the media at runtime
881556SrgrimesNANO_DRIVE=ad0
89206145Sjilles
90206145Sjilles# Target media size in 512 bytes sectors
91206145SjillesNANO_MEDIASIZE=1200000
92206145Sjilles
931556Srgrimes# Number of code images on media (1 or 2)
941556SrgrimesNANO_IMAGES=2
95213760Sobrien
96213760Sobrien# 0 -> Leave second image all zeroes so it compresses better.
97213760Sobrien# 1 -> Initialize second image with a copy of the first
98213760SobrienNANO_INIT_IMG2=1
99255068Sjilles
100213760Sobrien# Size of code file system in 512 bytes sectors
101253659Sjilles# If zero, size will be as large as possible.
102213760SobrienNANO_CODESIZE=0
103213760Sobrien
104213760Sobrien# Size of configuration file system in 512 bytes sectors
105213760Sobrien# Cannot be zero.
106213760SobrienNANO_CONFSIZE=2048
107213760Sobrien
108213760Sobrien# Size of data file system in 512 bytes sectors
1091556Srgrimes# If zero: no partition configured.
1101556Srgrimes# If negative: max size possible
111214525SjillesNANO_DATASIZE=0
112213811Sobrien
113213811Sobrien# Size of the /etc ramdisk in 512 bytes sectors
114213811SobrienNANO_RAM_ETCSIZE=10240
115213811Sobrien
116213811Sobrien# Size of the /tmp+/var ramdisk in 512 bytes sectors
117213811SobrienNANO_RAM_TMPVARSIZE=10240
118213811Sobrien
119213811Sobrien# Media geometry, only relevant if bios doesn't understand LBA.
120213811SobrienNANO_SECTS=63
121213811SobrienNANO_HEADS=16
122248980Sjilles
123213811Sobrien# boot0 flags/options and configuration
124213811SobrienNANO_BOOT0CFG="-o packet -s 1 -m 3"
125213811SobrienNANO_BOOTLOADER="boot/boot0sio"
126213811Sobrien
1271556Srgrimes# Backing type of md(4) device
12817987Speter# Can be "file" or "swap"
129213811SobrienNANO_MD_BACKING="file"
130206145Sjilles
131206145Sjilles#######################################################################
132206145Sjilles# Not a variable at this time
133206145Sjilles
134206145SjillesNANO_ARCH=i386
135206145Sjilles
136206145Sjilles#######################################################################
137206145Sjilles#
138206145Sjilles# The functions which do the real work.
139206145Sjilles# Can be overridden from the config file(s)
140206145Sjilles#
141206145Sjilles#######################################################################
142206145Sjilles
143206145Sjillesclean_build ( ) (
144206145Sjilles	echo "## Clean and create object directory (${MAKEOBJDIRPREFIX})"
145213811Sobrien
146206145Sjilles	if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
147206145Sjilles		true
148206145Sjilles	else
149206145Sjilles		chflags -R noschg ${MAKEOBJDIRPREFIX}
150206145Sjilles		rm -rf ${MAKEOBJDIRPREFIX}
151206145Sjilles	fi
152206145Sjilles	mkdir -p ${MAKEOBJDIRPREFIX}
153206145Sjilles	printenv > ${MAKEOBJDIRPREFIX}/_.env
154206145Sjilles)
155206145Sjilles
156206145Sjillesmake_conf_build ( ) (
157206145Sjilles	echo "## Construct build make.conf ($NANO_MAKE_CONF)"
158206145Sjilles
159206145Sjilles	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
160213811Sobrien	echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
161206145Sjilles)
162206145Sjilles
163206145Sjillesbuild_world ( ) (
164206145Sjilles	echo "## run buildworld"
165206145Sjilles	echo "### log: ${MAKEOBJDIRPREFIX}/_.bw"
166206145Sjilles
167206145Sjilles	cd ${NANO_SRC}
168206145Sjilles	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
169206145Sjilles		__MAKE_CONF=${NANO_MAKE_CONF} buildworld \
170206145Sjilles		> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
171206145Sjilles)
172206145Sjilles
173206145Sjillesbuild_kernel ( ) (
174206145Sjilles	echo "## build kernel ($NANO_KERNEL)"
175206145Sjilles	echo "### log: ${MAKEOBJDIRPREFIX}/_.bk"
176206145Sjilles
177206145Sjilles	if [ -f ${NANO_KERNEL} ] ; then
178206145Sjilles		cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
179206145Sjilles	fi
180213811Sobrien
181206145Sjilles	(cd ${NANO_SRC};
182206145Sjilles	# unset these just in case to avoid compiler complaints
183206145Sjilles	# when cross-building
184206145Sjilles	unset TARGET_CPUTYPE
185206145Sjilles	unset TARGET_BIG_ENDIAN
186206145Sjilles	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
187206145Sjilles		__MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
188206145Sjilles		> ${MAKEOBJDIRPREFIX}/_.bk 2>&1
189206145Sjilles	)
190206145Sjilles)
191206145Sjilles
192206145Sjillesclean_world ( ) (
193206145Sjilles	echo "## Clean and create world directory (${NANO_WORLDDIR})"
194206145Sjilles	if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
195206145Sjilles		true
1961556Srgrimes	else
1971556Srgrimes		chflags -R noschg ${NANO_WORLDDIR}/
1981556Srgrimes		rm -rf ${NANO_WORLDDIR}/
1991556Srgrimes	fi
2001556Srgrimes	mkdir -p ${NANO_WORLDDIR}/
2011556Srgrimes)
20290111Simp
20317987Spetermake_conf_install ( ) (
2041556Srgrimes	echo "## Construct install make.conf ($NANO_MAKE_CONF)"
2051556Srgrimes
206206145Sjilles	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
207206145Sjilles	echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
208206145Sjilles)
209206145Sjilles
210208656Sjillesinstall_world ( ) (
211206145Sjilles	echo "## installworld"
21260593Scracauer	echo "### log: ${MAKEOBJDIRPREFIX}/_.iw"
213254426Sjilles
2141556Srgrimes	cd ${NANO_SRC}
2151556Srgrimes	env TARGET_ARCH=${NANO_ARCH} \
2161556Srgrimes	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
2171556Srgrimes		DESTDIR=${NANO_WORLDDIR} \
2181556Srgrimes		> ${MAKEOBJDIRPREFIX}/_.iw 2>&1
2191556Srgrimes	chflags -R noschg ${NANO_WORLDDIR}
2201556Srgrimes)
2211556Srgrimes
2221556Srgrimesinstall_etc ( ) (
2231556Srgrimes
2241556Srgrimes	echo "## install /etc"
2251556Srgrimes	echo "### log: ${MAKEOBJDIRPREFIX}/_.etc"
226214531Sjilles
2271556Srgrimes	cd ${NANO_SRC}
2281556Srgrimes	env TARGET_ARCH=${NANO_ARCH} \
2291556Srgrimes	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
230213811Sobrien		DESTDIR=${NANO_WORLDDIR} \
231214525Sjilles		> ${MAKEOBJDIRPREFIX}/_.etc 2>&1
23217987Speter)
233214599Sjilles
23417987Speterinstall_kernel ( ) (
2351556Srgrimes	echo "## install kernel"
236214709Sjilles	echo "### log: ${MAKEOBJDIRPREFIX}/_.ik"
237214531Sjilles
2381556Srgrimes	cd ${NANO_SRC}
239214599Sjilles	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
2401556Srgrimes		DESTDIR=${NANO_WORLDDIR} \
24117987Speter		__MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
24217987Speter		> ${MAKEOBJDIRPREFIX}/_.ik 2>&1
24317987Speter)
244245382Sjilles
245223282Sjillesrun_customize() (
246245382Sjilles
24717987Speter	echo "## run customize scripts"
24817987Speter	for c in $NANO_CUSTOMIZE
24917987Speter	do
25017987Speter		echo "## customize \"$c\""
25117987Speter		echo "### log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
25217987Speter		echo "### `type $c`"
25317987Speter		( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
25417987Speter	done
25517987Speter)
256214599Sjilles
257214599Sjillesrun_late_customize() (
258214599Sjilles
259214599Sjilles	echo "## run late customize scripts"
260214599Sjilles	for c in $NANO_LATE_CUSTOMIZE
261214599Sjilles	do
262214599Sjilles		echo "## late customize \"$c\""
263214599Sjilles		echo "### log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
26417987Speter		echo "### `type $c`"
26517987Speter		( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
26617987Speter	done
26717987Speter)
268214599Sjilles
26917987Spetersetup_nanobsd ( ) (
270214599Sjilles	echo "## configure nanobsd setup"
27117987Speter	echo "### log: ${MAKEOBJDIRPREFIX}/_.dl"
27217987Speter
27317987Speter	(
27413882Sjoerg	cd ${NANO_WORLDDIR}
27517987Speter
27617987Speter	# Move /usr/local/etc to /etc/local so that the /cfg stuff
277102410Scharnier	# can stomp on it.  Otherwise packages like ipsec-tools which
2781556Srgrimes	# have hardcoded paths under ${prefix}/etc are not tweakable.
27917987Speter	if [ -d usr/local/etc ] ; then
28017987Speter		(
28117987Speter		mkdir etc/local
282214599Sjilles		cd usr/local/etc
283210488Sjilles		find . -print | cpio -dumpl ../../../etc/local
284210488Sjilles		cd ..
285214599Sjilles		rm -rf etc
28617987Speter		ln -s ../../etc/local etc
28717987Speter		)
28817987Speter	fi
289214709Sjilles
290245381Sjilles	for d in var etc
291245381Sjilles	do
292214599Sjilles		# link /$d under /conf
2931556Srgrimes		# we use hard links so we have them both places.
2941556Srgrimes		# the files in /$d will be hidden by the mount.
2951556Srgrimes		# XXX: configure /$d ramdisk size
2961556Srgrimes		mkdir -p conf/base/$d conf/default/$d
2971556Srgrimes		find $d -print | cpio -dumpl conf/base/
2981556Srgrimes	done
299214599Sjilles
3001556Srgrimes	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
301214525Sjilles	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
3021556Srgrimes
3031556Srgrimes	# pick up config files from the special partition
304214599Sjilles	echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
3051556Srgrimes
3061556Srgrimes	# Put /tmp on the /var ramdisk (could be symlink already)
3071556Srgrimes	rmdir tmp || true
3081556Srgrimes	rm tmp || true
3091556Srgrimes	ln -s var/tmp tmp
3101556Srgrimes
311213811Sobrien	) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
31290111Simp)
31390111Simp
3141556Srgrimessetup_nanobsd_etc ( ) (
3151556Srgrimes	echo "## configure nanobsd /etc"
3161556Srgrimes
3171556Srgrimes	(
3181556Srgrimes	cd ${NANO_WORLDDIR}
3191556Srgrimes
3201556Srgrimes	# create diskless marker file
3211556Srgrimes	touch etc/diskless
3221556Srgrimes
3231556Srgrimes	# Make root filesystem R/O by default
3241556Srgrimes	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
3251556Srgrimes
3261556Srgrimes	# save config file for scripts
3271556Srgrimes	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
3281556Srgrimes
3291556Srgrimes	echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
3301556Srgrimes	echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
3311556Srgrimes	mkdir -p cfg
3321556Srgrimes	)
3331556Srgrimes)
3341556Srgrimes
3351556Srgrimesprune_usr() (
3361556Srgrimes
3371556Srgrimes	# Remove all empty directories in /usr 
338213811Sobrien	find ${NANO_WORLDDIR}/usr -type d -depth -print |
33990111Simp		while read d
34090111Simp		do
34175336Sbrian			rmdir $d > /dev/null 2>&1 || true 
3421556Srgrimes		done
343214281Sjilles)
3441556Srgrimes
34575336Sbriancreate_i386_diskimage ( ) (
346214709Sjilles	echo "## build diskimage"
3471556Srgrimes	echo "### log: ${MAKEOBJDIRPREFIX}/_.di"
34875336Sbrian
34975336Sbrian	(
35075336Sbrian	echo $NANO_MEDIASIZE $NANO_IMAGES \
3511556Srgrimes		$NANO_SECTS $NANO_HEADS \
3521556Srgrimes		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
3531556Srgrimes	awk '
3541556Srgrimes	{
3551556Srgrimes		printf "# %s\n", $0
3561556Srgrimes
3571556Srgrimes		# size of cylinder in sectors
3581556Srgrimes		cs = $3 * $4
3591556Srgrimes
3601556Srgrimes		# number of full cylinders on media
3611556Srgrimes		cyl = int ($1 / cs)
362214709Sjilles
363214281Sjilles		# output fdisk geometry spec, truncate cyls to 1023
364214281Sjilles		if (cyl <= 1023)
365214281Sjilles			print "g c" cyl " h" $4 " s" $3
366214281Sjilles		else
367214281Sjilles			print "g c" 1023 " h" $4 " s" $3
368214281Sjilles
3691556Srgrimes		if ($7 > 0) { 
3701556Srgrimes			# size of data partition in full cylinders
3711556Srgrimes			dsl = int (($7 + cs - 1) / cs)
3721556Srgrimes		} else {
3731556Srgrimes			dsl = 0;
3741556Srgrimes		}
37575336Sbrian
37675336Sbrian		# size of config partition in full cylinders
37775336Sbrian		csl = int (($6 + cs - 1) / cs)
37875336Sbrian
37975336Sbrian		if ($5 == 0) {
38075336Sbrian			# size of image partition(s) in full cylinders
38175336Sbrian			isl = int ((cyl - dsl - csl) / $2)
3821556Srgrimes		} else {
3831556Srgrimes			isl = int (($5 + cs - 1) / cs)
3841556Srgrimes		}
3851556Srgrimes
386213811Sobrien		# First image partition start at second track
38790111Simp		print "p 1 165 " $3, isl * cs - $3
38890111Simp		c = isl * cs;
3891556Srgrimes
3901556Srgrimes		# Second image partition (if any) also starts offset one 
3911556Srgrimes		# track to keep them identical.
3921556Srgrimes		if ($2 > 1) {
393214281Sjilles			print "p 2 165 " $3 + c, isl * cs - $3
394218325Sjilles			c += isl * cs;
3951556Srgrimes		}
396214709Sjilles
397218325Sjilles		# Config partition starts at cylinder boundary.
39817987Speter		print "p 3 165 " c, csl * cs
39917987Speter		c += csl * cs
4001556Srgrimes
40120425Ssteve		# Data partition (if any) starts at cylinder boundary.
4021556Srgrimes		if ($7 > 0) {
4031556Srgrimes			print "p 4 165 " c, dsl * cs
4041556Srgrimes		} else if ($7 < 0 && $1 > c) {
4051556Srgrimes			print "p 4 165 " c, $1 - c
4061556Srgrimes		} else if ($1 < c) {
4071556Srgrimes			print "Disk space overcommitted by", \
4081556Srgrimes			    c - $1, "sectors" > "/dev/stderr"
4091556Srgrimes			exit 2
4101556Srgrimes		}
4111556Srgrimes
4121556Srgrimes		# Force slice 1 to be marked active. This is necessary
4131556Srgrimes		# for booting the image from a USB device to work.
414214525Sjilles		print "a 1"
415104554Stjr	}
4161556Srgrimes	' > ${MAKEOBJDIRPREFIX}/_.fdisk
4171556Srgrimes
418214525Sjilles	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
4191556Srgrimes	MNT=${MAKEOBJDIRPREFIX}/_.mnt
4201556Srgrimes	mkdir -p ${MNT}
4211556Srgrimes
4221556Srgrimes	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
4231556Srgrimes		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
424214525Sjilles			-y ${NANO_HEADS}`
425104554Stjr	else
4261556Srgrimes		echo "Creating md backing file..."
4271556Srgrimes		dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
428214525Sjilles			count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
4291556Srgrimes		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
4301556Srgrimes			-y ${NANO_HEADS}`
431214525Sjilles	fi
4321556Srgrimes
4331556Srgrimes	trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
4341556Srgrimes
4351556Srgrimes	fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
4361556Srgrimes	fdisk ${MD}
4371556Srgrimes	# XXX: params
438214709Sjilles	# XXX: pick up cached boot* files, they may not be in image anymore.
4391556Srgrimes	boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
4401556Srgrimes	bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
4411556Srgrimes	bsdlabel ${MD}s1
4421556Srgrimes
4431556Srgrimes	# Create first image
4441556Srgrimes	newfs ${NANO_NEWFS} /dev/${MD}s1a
445214525Sjilles	mount /dev/${MD}s1a ${MNT}
446104554Stjr	df -i ${MNT}
4471556Srgrimes	echo "Copying worlddir..."
4481556Srgrimes	( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
4491556Srgrimes	df -i ${MNT}
4501556Srgrimes	echo "Generating mtree..."
451214525Sjilles	( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
4521556Srgrimes	( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
4531556Srgrimes	umount ${MNT}
454214709Sjilles
4551556Srgrimes	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
4561556Srgrimes		# Duplicate to second image (if present)
4571556Srgrimes		echo "Duplicating to second image..."
4581556Srgrimes		dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
4591556Srgrimes		mount /dev/${MD}s2a ${MNT}
4601556Srgrimes		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
4611556Srgrimes		do
4621556Srgrimes			sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
463199282Sjilles		done
464199282Sjilles		umount ${MNT}
465199282Sjilles
4661556Srgrimes	fi
4671556Srgrimes	
4681556Srgrimes	# Create Config slice
4691556Srgrimes	newfs ${NANO_NEWFS} /dev/${MD}s3
4701556Srgrimes	# XXX: fill from where ?
4711556Srgrimes
4721556Srgrimes	# Create Data slice, if any.
4731556Srgrimes	if [ $NANO_DATASIZE -gt 0 ] ; then
4741556Srgrimes		newfs ${NANO_NEWFS} /dev/${MD}s4
4751556Srgrimes		# XXX: fill from where ?
4761556Srgrimes	fi
4771556Srgrimes
4781556Srgrimes	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
4791556Srgrimes		echo "Writing out _.disk.full..."
480149096Sstefanf		dd if=/dev/${MD} of=${IMG} bs=64k
481149096Sstefanf	fi
482149096Sstefanf
4831556Srgrimes	echo "Writing out _.disk.image..."
4841556Srgrimes	dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
485149096Sstefanf	mdconfig -d -u $MD
4861556Srgrimes	) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
4871556Srgrimes)
4881556Srgrimes
4891556Srgrimeslast_orders () (
4901556Srgrimes	# Redefine this function with any last orders you may have
4911556Srgrimes	# after the build completed, for instance to copy the finished
4921556Srgrimes	# image to a more convenient place:
4931556Srgrimes	# cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
4941556Srgrimes)
4951556Srgrimes
496214709Sjilles#######################################################################
4971556Srgrimes#
4981556Srgrimes# Optional convenience functions.
4991556Srgrimes#
5001556Srgrimes#######################################################################
5011556Srgrimes
5021556Srgrimes#######################################################################
503214525Sjilles# Common Flash device geometries
5041556Srgrimes#
5051556Srgrimes
506214709SjillesFlashDevice () {
5071556Srgrimes	if [ -d ${NANO_TOOLS} ] ; then
5081556Srgrimes		. ${NANO_TOOLS}/FlashDevice.sub
5091556Srgrimes	else
5101556Srgrimes		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
5111556Srgrimes	fi
5121556Srgrimes	sub_FlashDevice $1 $2
5131556Srgrimes}
5141556Srgrimes
5151556Srgrimes#######################################################################
5161556Srgrimes# USB device geometries
5171556Srgrimes#
5181556Srgrimes# Usage:
5191556Srgrimes#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
5201556Srgrimes#
5211556Srgrimes# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
522214709Sjilles#
523104202Stjr# Note that the capacity of a flash key is usually advertised in MB or
5241556Srgrimes# GB, *not* MiB/GiB. As such, the precise number of cylinders available
5251556Srgrimes# for C/H/S geometry may vary depending on the actual flash geometry.
5261556Srgrimes#
527104207Stjr# The following generic device layouts are understood:
528104207Stjr#  generic           An alias for generic-hdd.
5291556Srgrimes#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
5301556Srgrimes#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
5311556Srgrimes#
5321556Srgrimes# The generic-hdd device is preferred for flash devices larger than 1GB.
5331556Srgrimes#
534214709Sjilles
535214709SjillesUsbDevice () {
5361556Srgrimes	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
5371556Srgrimes	case $a1 in
5382760Ssef	generic-fdd)
5391556Srgrimes		NANO_HEADS=64
5401556Srgrimes		NANO_SECTS=32
5411556Srgrimes		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
542214709Sjilles		;;
543214525Sjilles	generic|generic-hdd)
5442760Ssef		NANO_HEADS=255
545214709Sjilles		NANO_SECTS=63
5462760Ssef		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
547223186Sjilles		;;
548223186Sjilles	*)
549223186Sjilles		echo "Unknown USB flash device"
550223186Sjilles		exit 2
551223186Sjilles		;;
552214709Sjilles	esac
553223186Sjilles}
5542760Ssef
5551556Srgrimes#######################################################################
556104202Stjr# Setup serial console
5571556Srgrimes
558214709Sjillescust_comconsole () (
5591556Srgrimes	# Enable getty on console
5601556Srgrimes	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
5611556Srgrimes
5621556Srgrimes	# Disable getty on syscons devices
563214525Sjilles	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
5641556Srgrimes
5651556Srgrimes	# Tell loader to use serial console early.
5661556Srgrimes	echo " -h" > ${NANO_WORLDDIR}/boot.config
567214709Sjilles)
568218325Sjilles
5691556Srgrimes#######################################################################
5701556Srgrimes# Allow root login via ssh
571214525Sjilles
5721556Srgrimescust_allow_ssh_root () (
5731556Srgrimes	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
574214709Sjilles	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
5751556Srgrimes)
576254843Sjilles
577210221Sjilles#######################################################################
57817987Speter# Install the stuff under ./Files
579101662Stjr
580101662Stjrcust_install_files () (
581254335Sjilles	cd ${NANO_TOOLS}/Files
582254335Sjilles	find . -print | grep -v /CVS | cpio -dumpv ${NANO_WORLDDIR}
583254335Sjilles)
584254843Sjilles
585254843Sjilles#######################################################################
586254843Sjilles# Install packages from ${NANO_PACKAGE_DIR}
58717987Speter
58817987Spetercust_pkg () (
5891556Srgrimes
5901556Srgrimes	# Copy packages into chroot
59175160Sbrian	mkdir -p ${NANO_WORLDDIR}/Pkg
592214281Sjilles	cp ${NANO_PACKAGE_DIR}/* ${NANO_WORLDDIR}/Pkg
5931556Srgrimes
5941556Srgrimes	# Count & report how many we have to install
5951556Srgrimes	todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
5961556Srgrimes	echo "=== TODO: $todo"
5971556Srgrimes	ls ${NANO_WORLDDIR}/Pkg
5981556Srgrimes	echo "==="
5991556Srgrimes	while true
6001556Srgrimes	do
6011556Srgrimes		# Record how many we have now
6021556Srgrimes		have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
6031556Srgrimes
6041556Srgrimes		# Attempt to install more packages
6051556Srgrimes		# ...but no more than 200 at a time due to pkg_add's internal
606218325Sjilles		# limitations.
6071556Srgrimes		chroot ${NANO_WORLDDIR} sh -c \
6081556Srgrimes			'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
6091556Srgrimes
6101556Srgrimes		# See what that got us
6111556Srgrimes		now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
6121556Srgrimes		echo "=== NOW $now"
6131556Srgrimes		ls ${NANO_WORLDDIR}/var/db/pkg
61475160Sbrian		echo "==="
615214281Sjilles
6161556Srgrimes
6171556Srgrimes		if [ $now -eq $todo ] ; then
6181556Srgrimes			echo "DONE $now packages"
619213811Sobrien			break
62090111Simp		elif [ $now -eq $have ] ; then
62190111Simp			echo "FAILED: Nothing happened on this pass"
6221556Srgrimes			exit 2
6231556Srgrimes		fi
624210087Sjilles	done
625214304Sjilles	rm -rf ${NANO_WORLDDIR}/Pkg
626222165Sjilles)
6271556Srgrimes
6281556Srgrimes#######################################################################
6291556Srgrimes# Convenience function:
6301556Srgrimes# 	Register $1 as customize function.
6311556Srgrimes
6321556Srgrimescustomize_cmd () {
6331556Srgrimes	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
6341556Srgrimes}
6358855Srgrimes
6361556Srgrimes#######################################################################
6371556Srgrimes# Convenience function:
6388855Srgrimes# 	Register $1 as late customize function to run just before
6391556Srgrimes#	image creation.
6401556Srgrimes
6411556Srgrimeslate_customize_cmd () {
642222165Sjilles	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
643222165Sjilles}
6441556Srgrimes
645222165Sjilles#######################################################################
6461556Srgrimes#
6471556Srgrimes# All set up to go...
6481556Srgrimes#
6491556Srgrimes#######################################################################
6501556Srgrimes
6511556Srgrimesusage () {
6521556Srgrimes	(
653222165Sjilles	echo "Usage: $0 [-b/-k/-w] [-c config_file]"
654222165Sjilles	echo "	-b	suppress builds (both kernel and world)"
6551556Srgrimes	echo "	-k	suppress buildkernel"
6561556Srgrimes	echo "	-w	suppress buildworld"
6571556Srgrimes	echo "	-i	suppress disk image build"
6581556Srgrimes	echo "	-c	specify config file"
6591556Srgrimes	) 1>&2
6601556Srgrimes	exit 2
6611556Srgrimes}
6621556Srgrimes
6631556Srgrimes#######################################################################
664179022Sstefanf# Parse arguments
665214291Sjilles
666214291Sjillesdo_kernel=true
667214291Sjillesdo_world=true
668214534Sjillesdo_image=true
669214534Sjilles
670214291Sjillesset +e
671214291Sjillesargs=`getopt bc:hkwi $*`
672214534Sjillesif [ $? -ne 0 ] ; then
673214534Sjilles	usage
674214534Sjilles	exit 2
6751556Srgrimesfi
676214291Sjillesset -e
677214304Sjilles
678214304Sjillesset -- $args
679214304Sjillesfor i
6801556Srgrimesdo
6811556Srgrimes	case "$i" 
682179022Sstefanf	in
683210087Sjilles	-b)
6841556Srgrimes		shift;
6851556Srgrimes		do_world=false
6861556Srgrimes		do_kernel=false
6871556Srgrimes		;;
6881556Srgrimes	-k)
6891556Srgrimes		shift;
6901556Srgrimes		do_kernel=false
6911556Srgrimes		;;
6921556Srgrimes	-c)
6931556Srgrimes		. "$2"
6941556Srgrimes		shift;
695210087Sjilles		shift;
6961556Srgrimes		;;
6971556Srgrimes	-h)
698213811Sobrien		usage
69990111Simp		;;
70090111Simp	-i)
70117987Speter		do_image=false
7021556Srgrimes		;;
70317987Speter	-w)
70417987Speter		shift;
70517987Speter		do_world=false
70617987Speter		;;
70717987Speter	--)
70817987Speter		shift;
70917987Speter		break;
71017987Speter	esac
711213760Sobriendone
712213760Sobrien
71390111Simpif [ $# -gt 0 ] ; then
71417987Speter	echo "$0: Extraneous arguments supplied"
71517987Speter	usage
71617987Speterfi
71717987Speter
71817987Speter#######################################################################
71917987Speter# Setup and Export Internal variables
72017987Speter#
72117987Speterif [ "x${NANO_OBJ}" = "x" ] ; then
72217987Speter	MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
72320425Ssteve	NANO_OBJ=${MAKEOBJDIRPREFIX}
72417987Speterelse
72517987Speter	MAKEOBJDIRPREFIX=${NANO_OBJ}
72617987Speterfi
72717987Speter
72817987Speterif [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
72917987Speter	NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
73017987Speterfi
73117987Speter
732213811SobrienNANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
73390111SimpNANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
73490111Simp
7351556Srgrimesif [ -d ${NANO_TOOLS} ] ; then
7361556Srgrimes	true
7371556Srgrimeselif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
7381556Srgrimes	NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
7391556Srgrimeselse
7401556Srgrimes	echo "NANO_TOOLS directory does not exist" 1>&2
7411556Srgrimes	exit 1
7421556Srgrimesfi
7431556Srgrimes
7441556Srgrimesexport MAKEOBJDIRPREFIX
7451556Srgrimes
7461556Srgrimesexport NANO_ARCH
7471556Srgrimesexport NANO_CODESIZE
7481556Srgrimesexport NANO_CONFSIZE
7491556Srgrimesexport NANO_CUSTOMIZE
7501556Srgrimesexport NANO_DATASIZE
7511556Srgrimesexport NANO_DRIVE
7521556Srgrimesexport NANO_HEADS
7531556Srgrimesexport NANO_IMAGES
7541556Srgrimesexport NANO_IMGNAME
7551556Srgrimesexport NANO_MAKE_CONF
7561556Srgrimesexport NANO_MEDIASIZE
7571556Srgrimesexport NANO_NAME
7581556Srgrimesexport NANO_NEWFS
7591556Srgrimesexport NANO_OBJ
7601556Srgrimesexport NANO_PMAKE
7611556Srgrimesexport NANO_SECTS
7621556Srgrimesexport NANO_SRC
76317987Speterexport NANO_TOOLS
7641556Srgrimesexport NANO_WORLDDIR
76517987Speterexport NANO_BOOT0CFG
7661556Srgrimesexport NANO_BOOTLOADER
7671556Srgrimes
7681556Srgrimes#######################################################################
7691556Srgrimes# And then it is as simple as that...
7701556Srgrimes
7711556Srgrimesif $do_world ; then
7721556Srgrimes	clean_build
7731556Srgrimes	make_conf_build
774213811Sobrien	build_world
77590111Simpelse
77690111Simp	echo "## Skipping buildworld (as instructed)"
7771556Srgrimesfi
7781556Srgrimes
7791556Srgrimesif $do_kernel ; then
7801556Srgrimes	build_kernel
7811556Srgrimeselse
7821556Srgrimes	echo "## Skipping buildkernel (as instructed)"
7831556Srgrimesfi
7841556Srgrimes
7851556Srgrimesclean_world
7861556Srgrimesmake_conf_install
7871556Srgrimesinstall_world
7881556Srgrimesinstall_etc
7891556Srgrimessetup_nanobsd_etc
7901556Srgrimesinstall_kernel
7911556Srgrimes
7921556Srgrimesrun_customize
7931556Srgrimessetup_nanobsd
7941556Srgrimesprune_usr
7951556Srgrimesrun_late_customize
7961556Srgrimesif $do_image ; then
7971556Srgrimes	create_${NANO_ARCH}_diskimage
798213811Sobrien	echo "# Created NanoBSD disk image: ${MAKEOBJDIRPREFIX}/${NANO_IMGNAME}"
79990111Simpelse
80090111Simp	echo "## Skipping image build (as instructed)"
8011556Srgrimesfi
8021556Srgrimeslast_orders
8031556Srgrimes
8041556Srgrimesecho "# NanoBSD image ${NANO_NAME} completed"
8051556Srgrimes