nanobsd.sh revision 197542
1148422Sphk#!/bin/sh
2150366Sphk#
3148422Sphk# Copyright (c) 2005 Poul-Henning Kamp.
4150366Sphk# All rights reserved.
5148422Sphk#
6150366Sphk# Redistribution and use in source and binary forms, with or without
7150366Sphk# modification, are permitted provided that the following conditions
8150366Sphk# are met:
9150366Sphk# 1. Redistributions of source code must retain the above copyright
10150366Sphk#    notice, this list of conditions and the following disclaimer.
11150366Sphk# 2. Redistributions in binary form must reproduce the above copyright
12150366Sphk#    notice, this list of conditions and the following disclaimer in the
13150366Sphk#    documentation and/or other materials provided with the distribution.
14148422Sphk#
15150366Sphk# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16150366Sphk# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17150366Sphk# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18150366Sphk# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19150366Sphk# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20150366Sphk# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21150366Sphk# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22150366Sphk# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23150366Sphk# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24150366Sphk# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25150366Sphk# SUCH DAMAGE.
26150366Sphk#
27148422Sphk# $FreeBSD: head/tools/tools/nanobsd/nanobsd.sh 197542 2009-09-27 21:01:07Z simon $
28148422Sphk#
29148422Sphk
30148422Sphkset -e
31148422Sphk
32150366Sphk#######################################################################
33150366Sphk#
34150366Sphk# Setup default values for all controlling variables.
35150366Sphk# These values can be overridden from the config file(s)
36150366Sphk#
37150366Sphk#######################################################################
38150366Sphk
39148422Sphk# Name of this NanoBSD build.  (Used to construct workdir names)
40148422SphkNANO_NAME=full
41148422Sphk
42148422Sphk# Source tree directory
43148422SphkNANO_SRC=/usr/src
44148422Sphk
45150366Sphk# Where nanobsd additional files live under the source tree
46150366SphkNANO_TOOLS=tools/tools/nanobsd
47150366Sphk
48170607Sphk# Where cust_pkg() finds packages to install
49170607SphkNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
50186181SphkNANO_PACKAGE_LIST="*"
51170607Sphk
52148422Sphk# Object tree directory
53148422Sphk# default is subdir of /usr/obj
54148648Sphk#NANO_OBJ=""
55148422Sphk
56175258Ssimon# The directory to put the final images
57175258Ssimon# default is ${NANO_OBJ}
58175258Ssimon#NANO_DISKIMGDIR=""
59175258Ssimon
60148422Sphk# Parallel Make
61148422SphkNANO_PMAKE="make -j 3"
62148422Sphk
63183284Sbms# The default name for any image we create.
64183284SbmsNANO_IMGNAME="_.disk.full"
65183284Sbms
66148422Sphk# Options to put in make.conf during buildworld only
67148422SphkCONF_BUILD=' '
68148422Sphk
69148422Sphk# Options to put in make.conf during installworld only
70148422SphkCONF_INSTALL=' '
71148422Sphk
72148422Sphk# Options to put in make.conf during both build- & installworld.
73148422SphkCONF_WORLD=' '
74148422Sphk
75148422Sphk# Kernel config file to use
76148422SphkNANO_KERNEL=GENERIC
77148422Sphk
78149073Sphk# Customize commands.
79149073SphkNANO_CUSTOMIZE=""
80149073Sphk
81181461Sphk# Late customize commands.
82181461SphkNANO_LATE_CUSTOMIZE=""
83181461Sphk
84148648Sphk# Newfs paramters to use
85148648SphkNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
86148648Sphk
87149082Sphk# The drive name of the media at runtime
88149082SphkNANO_DRIVE=ad0
89149082Sphk
90148648Sphk# Target media size in 512 bytes sectors
91183284SbmsNANO_MEDIASIZE=1200000
92148648Sphk
93148648Sphk# Number of code images on media (1 or 2)
94148422SphkNANO_IMAGES=2
95148648Sphk
96171986Sphk# 0 -> Leave second image all zeroes so it compresses better.
97171986Sphk# 1 -> Initialize second image with a copy of the first
98171986SphkNANO_INIT_IMG2=1
99171986Sphk
100149535Ssimon# Size of code file system in 512 bytes sectors
101149120Sphk# If zero, size will be as large as possible.
102149120SphkNANO_CODESIZE=0
103149120Sphk
104149120Sphk# Size of configuration file system in 512 bytes sectors
105149120Sphk# Cannot be zero.
106148422SphkNANO_CONFSIZE=2048
107148648Sphk
108148648Sphk# Size of data file system in 512 bytes sectors
109149120Sphk# If zero: no partition configured.
110149120Sphk# If negative: max size possible
111149120SphkNANO_DATASIZE=0
112148422Sphk
113154466Sphk# Size of the /etc ramdisk in 512 bytes sectors
114154466SphkNANO_RAM_ETCSIZE=10240
115154466Sphk
116154466Sphk# Size of the /tmp+/var ramdisk in 512 bytes sectors
117154466SphkNANO_RAM_TMPVARSIZE=10240
118154466Sphk
119148422Sphk# Media geometry, only relevant if bios doesn't understand LBA.
120170607SphkNANO_SECTS=63
121148422SphkNANO_HEADS=16
122148422Sphk
123168894Sadrian# boot0 flags/options and configuration
124168894SadrianNANO_BOOT0CFG="-o packet -s 1 -m 3"
125168894SadrianNANO_BOOTLOADER="boot/boot0sio"
126168894Sadrian
127175258Ssimon# Backing type of md(4) device
128175258Ssimon# Can be "file" or "swap"
129175258SsimonNANO_MD_BACKING="file"
130175258Ssimon
131183314Ssimon# Progress Print level
132183314SsimonPPLEVEL=3
133183314Ssimon
134148422Sphk#######################################################################
135148648Sphk# Not a variable at this time
136148648Sphk
137148648SphkNANO_ARCH=i386
138148648Sphk
139148648Sphk#######################################################################
140150366Sphk#
141150366Sphk# The functions which do the real work.
142150366Sphk# Can be overridden from the config file(s)
143150366Sphk#
144150366Sphk#######################################################################
145148422Sphk
146149120Sphkclean_build ( ) (
147183314Ssimon	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
148148422Sphk
149194431Sn_hibma	if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
150148422Sphk		chflags -R noschg ${MAKEOBJDIRPREFIX}
151194431Sn_hibma		rm -r ${MAKEOBJDIRPREFIX}
152148422Sphk	fi
153148422Sphk	mkdir -p ${MAKEOBJDIRPREFIX}
154150343Sphk	printenv > ${MAKEOBJDIRPREFIX}/_.env
155148422Sphk)
156148422Sphk
157148422Sphkmake_conf_build ( ) (
158194431Sn_hibma	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
159148422Sphk
160194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
161194431Sn_hibma	echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
162148422Sphk)
163148422Sphk
164148422Sphkbuild_world ( ) (
165183314Ssimon	pprint 2 "run buildworld"
166183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
167148422Sphk
168148422Sphk	cd ${NANO_SRC}
169183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
170194431Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
171148422Sphk		> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
172148422Sphk)
173148422Sphk
174148422Sphkbuild_kernel ( ) (
175183314Ssimon	pprint 2 "build kernel ($NANO_KERNEL)"
176183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
177148422Sphk
178148422Sphk	if [ -f ${NANO_KERNEL} ] ; then
179148422Sphk		cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
180148422Sphk	fi
181148422Sphk
182181083Ssam	(cd ${NANO_SRC};
183181083Ssam	# unset these just in case to avoid compiler complaints
184181083Ssam	# when cross-building
185181083Ssam	unset TARGET_CPUTYPE
186181083Ssam	unset TARGET_BIG_ENDIAN
187183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
188194431Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \
189194440Sn_hibma		> ${MAKEOBJDIRPREFIX}/_.bk 2>&1
190181083Ssam	)
191148422Sphk)
192148422Sphk
193148422Sphkclean_world ( ) (
194194431Sn_hibma	if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
195194431Sn_hibma		pprint 2 "Clean and create object directory (${NANO_OBJ})"
196194431Sn_hibma		if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then
197194431Sn_hibma			chflags -R noschg ${NANO_OBJ}
198194431Sn_hibma			rm -r ${NANO_OBJ}
199194431Sn_hibma		fi
200194431Sn_hibma		mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
201194431Sn_hibma		printenv > ${NANO_OBJ}/_.env
202148422Sphk	else
203194431Sn_hibma		pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
204194431Sn_hibma		if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
205194431Sn_hibma			chflags -R noschg ${NANO_WORLDDIR}
206194431Sn_hibma			rm -rf ${NANO_WORLDDIR}
207194431Sn_hibma		fi
208194431Sn_hibma		mkdir -p ${NANO_WORLDDIR}
209148422Sphk	fi
210148422Sphk)
211148422Sphk
212148422Sphkmake_conf_install ( ) (
213194431Sn_hibma	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
214148422Sphk
215194431Sn_hibma	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
216194431Sn_hibma	echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
217148422Sphk)
218148422Sphk
219148422Sphkinstall_world ( ) (
220183314Ssimon	pprint 2 "installworld"
221194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.iw"
222148422Sphk
223148422Sphk	cd ${NANO_SRC}
224183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
225194431Sn_hibma	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
226148422Sphk		DESTDIR=${NANO_WORLDDIR} \
227194431Sn_hibma		> ${NANO_OBJ}/_.iw 2>&1
228148422Sphk	chflags -R noschg ${NANO_WORLDDIR}
229148422Sphk)
230148422Sphk
231148422Sphkinstall_etc ( ) (
232148422Sphk
233183314Ssimon	pprint 2 "install /etc"
234194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.etc"
235148422Sphk
236160783Sthomas	cd ${NANO_SRC}
237183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
238194431Sn_hibma	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
239148422Sphk		DESTDIR=${NANO_WORLDDIR} \
240194431Sn_hibma		> ${NANO_OBJ}/_.etc 2>&1
241148422Sphk)
242148422Sphk
243148422Sphkinstall_kernel ( ) (
244183314Ssimon	pprint 2 "install kernel"
245194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.ik"
246148422Sphk
247148422Sphk	cd ${NANO_SRC}
248183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
249148422Sphk		DESTDIR=${NANO_WORLDDIR} \
250194431Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \
251194431Sn_hibma		> ${NANO_OBJ}/_.ik 2>&1
252148422Sphk)
253148422Sphk
254150366Sphkrun_customize() (
255150366Sphk
256183314Ssimon	pprint 2 "run customize scripts"
257150366Sphk	for c in $NANO_CUSTOMIZE
258150366Sphk	do
259183314Ssimon		pprint 2 "customize \"$c\""
260194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
261183314Ssimon		pprint 4 "`type $c`"
262194431Sn_hibma		( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
263150366Sphk	done
264150366Sphk)
265150366Sphk
266181461Sphkrun_late_customize() (
267181461Sphk
268183314Ssimon	pprint 2 "run late customize scripts"
269181461Sphk	for c in $NANO_LATE_CUSTOMIZE
270181461Sphk	do
271183314Ssimon		pprint 2 "late customize \"$c\""
272194431Sn_hibma		pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
273183314Ssimon		pprint 4 "`type $c`"
274194431Sn_hibma		( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
275181461Sphk	done
276181461Sphk)
277181461Sphk
278150366Sphksetup_nanobsd ( ) (
279183314Ssimon	pprint 2 "configure nanobsd setup"
280194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.dl"
281148422Sphk
282149103Sphk	(
283148422Sphk	cd ${NANO_WORLDDIR}
284148422Sphk
285170607Sphk	# Move /usr/local/etc to /etc/local so that the /cfg stuff
286170607Sphk	# can stomp on it.  Otherwise packages like ipsec-tools which
287170607Sphk	# have hardcoded paths under ${prefix}/etc are not tweakable.
288170607Sphk	if [ -d usr/local/etc ] ; then
289170607Sphk		(
290183727Sn_hibma		mkdir -p etc/local
291170607Sphk		cd usr/local/etc
292170607Sphk		find . -print | cpio -dumpl ../../../etc/local
293170607Sphk		cd ..
294170607Sphk		rm -rf etc
295170607Sphk		ln -s ../../etc/local etc
296170607Sphk		)
297170607Sphk	fi
298170607Sphk
299149082Sphk	for d in var etc
300149082Sphk	do
301149082Sphk		# link /$d under /conf
302149082Sphk		# we use hard links so we have them both places.
303149082Sphk		# the files in /$d will be hidden by the mount.
304149082Sphk		# XXX: configure /$d ramdisk size
305149082Sphk		mkdir -p conf/base/$d conf/default/$d
306149082Sphk		find $d -print | cpio -dumpl conf/base/
307149082Sphk	done
308148422Sphk
309154466Sphk	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
310154466Sphk	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
311154466Sphk
312148422Sphk	# pick up config files from the special partition
313149082Sphk	echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
314148422Sphk
315149082Sphk	# Put /tmp on the /var ramdisk (could be symlink already)
316149082Sphk	rmdir tmp || true
317149082Sphk	rm tmp || true
318148422Sphk	ln -s var/tmp tmp
319149082Sphk
320194431Sn_hibma	) > ${NANO_OBJ}/_.dl 2>&1
321148422Sphk)
322148422Sphk
323161341Ssimonsetup_nanobsd_etc ( ) (
324183314Ssimon	pprint 2 "configure nanobsd /etc"
325161341Ssimon
326161341Ssimon	(
327161341Ssimon	cd ${NANO_WORLDDIR}
328161341Ssimon
329161341Ssimon	# create diskless marker file
330161341Ssimon	touch etc/diskless
331161341Ssimon
332170324Sphk	# Make root filesystem R/O by default
333170324Sphk	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
334170324Sphk
335161341Ssimon	# save config file for scripts
336161341Ssimon	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
337161341Ssimon
338161341Ssimon	echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
339161341Ssimon	echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
340161341Ssimon	mkdir -p cfg
341161341Ssimon	)
342161341Ssimon)
343161341Ssimon
344149014Sphkprune_usr() (
345149014Sphk
346149014Sphk	# Remove all empty directories in /usr 
347149014Sphk	find ${NANO_WORLDDIR}/usr -type d -depth -print |
348149014Sphk		while read d
349149014Sphk		do
350149014Sphk			rmdir $d > /dev/null 2>&1 || true 
351149014Sphk		done
352149014Sphk)
353149014Sphk
354148422Sphkcreate_i386_diskimage ( ) (
355183314Ssimon	pprint 2 "build diskimage"
356194431Sn_hibma	pprint 3 "log: ${NANO_OBJ}/_.di"
357148422Sphk
358149103Sphk	(
359149120Sphk	echo $NANO_MEDIASIZE $NANO_IMAGES \
360149120Sphk		$NANO_SECTS $NANO_HEADS \
361149120Sphk		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
362148422Sphk	awk '
363150717Sphk	{
364149120Sphk		printf "# %s\n", $0
365149120Sphk
366148422Sphk		# size of cylinder in sectors
367149120Sphk		cs = $3 * $4
368148422Sphk
369148422Sphk		# number of full cylinders on media
370148422Sphk		cyl = int ($1 / cs)
371148422Sphk
372149120Sphk		# output fdisk geometry spec, truncate cyls to 1023
373148422Sphk		if (cyl <= 1023)
374149120Sphk			print "g c" cyl " h" $4 " s" $3
375148422Sphk		else
376149120Sphk			print "g c" 1023 " h" $4 " s" $3
377148422Sphk
378149120Sphk		if ($7 > 0) { 
379149120Sphk			# size of data partition in full cylinders
380149120Sphk			dsl = int (($7 + cs - 1) / cs)
381149120Sphk		} else {
382149120Sphk			dsl = 0;
383149120Sphk		}
384148422Sphk
385148422Sphk		# size of config partition in full cylinders
386149120Sphk		csl = int (($6 + cs - 1) / cs)
387148422Sphk
388150717Sphk		if ($5 == 0) {
389149120Sphk			# size of image partition(s) in full cylinders
390149120Sphk			isl = int ((cyl - dsl - csl) / $2)
391149120Sphk		} else {
392149120Sphk			isl = int (($5 + cs - 1) / cs)
393149120Sphk		}
394148422Sphk
395148422Sphk		# First image partition start at second track
396149120Sphk		print "p 1 165 " $3, isl * cs - $3
397148422Sphk		c = isl * cs;
398148422Sphk
399148422Sphk		# Second image partition (if any) also starts offset one 
400148422Sphk		# track to keep them identical.
401148422Sphk		if ($2 > 1) {
402149120Sphk			print "p 2 165 " $3 + c, isl * cs - $3
403148422Sphk			c += isl * cs;
404148422Sphk		}
405148422Sphk
406148422Sphk		# Config partition starts at cylinder boundary.
407148422Sphk		print "p 3 165 " c, csl * cs
408148422Sphk		c += csl * cs
409148422Sphk
410148422Sphk		# Data partition (if any) starts at cylinder boundary.
411149120Sphk		if ($7 > 0) {
412148422Sphk			print "p 4 165 " c, dsl * cs
413174936Ssimon		} else if ($7 < 0 && $1 > c) {
414174936Ssimon			print "p 4 165 " c, $1 - c
415150717Sphk		} else if ($1 < c) {
416150717Sphk			print "Disk space overcommitted by", \
417150717Sphk			    c - $1, "sectors" > "/dev/stderr"
418150717Sphk			exit 2
419148422Sphk		}
420183279Sbms
421183279Sbms		# Force slice 1 to be marked active. This is necessary
422183279Sbms		# for booting the image from a USB device to work.
423183279Sbms		print "a 1"
424150717Sphk	}
425194431Sn_hibma	' > ${NANO_OBJ}/_.fdisk
426148422Sphk
427183284Sbms	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
428194431Sn_hibma	MNT=${NANO_OBJ}/_.mnt
429148422Sphk	mkdir -p ${MNT}
430148422Sphk
431175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
432175258Ssimon		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
433175258Ssimon			-y ${NANO_HEADS}`
434175258Ssimon	else
435175258Ssimon		echo "Creating md backing file..."
436175258Ssimon		dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
437194437Sn_hibma			count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
438175258Ssimon		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
439175258Ssimon			-y ${NANO_HEADS}`
440175258Ssimon	fi
441148422Sphk
442197542Ssimon	trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
443148422Sphk
444194431Sn_hibma	fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
445149120Sphk	fdisk ${MD}
446148422Sphk	# XXX: params
447148422Sphk	# XXX: pick up cached boot* files, they may not be in image anymore.
448168894Sadrian	boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
449160388Sthomas	bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
450149120Sphk	bsdlabel ${MD}s1
451148422Sphk
452148422Sphk	# Create first image
453148648Sphk	newfs ${NANO_NEWFS} /dev/${MD}s1a
454194437Sn_hibma	mount /dev/${MD}s1a ${MNT}
455149014Sphk	df -i ${MNT}
456175258Ssimon	echo "Copying worlddir..."
457148422Sphk	( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
458148422Sphk	df -i ${MNT}
459175258Ssimon	echo "Generating mtree..."
460194431Sn_hibma	( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
461194431Sn_hibma	( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
462148422Sphk	umount ${MNT}
463148422Sphk
464171986Sphk	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
465148422Sphk		# Duplicate to second image (if present)
466175258Ssimon		echo "Duplicating to second image..."
467194437Sn_hibma		dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
468170322Sphk		mount /dev/${MD}s2a ${MNT}
469170322Sphk		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
470170322Sphk		do
471170322Sphk			sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
472170322Sphk		done
473170322Sphk		umount ${MNT}
474148422Sphk	fi
475148422Sphk	
476148422Sphk	# Create Config slice
477148648Sphk	newfs ${NANO_NEWFS} /dev/${MD}s3
478148422Sphk	# XXX: fill from where ?
479148422Sphk
480148422Sphk	# Create Data slice, if any.
481194437Sn_hibma	if [ $NANO_DATASIZE -ne 0 ] ; then
482148648Sphk		newfs ${NANO_NEWFS} /dev/${MD}s4
483148422Sphk		# XXX: fill from where ?
484148422Sphk	fi
485148422Sphk
486175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
487194440Sn_hibma		echo "Writing out ${NANO_IMGNAME}..."
488175258Ssimon		dd if=/dev/${MD} of=${IMG} bs=64k
489175258Ssimon	fi
490175258Ssimon
491175258Ssimon	echo "Writing out _.disk.image..."
492194437Sn_hibma	dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
493148422Sphk	mdconfig -d -u $MD
494197542Ssimon
495197542Ssimon	trap - 1 2 15 EXIT
496197542Ssimon
497194431Sn_hibma	) > ${NANO_OBJ}/_.di 2>&1
498148422Sphk)
499148422Sphk
500154466Sphklast_orders () (
501154466Sphk	# Redefine this function with any last orders you may have
502154466Sphk	# after the build completed, for instance to copy the finished
503154466Sphk	# image to a more convenient place:
504194431Sn_hibma	# cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
505154466Sphk)
506154466Sphk
507148422Sphk#######################################################################
508148422Sphk#
509150366Sphk# Optional convenience functions.
510148422Sphk#
511148422Sphk#######################################################################
512148422Sphk
513149014Sphk#######################################################################
514150367Sphk# Common Flash device geometries
515150367Sphk#
516150367Sphk
517150367SphkFlashDevice () {
518150717Sphk	if [ -d ${NANO_TOOLS} ] ; then
519150717Sphk		. ${NANO_TOOLS}/FlashDevice.sub
520150717Sphk	else
521150717Sphk		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
522150717Sphk	fi
523150367Sphk	sub_FlashDevice $1 $2
524150367Sphk}
525150367Sphk
526183284Sbms#######################################################################
527183284Sbms# USB device geometries
528183284Sbms#
529183284Sbms# Usage:
530183284Sbms#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
531183284Sbms#
532183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
533183284Sbms#
534183284Sbms# Note that the capacity of a flash key is usually advertised in MB or
535183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available
536183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry.
537183284Sbms#
538183284Sbms# The following generic device layouts are understood:
539183284Sbms#  generic           An alias for generic-hdd.
540183284Sbms#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
541183284Sbms#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
542183284Sbms#
543183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB.
544183284Sbms#
545150367Sphk
546183284SbmsUsbDevice () {
547183284Sbms	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
548183284Sbms	case $a1 in
549183284Sbms	generic-fdd)
550183284Sbms		NANO_HEADS=64
551183284Sbms		NANO_SECTS=32
552183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
553183284Sbms		;;
554183284Sbms	generic|generic-hdd)
555183284Sbms		NANO_HEADS=255
556183284Sbms		NANO_SECTS=63
557183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
558183284Sbms		;;
559183284Sbms	*)
560183284Sbms		echo "Unknown USB flash device"
561183284Sbms		exit 2
562183284Sbms		;;
563183284Sbms	esac
564183284Sbms}
565183284Sbms
566150367Sphk#######################################################################
567150366Sphk# Setup serial console
568150343Sphk
569150366Sphkcust_comconsole () (
570150366Sphk	# Enable getty on console
571181751Sphk	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
572150366Sphk
573150366Sphk	# Disable getty on syscons devices
574150366Sphk	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
575150366Sphk
576150366Sphk	# Tell loader to use serial console early.
577150366Sphk	echo " -h" > ${NANO_WORLDDIR}/boot.config
578150366Sphk)
579150366Sphk
580150366Sphk#######################################################################
581150366Sphk# Allow root login via ssh
582150366Sphk
583150366Sphkcust_allow_ssh_root () (
584150366Sphk	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
585150366Sphk	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
586150366Sphk)
587150366Sphk
588150366Sphk#######################################################################
589150366Sphk# Install the stuff under ./Files
590150366Sphk
591150366Sphkcust_install_files () (
592150511Ssimon	cd ${NANO_TOOLS}/Files
593183315Ssimon	find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
594150366Sphk)
595150366Sphk
596150366Sphk#######################################################################
597170607Sphk# Install packages from ${NANO_PACKAGE_DIR}
598170607Sphk
599170607Sphkcust_pkg () (
600170607Sphk
601170607Sphk	# Copy packages into chroot
602170607Sphk	mkdir -p ${NANO_WORLDDIR}/Pkg
603186181Sphk	(
604186181Sphk		cd ${NANO_PACKAGE_DIR}
605186181Sphk		find ${NANO_PACKAGE_LIST} -print |
606186181Sphk		    cpio -dumpv ${NANO_WORLDDIR}/Pkg
607186181Sphk	)
608170607Sphk
609170607Sphk	# Count & report how many we have to install
610170607Sphk	todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
611170607Sphk	echo "=== TODO: $todo"
612170607Sphk	ls ${NANO_WORLDDIR}/Pkg
613170607Sphk	echo "==="
614170607Sphk	while true
615170607Sphk	do
616182690Sthomas		# Record how many we have now
617170607Sphk		have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
618170607Sphk
619171986Sphk		# Attempt to install more packages
620171986Sphk		# ...but no more than 200 at a time due to pkg_add's internal
621171986Sphk		# limitations.
622171986Sphk		chroot ${NANO_WORLDDIR} sh -c \
623171986Sphk			'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
624170607Sphk
625170607Sphk		# See what that got us
626170607Sphk		now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
627170607Sphk		echo "=== NOW $now"
628170607Sphk		ls ${NANO_WORLDDIR}/var/db/pkg
629170607Sphk		echo "==="
630170607Sphk
631170607Sphk
632170607Sphk		if [ $now -eq $todo ] ; then
633170607Sphk			echo "DONE $now packages"
634170607Sphk			break
635170607Sphk		elif [ $now -eq $have ] ; then
636170607Sphk			echo "FAILED: Nothing happened on this pass"
637170607Sphk			exit 2
638170607Sphk		fi
639170607Sphk	done
640170607Sphk	rm -rf ${NANO_WORLDDIR}/Pkg
641170607Sphk)
642170607Sphk
643170607Sphk#######################################################################
644150366Sphk# Convenience function:
645150366Sphk# 	Register $1 as customize function.
646150366Sphk
647150343Sphkcustomize_cmd () {
648150343Sphk	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
649150343Sphk}
650150343Sphk
651150343Sphk#######################################################################
652181461Sphk# Convenience function:
653181461Sphk# 	Register $1 as late customize function to run just before
654181461Sphk#	image creation.
655181461Sphk
656181461Sphklate_customize_cmd () {
657181461Sphk	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
658181461Sphk}
659181461Sphk
660181461Sphk#######################################################################
661150366Sphk#
662150366Sphk# All set up to go...
663150366Sphk#
664150366Sphk#######################################################################
665150366Sphk
666183314Ssimon# Progress Print
667183314Ssimon#	Print $2 at level $1.
668183314Ssimonpprint() {
669183314Ssimon    if [ "$1" -le $PPLEVEL ]; then
670194958Sn_hibma	runtime=$(( `date +%s` - $NANO_STARTTIME ))
671194958Sn_hibma	printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
672183314Ssimon    fi
673183314Ssimon}
674183314Ssimon
675156496Sphkusage () {
676156496Sphk	(
677183314Ssimon	echo "Usage: $0 [-bikqvw] [-c config_file]"
678156496Sphk	echo "	-b	suppress builds (both kernel and world)"
679183314Ssimon	echo "	-i	suppress disk image build"
680156496Sphk	echo "	-k	suppress buildkernel"
681184048Ssam	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
682183314Ssimon	echo "	-q	make output more quite"
683183314Ssimon	echo "	-v	make output more verbose"
684156496Sphk	echo "	-w	suppress buildworld"
685156496Sphk	echo "	-c	specify config file"
686156496Sphk	) 1>&2
687156496Sphk	exit 2
688156496Sphk}
689156496Sphk
690150366Sphk#######################################################################
691149014Sphk# Parse arguments
692148422Sphk
693184048Ssamdo_clean=true
694156496Sphkdo_kernel=true
695156496Sphkdo_world=true
696181460Sphkdo_image=true
697150366Sphk
698156496Sphkset +e
699184048Ssamargs=`getopt bc:hiknqvw $*`
700149014Sphkif [ $? -ne 0 ] ; then
701156496Sphk	usage
702149014Sphk	exit 2
703149014Sphkfi
704156496Sphkset -e
705149014Sphk
706149014Sphkset -- $args
707149014Sphkfor i
708149014Sphkdo
709149014Sphk	case "$i" 
710149014Sphk	in
711150366Sphk	-b)
712156496Sphk		do_world=false
713156496Sphk		do_kernel=false
714183314Ssimon		shift
715150366Sphk		;;
716156496Sphk	-k)
717156496Sphk		do_kernel=false
718183314Ssimon		shift
719156496Sphk		;;
720149014Sphk	-c)
721149014Sphk		. "$2"
722183314Ssimon		shift
723183314Ssimon		shift
724149014Sphk		;;
725150366Sphk	-h)
726156496Sphk		usage
727150366Sphk		;;
728181460Sphk	-i)
729181460Sphk		do_image=false
730183314Ssimon		shift
731181481Ssam		;;
732184048Ssam	-n)
733184048Ssam		do_clean=false
734184048Ssam		shift
735184048Ssam		;;
736183314Ssimon	-q)
737183314Ssimon		PPLEVEL=$(($PPLEVEL - 1))
738183314Ssimon		shift
739183314Ssimon		;;
740183314Ssimon	-v)
741183314Ssimon		PPLEVEL=$(($PPLEVEL + 1))
742183314Ssimon		shift
743183314Ssimon		;;
744156496Sphk	-w)
745156496Sphk		do_world=false
746183314Ssimon		shift
747156496Sphk		;;
748149014Sphk	--)
749183314Ssimon		shift
750183314Ssimon		break
751149014Sphk	esac
752149014Sphkdone
753149014Sphk
754150366Sphkif [ $# -gt 0 ] ; then
755156496Sphk	echo "$0: Extraneous arguments supplied"
756156496Sphk	usage
757150366Sphkfi
758150366Sphk
759148422Sphk#######################################################################
760150366Sphk# Setup and Export Internal variables
761150366Sphk#
762194431Sn_hibmatest -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
763194431Sn_hibmatest -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
764194431Sn_hibmatest -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
765148422Sphk
766194431Sn_hibmaNANO_WORLDDIR=${NANO_OBJ}/_.w
767194431Sn_hibmaNANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
768194431Sn_hibmaNANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
769175258Ssimon
770150511Ssimonif [ -d ${NANO_TOOLS} ] ; then
771150511Ssimon	true
772150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
773150511Ssimon	NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
774150511Ssimonelse
775150511Ssimon	echo "NANO_TOOLS directory does not exist" 1>&2
776150511Ssimon	exit 1
777150511Ssimonfi
778150511Ssimon
779184048Ssamif $do_clean ; then
780184048Ssam	true
781184048Ssamelse
782184048Ssam	NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
783184048Ssamfi
784184048Ssam
785148422Sphkexport MAKEOBJDIRPREFIX
786148422Sphk
787148422Sphkexport NANO_ARCH
788149120Sphkexport NANO_CODESIZE
789148422Sphkexport NANO_CONFSIZE
790149073Sphkexport NANO_CUSTOMIZE
791148422Sphkexport NANO_DATASIZE
792149082Sphkexport NANO_DRIVE
793148422Sphkexport NANO_HEADS
794148648Sphkexport NANO_IMAGES
795183284Sbmsexport NANO_IMGNAME
796194431Sn_hibmaexport NANO_MAKE_CONF_BUILD
797194431Sn_hibmaexport NANO_MAKE_CONF_INSTALL
798149120Sphkexport NANO_MEDIASIZE
799148648Sphkexport NANO_NAME
800148648Sphkexport NANO_NEWFS
801148648Sphkexport NANO_OBJ
802148422Sphkexport NANO_PMAKE
803148648Sphkexport NANO_SECTS
804148422Sphkexport NANO_SRC
805150366Sphkexport NANO_TOOLS
806148648Sphkexport NANO_WORLDDIR
807168894Sadrianexport NANO_BOOT0CFG
808168894Sadrianexport NANO_BOOTLOADER
809148422Sphk
810148422Sphk#######################################################################
811150366Sphk# And then it is as simple as that...
812148422Sphk
813194958Sn_hibma# File descriptor 3 is used for logging output, see pprint
814194958Sn_hibmaexec 3>&1
815194958Sn_hibma
816194958Sn_hibmaNANO_STARTTIME=`date +%s`
817183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting"
818183314Ssimon
819156496Sphkif $do_world ; then
820184048Ssam	if $do_clean ; then
821184048Ssam		clean_build
822184048Ssam	else
823184048Ssam		pprint 2 "Using existing build tree (as instructed)"
824184048Ssam	fi
825150366Sphk	make_conf_build
826150366Sphk	build_world
827156496Sphkelse
828183314Ssimon	pprint 2 "Skipping buildworld (as instructed)"
829156496Sphkfi
830156496Sphk
831156496Sphkif $do_kernel ; then
832150366Sphk	build_kernel
833150366Sphkelse
834183314Ssimon	pprint 2 "Skipping buildkernel (as instructed)"
835150366Sphkfi
836149120Sphk
837149120Sphkclean_world
838149082Sphkmake_conf_install
839148422Sphkinstall_world
840148422Sphkinstall_etc
841161341Ssimonsetup_nanobsd_etc
842148422Sphkinstall_kernel
843149120Sphk
844149082Sphkrun_customize
845150366Sphksetup_nanobsd
846149014Sphkprune_usr
847181461Sphkrun_late_customize
848181460Sphkif $do_image ; then
849181460Sphk	create_${NANO_ARCH}_diskimage
850181460Sphkelse
851183314Ssimon	pprint 2 "Skipping image build (as instructed)"
852181460Sphkfi
853154466Sphklast_orders
854149120Sphk
855183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed"
856