Project

General

Profile

Download (73.1 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
kldload filemon >/dev/null 2>&1
52

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

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

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

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

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

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

    
102
	[ -d "${CORE_PKG_TMP}" ] \
103
		&& rm -rf ${CORE_PKG_TMP}
104

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

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

    
114
	mkdir -p ${CORE_PKG_TMP}
115

    
116
	cp -r ${_template_metadir} ${_metadir}
117

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

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

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

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

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

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

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

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

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

    
216
get_pkg_name() {
217
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
218
}
219

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

    
228
	[ -d "${KERNEL_BUILD_PATH}" ] \
229
		&& rm -rf ${KERNEL_BUILD_PATH}
230

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

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

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

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

    
255
		buildkernel
256

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

    
260
		ensure_kernel_exists $KERNEL_DESTDIR
261

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

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

    
270
		rm -rf $KERNEL_DESTDIR 2>&1 1>/dev/null
271

    
272
		echo " Done" | tee -a ${LOGFILE}
273
	done
274
}
275

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

    
282
	export KERNEL_NAME="${1}"
283

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

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

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

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

    
317
	unset KERNEL_NAME
318
}
319

    
320
# This builds FreeBSD (make buildworld)
321
# Imported from FreeSBIE
322
make_world() {
323
	LOGFILE=${BUILDER_LOGS}/buildworld.${TARGET}
324
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
325
	if [ -n "${NO_BUILDWORLD}" ]; then
326
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
327
		return
328
	fi
329

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

    
336
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
337
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
338

    
339
	[ -d "${INSTALLER_CHROOT_DIR}" ] \
340
		|| mkdir -p ${INSTALLER_CHROOT_DIR}
341

    
342
	makeargs="${MAKEJ} DESTDIR=${INSTALLER_CHROOT_DIR}"
343
	echo ">>> Installing world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
344
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld" | tee -a ${LOGFILE}
345
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
346
	cp ${FREEBSD_SRC_DIR}/release/rc.local ${INSTALLER_CHROOT_DIR}/etc
347
	echo ">>> Installing world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
348

    
349
	makeargs="${MAKEJ} WITHOUT_BSDINSTALL=1 DESTDIR=${STAGE_CHROOT_DIR}"
350
	echo ">>> Installing world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
351
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld" | tee -a ${LOGFILE}
352
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
353
	echo ">>> Installing world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
354

    
355
	makeargs="${MAKEJ} WITHOUT_BSDINSTALL=1 DESTDIR=${STAGE_CHROOT_DIR}"
356
	echo ">>> Distribution world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
357
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution " | tee -a ${LOGFILE}
358
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution  || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
359
	echo ">>> Distribution world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
360

    
361
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
362
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
363
	makeargs="${MAKEJ} DESTDIR=${STAGE_CHROOT_DIR}"
364
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
365
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install " | tee -a ${LOGFILE}
366
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
367
	# XXX FIX IT
368
#	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}
369
#	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
370
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
371

    
372
	unset makeargs
373
}
374

    
375
nanobsd_image_filename() {
376
	local _size="$1"
377
	local _type="$2"
378

    
379
	echo "$NANOBSD_IMG_TEMPLATE" | sed \
380
		-e "s,%%SIZE%%,${_size},g" \
381
		-e "s,%%TYPE%%,${_type},g"
382

    
383
	return 0
384
}
385

    
386
# This routine originated in nanobsd.sh
387
nanobsd_set_flash_details () {
388
	a1=$(echo $1 | tr '[:upper:]' '[:lower:]')
389

    
390
	# Source:
391
	#	SanDisk CompactFlash Memory Card
392
	#	Product Manual
393
	#	Version 10.9
394
	#	Document No. 20-10-00038
395
	#	April 2005
396
	# Table 2-7
397
	# NB: notice math error in SDCFJ-4096-388 line.
398
	#
399
	case "${a1}" in
400
		2048|2048m|2048mb|2g)
401
			NANO_MEDIASIZE=$((1989999616/512))
402
			;;
403
		4096|4096m|4096mb|4g)
404
			NANO_MEDIASIZE=$((3989999616/512))
405
			;;
406
		8192|8192m|8192mb|8g)
407
			NANO_MEDIASIZE=$((7989999616/512))
408
			;;
409
		16384|16384m|16384mb|16g)
410
			NANO_MEDIASIZE=$((15989999616/512))
411
			;;
412
		*)
413
			echo "Unknown Flash capacity"
414
			exit 2
415
			;;
416
	esac
417

    
418
	NANO_HEADS=16
419
	NANO_SECTS=63
420

    
421
	echo ">>> [nanoo] $1"
422
	echo ">>> [nanoo] NANO_MEDIASIZE: $NANO_MEDIASIZE"
423
	echo ">>> [nanoo] NANO_HEADS: $NANO_HEADS"
424
	echo ">>> [nanoo] NANO_SECTS: $NANO_SECTS"
425
	echo ">>> [nanoo] NANO_BOOT0CFG: $NANO_BOOT0CFG"
426
}
427

    
428
# This routine originated in nanobsd.sh
429
create_nanobsd_diskimage () {
430
	if [ -z "${1}" ]; then
431
		echo ">>> ERROR: Type of image has not been specified"
432
		print_error_pfS
433
	fi
434
	if [ -z "${2}" ]; then
435
		echo ">>> ERROR: Size of image has not been specified"
436
		print_error_pfS
437
	fi
438

    
439
	if [ "${1}" = "nanobsd" ]; then
440
		# It's serial
441
		export NANO_BOOTLOADER="boot/boot0sio"
442
	elif [ "${1}" = "nanobsd-vga" ]; then
443
		# It's vga
444
		export NANO_BOOTLOADER="boot/boot0"
445
	else
446
		echo ">>> ERROR: Type of image to create unknown"
447
		print_error_pfS
448
	fi
449

    
450
	if [ -z "${2}" ]; then
451
		echo ">>> ERROR: Media size(s) not specified."
452
		print_error_pfS
453
	fi
454

    
455
	if [ -z "${2}" ]; then
456
		echo ">>> ERROR: FLASH_SIZE is not set."
457
		print_error_pfS
458
	fi
459

    
460
	LOGFILE=${BUILDER_LOGS}/${1}.${TARGET}
461
	# Prepare folder to be put in image
462
	customize_stagearea_for_image "${1}"
463
	install_default_kernel ${DEFAULT_KERNEL} "no"
464

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

    
467
	local BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
468
	local LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
469

    
470
	if [ "${1}" = "nanobsd" ]; then
471
		# Tell loader to use serial console early.
472
		echo "-S115200 -h" >> ${BOOTCONF}
473

    
474
		# Remove old console options if present.
475
		[ -f "${LOADERCONF}" ] \
476
			&& sed -i "" -Ee "/(console|boot_multicons|boot_serial|hint.uart)/d" ${LOADERCONF}
477
		# Activate serial console+video console in loader.conf
478
		echo 'loader_color="NO"' >> ${LOADERCONF}
479
		echo 'beastie_disable="YES"' >> ${LOADERCONF}
480
		echo 'boot_serial="YES"' >> ${LOADERCONF}
481
		echo 'console="comconsole"' >> ${LOADERCONF}
482
		echo 'comconsole_speed="115200"' >> ${LOADERCONF}
483
	fi
484
	echo 'autoboot_delay="5"' >> ${LOADERCONF}
485

    
486
	# Old systems will run (pre|post)_upgrade_command from /tmp
487
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command ]; then
488
		cp -p \
489
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command \
490
			${FINAL_CHROOT_DIR}/tmp
491
	fi
492
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command ]; then
493
		cp -p \
494
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command \
495
			${FINAL_CHROOT_DIR}/tmp
496
	fi
497

    
498
	mkdir -p ${IMAGES_FINAL_DIR}/nanobsd
499

    
500
	for _NANO_MEDIASIZE in ${2}; do
501
		if [ -z "${_NANO_MEDIASIZE}" ]; then
