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