Project

General

Profile

Download (73.6 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# builder_common.sh
4
#
5
# Copyright (c) 2004-2015 Electric Sheep Fencing, LLC
6
# Copyright (C) 2014 Ermal Luçi
7
# All rights reserved.
8
#
9
# NanoBSD portions of the code
10
# Copyright (c) 2005 Poul-Henning Kamp.
11
# and copied from nanobsd.sh
12
# All rights reserved.
13
#
14
# FreeSBIE portions of the code
15
# Copyright (c) 2005 Dario Freni
16
# and copied from FreeSBIE project
17
# All rights reserved.
18
#
19
# Redistribution and use in source and binary forms, with or without
20
# modification, are permitted provided that the following conditions
21
# are met:
22
#
23
# 1. Redistributions of source code must retain the above copyright
24
#    notice, this list of conditions and the following disclaimer.
25
#
26
# 2. Redistributions in binary form must reproduce the above copyright
27
#    notice, this list of conditions and the following disclaimer in the
28
#    documentation and/or other materials provided with the distribution.
29
#
30
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
31
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
34
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
# OF THE POSSIBILITY OF SUCH DAMAGE.
42
# Redistribution and use in source and binary forms, with or without
43
# modification, are permitted provided that the following conditions are met:
44
#
45

    
46
if [ -z "${IMAGES_FINAL_DIR}" -o "${IMAGES_FINAL_DIR}" = "/" ]; then
47
	echo "IMAGES_FINAL_DIR is not defined"
48
	print_error_pfS
49
fi
50

    
51
lc() {
52
	echo "${1}" | tr '[[:upper:]]' '[[:lower:]]'
53
}
54

    
55
git_last_commit() {
56
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
57
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
58
	echo ">>> Last known commit $CURRENT_AUTHOR - $CURRENT_COMMIT"
59
	echo "$CURRENT_COMMIT" > $SCRATCHDIR/build_commit_info.txt
60
}
61

    
62
# Create core pkg repository
63
core_pkg_create_repo() {
64
	if [ ! -d "${CORE_PKG_REAL_PATH}/All" ]; then
65
		return
66
	fi
67

    
68
	############ ATTENTION ##############
69
	#
70
	# For some reason pkg-repo fail without / in the end of directory name
71
	# so removing it will break command
72
	#
73
	# https://github.com/freebsd/pkg/issues/1364
74
	#
75
	echo -n ">>> Creating core packages repository... "
76
	if pkg repo -q "${CORE_PKG_REAL_PATH}/"; then
77
		echo "Done!"
78
	else
79
		echo "Failed!"
80
		print_error_pfS
81
	fi
82

    
83
	# Use the same directory structure as poudriere does to avoid
84
	# breaking snapshot repositories during rsync
85
	ln -sf $(basename ${CORE_PKG_REAL_PATH}) ${CORE_PKG_PATH}/.latest
86
	ln -sf .latest/All ${CORE_PKG_ALL_PATH}
87
	ln -sf .latest/digests.txz ${CORE_PKG_PATH}/digests.txz
88
	ln -sf .latest/meta.txz ${CORE_PKG_PATH}/meta.txz
89
	ln -sf .latest/packagesite.txz ${CORE_PKG_PATH}/packagesite.txz
90
}
91

    
92
# Create core pkg (base, kernel)
93
core_pkg_create() {
94
	local _template="${1}"
95
	local _flavor="${2}"
96
	local _version="${3}"
97
	local _root="${4}"
98
	local _filter="${5}"
99

    
100
	[ -d "${CORE_PKG_TMP}" ] \
101
		&& rm -rf ${CORE_PKG_TMP}
102

    
103
	local _templates_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
104
	local _template_metadir=${_templates_path}/metadir
105
	local _metadir=${CORE_PKG_TMP}/${_template}_metadir
106

    
107
	if [ ! -d ${_template_metadir} ]; then
108
		echo "ERROR: Template dir not found for pkg ${_template}"
109
		exit
110
	fi
111

    
112
	mkdir -p ${CORE_PKG_TMP}
113

    
114
	cp -r ${_template_metadir} ${_metadir}
115

    
116
	local _manifest=${_metadir}/+MANIFEST
117
	local _plist=${CORE_PKG_TMP}/${_template}_plist
118
	local _exclude_plist=${CORE_PKG_TMP}/${_template}_exclude_plist
119

    
120
	if [ -f "${_templates_path}/pkg-plist" ]; then
121
		cp ${_templates_path}/pkg-plist ${_plist}
122
	else
123
		if [ -n "${_filter}" ]; then
124
			_filter="-name ${_filter}"
125
		fi
126
		(cd ${_root} && find . ${_filter} -type f -or -type l | sed 's,^.,,' | sort -u) > ${_plist}
127
	fi
128

    
129
	if [ -f "${_templates_path}/exclude_plist" ]; then
130
		cp ${_templates_path}/exclude_plist ${_exclude_plist}
131
	else
132
		touch ${_exclude_plist}
133
	fi
134

    
135
	sed \
136
		-i '' \
137
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
138
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
139
		-e "s,%%FLAVOR%%,${_flavor:+-}${_flavor},g" \
140
		-e "s,%%FLAVOR_DESC%%,${_flavor:+ (${_flavor})},g" \
141
		-e "s,%%VERSION%%,${_version},g" \
142
		${_metadir}/* \
143
		${_plist} \
144
		${exclude_plist}
145

    
146
	if [ -f "${_exclude_plist}" ]; then
147
		sort -u ${_exclude_plist} > ${_plist}.exclude
148
		mv ${_plist} ${_plist}.tmp
149
		comm -23 ${_plist}.tmp ${_plist}.exclude > ${_plist}
150
		rm -f ${_plist}.tmp ${plist}.exclude
151
	fi
152

    
153
	# Add license information
154
	local _portname=$(sed '/^name: /!d; s,^[^"]*",,; s,",,' ${_metadir}/+MANIFEST)
155
	local _licenses_dir="/usr/local/share/licenses/${_portname}-${_version}"
156
	mkdir -p ${_root}${_licenses_dir}
157
	cp ${BUILDER_ROOT}/license.txt ${_root}${_licenses_dir}/ESF
158
	echo "This package has a single license: ESF (Electric Sheep Fencing License)." \
159
		> ${_root}${_licenses_dir}/LICENSE
160
	cat <<EOF >${_root}${_licenses_dir}/catalog.mk
161
_LICENSE=ESF
162
_LICENSE_NAME=Electric Sheep Fencing License
163
_LICENSE_PERMS=dist-mirror dist-sell pkg-mirror pkg-sell auto-accept
164
_LICENSE_GROUPS=
165
_LICENSE_DISTFILES=
166
EOF
167
	cat <<EOF >>${_plist}
168
${_licenses_dir}/catalog.mk
169
${_licenses_dir}/LICENSE
170
${_licenses_dir}/ESF
171
EOF
172

    
173
	mkdir -p ${CORE_PKG_REAL_PATH}/All
174
	if ! pkg create -o ${CORE_PKG_REAL_PATH}/All -p ${_plist} -r ${_root} -m ${_metadir}; then
175
		echo ">>> ERROR: Error building package ${_template} ${_flavor}"
176
		print_error_pfS
177
	fi
178
}
179

    
180
# This routine will output that something went wrong
181
print_error_pfS() {
182
	echo
183
	echo "####################################"
184
	echo "Something went wrong, check errors!" >&2
185
	echo "####################################"
186
	echo
187
	echo "NOTE: a lot of times you can run './build.sh --clean-builder' to resolve."
188
	echo
189
	if [ "$1" != "" ]; then
190
		echo $1
191
	fi
192
	[ -n "${LOGFILE}" -a -f "${LOGFILE}" ] && \
193
		echo "Log saved on ${LOGFILE}" && \
194
		tail -n20 ${LOGFILE} >&2
195
	echo
196
	kill $$
197
	exit 1
198
}
199

    
200
# This routine will verify that the kernel has been
201
# installed OK to the staging area.
202
ensure_kernel_exists() {
203
	if [ ! -f "$1/boot/kernel/kernel.gz" ]; then
204
		echo ">>> ERROR: Could not locate $1/boot/kernel.gz"
205
		print_error_pfS
206
	fi
207
	KERNEL_SIZE=$(stat -f "%z" $1/boot/kernel/kernel.gz)
208
	if [ "$KERNEL_SIZE" -lt 3500 ]; then
209
		echo ">>> ERROR: Kernel $1/boot/kernel.gz appears to be smaller than it should be: $KERNEL_SIZE"
210
		print_error_pfS
211
	fi
212
}
213

    
214
get_pkg_name() {
215
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
216
}
217

    
218
# This routine builds all related kernels
219
build_all_kernels() {
220
	# Set KERNEL_BUILD_PATH if it has not been set
221
	if [ -z "${KERNEL_BUILD_PATH}" ]; then
222
		KERNEL_BUILD_PATH=$SCRATCHDIR/kernels
223
		echo ">>> KERNEL_BUILD_PATH has not been set. Setting to ${KERNEL_BUILD_PATH}!"
224
	fi
225

    
226
	[ -d "${KERNEL_BUILD_PATH}" ] \
227
		&& rm -rf ${KERNEL_BUILD_PATH}
228

    
229
	# Build embedded kernel
230
	for BUILD_KERNEL in $BUILD_KERNELS; do
231
		unset KERNCONF
232
		unset KERNEL_DESTDIR
233
		unset KERNELCONF
234
		unset KERNEL_NAME
235
		export KERNCONF=$BUILD_KERNEL
236
		export KERNEL_DESTDIR="$KERNEL_BUILD_PATH/$BUILD_KERNEL"
237
		export KERNELCONF="${FREEBSD_SRC_DIR}/sys/${TARGET}/conf/$BUILD_KERNEL"
238
		export KERNEL_NAME=${BUILD_KERNEL}
239

    
240
		LOGFILE="${BUILDER_LOGS}/kernel.${KERNCONF}.${TARGET}.log"
241
		echo ">>> Building $BUILD_KERNEL kernel."  | tee -a ${LOGFILE}
242

    
243
		if [ ! -e "${FREEBSD_SRC_DIR}/sys/${TARGET}/conf/${BUILD_KERNEL}" ]; then
244
			echo ">>> ERROR: Could not find $KERNELCONF"
245
			print_error_pfS
246
		fi
247

    
248
		if [ -n "${NO_BUILDKERNEL}" -a -f "${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz" ]; then
249
			echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
250
			continue
251
		fi
252

    
253
		buildkernel
254

    
255
		echo ">>> Staging $BUILD_KERNEL kernel..." | tee -a ${LOGFILE}
256
		installkernel
257

    
258
		ensure_kernel_exists $KERNEL_DESTDIR
259

    
260
		echo -n ">>> Creating pkg of $KERNEL_NAME-debug kernel to staging area... "  | tee -a ${LOGFILE}
261
		core_pkg_create kernel-debug ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} \*.symbols
262
		find ${KERNEL_DESTDIR} -name '*.symbols' -type f -delete
263
		echo " Done" | tee -a ${LOGFILE}
264

    
265
		echo -n ">>> Creating pkg of $KERNEL_NAME kernel to staging area... "  | tee -a ${LOGFILE}
266
		core_pkg_create kernel ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR}
267

    
268
		rm -rf $KERNEL_DESTDIR 2>&1 1>/dev/null
269

    
270
		echo " Done" | tee -a ${LOGFILE}
271
	done
272
}
273

    
274
install_default_kernel() {
275
	if [ -z "${1}" ]; then
276
		echo ">>> ERROR: install_default_kernel called without a kernel config name"| tee -a ${LOGFILE}
277
		print_error_pfS
278
	fi
279

    
280
	export KERNEL_NAME="${1}"
281

    
282
	echo -n ">>> Installing kernel to be used by image ${KERNEL_NAME}..." | tee -a ${LOGFILE}
283

    
284
	# Copy kernel package to chroot, otherwise pkg won't find it to install
285
	if ! pkg_chroot_add ${FINAL_CHROOT_DIR} kernel-${KERNEL_NAME}; then
286
		echo ">>> ERROR: Error installing kernel package $(get_pkg_name kernel-${KERNEL_NAME}).txz" | tee -a ${LOGFILE}
287
		print_error_pfS
288
	fi
289

    
290
	# Lock kernel to avoid user end up removing it for any reason
291
	pkg_chroot ${FINAL_CHROOT_DIR} lock -q -y $(get_pkg_name kernel-${KERNEL_NAME})
292

    
293
	if [ ! -f $FINAL_CHROOT_DIR/boot/kernel/kernel.gz ]; then
294
		echo ">>> ERROR: No kernel installed on $FINAL_CHROOT_DIR and the resulting image will be unusable. STOPPING!" | tee -a ${LOGFILE}
295
		print_error_pfS
296
	fi
297
	mkdir -p $FINAL_CHROOT_DIR/pkgs
298
	if [ -z "${2}" -o -n "${INSTALL_EXTRA_KERNELS}" ]; then
299
		cp ${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz $FINAL_CHROOT_DIR/pkgs
300
		if [ -n "${INSTALL_EXTRA_KERNELS}" ]; then
301
			for _EXTRA_KERNEL in $INSTALL_EXTRA_KERNELS; do
302
				_EXTRA_KERNEL_PATH=${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${_EXTRA_KERNEL}).txz
303
				if [ -f "${_EXTRA_KERNEL_PATH}" ]; then
304
					echo -n ". adding ${_EXTRA_KERNEL_PATH} on image /pkgs folder"
305
					cp ${_EXTRA_KERNEL_PATH} $FINAL_CHROOT_DIR/pkgs
306
				else
307
					echo ">>> ERROR: Requested kernel $(get_pkg_name kernel-${_EXTRA_KERNEL}).txz was not found to be put on image /pkgs folder!"
308
					print_error_pfS
309
				fi
310
			done
311
		fi
312
	fi
313
	echo "Done." | tee -a ${LOGFILE}
314

    
315
	unset KERNEL_NAME
316
}
317

    
318
# This builds FreeBSD (make buildworld)
319
# Imported from FreeSBIE
320
make_world() {
321

    
322
	LOGFILE=${BUILDER_LOGS}/buildworld.${TARGET}
323
	if [ -n "${NO_BUILDWORLD}" ]; then
324
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
325
		return
326
	fi
327

    
328
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
329
	makeargs="${MAKEJ}"
330
	echo ">>> Building world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
331
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} buildworld" | tee -a ${LOGFILE}
332
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} buildworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
333
	echo ">>> Building world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
334

    
335
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
336
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
337
	# Create if cleaned up
338
	makeargs="${MAKEJ} DESTDIR=${STAGE_CHROOT_DIR} WITHOUT_TOOLCHAIN=1"
339
	echo ">>> Installing world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
340
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld" | tee -a ${LOGFILE}
341
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
342
	echo ">>> Installing world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
343

    
344
	makeargs="${MAKEJ} DESTDIR=${STAGE_CHROOT_DIR}"
345
	echo ">>> Distribution world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
346
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution " | tee -a ${LOGFILE}
347
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution  || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
348
	echo ">>> Distribution world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
349

    
350
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
351
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
352
	makeargs="${MAKEJ} DESTDIR=${STAGE_CHROOT_DIR}"
353
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
354
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install " | tee -a ${LOGFILE}
355
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
356
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean all install" | tee -a ${LOGFILE}
357
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
358
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
359

    
360
	unset makeargs
361
}
362

    
363
nanobsd_image_filename() {
364
	local _size="$1"
365
	local _type="$2"
366

    
367
	echo "$NANOBSD_IMG_TEMPLATE" | sed \
368
		-e "s,%%SIZE%%,${_size},g" \
369
		-e "s,%%TYPE%%,${_type},g"
370

    
371
	return 0
372
}
373

    
374
# This routine originated in nanobsd.sh
375
nanobsd_set_flash_details () {
376
	a1=$(echo $1 | tr '[:upper:]' '[:lower:]')
377

    
378
	# Source:
379
	#	SanDisk CompactFlash Memory Card
380
	#	Product Manual
381
	#	Version 10.9
382
	#	Document No. 20-10-00038
383
	#	April 2005
384
	# Table 2-7
385
	# NB: notice math error in SDCFJ-4096-388 line.
386
	#
387
	case "${a1}" in
388
		2048|2048m|2048mb|2g)
389
			NANO_MEDIASIZE=$((1989999616/512))
390
			;;
391
		4096|4096m|4096mb|4g)
392
			NANO_MEDIASIZE=$((3989999616/512))
393
			;;
394
		8192|8192m|8192mb|8g)
395
			NANO_MEDIASIZE=$((7989999616/512))
396
			;;
397
		16384|16384m|16384mb|16g)
398
			NANO_MEDIASIZE=$((15989999616/512))
399
			;;
400
		*)
401
			echo "Unknown Flash capacity"
402
			exit 2
403
			;;
404
	esac
405

    
406
	NANO_HEADS=16
407
	NANO_SECTS=63
408

    
409
	echo ">>> [nanoo] $1"
410
	echo ">>> [nanoo] NANO_MEDIASIZE: $NANO_MEDIASIZE"
411
	echo ">>> [nanoo] NANO_HEADS: $NANO_HEADS"
412
	echo ">>> [nanoo] NANO_SECTS: $NANO_SECTS"
413
	echo ">>> [nanoo] NANO_BOOT0CFG: $NANO_BOOT0CFG"
414
}
415

    
416
# This routine originated in nanobsd.sh
417
create_nanobsd_diskimage () {
418
	if [ -z "${1}" ]; then
419
		echo ">>> ERROR: Type of image has not been specified"
420
		print_error_pfS
421
	fi
422
	if [ -z "${2}" ]; then
423
		echo ">>> ERROR: Size of image has not been specified"
424
		print_error_pfS
425
	fi
426

    
427
	if [ "${1}" = "nanobsd" ]; then
428
		# It's serial
429
		export NANO_BOOTLOADER="boot/boot0sio"
430
	elif [ "${1}" = "nanobsd-vga" ]; then
431
		# It's vga
432
		export NANO_BOOTLOADER="boot/boot0"
433
	else
434
		echo ">>> ERROR: Type of image to create unknown"
435
		print_error_pfS
436
	fi
437

    
438
	if [ -z "${2}" ]; then
439
		echo ">>> ERROR: Media size(s) not specified."
440
		print_error_pfS
441
	fi
442

    
443
	if [ -z "${2}" ]; then
444
		echo ">>> ERROR: FLASH_SIZE is not set."
445
		print_error_pfS
446
	fi
447

    
448
	LOGFILE=${BUILDER_LOGS}/${1}.${TARGET}
449
	# Prepare folder to be put in image
450
	customize_stagearea_for_image "${1}"
451
	install_default_kernel ${DEFAULT_KERNEL} "no"
452

    
453
	echo ">>> Fixing up NanoBSD Specific items..." | tee -a ${LOGFILE}
454

    
455
	echo "nanobsd" > $FINAL_CHROOT_DIR/etc/platform
456

    
457
	local BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
458
	local LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
459

    
460
	if [ "${1}" = "nanobsd" ]; then
461
		# Tell loader to use serial console early.
462
		echo "-S115200 -h" >> ${BOOTCONF}
463

    
464
		# Remove old console options if present.
465
		[ -f "${LOADERCONF}" ] \
466
			&& sed -i "" -Ee "/(console|boot_multicons|boot_serial|hint.uart)/d" ${LOADERCONF}
467
		# Activate serial console+video console in loader.conf
468
		echo 'loader_color="NO"' >> ${LOADERCONF}
469
		echo 'beastie_disable="YES"' >> ${LOADERCONF}
470
		echo 'boot_serial="YES"' >> ${LOADERCONF}
471
		echo 'console="comconsole"' >> ${LOADERCONF}
472
		echo 'comconsole_speed="115200"' >> ${LOADERCONF}
473
	fi
474
	echo 'autoboot_delay="5"' >> ${LOADERCONF}
475

    
476
	# Old systems will run (pre|post)_upgrade_command from /tmp
477
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command ]; then
478
		cp -p \
479
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command \
480
			${FINAL_CHROOT_DIR}/tmp
481
	fi
482
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command ]; then
483
		cp -p \
484
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command \
485
			${FINAL_CHROOT_DIR}/tmp
486
	fi
487

    
488
	mkdir -p ${IMAGES_FINAL_DIR}/nanobsd
489

    
490
	for _NANO_MEDIASIZE in ${2}; do
491
		if [ -z "${_NANO_MEDIASIZE}" ]; then
492
			continue;
493
		fi
494

    
495
		echo ">>> building NanoBSD(${1}) disk image with size ${_NANO_MEDIASIZE} for platform (${TARGET})..." | tee -a ${LOGFILE}
496
		echo "" > $BUILDER_LOGS/nanobsd_cmds.sh
497

    
498
		IMG="${IMAGES_FINAL_DIR}/nanobsd/$(nanobsd_image_filename ${_NANO_MEDIASIZE} ${1})"
499

    
500
		nanobsd_set_flash_details ${_NANO_MEDIASIZE}
501

    
502
		# These are defined in FlashDevice and on builder_default.sh
503
		echo $NANO_MEDIASIZE \
504
			$NANO_IMAGES \
505
			$NANO_SECTS \
506
			$NANO_HEADS \
507
			$NANO_CODESIZE \
508
			$NANO_CONFSIZE \
509
			$NANO_DATASIZE |
510
awk '
511
{
512
	printf "# %s\n", $0
513

    
514
	# size of cylinder in sectors
515
	cs = $3 * $4
516

    
517
	# number of full cylinders on media
518
	cyl = int ($1 / cs)
519

    
520
	# output fdisk geometry spec, truncate cyls to 1023
521
	if (cyl <= 1023)
522
		print "g c" cyl " h" $4 " s" $3
523
	else
524
		print "g c" 1023 " h" $4 " s" $3
525

    
526
	if ($7 > 0) {
527
		# size of data partition in full cylinders
528
		dsl = int (($7 + cs - 1) / cs)
529
	} else {
530
		dsl = 0;
531
	}
532

    
533
	# size of config partition in full cylinders
534
	csl = int (($6 + cs - 1) / cs)
535

    
536
	if ($5 == 0) {
537
		# size of image partition(s) in full cylinders
538
		isl = int ((cyl - dsl - csl) / $2)
539
	} else {
540
		isl = int (($5 + cs - 1) / cs)
541
	}
542

    
543
	# First image partition start at second track
544
	print "p 1 165 " $3, isl * cs - $3
545
	c = isl * cs;
546

    
547
	# Second image partition (if any) also starts offset one
548
	# track to keep them identical.
549
	if ($2 > 1) {
550
		print "p 2 165 " $3 + c, isl * cs - $3
551
		c += isl * cs;
552
	}
553

    
554
	# Config partition starts at cylinder boundary.
555
	print "p 3 165 " c, csl * cs
556
	c += csl * cs
557

    
558
	# Data partition (if any) starts at cylinder boundary.
559
	if ($7 > 0) {
560
		print "p 4 165 " c, dsl * cs
561
	} else if ($7 < 0 && $1 > c) {
562
		print "p 4 165 " c, $1 - c
563
	} else if ($1 < c) {
564
		print "Disk space overcommitted by", \
565
		    c - $1, "sectors" > "/dev/stderr"
566
		exit 2
567
	}
568

    
569
	# Force slice 1 to be marked active. This is necessary
570
	# for booting the image from a USB device to work.
571
	print "a 1"
572
}
573
	' > ${SCRATCHDIR}/_.fdisk
574

    
575
		MNT=${SCRATCHDIR}/_.mnt
576
		mkdir -p ${MNT}
577

    
578
		dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
579
			count=0 seek=$((${NANO_MEDIASIZE}/${NANO_SECTS})) 2>&1 >> ${LOGFILE}
580

    
581
		MD=$(mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} -y ${NANO_HEADS})
582
		trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
583

    
584
		fdisk -i -f ${SCRATCHDIR}/_.fdisk ${MD} 2>&1 >> ${LOGFILE}
585
		fdisk ${MD} 2>&1 >> ${LOGFILE}
586

    
587
		boot0cfg -t 100 -B -b ${FINAL_CHROOT_DIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 2>&1 >> ${LOGFILE}
588

    
589
		# Create first image
590
		bsdlabel -m i386 -w -B -b ${FINAL_CHROOT_DIR}/boot/boot ${MD}s1 2>&1 >> ${LOGFILE}
591
		bsdlabel -m i386 ${MD}s1 2>&1 >> ${LOGFILE}
592
		local _label=$(lc ${PRODUCT_NAME})
593
		newfs -L ${_label}0 ${NANO_NEWFS} /dev/${MD}s1a 2>&1 >> ${LOGFILE}
594
		mount /dev/ufs/${_label}0 ${MNT}
595
		if [ $? -ne 0 ]; then
596
			echo ">>> ERROR: Something wrong happened during mount of first slice image creation. STOPPING!" | tee -a ${LOGFILE}
597
			print_error_pfS
598
		fi
599
		# Consider the unmounting as well
600
		trap "umount /dev/ufs/${_label}0; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
601

    
602
		clone_directory_contents ${FINAL_CHROOT_DIR} ${MNT}
603

    
604
		# Set NanoBSD image size
605
		echo "${_NANO_MEDIASIZE}" > ${MNT}/etc/nanosize.txt
606

    
607
		echo "/dev/ufs/${_label}0 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
608
		if [ $NANO_CONFSIZE -gt 0 ] ; then
609
			echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
610
		fi
611

    
612
		umount ${MNT}
613
		# Restore the original trap
614
		trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
615

    
616
		# Setting NANO_IMAGES to 1 and NANO_INIT_IMG2 will tell
617
		# NanoBSD to only create one partition.  We default to 2
618
		# partitions in case anything happens to the first the
619
		# operator can boot from the 2nd and should be OK.
620

    
621
		# Before just going to use dd for duplicate think!
622
		# The images are created as sparse so lets take advantage
623
		# of that by just exec some commands.
624
		if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
625
			# Duplicate to second image (if present)
626
			echo ">>> Creating NanoBSD second slice by duplicating first slice." | tee -a ${LOGFILE}
627
			# Create second image
628
			dd if=/dev/${MD}s1 of=/dev/${MD}s2 conv=sparse bs=64k 2>&1 >> ${LOGFILE}
629
			tunefs -L ${_label}1 /dev/${MD}s2a 2>&1 >> ${LOGFILE}
630
			mount /dev/ufs/${_label}1 ${MNT}
631
			if [ $? -ne 0 ]; then
632
				echo ">>> ERROR: Something wrong happened during mount of second slice image creation. STOPPING!" | tee -a ${LOGFILE}
633
				print_error_pfS
634
			fi
635
			# Consider the unmounting as well
636
			trap "umount /dev/ufs/${_label}1; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
637

    
638
			echo "/dev/ufs/${_label}1 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
639
			if [ $NANO_CONFSIZE -gt 0 ] ; then
640
				echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
641
			fi
642

    
643
			umount ${MNT}
644
			# Restore the trap back
645
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
646
		fi
647

    
648
		# Create Data slice, if any.
649
		# Note the changing of the variable to NANO_CONFSIZE
650
		# from NANO_DATASIZE.  We also added glabel support
651
		# and populate the Product configuration from the /cf
652
		# directory located in FINAL_CHROOT_DIR
653
		if [ $NANO_CONFSIZE -gt 0 ] ; then
654
			echo ">>> Creating /cf area to hold config.xml"
655
			newfs -L cf ${NANO_NEWFS} /dev/${MD}s3 2>&1 >> ${LOGFILE}
656
			# Mount data partition and copy contents of /cf
657
			# Can be used later to create custom default config.xml while building
658
			mount /dev/ufs/cf ${MNT}
659
			if [ $? -ne 0 ]; then
660
				echo ">>> ERROR: Something wrong happened during mount of cf slice image creation. STOPPING!" | tee -a ${LOGFILE}
661
				print_error_pfS
662
			fi
663
			# Consider the unmounting as well
664
			trap "umount /dev/ufs/cf; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
665

    
666
			clone_directory_contents ${FINAL_CHROOT_DIR}/cf ${MNT}
667

    
668
			umount ${MNT}
669
			# Restore the trap back
670
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
671
		else
672
			">>> [nanoo] NANO_CONFSIZE is not set. Not adding a /conf partition.. You sure about this??" | tee -a ${LOGFILE}
673
		fi
674

    
675
		mdconfig -d -u $MD
676
		# Restore default action
677
		trap "-" 1 2 15 EXIT
678

    
679
		# Check each image and ensure that they are over
680
		# 3 megabytes.  If either image is under 20 megabytes
681
		# in size then error out.
682
		IMGSIZE=$(stat -f "%z" ${IMG})
683
		CHECKSIZE="20040710"
684
		if [ "$IMGSIZE" -lt "$CHECKSIZE" ]; then
685
			echo ">>> ERROR: Something went wrong when building NanoBSD.  The image size is under 20 megabytes!" | tee -a ${LOGFILE}
686
			print_error_pfS
687
		fi
688

    
689
		# Wrap up the show, Johnny
690
		echo ">>> NanoBSD Image completed for size: $_NANO_MEDIASIZE." | tee -a ${LOGFILE}
691

    
692
		gzip -qf $IMG &
693
		_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
694
	done
695

    
696
	unset IMG
697
	unset IMGSIZE
698
}
699

    
700
# This routine creates a ova image that contains
701
# a ovf and vmdk file. These files can be imported
702
# right into vmware or virtual box.
703
# (and many other emulation platforms)
704
# http://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
705
create_ova_image() {
706
	# XXX create a .ovf php creator that you can pass:
707
	#     1. populatedSize
708
	#     2. license
709
	#     3. product name
710
	#     4. version
711
	#     5. number of network interface cards
712
	#     6. allocationUnits
713
	#     7. capacity
714
	#     8. capacityAllocationUnits
715

    
716
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
717

    
718
	[ -d "${OVA_TMP}" ] \
719
		&& rm -rf ${OVA_TMP}
720

    
721
	mkdir -p $(dirname ${OVAPATH})
722

    
723
	local _mntdir=${OVA_TMP}/mnt
724
	mkdir -p ${_mntdir}
725

    
726
	if [ -z "${OVA_SWAP_PART_SIZE_IN_GB}" -o "${OVA_SWAP_PART_SIZE_IN_GB}" = "0" ]; then
727
		# first partition size (freebsd-ufs)
728
		local OVA_FIRST_PART_SIZE_IN_GB=${VMDK_DISK_CAPACITY_IN_GB}
729
		# Calculate real first partition size, removing 128 blocks (65536 bytes) beginning/loader
730
		local OVA_FIRST_PART_SIZE=$((${OVA_FIRST_PART_SIZE_IN_GB}*1024*1024*1024-65536))
731
		# Unset swap partition size variable
732
		unset OVA_SWAP_PART_SIZE
733
		# Parameter used by mkimg
734
		unset OVA_SWAP_PART_PARAM
735
	else
736
		# first partition size (freebsd-ufs)
737
		local OVA_FIRST_PART_SIZE_IN_GB=$((VMDK_DISK_CAPACITY_IN_GB-OVA_SWAP_PART_SIZE_IN_GB))
738
		# Use first partition size in g
739
		local OVA_FIRST_PART_SIZE="${OVA_FIRST_PART_SIZE_IN_GB}g"
740
		# Calculate real swap size, removing 128 blocks (65536 bytes) beginning/loader
741
		local OVA_SWAP_PART_SIZE=$((${OVA_SWAP_PART_SIZE_IN_GB}*1024*1024*1024-65536))
742
		# Parameter used by mkimg
743
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
744
	fi
745

    
746
	# Prepare folder to be put in image
747
	customize_stagearea_for_image "ova"
748
	install_default_kernel ${DEFAULT_KERNEL} "no"
749

    
750
	# Fill fstab
751
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
752
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
753
	if [ -n "${OVA_SWAP_PART_SIZE}" ]; then
754
		echo "/dev/gpt/swap0	none	swap	sw	0	0" >> ${FINAL_CHROOT_DIR}/etc/fstab
755
	fi
756

    
757
	# Create / partition
758
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
759
	truncate -s ${OVA_FIRST_PART_SIZE} ${OVA_TMP}/${OVFUFS}
760
	local _md=$(mdconfig -a -f ${OVA_TMP}/${OVAUFS})
761
	trap "mdconfig -d -u ${_md}; return" 1 2 15 EXIT
762

    
763
	newfs -L ${PRODUCT_NAME} -j /dev/${_md} 2>&1 >>${LOGFILE}
764

    
765
	if ! mount /dev/${_md} ${_mntdir} 2>&1 >>${LOGFILE}; then
766
		echo "Failed!" | tee -a ${LOGFILE}
767
		echo ">>> ERROR: Error mounting temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
768
		print_error_pfS
769
	fi
770
	trap "umount ${_mntdir}; mdconfig -d -u ${_md}; return" 1 2 15 EXIT
771

    
772
	echo "Done!" | tee -a ${LOGFILE}
773

    
774
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
775

    
776
	sync
777
	umount ${_mntdir} 2>&1 >>${LOGFILE}
778
	mdconfig -d -u ${_md}
779
	trap "-" 1 2 15 EXIT
780

    
781
	# Create raw disk
782
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
783
	mkimg \
784
		-s gpt \
785
		-f raw \
786
		-b ${FINAL_CHROOT_DIR}/boot/pmbr \
787
		-p freebsd-boot:=${FINAL_CHROOT_DIR}/boot/gptboot \
788
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
789
		${OVA_SWAP_PART_PARAM} \
790
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
791

    
792
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
793
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
794
			rm -f ${OVA_TMP}/${OVFUFS}
795
		fi
796
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
797
			rm -f ${OVA_TMP}/${OVFRAW}
798
		fi
799
		echo "Failed!" | tee -a ${LOGFILE}
800
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
801
		print_error_pfS
802
	fi
803
	echo "Done!" | tee -a ${LOGFILE}
804

    
805
	# We don't need it anymore
806
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
807

    
808
	# Convert raw to vmdk
809
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
810
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
811

    
812
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
813
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
814
			rm -f ${OVA_TMP}/${OVFRAW}
815
		fi
816
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
817
			rm -f ${OVA_TMP}/${OVFVMDK}
818
		fi
819
		echo "Failed!" | tee -a ${LOGFILE}
820
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
821
		print_error_pfS
822
	fi
823
	echo "Done!" | tee -a ${LOGFILE}
824

    
825
	rm -f ${OVA_TMP}/i${OVFRAW}
826

    
827
	ova_setup_ovf_template
828

    
829
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
830
	# Create OVA file for vmware
831
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
832
	echo "Done!" | tee -a ${LOGFILE}
833
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
834

    
835
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
836
}
837

    
838
# called from create_ova_image
839
ova_setup_ovf_template() {
840
	if [ ! -f ${OVFTEMPLATE} ]; then
841
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
842
		print_error_pfS
843
	fi
844

    
845
	#  OperatingSystemSection (${PRODUCT_NAME}.ovf)
846
	#  42   FreeBSD 32-Bit
847
	#  78   FreeBSD 64-Bit
848
	if [ "${TARGET}" = "amd64" ]; then
849
		local _os_id="78"
850
		local _os_type="freebsd64Guest"
851
		local _os_descr="FreeBSD 64-Bit"
852
	else
853
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
854
		print_error_pfS
855
	fi
856

    
857
	local POPULATED_SIZE=$(du -d0 -k $FINAL_CHROOT_DIR | cut -f1)
858
	local POPULATED_SIZE_IN_BYTES=$((${POPULATED_SIZE}*1024))
859
	local VMDK_FILE_SIZE=$(stat -f "%z" ${OVA_TMP}/${OVFVMDK})
860

    
861
	sed \
862
		-e "s,%%VMDK_FILE_SIZE%%,${VMDK_FILE_SIZE},g" \
863
		-e "s,%%VMDK_DISK_CAPACITY_IN_GB%%,${VMDK_DISK_CAPACITY_IN_GB},g" \
864
		-e "s,%%POPULATED_SIZE_IN_BYTES%%,${POPULATED_SIZE_IN_BYTES},g" \
865
		-e "s,%%OS_ID%%,${_os_id},g" \
866
		-e "s,%%OS_TYPE%%,${_os_type},g" \
867
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
868
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
869
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
870
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
871
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
872
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
873
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
874
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/license.txt" \
875
		-e "/^%%PRODUCT_LICENSE%%/d" \
876
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
877
}
878

    
879
# Cleans up previous builds
880
clean_builder() {
881
	# Clean out directories
882
	echo ">>> Cleaning up previous build environment...Please wait!"
883

    
884
	staginareas_clean_each_run
885

    
886
	if [ -d "${STAGE_CHROOT_DIR}" ]; then
887
		BASENAME=$(basename ${STAGE_CHROOT_DIR})
888
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR} ..."
889
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
890
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
891
		echo "Done."
892
	fi
893

    
894
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
895
		OBJTREE=$(make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
896
		if [ -d "${OBJTREE}" ]; then
897
			echo -n ">>> Cleaning FreeBSD objects dir staging..."
898
			echo -n "."
899
			chflags -R noschg ${OBJTREE} 2>&1 >/dev/null
900
			echo -n "."
901
			rm -rf ${OBJTREE}/*
902
			echo "Done!"
903
		fi
904
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
905
			echo -n ">>> Cleaning previously built kernel stage area..."
906
			rm -rf $KERNEL_BUILD_PATH/*
907
			echo "Done!"
908
		fi
909
	fi
910
	mkdir -p $KERNEL_BUILD_PATH
911

    
912
	echo -n ">>> Cleaning previously built images..."
913
	rm -rf $IMAGES_FINAL_DIR/*
914
	echo "Done!"
915

    
916
	if [ -z "${NO_CLEAN_FREEBSD_SRC}" ]; then
917
		if [ -d "$FREEBSD_SRC_DIR" ]; then
918
			echo -n ">>> Ensuring $FREEBSD_SRC_DIR is clean..."
919
			rm -rf ${FREEBSD_SRC_DIR}
920
			echo "Done!"
921
		fi
922
	fi
923

    
924
	echo -n ">>> Cleaning previous builder logs..."
925
	if [ -d "$BUILDER_LOGS" ]; then
926
		rm -rf ${BUILDER_LOGS}
927
	fi
928
	mkdir -p ${BUILDER_LOGS}
929

    
930
	echo "Done!"
931

    
932
	echo ">>> Cleaning of builder environment has finished."
933
}
934

    
935
clone_directory_contents() {
936
	if [ ! -e "$2" ]; then
937
		mkdir -p "$2"
938
	fi
939
	if [ ! -d "$1" -o ! -d "$2" ]; then
940
		if [ -z "${LOGFILE}" ]; then
941
			echo ">>> ERROR: Argument $1 supplied is not a directory!"
942
		else
943
			echo ">>> ERROR: Argument $1 supplied is not a directory!" | tee -a ${LOGFILE}
944
		fi
945
		print_error_pfS
946
	fi
947
	echo -n ">>> Using TAR to clone $1 to $2 ..."
948
	tar -C ${1} -c -f - . | tar -C ${2} -x -p -f -
949
	echo "Done!"
950
}
951

    
952
clone_to_staging_area() {
953
	# Clone everything to the final staging area
954
	echo -n ">>> Cloning everything to ${STAGE_CHROOT_DIR} staging area..."
955
	LOGFILE=${BUILDER_LOGS}/cloning.${TARGET}.log
956

    
957
	tar -C ${PRODUCT_SRC} -c -f - . | \
958
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
959

    
960
	if [ "${PRODUCT_NAME}" != "pfSense" ]; then
961
		mv ${STAGE_CHROOT_DIR}/usr/local/sbin/pfSense-upgrade \
962
			${STAGE_CHROOT_DIR}/usr/local/sbin/${PRODUCT_NAME}-upgrade
963
	fi
964

    
965
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
966
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
967
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
968
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
969
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
970
	fi
971

    
972
	## Add buildtime and lastcommit information
973
	# This is used for detecting updates.
974
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
975
	# Record last commit info if it is available.
976
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
977
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
978
	fi
979

    
980
	local _exclude_files="${CORE_PKG_TMP}/base_exclude_files"
981
	sed \
982
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
983
		-e "s,%%VERSION%%,${_version},g" \
984
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
985
		> ${_exclude_files}
986

    
987
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
988

    
989
	# Include a sample pkg stable conf to base
990
	setup_pkg_repo \
991
		${PKG_REPO_DEFAULT} \
992
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
993
		${TARGET} \
994
		${TARGET_ARCH}
995

    
996
	mtree \
997
		-c \
998
		-k uid,gid,mode,size,flags,sha256digest \
999
		-p ${STAGE_CHROOT_DIR} \
1000
		-X ${_exclude_files} \
1001
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
1002
	tar \
1003
		-C ${STAGE_CHROOT_DIR} \
1004
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
1005
		-X ${_exclude_files} \
1006
		.
1007

    
1008
	local _share_repos_path="${SCRATCHDIR}/repo-tmp/${PRODUCT_SHARE_DIR}/pkg/repos"
1009
	rm -rf ${SCRATCHDIR}/repo-tmp >/dev/null 2>&1
1010
	mkdir -p ${_share_repos_path} >/dev/null 2>&1
1011

    
1012
	setup_pkg_repo \
1013
		${PKG_REPO_DEFAULT} \
1014
		${_share_repos_path}/${PRODUCT_NAME}-repo.conf \
1015
		${TARGET} \
1016
		${TARGET_ARCH}
1017

    
1018
	cp -f ${PKG_REPO_DEFAULT%%.conf}.descr ${_share_repos_path}
1019

    
1020
	# Add additional repos
1021
	for _template in ${PKG_REPO_BASE}/${PRODUCT_NAME}-repo-*.conf; do
1022
		_template_filename=$(basename ${_template})
1023
		setup_pkg_repo \
1024
			${_template} \
1025
			${_share_repos_path}/${_template_filename} \
1026
			${TARGET} \
1027
			${TARGET_ARCH}
1028
		cp -f ${_template%%.conf}.descr ${_share_repos_path}
1029
	done
1030

    
1031
	core_pkg_create repo "" ${CORE_PKG_VERSION} ${SCRATCHDIR}/repo-tmp
1032

    
1033
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1034
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1035
	core_pkg_create base-nanobsd "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1036
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1037

    
1038
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
1039

    
1040
	# Save current WAN and LAN if value
1041
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
1042
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
1043

    
1044
	# Change default interface names to match vmware driver
1045
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
1046
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
1047
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1048

    
1049
	# Restore default values to be used by serial package
1050
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
1051
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
1052

    
1053
	# Activate serial console in config.xml
1054
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
1055
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
1056
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
1057
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
1058
	rm -f ${DEFAULTCONF}.tmp
1059

    
1060
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1061

    
1062
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1063

    
1064
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1065
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
1066

    
1067
	# Make sure pkg is present
1068
	pkg_bootstrap ${STAGE_CHROOT_DIR}
1069

    
1070
	echo "Done!"
1071
}
1072

    
1073
create_final_staging_area() {
1074
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
1075
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
1076
		print_error_pfS
1077
	fi
1078

    
1079
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1080
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
1081
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
1082
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
1083
		echo "Done." | tee -a ${LOGFILE}
1084
	fi
1085

    
1086
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
1087
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
1088
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
1089

    
1090
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
1091
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
1092
		print_error_pfS
1093
	fi
1094
}
1095

    
1096
customize_stagearea_for_image() {
1097
	local _image_type="$1"
1098
	local _default_config="" # filled with $2 below
1099
	local _image_variant="$3"
1100

    
1101
	if [ -n "$2" ]; then
1102
		_default_config="$2"
1103
	elif [ "${_image_type}" = "nanobsd" -o \
1104
	     "${_image_type}" = "memstickserial" -o \
1105
	     "${_image_type}" = "memstickadi" ]; then
1106
		_default_config="default-config-serial"
1107
	elif [ "${_image_type}" = "ova" ]; then
1108
		_default_config="default-config-vmware"
1109
	else
1110
		_default_config="default-config"
1111
	fi
1112

    
1113
	# Prepare final stage area
1114
	create_final_staging_area
1115

    
1116
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
1117
	pkg_chroot_add ${FINAL_CHROOT_DIR} repo
1118

    
1119
	if [ "${_image_type}" = "nanobsd" -o \
1120
	     "${_image_type}" = "nanobsd-vga" ]; then
1121

    
1122
		mkdir -p ${FINAL_CHROOT_DIR}/root/var/db \
1123
			 ${FINAL_CHROOT_DIR}/root/var/cache \
1124
			 ${FINAL_CHROOT_DIR}/var/db/pkg \
1125
			 ${FINAL_CHROOT_DIR}/var/cache/pkg
1126
		mv -f ${FINAL_CHROOT_DIR}/var/db/pkg ${FINAL_CHROOT_DIR}/root/var/db
1127
		mv -f ${FINAL_CHROOT_DIR}/var/cache/pkg ${FINAL_CHROOT_DIR}/root/var/cache
1128
		ln -sf ../../root/var/db/pkg ${FINAL_CHROOT_DIR}/var/db/pkg
1129
		ln -sf ../../root/var/cache/pkg ${FINAL_CHROOT_DIR}/var/cache/pkg
1130

    
1131
		pkg_chroot_add ${FINAL_CHROOT_DIR} base-nanobsd
1132
	else
1133
		pkg_chroot_add ${FINAL_CHROOT_DIR} base
1134
	fi
1135

    
1136
	if [ "${_image_type}" = "iso" -o \
1137
	     "${_image_type}" = "memstick" -o \
1138
	     "${_image_type}" = "memstickserial" -o \
1139
	     "${_image_type}" = "memstickadi" ]; then
1140
		install_bsdinstaller
1141
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
1142
		cp ${CORE_PKG_ALL_PATH}/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
1143
	fi
1144

    
1145
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
1146

    
1147
	# XXX: Workaround to avoid pkg to complain regarding release
1148
	#      repo on first boot since packages are installed from
1149
	#      staging server during build phase
1150
	if [ -n "${USE_PKG_REPO_STAGING}" ]; then
1151
		_read_cmd="select value from repodata where key='packagesite'"
1152
		if [ -n "${_IS_RELEASE}" ]; then
1153
			local _tgt_server="${PKG_REPO_SERVER_RELEASE}"
1154
		else
1155
			local _tgt_server="${PKG_REPO_SERVER_DEVEL}"
1156
		fi
1157
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
1158
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
1159
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER_STAGING},${_tgt_server},")
1160
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
1161
		done
1162
	fi
1163

    
1164
	if [ -n "$_image_variant" -a \
1165
	    -d ${BUILDER_TOOLS}/templates/custom_logos/${_image_variant} ]; then
1166
		mkdir -p ${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
1167
		cp -f \
1168
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.png \
1169
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
1170
	fi
1171
}
1172

    
1173
create_distribution_tarball() {
1174
	mkdir -p ${FINAL_CHROOT_DIR}/install
1175

    
1176
	tar -C ${FINAL_CHROOT_DIR} --exclude ./install --exclude ./pkgs -cJf ${FINAL_CHROOT_DIR}/install/${PRODUCT_NAME}.txz .
1177
}
1178

    
1179
create_iso_image() {
1180
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
1181
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
1182
	if [ -z "${DEFAULT_KERNEL}" ]; then
1183
		echo ">>> ERROR: Could not identify DEFAULT_KERNEL to install on image!" | tee -a ${LOGFILE}
1184
		print_error_pfS
1185
	fi
1186

    
1187
	mkdir -p $(dirname ${ISOPATH})
1188

    
1189
	customize_stagearea_for_image "iso"
1190
	install_default_kernel ${DEFAULT_KERNEL}
1191

    
1192
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1193

    
1194
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
1195
	echo "/dev/iso9660/${FSLABEL} / cd9660 ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1196

    
1197
	# This check is for supporting create memstick/ova images
1198
	echo -n ">>> Running command: script -aq $LOGFILE makefs -t cd9660 -o bootimage=\"i386;${FINAL_CHROOT_DIR}/boot/cdboot \"-o no-emul-boot -o rockridge " | tee -a ${LOGFILE}
1199
	echo "-o label=${FSLABEL} -o publisher=\"${PRODUCT_NAME} project.\" $ISOPATH ${FINAL_CHROOT_DIR}" | tee -a ${LOGFILE}
1200

    
1201
	create_distribution_tarball
1202

    
1203
	# Remove /rescue from iso since cd9660 cannot deal with hardlinks
1204
	rm -rf ${FINAL_CHROOT_DIR}/rescue
1205

    
1206
	makefs -t cd9660 -o bootimage="i386;${FINAL_CHROOT_DIR}/boot/cdboot" -o no-emul-boot -o rockridge \
1207
		-o label=${FSLABEL} -o publisher="${PRODUCT_NAME} project." $ISOPATH ${FINAL_CHROOT_DIR} 2>&1 >> ${LOGFILE}
1208
	if [ $? -ne 0 -o ! -f $ISOPATH ]; then
1209
		if [ -f ${ISOPATH} ]; then
1210
			rm -f $ISOPATH
1211
		fi
1212
		echo ">>> ERROR: Something wrong happened during ISO image creation. STOPPING!" | tee -a ${LOGFILE}
1213
		print_error_pfS
1214
	fi
1215
	gzip -qf $ISOPATH &
1216
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1217

    
1218
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1219
}
1220

    
1221
create_memstick_image() {
1222
	local _variant="$1"
1223

    
1224
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
1225
	if [ "${MEMSTICKPATH}" = "" ]; then
1226
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1227
		return
1228
	fi
1229

    
1230
	mkdir -p $(dirname ${MEMSTICKPATH})
1231

    
1232
	local _image_path=${MEMSTICKPATH}
1233
	if [ -n "${_variant}" ]; then
1234
		_image_path=$(echo "$_image_path" | \
1235
			sed "s/-memstick-/-memstick-${_variant}-/")
1236
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
1237
	fi
1238

    
1239
	customize_stagearea_for_image "memstick" "" $_variant
1240
	install_default_kernel ${DEFAULT_KERNEL}
1241

    
1242
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1243

    
1244
	echo ">>> Creating memstick to ${_image_path}." 2>&1 | tee -a ${LOGFILE}
1245
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1246
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1247

    
1248
	create_distribution_tarball
1249

    
1250
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${_image_path} ${FINAL_CHROOT_DIR}
1251
	if [ $? -ne 0 ]; then
1252
		if [ -f ${_image_path} ]; then
1253
			rm -f $_image_path
1254
		fi
1255
		echo ">>> ERROR: Something wrong happened during MEMSTICK image creation. STOPPING!" | tee -a ${LOGFILE}
1256
		print_error_pfS
1257
	fi
1258
	MD=$(mdconfig -a -t vnode -f $_image_path)
1259
	# Just in case
1260
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1261
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1262
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1263
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1264
	trap "-" 1 2 15 EXIT
1265
	mdconfig -d -u ${MD} 2>&1 | tee -a ${LOGFILE}
1266
	gzip -qf $_image_path &
1267
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1268

    
1269
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1270
}
1271

    
1272
create_memstick_serial_image() {
1273
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
1274
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
1275
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1276
		return
1277
	fi
1278

    
1279
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
1280

    
1281
	customize_stagearea_for_image "memstickserial"
1282
	install_default_kernel ${DEFAULT_KERNEL}
1283

    
1284
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1285

    
1286
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1287
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1288

    
1289
	echo ">>> Creating serial memstick to ${MEMSTICKSERIALPATH}." 2>&1 | tee -a ${LOGFILE}
1290

    
1291
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1292
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1293

    
1294
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
1295
	# Activate serial console in boot.config
1296
	if [ -f ${BOOTCONF} ]; then
1297
		sed -i "" '/-D$/d' ${BOOTCONF}
1298
	fi
1299
	echo "-S115200 -D" >> ${BOOTCONF}
1300

    
1301
	# Remove old console options if present.
1302
	[ -f "${LOADERCONF}" ] \
1303
		&& sed -i "" -Ee "/(console|boot_multicons|boot_serial)/d" ${LOADERCONF}
1304
	# Activate serial console+video console in loader.conf
1305
	echo 'boot_multicons="YES"' >>  ${LOADERCONF}
1306
	echo 'boot_serial="YES"' >> ${LOADERCONF}
1307
	echo 'console="comconsole,vidconsole"' >> ${LOADERCONF}
1308
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
1309

    
1310
	create_distribution_tarball
1311

    
1312
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${MEMSTICKSERIALPATH} ${FINAL_CHROOT_DIR}
1313
	if [ $? -ne 0 ]; then
1314
		if [ -f ${MEMSTICKSERIALPATH} ]; then
1315
			rm -f $MEMSTICKSERIALPATH
1316
		fi
1317
		echo ">>> ERROR: Something wrong happened during MEMSTICKSERIAL image creation. STOPPING!" | tee -a ${LOGFILE}
1318
		print_error_pfS
1319
	fi
1320
	MD=$(mdconfig -a -t vnode -f $MEMSTICKSERIALPATH)
1321
	# Just in case
1322
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1323
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1324
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1325
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1326
	trap "-" 1 2 15 EXIT
1327
	mdconfig -d -u ${MD} 2>&1 >> ${LOGFILE}
1328
	gzip -qf $MEMSTICKSERIALPATH &
1329
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1330

    
1331
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1332
}
1333

    
1334
create_memstick_adi_image() {
1335
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
1336
	if [ "${MEMSTICKADIPATH}" = "" ]; then
1337
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1338
		return
1339
	fi
1340

    
1341
	mkdir -p $(dirname ${MEMSTICKADIPATH})
1342

    
1343
	customize_stagearea_for_image "memstickadi"
1344
	install_default_kernel ${DEFAULT_KERNEL}
1345

    
1346
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1347

    
1348
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1349
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1350

    
1351
	echo ">>> Creating serial memstick to ${MEMSTICKADIPATH}." 2>&1 | tee -a ${LOGFILE}
1352

    
1353
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1354
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1355

    
1356
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
1357
	# Activate serial console in boot.config
1358
	if [ -f ${BOOTCONF} ]; then
1359
		sed -i "" '/-[Dh]$/d' ${BOOTCONF}
1360
	fi
1361
	echo "-S115200 -h" >> ${BOOTCONF}
1362

    
1363
	# Remove old console options if present.
1364
	[ -f "${LOADERCONF}" ] \
1365
		&& sed -i "" -Ee "/(console|boot_multicons|boot_serial|hint.uart)/d" ${LOADERCONF}
1366
	# Activate serial console+video console in loader.conf
1367
	echo 'boot_serial="YES"' >> ${LOADERCONF}
1368
	echo 'console="comconsole"' >> ${LOADERCONF}
1369
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
1370
	echo 'comconsole_port="0x2F8"' >> ${LOADERCONF}
1371
	echo 'hint.uart.0.flags="0x00"' >> ${LOADERCONF}
1372
	echo 'hint.uart.1.flags="0x10"' >> ${LOADERCONF}
1373

    
1374
	create_distribution_tarball
1375

    
1376
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${MEMSTICKADIPATH} ${FINAL_CHROOT_DIR}
1377
	if [ $? -ne 0 ]; then
1378
		if [ -f ${MEMSTICKADIPATH} ]; then
1379
			rm -f $MEMSTICKADIPATH
1380
		fi
1381
		echo ">>> ERROR: Something wrong happened during MEMSTICKADI image creation. STOPPING!" | tee -a ${LOGFILE}
1382
		print_error_pfS
1383
	fi
1384
	MD=$(mdconfig -a -t vnode -f $MEMSTICKADIPATH)
1385
	# Just in case
1386
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1387
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1388
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1389
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1390
	trap "-" 1 2 15 EXIT
1391
	mdconfig -d -u ${MD} 2>&1 >> ${LOGFILE}
1392
	gzip -qf $MEMSTICKADIPATH &
1393
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1394

    
1395
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1396
}
1397

    
1398
# Create pkg conf on desired place with desired arch/branch
1399
setup_pkg_repo() {
1400
	if [ -z "${4}" ]; then
1401
		return
1402
	fi
1403

    
1404
	local _template="${1}"
1405
	local _target="${2}"
1406
	local _arch="${3}"
1407
	local _target_arch="${4}"
1408
	local _staging="${5}"
1409

    
1410
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
1411
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
1412
		print_error_pfS
1413
	fi
1414

    
1415
	if [ -n "${_staging}" -a -n "${USE_PKG_REPO_STAGING}" ]; then
1416
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_STAGING}
1417
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_STAGING}
1418
		local _pkg_repo_server_release=${PKG_REPO_SERVER_STAGING}
1419
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_STAGING}
1420
	else
1421
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_DEVEL}
1422
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_DEVEL}
1423
		local _pkg_repo_server_release=${PKG_REPO_SERVER_RELEASE}
1424
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_RELEASE}
1425
	fi
1426

    
1427
	mkdir -p $(dirname ${_target}) >/dev/null 2>&1
1428

    
1429
	sed \
1430
		-e "s/%%ARCH%%/${_target_arch}/" \
1431
		-e "s/%%PKG_REPO_BRANCH_DEVEL%%/${_pkg_repo_branch_devel}/g" \
1432
		-e "s/%%PKG_REPO_BRANCH_RELEASE%%/${_pkg_repo_branch_release}/g" \
1433
		-e "s,%%PKG_REPO_SERVER_DEVEL%%,${_pkg_repo_server_devel},g" \
1434
		-e "s,%%PKG_REPO_SERVER_RELEASE%%,${_pkg_repo_server_release},g" \
1435
		-e "s,%%POUDRIERE_PORTS_NAME%%,${POUDRIERE_PORTS_NAME},g" \
1436
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
1437
		${_template} \
1438
		> ${_target}
1439
}
1440

    
1441
# This routine ensures any ports / binaries that the builder
1442
# system needs are on disk and ready for execution.
1443
builder_setup() {
1444
	# If Product-builder is already installed, just leave
1445
	if pkg info -e -q ${PRODUCT_NAME}-builder; then
1446
		return
1447
	fi
1448

    
1449
	if [ ! -f ${PKG_REPO_PATH} ]; then
1450
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
1451
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
1452

    
1453
		update_freebsd_sources
1454

    
1455
		local _arch=$(uname -m)
1456
		setup_pkg_repo \
1457
			${PKG_REPO_DEFAULT} \
1458
			${PKG_REPO_PATH} \
1459
			${_arch} \
1460
			${_arch} \
1461
			"staging"
1462

    
1463
		# Use fingerprint keys from repo
1464
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
1465
			${PKG_REPO_PATH}
1466
	fi
1467

    
1468
	pkg install ${PRODUCT_NAME}-builder
1469
}
1470

    
1471
# Updates FreeBSD sources
1472
update_freebsd_sources() {
1473
	if [ "${1}" = "full" ]; then
1474
		local _full=1
1475
		local _clone_params=""
1476
	else
1477
		local _full=0
1478
		local _clone_params="--depth 1 --single-branch"
1479
	fi
1480

    
1481
	if [ ! -d "${FREEBSD_SRC_DIR}" ]; then
1482
		mkdir -p ${FREEBSD_SRC_DIR}
1483
	fi
1484

    
1485
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
1486
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
1487
		return
1488
	fi
1489

    
1490
	echo -n ">>> Obtaining FreeBSD sources ${FREEBSD_BRANCH}..."
1491
	local _FREEBSD_BRANCH=${FREEBSD_BRANCH:-"devel"}
1492
	local _CLONE=1
1493

    
1494
	if [ -d "${FREEBSD_SRC_DIR}/.git" ]; then
1495
		CUR_BRANCH=$(cd ${FREEBSD_SRC_DIR} && git branch | grep '^\*' | cut -d' ' -f2)
1496
		if [ ${_full} -eq 0 -a "${CUR_BRANCH}" = "${_FREEBSD_BRANCH}" ]; then
1497
			_CLONE=0
1498
			( cd ${FREEBSD_SRC_DIR} && git clean -fd; git fetch origin; git reset --hard origin/${_FREEBSD_BRANCH} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1499
		else
1500
			rm -rf ${FREEBSD_SRC_DIR}
1501
		fi
1502
	fi
1503

    
1504
	if [ ${_CLONE} -eq 1 ]; then
1505
		( git clone --branch ${_FREEBSD_BRANCH} ${_clone_params} ${FREEBSD_REPO_BASE} ${FREEBSD_SRC_DIR} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1506
	fi
1507

    
1508
	if [ ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1509
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1510
		print_error_pfS
1511
	fi
1512

    
1513
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1514
		( cd ${FREEBSD_SRC_DIR} && git checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1515
	fi
1516
	echo "Done!"
1517
}
1518

    
1519
pkg_chroot() {
1520
	local _root="${1}"
1521
	shift
1522

    
1523
	if [ $# -eq 0 ]; then
1524
		return -1
1525
	fi
1526

    
1527
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1528
		return -1
1529
	fi
1530

    
1531
	mkdir -p \
1532
		${SCRATCHDIR}/pkg_cache \
1533
		${_root}/var/cache/pkg \
1534
		${_root}/dev
1535

    
1536
	/sbin/mount -t nullfs ${SCRATCHDIR}/pkg_cache ${_root}/var/cache/pkg
1537
	/sbin/mount -t devfs devfs ${_root}/dev
1538
	cp -f /etc/resolv.conf ${_root}/etc/resolv.conf
1539
	touch ${BUILDER_LOGS}/install_pkg_install_ports.txt
1540
	script -aq ${BUILDER_LOGS}/install_pkg_install_ports.txt pkg -c ${_root} $@ >/dev/null 2>&1
1541
	local result=$?
1542
	rm -f ${_root}/etc/resolv.conf
1543
	/sbin/umount -f ${_root}/dev
1544
	/sbin/umount -f ${_root}/var/cache/pkg
1545

    
1546
	return $result
1547
}
1548

    
1549

    
1550
pkg_chroot_add() {
1551
	if [ -z "${1}" -o -z "${2}" ]; then
1552
		return 1
1553
	fi
1554

    
1555
	local _target="${1}"
1556
	local _pkg="$(get_pkg_name ${2}).txz"
1557

    
1558
	if [ ! -d "${_target}" ]; then
1559
		echo ">>> ERROR: Target dir ${_target} not found"
1560
		print_error_pfS
1561
	fi
1562

    
1563
	if [ ! -f ${CORE_PKG_ALL_PATH}/${_pkg} ]; then
1564
		echo ">>> ERROR: Package ${_pkg} not found"
1565
		print_error_pfS
1566
	fi
1567

    
1568
	cp ${CORE_PKG_ALL_PATH}/${_pkg} ${_target}
1569
	pkg_chroot ${_target} add /${_pkg}
1570
	rm -f ${_target}/${_pkg}
1571
}
1572

    
1573
pkg_bootstrap() {
1574
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1575

    
1576
	setup_pkg_repo \
1577
		${PKG_REPO_DEFAULT} \
1578
		${_root}${PKG_REPO_PATH} \
1579
		${TARGET} \
1580
		${TARGET_ARCH} \
1581
		"staging"
1582

    
1583
	pkg_chroot ${_root} bootstrap -f
1584
}
1585

    
1586
# This routine assists with installing various
1587
# freebsd ports files into the pfsense-fs staging
1588
# area.
1589
install_pkg_install_ports() {
1590
	local MAIN_PKG="${1}"
1591

    
1592
	if [ -z "${MAIN_PKG}" ]; then
1593
		MAIN_PKG=${PRODUCT_NAME}
1594
	fi
1595

    
1596
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1597

    
1598
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1599
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1600

    
1601
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1602
		mkdir -p ${SCRATCHDIR}/pkg_cache
1603

    
1604
	echo -n ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... "
1605
	# First mark all packages as automatically installed
1606
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 1 -a
1607
	# Install all necessary packages
1608
	if ! pkg_chroot ${STAGE_CHROOT_DIR} install ${MAIN_PKG} ${custom_package_list}; then
1609
		echo "Failed!"
1610
		print_error_pfS
1611
	fi
1612
	# Make sure required packages are set as non-automatic
1613
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 0 pkg ${MAIN_PKG} ${custom_package_list}
1614
	# Remove unnecessary packages
1615
	pkg_chroot ${STAGE_CHROOT_DIR} autoremove
1616
	echo "Done!"
1617
}
1618

    
1619
install_bsdinstaller() {
1620
	local _params=""
1621

    
1622
	# Use staging repo on RELEASE
1623
	if [ -n "${_IS_RELEASE}" ]; then
1624
		mkdir -p ${FINAL_CHROOT_DIR}/tmp/pkg-repo
1625
		cp -f ${STAGE_CHROOT_DIR}${PKG_REPO_PATH} \
1626
			${FINAL_CHROOT_DIR}/tmp/pkg-repo
1627
		_params="--repo-conf-dir /tmp/pkg-repo "
1628
	fi
1629

    
1630
	echo ">>> Installing BSDInstaller in chroot (${FINAL_CHROOT_DIR})... (starting)"
1631
	pkg_chroot ${FINAL_CHROOT_DIR} ${_params}install -f bsdinstaller
1632
	sed -i '' -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME}," \
1633
		  -e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION}," \
1634
		  -e "s,%%ARCH%%,${TARGET}," \
1635
		  ${FINAL_CHROOT_DIR}/usr/local/share/dfuibe_lua/conf/pfSense.lua \
1636
		  ${FINAL_CHROOT_DIR}/usr/local/share/dfuibe_lua/conf/pfSense_rescue.lua
1637
	if [ -n "${_IS_RELEASE}" ]; then
1638
		rm -rf ${FINAL_CHROOT_DIR}/tmp/pkg-repo
1639
	fi
1640
	echo ">>> Installing BSDInstaller in chroot (${FINAL_CHROOT_DIR})... (finished)"
1641
}
1642

    
1643
staginareas_clean_each_run() {
1644
	echo -n ">>> Cleaning build directories: "
1645
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1646
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1647
		echo -n "$BASENAME "
1648
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1649
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1650
	fi
1651
	echo "Done!"
1652
}
1653

    
1654
# Imported from FreeSBIE
1655
buildkernel() {
1656
	if [ -n "${NO_BUILDKERNEL}" ]; then
1657
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1658
		return
1659
	fi
1660

    
1661
	if [ -z "${KERNCONF}" ]; then
1662
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1663
		print_error_pfS
1664
	fi
1665

    
1666
	if [ -n "${KERNELCONF}" ]; then
1667
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1668
		export KERNCONF=$(basename ${KERNELCONF})
1669
	fi
1670

    
1671
	echo ">>> KERNCONFDIR: ${KERNCONFDIR}"
1672
	echo ">>> ARCH:        ${TARGET_ARCH}"
1673

    
1674
	makeargs="${MAKEJ}"
1675
	echo ">>> Builder is running the command: script -aq $LOGFILE make $makeargs buildkernel KERNCONF=${KERNCONF}" | tee -a $LOGFILE
1676
	(script -q $LOGFILE make -C ${FREEBSD_SRC_DIR} $makeargs buildkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1677
}
1678

    
1679
# Imported from FreeSBIE
1680
installkernel() {
1681
	if [ -z "${KERNCONF}" ]; then
1682
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1683
		print_error_pfS
1684
	fi
1685

    
1686
	if [ -n "${KERNELCONF}" ]; then
1687
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1688
		export KERNCONF=$(basename ${KERNELCONF})
1689
	fi
1690

    
1691
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1692
	makeargs="${MAKEJ} DESTDIR=${KERNEL_DESTDIR}"
1693
	echo ">>> Builder is running the command: script -aq $LOGFILE make ${makeargs} installkernel KERNCONF=${KERNCONF}"  | tee -a $LOGFILE
1694
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1695
	gzip -f9 $KERNEL_DESTDIR/boot/kernel/kernel
1696
}
1697

    
1698
# Launch is ran first to setup a few variables that we need
1699
# Imported from FreeSBIE
1700
launch() {
1701
	if [ "$(id -u)" != "0" ]; then
1702
		echo "Sorry, this must be done as root."
1703
	fi
1704

    
1705
	echo ">>> Operation $0 has started at $(date)"
1706
}
1707

    
1708
finish() {
1709
	echo ">>> Operation $0 has ended at $(date)"
1710
}
1711

    
1712
pkg_repo_rsync() {
1713
	local _repo_path_param="${1}"
1714
	local _ignore_final_rsync="${2}"
1715

    
1716
	if [ -z "${_repo_path_param}" -o ! -d "${_repo_path_param}" ]; then
1717
		return
1718
	fi
1719

    
1720
	if [ -n "${SKIP_FINAL_RSYNC}" ]; then
1721
		_ignore_final_rsync="1"
1722
	fi
1723

    
1724
	# Sanitize path
1725
	_repo_path=$(realpath ${_repo_path_param})
1726

    
1727
	local _repo_dir=$(dirname ${_repo_path})
1728
	local _repo_base=$(basename ${_repo_path})
1729

    
1730
	# Add ./ it's an rsync trick to make it chdir to directory before sending it
1731
	_repo_path="${_repo_dir}/./${_repo_base}"
1732

    
1733
	if [ -z "${LOGFILE}" ]; then
1734
		local _logfile="/dev/null"
1735
	else
1736
		local _logfile="${LOGFILE}"
1737
	fi
1738

    
1739
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" ]; then
1740

    
1741
		# Detect poudriere directory structure
1742
		if [ -L "${_repo_path}/.latest" ]; then
1743
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1744
		else
1745
			local _real_repo_path=${_repo_path}
1746
		fi
1747

    
1748
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1749
		############ ATTENTION ##############
1750
		#
1751
		# For some reason pkg-repo fail without / in the end of directory name
1752
		# so removing it will break command
1753
		#
1754
		# https://github.com/freebsd/pkg/issues/1364
1755
		#
1756
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1757
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1758
			echo "Done!" | tee -a ${_logfile}
1759
		else
1760
			echo "Failed!" | tee -a ${_logfile}
1761
			echo ">>> ERROR: An error occurred trying to sign repo"
1762
			print_error_pfS
1763
		fi
1764

    
1765
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1766
		if [ -e ${_pkgfile} ]; then
1767
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1768

    
1769
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1770
			    > ${_pkgfile}.sig 2>/dev/null; then
1771
				echo "Done!" | tee -a ${_logfile}
1772
			else
1773
				echo "Failed!" | tee -a ${_logfile}
1774
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1775
				print_error_pfS
1776
			fi
1777
		fi
1778
	fi
1779

    
1780
	if [ -n "${DO_NOT_UPLOAD}" ]; then
1781
		return
1782
	fi
1783

    
1784
	# Make sure destination directory exist
1785
	ssh -p ${PKG_RSYNC_SSH_PORT} \
1786
		${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME} \
1787
		"mkdir -p ${PKG_RSYNC_DESTDIR}"
1788

    
1789
	echo -n ">>> Sending updated repository to ${PKG_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1790
	if script -aq ${_logfile} rsync -Have "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1791
		--timeout=60 --delete-delay ${_repo_path} \
1792
		${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1793
	then
1794
		echo "Done!" | tee -a ${_logfile}
1795
	else
1796
		echo "Failed!" | tee -a ${_logfile}
1797
		echo ">>> ERROR: An error occurred sending repo to remote hostname"
1798
		print_error_pfS
1799
	fi
1800

    
1801
	if [ -z "${USE_PKG_REPO_STAGING}" -o -n "${_ignore_final_rsync}" ]; then
1802
		return
1803
	fi
1804

    
1805
	if [ -n "${_IS_RELEASE}" -o "${_repo_path_param}" = "${CORE_PKG_PATH}" ]; then
1806
		# Send .real* directories first to prevent having a broken repo while transfer happens
1807
		local _cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1808
			--timeout=60 ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1809
			--include=\"/*\" --include=\"*/.real*\" --include=\"*/.real*/*\" \
1810
			--exclude=\"*\" \
1811
			${PKG_FINAL_RSYNC_USERNAME}@${PKG_FINAL_RSYNC_HOSTNAME}:${PKG_FINAL_RSYNC_DESTDIR}"
1812

    
1813
		echo -n ">>> Sending updated packages to ${PKG_FINAL_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1814
		if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1815
			${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME} ${_cmd} >/dev/null 2>&1; then
1816
			echo "Done!" | tee -a ${_logfile}
1817
		else
1818
			echo "Failed!" | tee -a ${_logfile}
1819
			echo ">>> ERROR: An error occurred sending repo to final hostname"
1820
			print_error_pfS
1821
		fi
1822

    
1823
		_cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1824
			--timeout=60 --delete-delay ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1825
			${PKG_FINAL_RSYNC_USERNAME}@${PKG_FINAL_RSYNC_HOSTNAME}:${PKG_FINAL_RSYNC_DESTDIR}"
1826

    
1827
		echo -n ">>> Sending updated repositories metadata to ${PKG_FINAL_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1828
		if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1829
			${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME} ${_cmd} >/dev/null 2>&1; then
1830
			echo "Done!" | tee -a ${_logfile}
1831
		else
1832
			echo "Failed!" | tee -a ${_logfile}
1833
			echo ">>> ERROR: An error occurred sending repo to final hostname"
1834
			print_error_pfS
1835
		fi
1836
	fi
1837
}
1838

    
1839
poudriere_possible_archs() {
1840
	local _arch=$(uname -m)
1841
	local _archs=""
1842

    
1843
	# If host is amd64, we'll create both repos, and if possible armv6
1844
	if [ "${_arch}" = "amd64" ]; then
1845
		_archs="amd64.amd64"
1846

    
1847
		if [ -f /usr/local/bin/qemu-arm-static ]; then
1848
			# Make sure binmiscctl is ok
1849
			/usr/local/etc/rc.d/qemu_user_static forcestart >/dev/null 2>&1
1850

    
1851
			if binmiscctl lookup armv6 >/dev/null 2>&1; then
1852
				_archs="${_archs} arm.armv6"
1853
			fi
1854
		fi
1855
	fi
1856

    
1857
	if [ -n "${ARCH_LIST}" ]; then
1858
		local _found=0
1859
		for _desired_arch in ${ARCH_LIST}; do
1860
			_found=0
1861
			for _possible_arch in ${_archs}; do
1862
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1863
					_found=1
1864
					break
1865
				fi
1866
			done
1867
			if [ ${_found} -eq 0 ]; then
1868
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1869
				print_error_pfS
1870
			fi
1871
		done
1872
		_archs="${ARCH_LIST}"
1873
	fi
1874

    
1875
	echo ${_archs}
1876
}
1877

    
1878
poudriere_jail_name() {
1879
	local _jail_arch="${1}"
1880

    
1881
	if [ -z "${_jail_arch}" ]; then
1882
		return 1
1883
	fi
1884

    
1885
	# Remove arch
1886
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1887
}
1888

    
1889
poudriere_rename_ports() {
1890
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1891
		return;
1892
	fi
1893

    
1894
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1895

    
1896
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1897

    
1898
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1899
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1900
		local _pdir=$(dirname ${d})
1901
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1902
		local _plist=""
1903

    
1904
		if [ -e ${_pdir}/${_pname} ]; then
1905
			rm -rf ${_pdir}/${_pname}
1906
		fi
1907

    
1908
		cp -r ${d} ${_pdir}/${_pname}
1909

    
1910
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1911
			_plist=${_pdir}/${_pname}/pkg-plist
1912
		fi
1913

    
1914
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1915
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1916
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1917
			${_pdir}/${_pname}/Makefile \
1918
			${_pdir}/${_pname}/pkg-descr ${_plist}
1919

    
1920
		# PHP module is special
1921
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1922
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1923
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1924
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1925
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1926
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1927
				${_pdir}/${_pname}/files/config.m4
1928

    
1929
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1930
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1931
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1932
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1933
				${_pdir}/${_pname}/files/pfSense.c \
1934
				${_pdir}/${_pname}/files/php_pfSense.h
1935
		fi
1936

    
1937
		if [ -d ${_pdir}/${_pname}/files ]; then
1938
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1939
				local _fddir=$(dirname ${fd})
1940
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1941

    
1942
				mv ${fd} ${_fddir}/${_fdname}
1943
			done
1944
		fi
1945
	done
1946
	echo "Done!" | tee -a ${LOGFILE}
1947
}
1948

    
1949
poudriere_create_ports_tree() {
1950
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1951

    
1952
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1953
		local _branch=""
1954
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1955
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1956
			print_error_pfS
1957
		fi
1958
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1959
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1960
		fi
1961
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1962
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git -U ${POUDRIERE_PORTS_GIT_URL} ${_branch} >/dev/null 2>&1; then
1963
			echo "" | tee -a ${LOGFILE}
1964
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1965
			print_error_pfS
1966
		fi
1967
		echo "Done!" | tee -a ${LOGFILE}
1968
		poudriere_rename_ports
1969
	fi
1970
}
1971

    
1972
poudriere_init() {
1973
	local _error=0
1974
	local _archs=$(poudriere_possible_archs)
1975

    
1976
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1977

    
1978
	# Sanity checks
1979
	if [ -z "${ZFS_TANK}" ]; then
1980
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1981
		error=1
1982
	fi
1983

    
1984
	if [ -z "${ZFS_ROOT}" ]; then
1985
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1986
		error=1
1987
	fi
1988

    
1989
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1990
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1991
		error=1
1992
	fi
1993

    
1994
	if [ ${_error} -eq 1 ]; then
1995
		print_error_pfS
1996
	fi
1997

    
1998
	# Check if zpool exists
1999
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
2000
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
2001
		print_error_pfS
2002
	fi
2003

    
2004
	# Check if zfs rootfs exists
2005
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
2006
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
2007
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
2008
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
2009
			echo "Done!"
2010
		else
2011
			echo "Failed!"
2012
			print_error_pfS
2013
		fi
2014
	fi
2015

    
2016
	# Make sure poudriere is installed
2017
	if ! pkg info --quiet poudriere-devel; then
2018
		echo ">>> Installing poudriere-devel..." | tee -a ${LOGFILE}
2019
		if ! pkg install poudriere-devel >/dev/null 2>&1; then
2020
			echo ">>> ERROR: poudriere-devel was not installed, aborting..." | tee -a ${LOGFILE}
2021
			print_error_pfS
2022
		fi
2023
	fi
2024

    
2025
	# Create poudriere.conf
2026
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
2027
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
2028
		print_error_pfS
2029
	fi
2030
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
2031
	cat <<EOF >/usr/local/etc/poudriere.conf
2032
ZPOOL=${ZFS_TANK}
2033
ZROOTFS=${ZFS_ROOT}
2034
RESOLV_CONF=/etc/resolv.conf
2035
BASEFS=/usr/local/poudriere
2036
USE_PORTLINT=no
2037
USE_TMPFS=yes
2038
NOLINUX=yes
2039
DISTFILES_CACHE=/usr/ports/distfiles
2040
CHECK_CHANGED_OPTIONS=yes
2041
CHECK_CHANGED_DEPS=yes
2042
ATOMIC_PACKAGE_REPOSITORY=yes
2043
COMMIT_PACKAGES_ON_FAILURE=no
2044
KEEP_OLD_PACKAGES=yes
2045
KEEP_OLD_PACKAGES_COUNT=5
2046
EOF
2047

    
2048
	# Create specific items conf
2049
	[ ! -d /usr/local/etc/poudriere.d ] \
2050
		&& mkdir -p /usr/local/etc/poudriere.d
2051

    
2052
	# Create DISTFILES_CACHE if it doesn't exist
2053
	if [ ! -d /usr/ports/distfiles ]; then
2054
		mkdir -p /usr/ports/distfiles
2055
	fi
2056

    
2057
	# Remove old jails
2058
	for jail_arch in ${_archs}; do
2059
		jail_name=$(poudriere_jail_name ${jail_arch})
2060

    
2061
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2062
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
2063
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
2064
		fi
2065
	done
2066

    
2067
	# Remove old ports tree
2068
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2069
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
2070
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
2071
	fi
2072

    
2073
	local native_xtools=""
2074
	# Now we are ready to create jails
2075
	for jail_arch in ${_archs}; do
2076
		jail_name=$(poudriere_jail_name ${jail_arch})
2077

    
2078
		if [ "${jail_arch}" = "arm.armv6" ]; then
2079
			native_xtools="-x"
2080
		else
2081
			native_xtools=""
2082
		fi
2083

    
2084
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2085
		# XXX: Change -m to git when it's available in poudriere
2086
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
2087
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
2088
			echo "" | tee -a ${LOGFILE}
2089
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2090
			print_error_pfS
2091
		fi
2092
		echo "Done!" | tee -a ${LOGFILE}
2093
	done
2094

    
2095
	poudriere_create_ports_tree
2096

    
2097
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
2098
}
2099

    
2100
poudriere_update_jails() {
2101
	local _archs=$(poudriere_possible_archs)
2102

    
2103
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2104

    
2105
	local native_xtools=""
2106
	for jail_arch in ${_archs}; do
2107
		jail_name=$(poudriere_jail_name ${jail_arch})
2108

    
2109
		local _create_or_update="-u"
2110
		local _create_or_update_text="Updating"
2111
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2112
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
2113
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
2114
			_create_or_update_text="Creating"
2115
		fi
2116

    
2117
		if [ "${jail_arch}" = "arm.armv6" ]; then
2118
			native_xtools="-x"
2119
		else
2120
			native_xtools=""
2121
		fi
2122

    
2123
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2124
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
2125
			echo "" | tee -a ${LOGFILE}
2126
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2127
			print_error_pfS
2128
		fi
2129
		echo "Done!" | tee -a ${LOGFILE}
2130
	done
2131
}
2132

    
2133
poudriere_update_ports() {
2134
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2135

    
2136
	# Create ports tree if necessary
2137
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2138
		poudriere_create_ports_tree
2139
	else
2140
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2141
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
2142
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
2143
		echo "Done!" | tee -a ${LOGFILE}
2144
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2145
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
2146
		echo "Done!" | tee -a ${LOGFILE}
2147
		poudriere_rename_ports
2148
	fi
2149
}
2150

    
2151
poudriere_bulk() {
2152
	local _archs=$(poudriere_possible_archs)
2153

    
2154
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2155

    
2156
	if [ -z "${DO_NOT_UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
2157
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
2158
		print_error_pfS
2159
	fi
2160

    
2161
	rm -f ${LOGFILE}
2162

    
2163
	poudriere_create_ports_tree
2164

    
2165
	[ -d /usr/local/etc/poudriere.d ] || \
2166
		mkdir -p /usr/local/etc/poudriere.d
2167

    
2168
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
2169
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" /usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
2170
	fi
2171

    
2172
	# Change version of pfSense meta ports for snapshots
2173
	if [ -z "${_IS_RELEASE}" ]; then
2174
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
2175
		sed -i '' \
2176
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
2177
			-e "/^PORTREVISION=/d" \
2178
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile
2179
	fi
2180

    
2181
	for jail_arch in ${_archs}; do
2182
		jail_name=$(poudriere_jail_name ${jail_arch})
2183

    
2184
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2185
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
2186
			continue
2187
		fi
2188

    
2189
		if [ -f "${POUDRIERE_BULK}.${jail_arch}" ]; then
2190
			_ref_bulk="${POUDRIERE_BULK}.${jail_arch}"
2191
		else
2192
			_ref_bulk="${POUDRIERE_BULK}"
2193
		fi
2194

    
2195
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}
2196
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
2197

    
2198
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
2199
			echo ">>> ERROR: Something went wrong..."
2200
			print_error_pfS
2201
		fi
2202

    
2203
		echo ">>> Cleaning up old packages from repo..."
2204
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
2205
			echo ">>> ERROR: Something went wrong..."
2206
			print_error_pfS
2207
		fi
2208

    
2209
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
2210
	done
2211
}
2212

    
2213
# This routine is called to write out to stdout
2214
# a string. The string is appended to $SNAPSHOTSLOGFILE
2215
# and we scp the log file to the builder host if
2216
# needed for the real time logging functions.
2217
snapshots_update_status() {
2218
	if [ -z "$1" ]; then
2219
		return
2220
	fi
2221
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
2222
		return
2223
	fi
2224
	echo "$*"
2225
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
2226
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2227
		LU=$(cat $SNAPSHOTSLASTUPDATE 2>/dev/null)
2228
		CT=$(date "+%H%M%S")
2229
		# Only update every minute
2230
		if [ "$LU" != "$CT" ]; then
2231
			ssh ${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP} \
2232
				"mkdir -p ${SNAPSHOTS_RSYNCLOGS}"
2233
			scp -q $SNAPSHOTSLOGFILE \
2234
				${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log
2235
			date "+%H%M%S" > $SNAPSHOTSLASTUPDATE
2236
		fi
2237
	fi
2238
}
2239

    
2240
# Copy the current log file to $filename.old on
2241
# the snapshot www server (real time logs)
2242
snapshots_rotate_logfile() {
2243
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2244
		scp -q $SNAPSHOTSLOGFILE \
2245
			${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log.old
2246
	fi
2247

    
2248
	# Cleanup log file
2249
	rm -f $SNAPSHOTSLOGFILE;    touch $SNAPSHOTSLOGFILE
2250
	rm -f $SNAPSHOTSLASTUPDATE; touch $SNAPSHOTSLASTUPDATE
2251

    
2252
}
2253

    
2254
create_sha256() {
2255
	local _file="${1}"
2256

    
2257
	if [ ! -f "${_file}" ]; then
2258
		return 1
2259
	fi
2260

    
2261
	( \
2262
		cd $(dirname ${_file}) && \
2263
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
2264
	)
2265
}
2266

    
2267
snapshots_create_latest_symlink() {
2268
	local _image="${1}"
2269

    
2270
	if [ -z "${_image}" ]; then
2271
		return
2272
	fi
2273

    
2274
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
2275
		return
2276
	fi
2277

    
2278
	if [ ! -f "${_image}" ]; then
2279
		return
2280
	fi
2281

    
2282
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
2283
	ln -sf $(basename ${_image}) ${_symlink}
2284
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
2285
}
2286

    
2287
snapshots_create_sha256() {
2288
	local _img=""
2289

    
2290
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
2291
		if [ -f "${_img}.gz" ]; then
2292
			_img="${_img}.gz"
2293
		fi
2294
		if [ ! -f "${_img}" ]; then
2295
			continue
2296
		fi
2297
		create_sha256 ${_img}
2298
		snapshots_create_latest_symlink ${_img}
2299
	done
2300

    
2301
	for NANOTYPE in nanobsd nanobsd-vga; do
2302
		for FILESIZE in ${FLASH_SIZE}; do
2303
			FILENAMEFULL="$(nanobsd_image_filename ${FILESIZE} ${NANOTYPE}).gz"
2304

    
2305
			if [ -f $IMAGES_FINAL_DIR/nanobsd/$FILENAMEFULL ]; then
2306
				create_sha256 $IMAGES_FINAL_DIR/nanobsd/$FILENAMEFULL
2307
			fi
2308
		done
2309
	done
2310
}
2311

    
2312
snapshots_scp_files() {
2313
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
2314
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60 --bwlimit=${RSYNCKBYTELIMIT}" #--bwlimit=50
2315
	fi
2316

    
2317
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
2318
	pkg_repo_rsync "${CORE_PKG_PATH}"
2319
	snapshots_update_status ">>> Finished copying core pkg repo"
2320

    
2321
	snapshots_update_status ">>> Copying files to ${RSYNCIP}"
2322

    
2323
	# Ensure directory(s) are available
2324
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/installer"
2325
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/nanobsd"
2326
	if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
2327
		ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/virtualization"
2328
	fi
2329
	# ensure permissions are correct for r+w
2330
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw ${RSYNCPATH}/."
2331
	rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
2332
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/installer/
2333
	rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/nanobsd/* \
2334
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/nanobsd/
2335
	if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
2336
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
2337
			${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/virtualization/
2338
	fi
2339

    
2340
	snapshots_update_status ">>> Finished copying files."
2341
}
(2-2/3)