502
			continue;
503
		fi
504

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

    
508
		IMG="${IMAGES_FINAL_DIR}/nanobsd/$(nanobsd_image_filename ${_NANO_MEDIASIZE} ${1})"
509

    
510
		nanobsd_set_flash_details ${_NANO_MEDIASIZE}
511

    
512
		# These are defined in FlashDevice and on builder_default.sh
513
		echo $NANO_MEDIASIZE \
514
			$NANO_IMAGES \
515
			$NANO_SECTS \
516
			$NANO_HEADS \
517
			$NANO_CODESIZE \
518
			$NANO_CONFSIZE \
519
			$NANO_DATASIZE |
520
awk '
521
{
522
	printf "# %s\n", $0
523

    
524
	# size of cylinder in sectors
525
	cs = $3 * $4
526

    
527
	# number of full cylinders on media
528
	cyl = int ($1 / cs)
529

    
530
	# output fdisk geometry spec, truncate cyls to 1023
531
	if (cyl <= 1023)
532
		print "g c" cyl " h" $4 " s" $3
533
	else
534
		print "g c" 1023 " h" $4 " s" $3
535

    
536
	if ($7 > 0) {
537
		# size of data partition in full cylinders
538
		dsl = int (($7 + cs - 1) / cs)
539
	} else {
540
		dsl = 0;
541
	}
542

    
543
	# size of config partition in full cylinders
544
	csl = int (($6 + cs - 1) / cs)
545

    
546
	if ($5 == 0) {
547
		# size of image partition(s) in full cylinders
548
		isl = int ((cyl - dsl - csl) / $2)
549
	} else {
550
		isl = int (($5 + cs - 1) / cs)
551
	}
552

    
553
	# First image partition start at second track
554
	print "p 1 165 " $3, isl * cs - $3
555
	c = isl * cs;
556

    
557
	# Second image partition (if any) also starts offset one
558
	# track to keep them identical.
559
	if ($2 > 1) {
560
		print "p 2 165 " $3 + c, isl * cs - $3
561
		c += isl * cs;
562
	}
563

    
564
	# Config partition starts at cylinder boundary.
565
	print "p 3 165 " c, csl * cs
566
	c += csl * cs
567

    
568
	# Data partition (if any) starts at cylinder boundary.
569
	if ($7 > 0) {
570
		print "p 4 165 " c, dsl * cs
571
	} else if ($7 < 0 && $1 > c) {
572
		print "p 4 165 " c, $1 - c
573
	} else if ($1 < c) {
574
		print "Disk space overcommitted by", \
575
		    c - $1, "sectors" > "/dev/stderr"
576
		exit 2
577
	}
578

    
579
	# Force slice 1 to be marked active. This is necessary
580
	# for booting the image from a USB device to work.
581
	print "a 1"
582
}
583
	' > ${SCRATCHDIR}/_.fdisk
584

    
585
		MNT=${SCRATCHDIR}/_.mnt
586
		mkdir -p ${MNT}
587

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

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

    
594
		fdisk -i -f ${SCRATCHDIR}/_.fdisk ${MD} 2>&1 >> ${LOGFILE}
595
		fdisk ${MD} 2>&1 >> ${LOGFILE}
596

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

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

    
612
		clone_directory_contents ${FINAL_CHROOT_DIR} ${MNT}
613

    
614
		# Set NanoBSD image size
615
		echo "${_NANO_MEDIASIZE}" > ${MNT}/etc/nanosize.txt
616

    
617
		echo "/dev/ufs/${_label}0 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
618
		if [ $NANO_CONFSIZE -gt 0 ] ; then
619
			echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
620
		fi
621

    
622
		umount ${MNT}
623
		# Restore the original trap
624
		trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
625

    
626
		# Setting NANO_IMAGES to 1 and NANO_INIT_IMG2 will tell
627
		# NanoBSD to only create one partition.  We default to 2
628
		# partitions in case anything happens to the first the
629
		# operator can boot from the 2nd and should be OK.
630

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

    
648
			echo "/dev/ufs/${_label}1 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
649
			if [ $NANO_CONFSIZE -gt 0 ] ; then
650
				echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
651
			fi
652

    
653
			umount ${MNT}
654
			# Restore the trap back
655
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
656
		fi
657

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

    
676
			clone_directory_contents ${FINAL_CHROOT_DIR}/cf ${MNT}
677

    
678
			umount ${MNT}
679
			# Restore the trap back
680
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
681
		else
682
			">>> [nanoo] NANO_CONFSIZE is not set. Not adding a /conf partition.. You sure about this??" | tee -a ${LOGFILE}
683
		fi
684

    
685
		mdconfig -d -u $MD
686
		# Restore default action
687
		trap "-" 1 2 15 EXIT
688

    
689
		# Check each image and ensure that they are over
690
		# 3 megabytes.  If either image is under 20 megabytes
691
		# in size then error out.
692
		IMGSIZE=$(stat -f "%z" ${IMG})
693
		CHECKSIZE="20040710"
694
		if [ "$IMGSIZE" -lt "$CHECKSIZE" ]; then
695
			echo ">>> ERROR: Something went wrong when building NanoBSD.  The image size is under 20 megabytes!" | tee -a ${LOGFILE}
696
			print_error_pfS
697
		fi
698

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

    
702
		gzip -qf $IMG &
703
		_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
704
	done
705

    
706
	unset IMG
707
	unset IMGSIZE
