nanobsd.sh revision 186181
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 186181 2008-12-16 15:05:52Z phk $
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
54148422Sphk# XXX: MAKEOBJDIRPREFIX handling... ?
55148648Sphk#NANO_OBJ=""
56148422Sphk
57175258Ssimon# The directory to put the final images
58175258Ssimon# default is ${NANO_OBJ}
59175258Ssimon#NANO_DISKIMGDIR=""
60175258Ssimon
61148422Sphk# Parallel Make
62148422SphkNANO_PMAKE="make -j 3"
63148422Sphk
64183284Sbms# The default name for any image we create.
65183284SbmsNANO_IMGNAME="_.disk.full"
66183284Sbms
67148422Sphk# Options to put in make.conf during buildworld only
68148422SphkCONF_BUILD=' '
69148422Sphk
70148422Sphk# Options to put in make.conf during installworld only
71148422SphkCONF_INSTALL=' '
72148422Sphk
73148422Sphk# Options to put in make.conf during both build- & installworld.
74148422SphkCONF_WORLD=' '
75148422Sphk
76148422Sphk# Kernel config file to use
77148422SphkNANO_KERNEL=GENERIC
78148422Sphk
79149073Sphk# Customize commands.
80149073SphkNANO_CUSTOMIZE=""
81149073Sphk
82181461Sphk# Late customize commands.
83181461SphkNANO_LATE_CUSTOMIZE=""
84181461Sphk
85148648Sphk# Newfs paramters to use
86148648SphkNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
87148648Sphk
88149082Sphk# The drive name of the media at runtime
89149082SphkNANO_DRIVE=ad0
90149082Sphk
91148648Sphk# Target media size in 512 bytes sectors
92183284SbmsNANO_MEDIASIZE=1200000
93148648Sphk
94148648Sphk# Number of code images on media (1 or 2)
95148422SphkNANO_IMAGES=2
96148648Sphk
97171986Sphk# 0 -> Leave second image all zeroes so it compresses better.
98171986Sphk# 1 -> Initialize second image with a copy of the first
99171986SphkNANO_INIT_IMG2=1
100171986Sphk
101149535Ssimon# Size of code file system in 512 bytes sectors
102149120Sphk# If zero, size will be as large as possible.
103149120SphkNANO_CODESIZE=0
104149120Sphk
105149120Sphk# Size of configuration file system in 512 bytes sectors
106149120Sphk# Cannot be zero.
107148422SphkNANO_CONFSIZE=2048
108148648Sphk
109148648Sphk# Size of data file system in 512 bytes sectors
110149120Sphk# If zero: no partition configured.
111149120Sphk# If negative: max size possible
112149120SphkNANO_DATASIZE=0
113148422Sphk
114154466Sphk# Size of the /etc ramdisk in 512 bytes sectors
115154466SphkNANO_RAM_ETCSIZE=10240
116154466Sphk
117154466Sphk# Size of the /tmp+/var ramdisk in 512 bytes sectors
118154466SphkNANO_RAM_TMPVARSIZE=10240
119154466Sphk
120148422Sphk# Media geometry, only relevant if bios doesn't understand LBA.
121170607SphkNANO_SECTS=63
122148422SphkNANO_HEADS=16
123148422Sphk
124168894Sadrian# boot0 flags/options and configuration
125168894SadrianNANO_BOOT0CFG="-o packet -s 1 -m 3"
126168894SadrianNANO_BOOTLOADER="boot/boot0sio"
127168894Sadrian
128175258Ssimon# Backing type of md(4) device
129175258Ssimon# Can be "file" or "swap"
130175258SsimonNANO_MD_BACKING="file"
131175258Ssimon
132183314Ssimon# Progress Print level
133183314SsimonPPLEVEL=3
134183314Ssimon
135148422Sphk#######################################################################
136148648Sphk# Not a variable at this time
137148648Sphk
138148648SphkNANO_ARCH=i386
139148648Sphk
140148648Sphk#######################################################################
141150366Sphk#
142150366Sphk# The functions which do the real work.
143150366Sphk# Can be overridden from the config file(s)
144150366Sphk#
145150366Sphk#######################################################################
146148422Sphk
147149120Sphkclean_build ( ) (
148183314Ssimon	pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
149148422Sphk
150148422Sphk	if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
151148422Sphk		true
152148422Sphk	else
153148422Sphk		chflags -R noschg ${MAKEOBJDIRPREFIX}
154148422Sphk		rm -rf ${MAKEOBJDIRPREFIX}
155148422Sphk	fi
156148422Sphk	mkdir -p ${MAKEOBJDIRPREFIX}
157150343Sphk	printenv > ${MAKEOBJDIRPREFIX}/_.env
158148422Sphk)
159148422Sphk
160148422Sphkmake_conf_build ( ) (
161183314Ssimon	pprint 2 "Construct build make.conf ($NANO_MAKE_CONF)"
162148422Sphk
163148648Sphk	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
164148648Sphk	echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
165148422Sphk)
166148422Sphk
167148422Sphkbuild_world ( ) (
168183314Ssimon	pprint 2 "run buildworld"
169183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
170148422Sphk
171148422Sphk	cd ${NANO_SRC}
172183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
173183284Sbms		__MAKE_CONF=${NANO_MAKE_CONF} buildworld \
174148422Sphk		> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
175148422Sphk)
176148422Sphk
177148422Sphkbuild_kernel ( ) (
178183314Ssimon	pprint 2 "build kernel ($NANO_KERNEL)"
179183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
180148422Sphk
181148422Sphk	if [ -f ${NANO_KERNEL} ] ; then
182148422Sphk		cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
183148422Sphk	fi
184148422Sphk
185181083Ssam	(cd ${NANO_SRC};
186181083Ssam	# unset these just in case to avoid compiler complaints
187181083Ssam	# when cross-building
188181083Ssam	unset TARGET_CPUTYPE
189181083Ssam	unset TARGET_BIG_ENDIAN
190183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
191163922Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
192148422Sphk		> ${MAKEOBJDIRPREFIX}/_.bk 2>&1
193181083Ssam	)
194148422Sphk)
195148422Sphk
196148422Sphkclean_world ( ) (
197183314Ssimon	pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
198148422Sphk	if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
199148422Sphk		true
200148422Sphk	else
201148422Sphk		chflags -R noschg ${NANO_WORLDDIR}/
202148422Sphk		rm -rf ${NANO_WORLDDIR}/
203148422Sphk	fi
204148422Sphk	mkdir -p ${NANO_WORLDDIR}/
205148422Sphk)
206148422Sphk
207148422Sphkmake_conf_install ( ) (
208183314Ssimon	pprint 2 "Construct install make.conf ($NANO_MAKE_CONF)"
209148422Sphk
210148648Sphk	echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
211148648Sphk	echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
212148422Sphk)
213148422Sphk
214148422Sphkinstall_world ( ) (
215183314Ssimon	pprint 2 "installworld"
216183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.iw"
217148422Sphk
218148422Sphk	cd ${NANO_SRC}
219183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
220148422Sphk	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
221148422Sphk		DESTDIR=${NANO_WORLDDIR} \
222148422Sphk		> ${MAKEOBJDIRPREFIX}/_.iw 2>&1
223148422Sphk	chflags -R noschg ${NANO_WORLDDIR}
224148422Sphk)
225148422Sphk
226148422Sphkinstall_etc ( ) (
227148422Sphk
228183314Ssimon	pprint 2 "install /etc"
229183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.etc"
230148422Sphk
231160783Sthomas	cd ${NANO_SRC}
232183284Sbms	env TARGET_ARCH=${NANO_ARCH} \
233148422Sphk	${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
234148422Sphk		DESTDIR=${NANO_WORLDDIR} \
235148422Sphk		> ${MAKEOBJDIRPREFIX}/_.etc 2>&1
236148422Sphk)
237148422Sphk
238148422Sphkinstall_kernel ( ) (
239183314Ssimon	pprint 2 "install kernel"
240183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.ik"
241148422Sphk
242148422Sphk	cd ${NANO_SRC}
243183284Sbms	env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
244148422Sphk		DESTDIR=${NANO_WORLDDIR} \
245164660Sn_hibma		__MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
246148422Sphk		> ${MAKEOBJDIRPREFIX}/_.ik 2>&1
247148422Sphk)
248148422Sphk
249150366Sphkrun_customize() (
250150366Sphk
251183314Ssimon	pprint 2 "run customize scripts"
252150366Sphk	for c in $NANO_CUSTOMIZE
253150366Sphk	do
254183314Ssimon		pprint 2 "customize \"$c\""
255183314Ssimon		pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
256183314Ssimon		pprint 4 "`type $c`"
257150366Sphk		( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
258150366Sphk	done
259150366Sphk)
260150366Sphk
261181461Sphkrun_late_customize() (
262181461Sphk
263183314Ssimon	pprint 2 "run late customize scripts"
264181461Sphk	for c in $NANO_LATE_CUSTOMIZE
265181461Sphk	do
266183314Ssimon		pprint 2 "late customize \"$c\""
267183314Ssimon		pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
268183314Ssimon		pprint 4 "`type $c`"
269181461Sphk		( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
270181461Sphk	done
271181461Sphk)
272181461Sphk
273150366Sphksetup_nanobsd ( ) (
274183314Ssimon	pprint 2 "configure nanobsd setup"
275183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.dl"
276148422Sphk
277149103Sphk	(
278148422Sphk	cd ${NANO_WORLDDIR}
279148422Sphk
280170607Sphk	# Move /usr/local/etc to /etc/local so that the /cfg stuff
281170607Sphk	# can stomp on it.  Otherwise packages like ipsec-tools which
282170607Sphk	# have hardcoded paths under ${prefix}/etc are not tweakable.
283170607Sphk	if [ -d usr/local/etc ] ; then
284170607Sphk		(
285183727Sn_hibma		mkdir -p etc/local
286170607Sphk		cd usr/local/etc
287170607Sphk		find . -print | cpio -dumpl ../../../etc/local
288170607Sphk		cd ..
289170607Sphk		rm -rf etc
290170607Sphk		ln -s ../../etc/local etc
291170607Sphk		)
292170607Sphk	fi
293170607Sphk
294149082Sphk	for d in var etc
295149082Sphk	do
296149082Sphk		# link /$d under /conf
297149082Sphk		# we use hard links so we have them both places.
298149082Sphk		# the files in /$d will be hidden by the mount.
299149082Sphk		# XXX: configure /$d ramdisk size
300149082Sphk		mkdir -p conf/base/$d conf/default/$d
301149082Sphk		find $d -print | cpio -dumpl conf/base/
302149082Sphk	done
303148422Sphk
304154466Sphk	echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
305154466Sphk	echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
306154466Sphk
307148422Sphk	# pick up config files from the special partition
308149082Sphk	echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
309148422Sphk
310149082Sphk	# Put /tmp on the /var ramdisk (could be symlink already)
311149082Sphk	rmdir tmp || true
312149082Sphk	rm tmp || true
313148422Sphk	ln -s var/tmp tmp
314149082Sphk
315149103Sphk	) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
316148422Sphk)
317148422Sphk
318161341Ssimonsetup_nanobsd_etc ( ) (
319183314Ssimon	pprint 2 "configure nanobsd /etc"
320161341Ssimon
321161341Ssimon	(
322161341Ssimon	cd ${NANO_WORLDDIR}
323161341Ssimon
324161341Ssimon	# create diskless marker file
325161341Ssimon	touch etc/diskless
326161341Ssimon
327170324Sphk	# Make root filesystem R/O by default
328170324Sphk	echo "root_rw_mount=NO" >> etc/defaults/rc.conf
329170324Sphk
330161341Ssimon	# save config file for scripts
331161341Ssimon	echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
332161341Ssimon
333161341Ssimon	echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
334161341Ssimon	echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
335161341Ssimon	mkdir -p cfg
336161341Ssimon	)
337161341Ssimon)
338161341Ssimon
339149014Sphkprune_usr() (
340149014Sphk
341149014Sphk	# Remove all empty directories in /usr 
342149014Sphk	find ${NANO_WORLDDIR}/usr -type d -depth -print |
343149014Sphk		while read d
344149014Sphk		do
345149014Sphk			rmdir $d > /dev/null 2>&1 || true 
346149014Sphk		done
347149014Sphk)
348149014Sphk
349148422Sphkcreate_i386_diskimage ( ) (
350183314Ssimon	pprint 2 "build diskimage"
351183314Ssimon	pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.di"
352148422Sphk
353149103Sphk	(
354149120Sphk	echo $NANO_MEDIASIZE $NANO_IMAGES \
355149120Sphk		$NANO_SECTS $NANO_HEADS \
356149120Sphk		$NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
357148422Sphk	awk '
358150717Sphk	{
359149120Sphk		printf "# %s\n", $0
360149120Sphk
361148422Sphk		# size of cylinder in sectors
362149120Sphk		cs = $3 * $4
363148422Sphk
364148422Sphk		# number of full cylinders on media
365148422Sphk		cyl = int ($1 / cs)
366148422Sphk
367149120Sphk		# output fdisk geometry spec, truncate cyls to 1023
368148422Sphk		if (cyl <= 1023)
369149120Sphk			print "g c" cyl " h" $4 " s" $3
370148422Sphk		else
371149120Sphk			print "g c" 1023 " h" $4 " s" $3
372148422Sphk
373149120Sphk		if ($7 > 0) { 
374149120Sphk			# size of data partition in full cylinders
375149120Sphk			dsl = int (($7 + cs - 1) / cs)
376149120Sphk		} else {
377149120Sphk			dsl = 0;
378149120Sphk		}
379148422Sphk
380148422Sphk		# size of config partition in full cylinders
381149120Sphk		csl = int (($6 + cs - 1) / cs)
382148422Sphk
383150717Sphk		if ($5 == 0) {
384149120Sphk			# size of image partition(s) in full cylinders
385149120Sphk			isl = int ((cyl - dsl - csl) / $2)
386149120Sphk		} else {
387149120Sphk			isl = int (($5 + cs - 1) / cs)
388149120Sphk		}
389148422Sphk
390148422Sphk		# First image partition start at second track
391149120Sphk		print "p 1 165 " $3, isl * cs - $3
392148422Sphk		c = isl * cs;
393148422Sphk
394148422Sphk		# Second image partition (if any) also starts offset one 
395148422Sphk		# track to keep them identical.
396148422Sphk		if ($2 > 1) {
397149120Sphk			print "p 2 165 " $3 + c, isl * cs - $3
398148422Sphk			c += isl * cs;
399148422Sphk		}
400148422Sphk
401148422Sphk		# Config partition starts at cylinder boundary.
402148422Sphk		print "p 3 165 " c, csl * cs
403148422Sphk		c += csl * cs
404148422Sphk
405148422Sphk		# Data partition (if any) starts at cylinder boundary.
406149120Sphk		if ($7 > 0) {
407148422Sphk			print "p 4 165 " c, dsl * cs
408174936Ssimon		} else if ($7 < 0 && $1 > c) {
409174936Ssimon			print "p 4 165 " c, $1 - c
410150717Sphk		} else if ($1 < c) {
411150717Sphk			print "Disk space overcommitted by", \
412150717Sphk			    c - $1, "sectors" > "/dev/stderr"
413150717Sphk			exit 2
414148422Sphk		}
415183279Sbms
416183279Sbms		# Force slice 1 to be marked active. This is necessary
417183279Sbms		# for booting the image from a USB device to work.
418183279Sbms		print "a 1"
419150717Sphk	}
420148422Sphk	' > ${MAKEOBJDIRPREFIX}/_.fdisk
421148422Sphk
422183284Sbms	IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
423148422Sphk	MNT=${MAKEOBJDIRPREFIX}/_.mnt
424148422Sphk	mkdir -p ${MNT}
425148422Sphk
426175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
427175258Ssimon		MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
428175258Ssimon			-y ${NANO_HEADS}`
429175258Ssimon	else
430175258Ssimon		echo "Creating md backing file..."
431175258Ssimon		dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
432175258Ssimon			count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
433175258Ssimon		MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
434175258Ssimon			-y ${NANO_HEADS}`
435175258Ssimon	fi
436148422Sphk
437149014Sphk	trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
438148422Sphk
439148422Sphk	fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
440149120Sphk	fdisk ${MD}
441148422Sphk	# XXX: params
442148422Sphk	# XXX: pick up cached boot* files, they may not be in image anymore.
443168894Sadrian	boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
444160388Sthomas	bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
445149120Sphk	bsdlabel ${MD}s1
446148422Sphk
447148422Sphk	# Create first image
448148648Sphk	newfs ${NANO_NEWFS} /dev/${MD}s1a
449148422Sphk	mount /dev/${MD}s1a ${MNT}
450149014Sphk	df -i ${MNT}
451175258Ssimon	echo "Copying worlddir..."
452148422Sphk	( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
453148422Sphk	df -i ${MNT}
454175258Ssimon	echo "Generating mtree..."
455148648Sphk	( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
456148648Sphk	( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
457148422Sphk	umount ${MNT}
458148422Sphk
459171986Sphk	if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
460148422Sphk		# Duplicate to second image (if present)
461175258Ssimon		echo "Duplicating to second image..."
462148422Sphk		dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
463170322Sphk		mount /dev/${MD}s2a ${MNT}
464170322Sphk		for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
465170322Sphk		do
466170322Sphk			sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
467170322Sphk		done
468170322Sphk		umount ${MNT}
469170322Sphk
470148422Sphk	fi
471148422Sphk	
472148422Sphk	# Create Config slice
473148648Sphk	newfs ${NANO_NEWFS} /dev/${MD}s3
474148422Sphk	# XXX: fill from where ?
475148422Sphk
476148422Sphk	# Create Data slice, if any.
477148422Sphk	if [ $NANO_DATASIZE -gt 0 ] ; then
478148648Sphk		newfs ${NANO_NEWFS} /dev/${MD}s4
479148422Sphk		# XXX: fill from where ?
480148422Sphk	fi
481148422Sphk
482175258Ssimon	if [ "${NANO_MD_BACKING}" = "swap" ] ; then
483175258Ssimon		echo "Writing out _.disk.full..."
484175258Ssimon		dd if=/dev/${MD} of=${IMG} bs=64k
485175258Ssimon	fi
486175258Ssimon
487175258Ssimon	echo "Writing out _.disk.image..."
488175258Ssimon	dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
489148422Sphk	mdconfig -d -u $MD
490149103Sphk	) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
491148422Sphk)
492148422Sphk
493154466Sphklast_orders () (
494154466Sphk	# Redefine this function with any last orders you may have
495154466Sphk	# after the build completed, for instance to copy the finished
496154466Sphk	# image to a more convenient place:
497154466Sphk	# cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
498154466Sphk)
499154466Sphk
500148422Sphk#######################################################################
501148422Sphk#
502150366Sphk# Optional convenience functions.
503148422Sphk#
504148422Sphk#######################################################################
505148422Sphk
506149014Sphk#######################################################################
507150367Sphk# Common Flash device geometries
508150367Sphk#
509150367Sphk
510150367SphkFlashDevice () {
511150717Sphk	if [ -d ${NANO_TOOLS} ] ; then
512150717Sphk		. ${NANO_TOOLS}/FlashDevice.sub
513150717Sphk	else
514150717Sphk		. ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
515150717Sphk	fi
516150367Sphk	sub_FlashDevice $1 $2
517150367Sphk}
518150367Sphk
519183284Sbms#######################################################################
520183284Sbms# USB device geometries
521183284Sbms#
522183284Sbms# Usage:
523183284Sbms#	UsbDevice Generic 1000	# a generic flash key sold as having 1GB
524183284Sbms#
525183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
526183284Sbms#
527183284Sbms# Note that the capacity of a flash key is usually advertised in MB or
528183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available
529183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry.
530183284Sbms#
531183284Sbms# The following generic device layouts are understood:
532183284Sbms#  generic           An alias for generic-hdd.
533183284Sbms#  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
534183284Sbms#  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
535183284Sbms#
536183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB.
537183284Sbms#
538150367Sphk
539183284SbmsUsbDevice () {
540183284Sbms	a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
541183284Sbms	case $a1 in
542183284Sbms	generic-fdd)
543183284Sbms		NANO_HEADS=64
544183284Sbms		NANO_SECTS=32
545183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
546183284Sbms		;;
547183284Sbms	generic|generic-hdd)
548183284Sbms		NANO_HEADS=255
549183284Sbms		NANO_SECTS=63
550183284Sbms		NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
551183284Sbms		;;
552183284Sbms	*)
553183284Sbms		echo "Unknown USB flash device"
554183284Sbms		exit 2
555183284Sbms		;;
556183284Sbms	esac
557183284Sbms}
558183284Sbms
559150367Sphk#######################################################################
560150366Sphk# Setup serial console
561150343Sphk
562150366Sphkcust_comconsole () (
563150366Sphk	# Enable getty on console
564181751Sphk	sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
565150366Sphk
566150366Sphk	# Disable getty on syscons devices
567150366Sphk	sed -i "" -e '/^ttyv[0-8]/s/	on/	off/' ${NANO_WORLDDIR}/etc/ttys
568150366Sphk
569150366Sphk	# Tell loader to use serial console early.
570150366Sphk	echo " -h" > ${NANO_WORLDDIR}/boot.config
571150366Sphk)
572150366Sphk
573150366Sphk#######################################################################
574150366Sphk# Allow root login via ssh
575150366Sphk
576150366Sphkcust_allow_ssh_root () (
577150366Sphk	sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
578150366Sphk	    ${NANO_WORLDDIR}/etc/ssh/sshd_config
579150366Sphk)
580150366Sphk
581150366Sphk#######################################################################
582150366Sphk# Install the stuff under ./Files
583150366Sphk
584150366Sphkcust_install_files () (
585150511Ssimon	cd ${NANO_TOOLS}/Files
586183315Ssimon	find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
587150366Sphk)
588150366Sphk
589150366Sphk#######################################################################
590170607Sphk# Install packages from ${NANO_PACKAGE_DIR}
591170607Sphk
592170607Sphkcust_pkg () (
593170607Sphk
594170607Sphk	# Copy packages into chroot
595170607Sphk	mkdir -p ${NANO_WORLDDIR}/Pkg
596186181Sphk	(
597186181Sphk		cd ${NANO_PACKAGE_DIR}
598186181Sphk		find ${NANO_PACKAGE_LIST} -print |
599186181Sphk		    cpio -dumpv ${NANO_WORLDDIR}/Pkg
600186181Sphk	)
601170607Sphk
602170607Sphk	# Count & report how many we have to install
603170607Sphk	todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
604170607Sphk	echo "=== TODO: $todo"
605170607Sphk	ls ${NANO_WORLDDIR}/Pkg
606170607Sphk	echo "==="
607170607Sphk	while true
608170607Sphk	do
609182690Sthomas		# Record how many we have now
610170607Sphk		have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
611170607Sphk
612171986Sphk		# Attempt to install more packages
613171986Sphk		# ...but no more than 200 at a time due to pkg_add's internal
614171986Sphk		# limitations.
615171986Sphk		chroot ${NANO_WORLDDIR} sh -c \
616171986Sphk			'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
617170607Sphk
618170607Sphk		# See what that got us
619170607Sphk		now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
620170607Sphk		echo "=== NOW $now"
621170607Sphk		ls ${NANO_WORLDDIR}/var/db/pkg
622170607Sphk		echo "==="
623170607Sphk
624170607Sphk
625170607Sphk		if [ $now -eq $todo ] ; then
626170607Sphk			echo "DONE $now packages"
627170607Sphk			break
628170607Sphk		elif [ $now -eq $have ] ; then
629170607Sphk			echo "FAILED: Nothing happened on this pass"
630170607Sphk			exit 2
631170607Sphk		fi
632170607Sphk	done
633170607Sphk	rm -rf ${NANO_WORLDDIR}/Pkg
634170607Sphk)
635170607Sphk
636170607Sphk#######################################################################
637150366Sphk# Convenience function:
638150366Sphk# 	Register $1 as customize function.
639150366Sphk
640150343Sphkcustomize_cmd () {
641150343Sphk	NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
642150343Sphk}
643150343Sphk
644150343Sphk#######################################################################
645181461Sphk# Convenience function:
646181461Sphk# 	Register $1 as late customize function to run just before
647181461Sphk#	image creation.
648181461Sphk
649181461Sphklate_customize_cmd () {
650181461Sphk	NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
651181461Sphk}
652181461Sphk
653181461Sphk#######################################################################
654150366Sphk#
655150366Sphk# All set up to go...
656150366Sphk#
657150366Sphk#######################################################################
658150366Sphk
659183314Ssimon# Progress Print
660183314Ssimon#	Print $2 at level $1.
661183314Ssimonpprint() {
662183314Ssimon    if [ "$1" -le $PPLEVEL ]; then
663183314Ssimon	printf "%.${1}s %s\n" "#####" "$2"
664183314Ssimon    fi
665183314Ssimon}
666183314Ssimon
667156496Sphkusage () {
668156496Sphk	(
669183314Ssimon	echo "Usage: $0 [-bikqvw] [-c config_file]"
670156496Sphk	echo "	-b	suppress builds (both kernel and world)"
671183314Ssimon	echo "	-i	suppress disk image build"
672156496Sphk	echo "	-k	suppress buildkernel"
673184048Ssam	echo "	-n	add -DNO_CLEAN to buildworld, buildkernel, etc"
674183314Ssimon	echo "	-q	make output more quite"
675183314Ssimon	echo "	-v	make output more verbose"
676156496Sphk	echo "	-w	suppress buildworld"
677156496Sphk	echo "	-c	specify config file"
678156496Sphk	) 1>&2
679156496Sphk	exit 2
680156496Sphk}
681156496Sphk
682150366Sphk#######################################################################
683149014Sphk# Parse arguments
684148422Sphk
685184048Ssamdo_clean=true
686156496Sphkdo_kernel=true
687156496Sphkdo_world=true
688181460Sphkdo_image=true
689150366Sphk
690156496Sphkset +e
691184048Ssamargs=`getopt bc:hiknqvw $*`
692149014Sphkif [ $? -ne 0 ] ; then
693156496Sphk	usage
694149014Sphk	exit 2
695149014Sphkfi
696156496Sphkset -e
697149014Sphk
698149014Sphkset -- $args
699149014Sphkfor i
700149014Sphkdo
701149014Sphk	case "$i" 
702149014Sphk	in
703150366Sphk	-b)
704156496Sphk		do_world=false
705156496Sphk		do_kernel=false
706183314Ssimon		shift
707150366Sphk		;;
708156496Sphk	-k)
709156496Sphk		do_kernel=false
710183314Ssimon		shift
711156496Sphk		;;
712149014Sphk	-c)
713149014Sphk		. "$2"
714183314Ssimon		shift
715183314Ssimon		shift
716149014Sphk		;;
717150366Sphk	-h)
718156496Sphk		usage
719150366Sphk		;;
720181460Sphk	-i)
721181460Sphk		do_image=false
722183314Ssimon		shift
723181481Ssam		;;
724184048Ssam	-n)
725184048Ssam		do_clean=false
726184048Ssam		shift
727184048Ssam		;;
728183314Ssimon	-q)
729183314Ssimon		PPLEVEL=$(($PPLEVEL - 1))
730183314Ssimon		shift
731183314Ssimon		;;
732183314Ssimon	-v)
733183314Ssimon		PPLEVEL=$(($PPLEVEL + 1))
734183314Ssimon		shift
735183314Ssimon		;;
736156496Sphk	-w)
737156496Sphk		do_world=false
738183314Ssimon		shift
739156496Sphk		;;
740149014Sphk	--)
741183314Ssimon		shift
742183314Ssimon		break
743149014Sphk	esac
744149014Sphkdone
745149014Sphk
746150366Sphkif [ $# -gt 0 ] ; then
747156496Sphk	echo "$0: Extraneous arguments supplied"
748156496Sphk	usage
749150366Sphkfi
750150366Sphk
751148422Sphk#######################################################################
752150366Sphk# Setup and Export Internal variables
753150366Sphk#
754148422Sphkif [ "x${NANO_OBJ}" = "x" ] ; then
755148422Sphk	MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
756149073Sphk	NANO_OBJ=${MAKEOBJDIRPREFIX}
757148422Sphkelse
758148422Sphk	MAKEOBJDIRPREFIX=${NANO_OBJ}
759148422Sphkfi
760148422Sphk
761175258Ssimonif [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
762175258Ssimon	NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
763175258Ssimonfi
764175258Ssimon
765148422SphkNANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
766148422SphkNANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
767148422Sphk
768150511Ssimonif [ -d ${NANO_TOOLS} ] ; then
769150511Ssimon	true
770150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
771150511Ssimon	NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
772150511Ssimonelse
773150511Ssimon	echo "NANO_TOOLS directory does not exist" 1>&2
774150511Ssimon	exit 1
775150511Ssimonfi
776150511Ssimon
777184048Ssamif $do_clean ; then
778184048Ssam	true
779184048Ssamelse
780184048Ssam	NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
781184048Ssamfi
782184048Ssam
783148422Sphkexport MAKEOBJDIRPREFIX
784148422Sphk
785148422Sphkexport NANO_ARCH
786149120Sphkexport NANO_CODESIZE
787148422Sphkexport NANO_CONFSIZE
788149073Sphkexport NANO_CUSTOMIZE
789148422Sphkexport NANO_DATASIZE
790149082Sphkexport NANO_DRIVE
791148422Sphkexport NANO_HEADS
792148648Sphkexport NANO_IMAGES
793183284Sbmsexport NANO_IMGNAME
794148648Sphkexport NANO_MAKE_CONF
795149120Sphkexport NANO_MEDIASIZE
796148648Sphkexport NANO_NAME
797148648Sphkexport NANO_NEWFS
798148648Sphkexport NANO_OBJ
799148422Sphkexport NANO_PMAKE
800148648Sphkexport NANO_SECTS
801148422Sphkexport NANO_SRC
802150366Sphkexport NANO_TOOLS
803148648Sphkexport NANO_WORLDDIR
804168894Sadrianexport NANO_BOOT0CFG
805168894Sadrianexport NANO_BOOTLOADER
806148422Sphk
807148422Sphk#######################################################################
808150366Sphk# And then it is as simple as that...
809148422Sphk
810183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting"
811183314Ssimon
812156496Sphkif $do_world ; then
813184048Ssam	if $do_clean ; then
814184048Ssam		clean_build
815184048Ssam	else
816184048Ssam		pprint 2 "Using existing build tree (as instructed)"
817184048Ssam	fi
818150366Sphk	make_conf_build
819150366Sphk	build_world
820156496Sphkelse
821183314Ssimon	pprint 2 "Skipping buildworld (as instructed)"
822156496Sphkfi
823156496Sphk
824156496Sphkif $do_kernel ; then
825150366Sphk	build_kernel
826150366Sphkelse
827183314Ssimon	pprint 2 "Skipping buildkernel (as instructed)"
828150366Sphkfi
829149120Sphk
830149120Sphkclean_world
831149082Sphkmake_conf_install
832148422Sphkinstall_world
833148422Sphkinstall_etc
834161341Ssimonsetup_nanobsd_etc
835148422Sphkinstall_kernel
836149120Sphk
837149082Sphkrun_customize
838150366Sphksetup_nanobsd
839149014Sphkprune_usr
840181461Sphkrun_late_customize
841181460Sphkif $do_image ; then
842181460Sphk	create_${NANO_ARCH}_diskimage
843183284Sbms	echo "# Created NanoBSD disk image: ${MAKEOBJDIRPREFIX}/${NANO_IMGNAME}"
844181460Sphkelse
845183314Ssimon	pprint 2 "Skipping image build (as instructed)"
846181460Sphkfi
847154466Sphklast_orders
848149120Sphk
849183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed"
850