708
}
709

    
710
# This routine creates a ova image that contains
711
# a ovf and vmdk file. These files can be imported
712
# right into vmware or virtual box.
713
# (and many other emulation platforms)
714
# http://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
715
create_ova_image() {
716
	# XXX create a .ovf php creator that you can pass:
717
	#     1. populatedSize
718
	#     2. license
719
	#     3. product name
720
	#     4. version
721
	#     5. number of network interface cards
722
	#     6. allocationUnits
723
	#     7. capacity
724
	#     8. capacityAllocationUnits
725

    
726
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
727

    
728
	[ -d "${OVA_TMP}" ] \
729
		&& rm -rf ${OVA_TMP}
730

    
731
	mkdir -p $(dirname ${OVAPATH})
732

    
733
	local _mntdir=${OVA_TMP}/mnt
734
	mkdir -p ${_mntdir}
735

    
736
	if [ -z "${OVA_SWAP_PART_SIZE_IN_GB}" -o "${OVA_SWAP_PART_SIZE_IN_GB}" = "0" ]; then
737
		# first partition size (freebsd-ufs)
738
		local OVA_FIRST_PART_SIZE_IN_GB=${VMDK_DISK_CAPACITY_IN_GB}
739
		# Calculate real first partition size, removing 128 blocks (65536 bytes) beginning/loader
740
		local OVA_FIRST_PART_SIZE=$((${OVA_FIRST_PART_SIZE_IN_GB}*1024*1024*1024-65536))
741
		# Unset swap partition size variable
742
		unset OVA_SWAP_PART_SIZE
743
		# Parameter used by mkimg
744
		unset OVA_SWAP_PART_PARAM
745
	else
746
		# first partition size (freebsd-ufs)
747
		local OVA_FIRST_PART_SIZE_IN_GB=$((VMDK_DISK_CAPACITY_IN_GB-OVA_SWAP_PART_SIZE_IN_GB))
748
		# Use first partition size in g
749
		local OVA_FIRST_PART_SIZE="${OVA_FIRST_PART_SIZE_IN_GB}g"
750
		# Calculate real swap size, removing 128 blocks (65536 bytes) beginning/loader
751
		local OVA_SWAP_PART_SIZE=$((${OVA_SWAP_PART_SIZE_IN_GB}*1024*1024*1024-65536))
752
		# Parameter used by mkimg
753
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
754
	fi
755

    
756
	# Prepare folder to be put in image
757
	customize_stagearea_for_image "ova"
758
	install_default_kernel ${DEFAULT_KERNEL} "no"
759

    
760
	# Fill fstab
761
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
762
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
763
	if [ -n "${OVA_SWAP_PART_SIZE}" ]; then
764
		echo "/dev/gpt/swap0	none	swap	sw	0	0" >> ${FINAL_CHROOT_DIR}/etc/fstab
765
	fi
766

    
767
	# Create / partition
768
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
769
	truncate -s ${OVA_FIRST_PART_SIZE} ${OVA_TMP}/${OVFUFS}
770
	local _md=$(mdconfig -a -f ${OVA_TMP}/${OVAUFS})
771
	trap "mdconfig -d -u ${_md}; return" 1 2 15 EXIT
772

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

    
775
	if ! mount /dev/${_md} ${_mntdir} 2>&1 >>${LOGFILE}; then
776
		echo "Failed!" | tee -a ${LOGFILE}
777
		echo ">>> ERROR: Error mounting temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
778
		print_error_pfS
779
	fi
780
	trap "umount ${_mntdir}; mdconfig -d -u ${_md}; return" 1 2 15 EXIT
781

    
782
	echo "Done!" | tee -a ${LOGFILE}
783

    
784
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
785

    
786
	sync
787
	umount ${_mntdir} 2>&1 >>${LOGFILE}
788
	mdconfig -d -u ${_md}
789
	trap "-" 1 2 15 EXIT
790

    
791
	# Create raw disk
792
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
793
	mkimg \
794
		-s gpt \
795
		-f raw \
796
		-b ${FINAL_CHROOT_DIR}/boot/pmbr \
797
		-p freebsd-boot:=${FINAL_CHROOT_DIR}/boot/gptboot \
798
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
799
		${OVA_SWAP_PART_PARAM} \
800
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
801

    
802
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
803
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
804
			rm -f ${OVA_TMP}/${OVFUFS}
805
		fi
806
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
807
			rm -f ${OVA_TMP}/${OVFRAW}
808
		fi
809
		echo "Failed!" | tee -a ${LOGFILE}
810
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
811
		print_error_pfS
812
	fi
813
	echo "Done!" | tee -a ${LOGFILE}
814

    
815
	# We don't need it anymore
816
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
817

    
818
	# Convert raw to vmdk
819
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
820
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
821

    
822
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
823
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
824
			rm -f ${OVA_TMP}/${OVFRAW}
825
		fi
826
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
827
			rm -f ${OVA_TMP}/${OVFVMDK}
828
		fi
829
		echo "Failed!" | tee -a ${LOGFILE}
830
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
831
		print_error_pfS
832
	fi
833
	echo "Done!" | tee -a ${LOGFILE}
834

    
835
	rm -f ${OVA_TMP}/i${OVFRAW}
836

    
837
	ova_setup_ovf_template
838

    
839
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
840
	# Create OVA file for vmware
841
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
842
	echo "Done!" | tee -a ${LOGFILE}
843
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
844

    
845
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
846
}
847

    
848
# called from create_ova_image
849
ova_setup_ovf_template() {
850
	if [ ! -f ${OVFTEMPLATE} ]; then
851
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
852
		print_error_pfS
853
	fi
854

    
855
	#  OperatingSystemSection (${PRODUCT_NAME}.ovf)
856
	#  42   FreeBSD 32-Bit
857
	#  78   FreeBSD 64-Bit
858
	if [ "${TARGET}" = "amd64" ]; then
859
		local _os_id="78"
860
		local _os_type="freebsd64Guest"
861
		local _os_descr="FreeBSD 64-Bit"
862
	else
863
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
864
		print_error_pfS
865
	fi
866

    
867
	local POPULATED_SIZE=$(du -d0 -k $FINAL_CHROOT_DIR | cut -f1)
868
	local POPULATED_SIZE_IN_BYTES=$((${POPULATED_SIZE}*1024))
869
	local VMDK_FILE_SIZE=$(stat -f "%z" ${OVA_TMP}/${OVFVMDK})
870

    
871
	sed \
872
		-e "s,%%VMDK_FILE_SIZE%%,${VMDK_FILE_SIZE},g" \
873
		-e "s,%%VMDK_DISK_CAPACITY_IN_GB%%,${VMDK_DISK_CAPACITY_IN_GB},g" \
874
		-e "s,%%POPULATED_SIZE_IN_BYTES%%,${POPULATED_SIZE_IN_BYTES},g" \
875
		-e "s,%%OS_ID%%,${_os_id},g" \
876
		-e "s,%%OS_TYPE%%,${_os_type},g" \
877
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
878
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
879
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
880
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
881
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
882
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
883
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
884
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/license.txt" \
885
		-e "/^%%PRODUCT_LICENSE%%/d" \
886
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
887
}
888

    
889
# Cleans up previous builds
890
clean_builder() {
891
	# Clean out directories
892
	echo ">>> Cleaning up previous build environment...Please wait!"
893

    
894
	staginareas_clean_each_run
895

    
896
	if [ -d "${STAGE_CHROOT_DIR}" ]; then
897
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR}... "
898
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
899
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
900
		echo "Done."
901
	fi
902

    
903
	if [ -d "${INSTALLER_CHROOT_DIR}" ]; then
904
		echo -n ">>> Cleaning ${INSTALLER_CHROOT_DIR}... "
905
		chflags -R noschg ${INSTALLER_CHROOT_DIR} 2>&1 >/dev/null
906
		rm -rf ${INSTALLER_CHROOT_DIR}/* 2>/dev/null
907
		echo "Done."
908
	fi
909

    
910
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
911
		OBJTREE=$(make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
912
		if [ -d "${OBJTREE}" ]; then
913
			echo -n ">>> Cleaning FreeBSD objects dir staging..."
914
			echo -n "."
915
			chflags -R noschg ${OBJTREE} 2>&1 >/dev/null
916
			echo -n "."
917
			rm -rf ${OBJTREE}/*
918
			echo "Done!"
919
		fi
920
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
921
			echo -n ">>> Cleaning previously built kernel stage area..."
922
			rm -rf $KERNEL_BUILD_PATH/*
923
			echo "Done!"
924
		fi
925
	fi
926
	mkdir -p $KERNEL_BUILD_PATH
927

    
928
	echo -n ">>> Cleaning previously built images..."
929
	rm -rf $IMAGES_FINAL_DIR/*
930
	echo "Done!"
931

    
932
	if [ -z "${NO_CLEAN_FREEBSD_SRC}" ]; then
933
		if [ -d "$FREEBSD_SRC_DIR" ]; then
934
			echo -n ">>> Ensuring $FREEBSD_SRC_DIR is clean..."
935
			rm -rf ${FREEBSD_SRC_DIR}
936
			echo "Done!"
937
		fi
938
	fi
939

    
940
	echo -n ">>> Cleaning previous builder logs..."
941
	if [ -d "$BUILDER_LOGS" ]; then
942
		rm -rf ${BUILDER_LOGS}
943
	fi
944
	mkdir -p ${BUILDER_LOGS}
945

    
946
	echo "Done!"
947

    
948
	echo ">>> Cleaning of builder environment has finished."
949
}
950

    
951
clone_directory_contents() {
952
	if [ ! -e "$2" ]; then
953
		mkdir -p "$2"
954
	fi
955
	if [ ! -d "$1" -o ! -d "$2" ]; then
956
		if [ -z "${LOGFILE}" ]; then
957
			echo ">>> ERROR: Argument $1 supplied is not a directory!"
958
		else
959
			echo ">>> ERROR: Argument $1 supplied is not a directory!" | tee -a ${LOGFILE}
960
		fi
961
		print_error_pfS
962
	fi
963
	echo -n ">>> Using TAR to clone $1 to $2 ..."
964
	tar -C ${1} -c -f - . | tar -C ${2} -x -p -f -
965
	echo "Done!"
966
}
967

    
968
clone_to_staging_area() {
969
	# Clone everything to the final staging area
970
	echo -n ">>> Cloning everything to ${STAGE_CHROOT_DIR} staging area..."
971
	LOGFILE=${BUILDER_LOGS}/cloning.${TARGET}.log
972

    
973
	tar -C ${PRODUCT_SRC} -c -f - . | \
974
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
975

    
976
	if [ "${PRODUCT_NAME}" != "pfSense" ]; then
977
		mv ${STAGE_CHROOT_DIR}/usr/local/sbin/pfSense-upgrade \
978
			${STAGE_CHROOT_DIR}/usr/local/sbin/${PRODUCT_NAME}-upgrade
979
	fi
980

    
981
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
982
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
983
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
984
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
985
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
986
	fi
987

    
988
	## Add buildtime and lastcommit information
989
	# This is used for detecting updates.
990
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
991
	# Record last commit info if it is available.
992
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
993
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
994
	fi
995

    
996
	local _exclude_files="${CORE_PKG_TMP}/base_exclude_files"
997
	sed \
998
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
999
		-e "s,%%VERSION%%,${_version},g" \
1000
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
1001
		> ${_exclude_files}
1002

    
1003
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
1004

    
1005
	# Include a sample pkg stable conf to base
1006
	setup_pkg_repo \
1007
		${PKG_REPO_DEFAULT} \
1008
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
1009
		${TARGET} \
1010
		${TARGET_ARCH}
1011

    
1012
	mtree \
1013
		-c \
1014
		-k uid,gid,mode,size,flags,sha256digest \
1015
		-p ${STAGE_CHROOT_DIR} \
1016
		-X ${_exclude_files} \
1017
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
1018
	tar \
1019
		-C ${STAGE_CHROOT_DIR} \
1020
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
1021
		-X ${_exclude_files} \
1022
		.
1023

    
1024
	local _share_repos_path="${SCRATCHDIR}/repo-tmp/${PRODUCT_SHARE_DIR}/pkg/repos"
1025
	rm -rf ${SCRATCHDIR}/repo-tmp >/dev/null 2>&1
1026
	mkdir -p ${_share_repos_path} >/dev/null 2>&1
1027

    
1028
	setup_pkg_repo \
1029
		${PKG_REPO_DEFAULT} \
1030
		${_share_repos_path}/${PRODUCT_NAME}-repo.conf \
1031
		${TARGET} \
1032
		${TARGET_ARCH}
1033

    
1034
	cp -f ${PKG_REPO_DEFAULT%%.conf}.descr ${_share_repos_path}
1035

    
1036
	# Add additional repos
1037
	for _template in ${PKG_REPO_BASE}/${PRODUCT_NAME}-repo-*.conf; do
1038
		_template_filename=$(basename ${_template})
1039
		setup_pkg_repo \
1040
			${_template} \
1041
			${_share_repos_path}/${_template_filename} \
1042
			${TARGET} \
1043
			${TARGET_ARCH}
1044
		cp -f ${_template%%.conf}.descr ${_share_repos_path}
1045
	done
1046

    
1047
	core_pkg_create repo "" ${CORE_PKG_VERSION} ${SCRATCHDIR}/repo-tmp
1048

    
1049
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1050
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1051
	core_pkg_create base-nanobsd "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1052
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1053

    
1054
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
1055

    
1056
	# Save current WAN and LAN if value
1057
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
1058
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
1059

    
1060
	# Change default interface names to match vmware driver
1061
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
1062
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
1063
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1064

    
1065
	# Restore default values to be used by serial package
1066
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
1067
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
1068

    
1069
	# Activate serial console in config.xml
1070
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
1071
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
1072
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
1073
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
1074
	rm -f ${DEFAULTCONF}.tmp
1075

    
1076
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1077

    
1078
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1079

    
1080
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1081
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
1082

    
1083
	# Make sure pkg is present
1084
	pkg_bootstrap ${STAGE_CHROOT_DIR}
1085

    
1086
	echo "Done!"
1087
}
1088

    
1089
create_final_staging_area() {
1090
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
1091
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
1092
		print_error_pfS
1093
	fi
1094

    
1095
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1096
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
1097
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
1098
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
1099
		echo "Done." | tee -a ${LOGFILE}
1100
	fi
1101

    
1102
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
1103
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
1104
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
1105

    
1106
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
1107
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
1108
		print_error_pfS
1109
	fi
1110
}
1111

    
1112
customize_stagearea_for_image() {
1113
	local _image_type="$1"
1114
	local _default_config="" # filled with $2 below
1115
	local _image_variant="$3"
1116

    
1117
	if [ -n "$2" ]; then
1118
		_default_config="$2"
1119
	elif [ "${_image_type}" = "nanobsd" -o \
1120
	     "${_image_type}" = "memstickserial" -o \
1121
	     "${_image_type}" = "memstickadi" ]; then
1122
		_default_config="default-config-serial"
1123
	elif [ "${_image_type}" = "ova" ]; then
1124
		_default_config="default-config-vmware"
1125
	else
1126
		_default_config="default-config"
1127
	fi
1128

    
1129
	# Prepare final stage area
1130
	create_final_staging_area
1131

    
1132
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
1133
	pkg_chroot_add ${FINAL_CHROOT_DIR} repo
1134

    
1135
	if [ "${_image_type}" = "nanobsd" -o \
1136
	     "${_image_type}" = "nanobsd-vga" ]; then
1137

    
1138
		mkdir -p ${FINAL_CHROOT_DIR}/root/var/db \
1139
			 ${FINAL_CHROOT_DIR}/root/var/cache \
1140
			 ${FINAL_CHROOT_DIR}/var/db/pkg \
1141
			 ${FINAL_CHROOT_DIR}/var/cache/pkg
1142
		mv -f ${FINAL_CHROOT_DIR}/var/db/pkg ${FINAL_CHROOT_DIR}/root/var/db
1143
		mv -f ${FINAL_CHROOT_DIR}/var/cache/pkg ${FINAL_CHROOT_DIR}/root/var/cache
1144
		ln -sf ../../root/var/db/pkg ${FINAL_CHROOT_DIR}/var/db/pkg
1145
		ln -sf ../../root/var/cache/pkg ${FINAL_CHROOT_DIR}/var/cache/pkg
1146

    
1147
		pkg_chroot_add ${FINAL_CHROOT_DIR} base-nanobsd
1148
	else
1149
		pkg_chroot_add ${FINAL_CHROOT_DIR} base
1150
	fi
1151

    
1152
	if [ "${_image_type}" = "iso" -o \
1153
	     "${_image_type}" = "memstick" -o \
1154
	     "${_image_type}" = "memstickserial" -o \
1155
	     "${_image_type}" = "memstickadi" ]; then
1156
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
1157
		cp ${CORE_PKG_ALL_PATH}/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
1158
	fi
1159

    
1160
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
1161

    
1162
	# XXX: Workaround to avoid pkg to complain regarding release
1163
	#      repo on first boot since packages are installed from
1164
	#      staging server during build phase
1165
	if [ -n "${USE_PKG_REPO_STAGING}" ]; then
1166
		_read_cmd="select value from repodata where key='packagesite'"
1167
		if [ -n "${_IS_RELEASE}" ]; then
1168
			local _tgt_server="${PKG_REPO_SERVER_RELEASE}"
1169
		else
1170
			local _tgt_server="${PKG_REPO_SERVER_DEVEL}"
1171
		fi
1172
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
1173
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
1174
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER_STAGING},${_tgt_server},")
1175
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
1176
		done
1177
	fi
1178

    
1179
	if [ -n "$_image_variant" -a \
1180
	    -d ${BUILDER_TOOLS}/templates/custom_logos/${_image_variant} ]; then
1181
		mkdir -p ${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
1182
		cp -f \
1183
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.png \
1184
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
1185
	fi
1186
}
1187

    
1188
create_distribution_tarball() {
1189
	mkdir -p ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist
1190

    
1191
	tar -C ${FINAL_CHROOT_DIR} --exclude ./install --exclude ./pkgs \
1192
		-cJf ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/base.txz .
1193

    
1194
	(cd ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist && \
1195
		sh ${FREEBSD_SRC_DIR}/release/scripts/make-manifest.sh base.txz) \
1196
		> ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/MANIFEST
1197
}
1198

    
1199
create_iso_image() {
1200
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
1201
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
1202
	if [ -z "${DEFAULT_KERNEL}" ]; then
1203
		echo ">>> ERROR: Could not identify DEFAULT_KERNEL to install on image!" | tee -a ${LOGFILE}
1204
		print_error_pfS
1205
	fi
1206

    
1207
	mkdir -p $(dirname ${ISOPATH})
1208

    
1209
	customize_stagearea_for_image "iso"
1210
	install_default_kernel ${DEFAULT_KERNEL}
1211

    
1212
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1213

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

    
1217
	# This check is for supporting create memstick/ova images
1218
	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}
1219
	echo "-o label=${FSLABEL} -o publisher=\"${PRODUCT_NAME} project.\" $ISOPATH ${FINAL_CHROOT_DIR}" | tee -a ${LOGFILE}
1220

    
1221
	create_distribution_tarball
1222

    
1223
	# Remove /rescue from iso since cd9660 cannot deal with hardlinks
1224
	rm -rf ${FINAL_CHROOT_DIR}/rescue
1225

    
1226
	makefs -t cd9660 -o bootimage="i386;${FINAL_CHROOT_DIR}/boot/cdboot" -o no-emul-boot -o rockridge \
1227
		-o label=${FSLABEL} -o publisher="${PRODUCT_NAME} project." $ISOPATH ${FINAL_CHROOT_DIR} 2>&1 >> ${LOGFILE}
1228
	if [ $? -ne 0 -o ! -f $ISOPATH ]; then
1229
		if [ -f ${ISOPATH} ]; then
1230
			rm -f $ISOPATH
1231
		fi
1232
		echo ">>> ERROR: Something wrong happened during ISO image creation. STOPPING!" | tee -a ${LOGFILE}
1233
		print_error_pfS
1234
	fi
1235
	gzip -qf $ISOPATH &
1236
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1237

    
1238
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1239
}
1240

    
1241
create_memstick_image() {
1242
	local _variant="$1"
1243

    
1244
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
1245
	if [ "${MEMSTICKPATH}" = "" ]; then
1246
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1247
		return
1248
	fi
1249

    
1250
	mkdir -p $(dirname ${MEMSTICKPATH})
1251

    
1252
	local _image_path=${MEMSTICKPATH}
1253
	if [ -n "${_variant}" ]; then
1254
		_image_path=$(echo "$_image_path" | \
1255
			sed "s/-memstick-/-memstick-${_variant}-/")
1256
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
1257
	fi
1258

    
1259
	customize_stagearea_for_image "memstick" "" $_variant
1260
	install_default_kernel ${DEFAULT_KERNEL}
1261

    
1262
	echo ">>> Creating memstick to ${_image_path}." 2>&1 | tee -a ${LOGFILE}
1263
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1264

    
1265
	create_distribution_tarball
1266

    
1267
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
1268
		${INSTALLER_CHROOT_DIR} \
1269
		${_image_path}
1270

    
1271
	gzip -qf $_image_path &
1272
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1273

    
1274
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1275
}
1276

    
1277
create_memstick_serial_image() {
1278
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
1279
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
1280
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1281
		return
1282
	fi
1283

    
1284
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
1285

    
1286
	customize_stagearea_for_image "memstickserial"
1287
	install_default_kernel ${DEFAULT_KERNEL}
1288

    
1289
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1290

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

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

    
1296
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1297
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1298

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

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

    
1315
	create_distribution_tarball
1316

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

    
1336
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1337
}
1338

    
1339
create_memstick_adi_image() {
1340
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
1341
	if [ "${MEMSTICKADIPATH}" = "" ]; then
1342
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1343
		return
1344
	fi
1345

    
1346
	mkdir -p $(dirname ${MEMSTICKADIPATH})
1347

    
1348
	customize_stagearea_for_image "memstickadi"
1349
	install_default_kernel ${DEFAULT_KERNEL}
1350

    
1351
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1352

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

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

    
1358
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1359
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1360

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

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

    
1379
	create_distribution_tarball
1380

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

    
1400
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1401
}
1402

    
1403
# Create pkg conf on desired place with desired arch/branch
1404
setup_pkg_repo() {
1405
	if [ -z "${4}" ]; then
1406
		return
1407
	fi
1408

    
1409
	local _template="${1}"
1410
	local _target="${2}"
1411
	local _arch="${3}"
1412
	local _target_arch="${4}"
1413
	local _staging="${5}"
1414

    
1415
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
1416
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
1417
		print_error_pfS
1418
	fi
1419

    
1420
	if [ -n "${_staging}" -a -n "${USE_PKG_REPO_STAGING}" ]; then
1421
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_STAGING}
1422
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_STAGING}
1423
		local _pkg_repo_server_release=${PKG_REPO_SERVER_STAGING}
1424
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_STAGING}
1425
	else
1426
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_DEVEL}
1427
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_DEVEL}
1428
		local _pkg_repo_server_release=${PKG_REPO_SERVER_RELEASE}
1429
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_RELEASE}
1430
	fi
1431

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

    
1434
	sed \
1435
		-e "s/%%ARCH%%/${_target_arch}/" \
1436
		-e "s/%%PKG_REPO_BRANCH_DEVEL%%/${_pkg_repo_branch_devel}/g" \
1437
		-e "s/%%PKG_REPO_BRANCH_RELEASE%%/${_pkg_repo_branch_release}/g" \
1438
		-e "s,%%PKG_REPO_SERVER_DEVEL%%,${_pkg_repo_server_devel},g" \
1439
		-e "s,%%PKG_REPO_SERVER_RELEASE%%,${_pkg_repo_server_release},g" \
1440
		-e "s,%%POUDRIERE_PORTS_NAME%%,${POUDRIERE_PORTS_NAME},g" \
1441
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
1442
		${_template} \
1443
		> ${_target}
1444
}
1445

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

    
1454
	if [ ! -f ${PKG_REPO_PATH} ]; then
1455
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
1456
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
1457

    
1458
		update_freebsd_sources
1459

    
1460
		local _arch=$(uname -m)
1461
		setup_pkg_repo \
1462
			${PKG_REPO_DEFAULT} \
1463
			${PKG_REPO_PATH} \
1464
			${_arch} \
1465
			${_arch} \
1466
			"staging"
1467

    
1468
		# Use fingerprint keys from repo
1469
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
1470
			${PKG_REPO_PATH}
1471
	fi
1472

    
1473
	pkg install ${PRODUCT_NAME}-builder
1474
}
1475

    
1476
# Updates FreeBSD sources
1477
update_freebsd_sources() {
1478
	if [ "${1}" = "full" ]; then
1479
		local _full=1
1480
		local _clone_params=""
1481
	else
1482
		local _full=0
1483
		local _clone_params="--depth 1 --single-branch"
1484
	fi
1485

    
1486
	if [ ! -d "${FREEBSD_SRC_DIR}" ]; then
1487
		mkdir -p ${FREEBSD_SRC_DIR}
1488
	fi
1489

    
1490
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
1491
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
1492
		return
1493
	fi
1494

    
1495
	echo -n ">>> Obtaining FreeBSD sources ${FREEBSD_BRANCH}..."
1496
	local _FREEBSD_BRANCH=${FREEBSD_BRANCH:-"devel"}
1497
	local _CLONE=1
1498

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

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

    
1513
	if [ ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1514
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1515
		print_error_pfS
1516
	fi
1517

    
1518
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1519
		( cd ${FREEBSD_SRC_DIR} && git checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1520
	fi
1521
	echo "Done!"
1522
}
1523

    
1524
pkg_chroot() {
1525
	local _root="${1}"
1526
	shift
1527

    
1528
	if [ $# -eq 0 ]; then
1529
		return -1
1530
	fi
1531

    
1532
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1533
		return -1
1534
	fi
1535

    
1536
	mkdir -p \
1537
		${SCRATCHDIR}/pkg_cache \
1538
		${_root}/var/cache/pkg \
1539
		${_root}/dev
1540

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

    
1551
	return $result
1552
}
1553

    
1554

    
1555
pkg_chroot_add() {
1556
	if [ -z "${1}" -o -z "${2}" ]; then
1557
		return 1
1558
	fi
1559

    
1560
	local _target="${1}"
1561
	local _pkg="$(get_pkg_name ${2}).txz"
1562

    
1563
	if [ ! -d "${_target}" ]; then
1564
		echo ">>> ERROR: Target dir ${_target} not found"
1565
		print_error_pfS
1566
	fi
1567

    
1568
	if [ ! -f ${CORE_PKG_ALL_PATH}/${_pkg} ]; then
1569
		echo ">>> ERROR: Package ${_pkg} not found"
1570
		print_error_pfS
1571
	fi
1572

    
1573
	cp ${CORE_PKG_ALL_PATH}/${_pkg} ${_target}
1574
	pkg_chroot ${_target} add /${_pkg}
1575
	rm -f ${_target}/${_pkg}
1576
}
1577

    
1578
pkg_bootstrap() {
1579
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1580

    
1581
	setup_pkg_repo \
1582
		${PKG_REPO_DEFAULT} \
1583
		${_root}${PKG_REPO_PATH} \
1584
		${TARGET} \
1585
		${TARGET_ARCH} \
1586
		"staging"
1587

    
1588
	pkg_chroot ${_root} bootstrap -f
1589
}
1590

    
1591
# This routine assists with installing various
1592
# freebsd ports files into the pfsense-fs staging
1593
# area.
1594
install_pkg_install_ports() {
1595
	local MAIN_PKG="${1}"
1596

    
1597
	if [ -z "${MAIN_PKG}" ]; then
1598
		MAIN_PKG=${PRODUCT_NAME}
1599
	fi
1600

    
1601
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1602

    
1603
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1604
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1605

    
1606
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1607
		mkdir -p ${SCRATCHDIR}/pkg_cache
1608

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

    
1624
staginareas_clean_each_run() {
1625
	echo -n ">>> Cleaning build directories: "
1626
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1627
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1628
		echo -n "$BASENAME "
1629
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1630
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1631
	fi
1632
	echo "Done!"
1633
}
1634

    
1635
# Imported from FreeSBIE
1636
buildkernel() {
1637
	if [ -n "${NO_BUILDKERNEL}" ]; then
1638
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1639
		return
1640
	fi
1641

    
1642
	if [ -z "${KERNCONF}" ]; then
1643
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1644
		print_error_pfS
1645
	fi
1646

    
1647
	if [ -n "${KERNELCONF}" ]; then
1648
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1649
		export KERNCONF=$(basename ${KERNELCONF})
1650
	fi
1651

    
1652
	echo ">>> KERNCONFDIR: ${KERNCONFDIR}"
1653
	echo ">>> ARCH:        ${TARGET_ARCH}"
1654

    
1655
	makeargs="${MAKEJ}"
1656
	echo ">>> Builder is running the command: script -aq $LOGFILE make $makeargs buildkernel KERNCONF=${KERNCONF}" | tee -a $LOGFILE
1657
	(script -q $LOGFILE make -C ${FREEBSD_SRC_DIR} $makeargs buildkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1658
}
1659

    
1660
# Imported from FreeSBIE
1661
installkernel() {
1662
	local _destdir=${1:-${KERNEL_DESTDIR}}
1663

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

    
1669
	if [ -n "${KERNELCONF}" ]; then
1670
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1671
		export KERNCONF=$(basename ${KERNELCONF})
1672
	fi
1673

    
1674
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1675
	makeargs="${MAKEJ} DESTDIR=${_destdir}"
1676
	echo ">>> Builder is running the command: script -aq $LOGFILE make ${makeargs} installkernel KERNCONF=${KERNCONF}"  | tee -a $LOGFILE
1677
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1678
	gzip -f9 ${_destdir}/boot/kernel/kernel
1679
}
1680

    
1681
# Launch is ran first to setup a few variables that we need
1682
# Imported from FreeSBIE
1683
launch() {
1684
	if [ "$(id -u)" != "0" ]; then
1685
		echo "Sorry, this must be done as root."
1686
	fi
1687

    
1688
	echo ">>> Operation $0 has started at $(date)"
1689
}
1690

    
1691
finish() {
1692
	echo ">>> Operation $0 has ended at $(date)"
1693
}
1694

    
1695
pkg_repo_rsync() {
1696
	local _repo_path_param="${1}"
1697
	local _ignore_final_rsync="${2}"
1698

    
1699
	if [ -z "${_repo_path_param}" -o ! -d "${_repo_path_param}" ]; then
1700
		return
1701
	fi
1702

    
1703
	if [ -n "${SKIP_FINAL_RSYNC}" ]; then
1704
		_ignore_final_rsync="1"
1705
	fi
1706

    
1707
	# Sanitize path
1708
	_repo_path=$(realpath ${_repo_path_param})
1709

    
1710
	local _repo_dir=$(dirname ${_repo_path})
1711
	local _repo_base=$(basename ${_repo_path})
1712

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

    
1716
	if [ -z "${LOGFILE}" ]; then
1717
		local _logfile="/dev/null"
1718
	else
1719
		local _logfile="${LOGFILE}"
1720
	fi
1721

    
1722
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" ]; then
1723

    
1724
		# Detect poudriere directory structure
1725
		if [ -L "${_repo_path}/.latest" ]; then
1726
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1727
		else
1728
			local _real_repo_path=${_repo_path}
1729
		fi
1730

    
1731
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1732
		############ ATTENTION ##############
1733
		#
1734
		# For some reason pkg-repo fail without / in the end of directory name
1735
		# so removing it will break command
1736
		#
1737
		# https://github.com/freebsd/pkg/issues/1364
1738
		#
1739
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1740
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1741
			echo "Done!" | tee -a ${_logfile}
1742
		else
1743
			echo "Failed!" | tee -a ${_logfile}
1744
			echo ">>> ERROR: An error occurred trying to sign repo"
1745
			print_error_pfS
1746
		fi
1747

    
1748
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1749
		if [ -e ${_pkgfile} ]; then
1750
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1751

    
1752
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1753
			    > ${_pkgfile}.sig 2>/dev/null; then
1754
				echo "Done!" | tee -a ${_logfile}
1755
			else
1756
				echo "Failed!" | tee -a ${_logfile}
1757
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1758
				print_error_pfS
1759
			fi
1760
		fi
1761
	fi
1762

    
1763
	if [ -n "${DO_NOT_UPLOAD}" ]; then
1764
		return
1765
	fi
1766

    
1767
	# Make sure destination directory exist
1768
	ssh -p ${PKG_RSYNC_SSH_PORT} \
1769
		${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME} \
1770
		"mkdir -p ${PKG_RSYNC_DESTDIR}"
1771

    
1772
	echo -n ">>> Sending updated repository to ${PKG_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1773
	if script -aq ${_logfile} rsync -Have "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1774
		--timeout=60 --delete-delay ${_repo_path} \
1775
		${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1776
	then
1777
		echo "Done!" | tee -a ${_logfile}
1778
	else
1779
		echo "Failed!" | tee -a ${_logfile}
1780
		echo ">>> ERROR: An error occurred sending repo to remote hostname"
1781
		print_error_pfS
1782
	fi
1783

    
1784
	if [ -z "${USE_PKG_REPO_STAGING}" -o -n "${_ignore_final_rsync}" ]; then
1785
		return
1786
	fi
1787

    
1788
	if [ -n "${_IS_RELEASE}" -o "${_repo_path_param}" = "${CORE_PKG_PATH}" ]; then
1789
		# Send .real* directories first to prevent having a broken repo while transfer happens
1790
		local _cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1791
			--timeout=60 ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1792
			--include=\"/*\" --include=\"*/.real*\" --include=\"*/.real*/*\" \
1793
			--exclude=\"*\" \
1794
			${PKG_FINAL_RSYNC_USERNAME}@${PKG_FINAL_RSYNC_HOSTNAME}:${PKG_FINAL_RSYNC_DESTDIR}"
1795

    
1796
		echo -n ">>> Sending updated packages to ${PKG_FINAL_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1797
		if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1798
			${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME} ${_cmd} >/dev/null 2>&1; then
1799
			echo "Done!" | tee -a ${_logfile}
1800
		else
1801
			echo "Failed!" | tee -a ${_logfile}
1802
			echo ">>> ERROR: An error occurred sending repo to final hostname"
1803
			print_error_pfS
1804
		fi
1805

    
1806
		_cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1807
			--timeout=60 --delete-delay ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1808
			${PKG_FINAL_RSYNC_USERNAME}@${PKG_FINAL_RSYNC_HOSTNAME}:${PKG_FINAL_RSYNC_DESTDIR}"
1809

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

    
1822
poudriere_possible_archs() {
1823
	local _arch=$(uname -m)
1824
	local _archs=""
1825

    
1826
	# If host is amd64, we'll create both repos, and if possible armv6
1827
	if [ "${_arch}" = "amd64" ]; then
1828
		_archs="amd64.amd64"
1829

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

    
1834
			if binmiscctl lookup armv6 >/dev/null 2>&1; then
1835
				_archs="${_archs} arm.armv6"
1836
			fi
1837
		fi
1838
	fi
1839

    
1840
	if [ -n "${ARCH_LIST}" ]; then
1841
		local _found=0
1842
		for _desired_arch in ${ARCH_LIST}; do
1843
			_found=0
1844
			for _possible_arch in ${_archs}; do
1845
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1846
					_found=1
1847
					break
1848
				fi
1849
			done
1850
			if [ ${_found} -eq 0 ]; then
1851
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1852
				print_error_pfS
1853
			fi
1854
		done
1855
		_archs="${ARCH_LIST}"
1856
	fi
1857

    
1858
	echo ${_archs}
1859
}
1860

    
1861
poudriere_jail_name() {
1862
	local _jail_arch="${1}"
1863

    
1864
	if [ -z "${_jail_arch}" ]; then
1865
		return 1
1866
	fi
1867

    
1868
	# Remove arch
1869
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1870
}
1871

    
1872
poudriere_rename_ports() {
1873
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1874
		return;
1875
	fi
1876

    
1877
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1878

    
1879
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1880

    
1881
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1882
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1883
		local _pdir=$(dirname ${d})
1884
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1885
		local _plist=""
1886

    
1887
		if [ -e ${_pdir}/${_pname} ]; then
1888
			rm -rf ${_pdir}/${_pname}
1889
		fi
1890

    
1891
		cp -r ${d} ${_pdir}/${_pname}
1892

    
1893
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1894
			_plist=${_pdir}/${_pname}/pkg-plist
1895
		fi
1896

    
1897
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1898
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1899
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1900
			${_pdir}/${_pname}/Makefile \
1901
			${_pdir}/${_pname}/pkg-descr ${_plist}
1902

    
1903
		# PHP module is special
1904
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1905
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1906
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1907
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1908
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1909
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1910
				${_pdir}/${_pname}/files/config.m4
1911

    
1912
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1913
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1914
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1915
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1916
				${_pdir}/${_pname}/files/pfSense.c \
1917
				${_pdir}/${_pname}/files/php_pfSense.h
1918
		fi
1919

    
1920
		if [ -d ${_pdir}/${_pname}/files ]; then
1921
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1922
				local _fddir=$(dirname ${fd})
1923
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1924

    
1925
				mv ${fd} ${_fddir}/${_fdname}
1926
			done
1927
		fi
1928
	done
1929
	echo "Done!" | tee -a ${LOGFILE}
1930
}
1931

    
1932
poudriere_create_ports_tree() {
1933
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1934

    
1935
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1936
		local _branch=""
1937
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1938
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1939
			print_error_pfS
1940
		fi
1941
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1942
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1943
		fi
1944
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1945
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git -U ${POUDRIERE_PORTS_GIT_URL} ${_branch} >/dev/null 2>&1; then
1946
			echo "" | tee -a ${LOGFILE}
1947
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1948
			print_error_pfS
1949
		fi
1950
		echo "Done!" | tee -a ${LOGFILE}
1951
		poudriere_rename_ports
1952
	fi
1953
}
1954

    
1955
poudriere_init() {
1956
	local _error=0
1957
	local _archs=$(poudriere_possible_archs)
1958

    
1959
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1960

    
1961
	# Sanity checks
1962
	if [ -z "${ZFS_TANK}" ]; then
1963
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1964
		error=1
1965
	fi
1966

    
1967
	if [ -z "${ZFS_ROOT}" ]; then
1968
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1969
		error=1
1970
	fi
1971

    
1972
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1973
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1974
		error=1
1975
	fi
1976

    
1977
	if [ ${_error} -eq 1 ]; then
1978
		print_error_pfS
1979
	fi
1980

    
1981
	# Check if zpool exists
1982
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
1983
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
1984
		print_error_pfS
1985
	fi
1986

    
1987
	# Check if zfs rootfs exists
1988
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1989
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
1990
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
1991
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1992
			echo "Done!"
1993
		else
1994
			echo "Failed!"
1995
			print_error_pfS
1996
		fi
1997
	fi
1998

    
1999
	# Make sure poudriere is installed
2000
	if ! pkg info --quiet poudriere-devel; then
2001
		echo ">>> Installing poudriere-devel..." | tee -a ${LOGFILE}
2002
		if ! pkg install poudriere-devel >/dev/null 2>&1; then
2003
			echo ">>> ERROR: poudriere-devel was not installed, aborting..." | tee -a ${LOGFILE}
2004
			print_error_pfS
2005
		fi
2006
	fi
2007

    
2008
	# Create poudriere.conf
2009
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
2010
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
2011
		print_error_pfS
2012
	fi
2013
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
2014
	cat <<EOF >/usr/local/etc/poudriere.conf
2015
ZPOOL=${ZFS_TANK}
2016
ZROOTFS=${ZFS_ROOT}
2017
RESOLV_CONF=/etc/resolv.conf
2018
BASEFS=/usr/local/poudriere
2019
USE_PORTLINT=no
2020
USE_TMPFS=yes
2021
NOLINUX=yes
2022
DISTFILES_CACHE=/usr/ports/distfiles
2023
CHECK_CHANGED_OPTIONS=yes
2024
CHECK_CHANGED_DEPS=yes
2025
ATOMIC_PACKAGE_REPOSITORY=yes
2026
COMMIT_PACKAGES_ON_FAILURE=no
2027
KEEP_OLD_PACKAGES=yes
2028
KEEP_OLD_PACKAGES_COUNT=5
2029
EOF
2030

    
2031
	# Create specific items conf
2032
	[ ! -d /usr/local/etc/poudriere.d ] \
2033
		&& mkdir -p /usr/local/etc/poudriere.d
2034

    
2035
	# Create DISTFILES_CACHE if it doesn't exist
2036
	if [ ! -d /usr/ports/distfiles ]; then
2037
		mkdir -p /usr/ports/distfiles
2038
	fi
2039

    
2040
	# Remove old jails
2041
	for jail_arch in ${_archs}; do
2042
		jail_name=$(poudriere_jail_name ${jail_arch})
2043

    
2044
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2045
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
2046
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
2047
		fi
2048
	done
2049

    
2050
	# Remove old ports tree
2051
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2052
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
2053
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
2054
	fi
2055

    
2056
	local native_xtools=""
2057
	# Now we are ready to create jails
2058
	for jail_arch in ${_archs}; do
2059
		jail_name=$(poudriere_jail_name ${jail_arch})
2060

    
2061
		if [ "${jail_arch}" = "arm.armv6" ]; then
2062
			native_xtools="-x"
2063
		else
2064
			native_xtools=""
2065
		fi
2066

    
2067
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2068
		# XXX: Change -m to git when it's available in poudriere
2069
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
2070
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
2071
			echo "" | tee -a ${LOGFILE}
2072
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2073
			print_error_pfS
2074
		fi
2075
		echo "Done!" | tee -a ${LOGFILE}
2076
	done
2077

    
2078
	poudriere_create_ports_tree
2079

    
2080
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
2081
}
2082

    
2083
poudriere_update_jails() {
2084
	local _archs=$(poudriere_possible_archs)
2085

    
2086
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2087

    
2088
	local native_xtools=""
2089
	for jail_arch in ${_archs}; do
2090
		jail_name=$(poudriere_jail_name ${jail_arch})
2091

    
2092
		local _create_or_update="-u"
2093
		local _create_or_update_text="Updating"
2094
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2095
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
2096
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
2097
			_create_or_update_text="Creating"
2098
		fi
2099

    
2100
		if [ "${jail_arch}" = "arm.armv6" ]; then
2101
			native_xtools="-x"
2102
		else
2103
			native_xtools=""
2104
		fi
2105

    
2106
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2107
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
2108
			echo "" | tee -a ${LOGFILE}
2109
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2110
			print_error_pfS
2111
		fi
2112
		echo "Done!" | tee -a ${LOGFILE}
2113
	done
2114
}
2115

    
2116
poudriere_update_ports() {
2117
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2118

    
2119
	# Create ports tree if necessary
2120
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2121
		poudriere_create_ports_tree
2122
	else
2123
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2124
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
2125
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
2126
		echo "Done!" | tee -a ${LOGFILE}
2127
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2128
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
2129
		echo "Done!" | tee -a ${LOGFILE}
2130
		poudriere_rename_ports
2131
	fi
2132
}
2133

    
2134
poudriere_bulk() {
2135
	local _archs=$(poudriere_possible_archs)
2136

    
2137
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2138

    
2139
	if [ -z "${DO_NOT_UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
2140
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
2141
		print_error_pfS
2142
	fi
2143

    
2144
	rm -f ${LOGFILE}
2145

    
2146
	poudriere_create_ports_tree
2147

    
2148
	[ -d /usr/local/etc/poudriere.d ] || \
2149
		mkdir -p /usr/local/etc/poudriere.d
2150

    
2151
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
2152
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" /usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
2153
	fi
2154

    
2155
	# Change version of pfSense meta ports for snapshots
2156
	if [ -z "${_IS_RELEASE}" ]; then
2157
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
2158
		sed -i '' \
2159
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
2160
			-e "/^PORTREVISION=/d" \
2161
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile
2162
	fi
2163

    
2164
	for jail_arch in ${_archs}; do
2165
		jail_name=$(poudriere_jail_name ${jail_arch})
2166

    
2167
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2168
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
2169
			continue
2170
		fi
2171

    
2172
		if [ -f "${POUDRIERE_BULK}.${jail_arch}" ]; then
2173
			_ref_bulk="${POUDRIERE_BULK}.${jail_arch}"
2174
		else
2175
			_ref_bulk="${POUDRIERE_BULK}"
2176
		fi
2177

    
2178
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}
2179
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
2180

    
2181
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
2182
			echo ">>> ERROR: Something went wrong..."
2183
			print_error_pfS
2184
		fi
2185

    
2186
		echo ">>> Cleaning up old packages from repo..."
2187
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
2188
			echo ">>> ERROR: Something went wrong..."
2189
			print_error_pfS
2190
		fi
2191

    
2192
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
2193
	done
2194
}
2195

    
2196
# This routine is called to write out to stdout
2197
# a string. The string is appended to $SNAPSHOTSLOGFILE
2198
# and we scp the log file to the builder host if
2199
# needed for the real time logging functions.
2200
snapshots_update_status() {
2201
	if [ -z "$1" ]; then
2202
		return
2203
	fi
2204
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
2205
		return
2206
	fi
2207
	echo "$*"
2208
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
2209
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2210
		LU=$(cat $SNAPSHOTSLASTUPDATE 2>/dev/null)
2211
		CT=$(date "+%H%M%S")
2212
		# Only update every minute
2213
		if [ "$LU" != "$CT" ]; then
2214
			ssh ${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP} \
2215
				"mkdir -p ${SNAPSHOTS_RSYNCLOGS}"
2216
			scp -q $SNAPSHOTSLOGFILE \
2217
				${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log
2218
			date "+%H%M%S" > $SNAPSHOTSLASTUPDATE
2219
		fi
2220
	fi
2221
}
2222

    
2223
# Copy the current log file to $filename.old on
2224
# the snapshot www server (real time logs)
2225
snapshots_rotate_logfile() {
2226
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2227
		scp -q $SNAPSHOTSLOGFILE \
2228
			${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log.old
2229
	fi
2230

    
2231
	# Cleanup log file
2232
	rm -f $SNAPSHOTSLOGFILE;    touch $SNAPSHOTSLOGFILE
2233
	rm -f $SNAPSHOTSLASTUPDATE; touch $SNAPSHOTSLASTUPDATE
2234

    
2235
}
2236

    
2237
create_sha256() {
2238
	local _file="${1}"
2239

    
2240
	if [ ! -f "${_file}" ]; then
2241
		return 1
2242
	fi
2243

    
2244
	( \
2245
		cd $(dirname ${_file}) && \
2246
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
2247
	)
2248
}
2249

    
2250
snapshots_create_latest_symlink() {
2251
	local _image="${1}"
2252

    
2253
	if [ -z "${_image}" ]; then
2254
		return
2255
	fi
2256

    
2257
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
2258
		return
2259
	fi
2260

    
2261
	if [ ! -f "${_image}" ]; then
2262
		return
2263
	fi
2264

    
2265
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
2266
	ln -sf $(basename ${_image}) ${_symlink}
2267
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
2268
}
2269

    
2270
snapshots_create_sha256() {
2271
	local _img=""
2272

    
2273
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
2274
		if [ -f "${_img}.gz" ]; then
2275
			_img="${_img}.gz"
2276
		fi
2277
		if [ ! -f "${_img}" ]; then
2278
			continue
2279
		fi
2280
		create_sha256 ${_img}
2281
		snapshots_create_latest_symlink ${_img}
2282
	done
2283

    
2284
	for NANOTYPE in nanobsd nanobsd-vga; do
2285
		for FILESIZE in ${FLASH_SIZE}; do
2286
			FILENAMEFULL="$(nanobsd_image_filename ${FILESIZE} ${NANOTYPE}).gz"
2287

    
2288
			if [ -f $IMAGES_FINAL_DIR/nanobsd/$FILENAMEFULL ]; then
2289
				create_sha256 $IMAGES_FINAL_DIR/nanobsd/$FILENAMEFULL
2290
			fi
2291
		done
2292
	done
2293
}
2294

    
2295
snapshots_scp_files() {
2296
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
2297
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60 --bwlimit=${RSYNCKBYTELIMIT}" #--bwlimit=50
2298
	fi
2299

    
2300
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
2301
	pkg_repo_rsync "${CORE_PKG_PATH}"
2302
	snapshots_update_status ">>> Finished copying core pkg repo"
2303

    
2304
	snapshots_update_status ">>> Copying files to ${RSYNCIP}"
2305

    
2306
	# Ensure directory(s) are available
2307
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/installer"
2308
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/nanobsd"
2309
	if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
2310
		ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/virtualization"
2311
	fi
2312
	# ensure permissions are correct for r+w
2313
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw ${RSYNCPATH}/."
2314
	rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
2315
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/installer/
2316
	rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/nanobsd/* \
2317
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/nanobsd/
2318
	if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
2319
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
2320
			${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/virtualization/
2321
	fi
2322

    
2323
	snapshots_update_status ">>> Finished copying files."
2324
}
(2-2/3)