Project

General

Profile

Download (60.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# builder_common.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2013 BSD Perimeter
7
# Copyright (c) 2013-2016 Electric Sheep Fencing
8
# Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9
# All rights reserved.
10
#
11
# FreeSBIE portions of the code
12
# Copyright (c) 2005 Dario Freni
13
# and copied from FreeSBIE project
14
# All rights reserved.
15
#
16
# Licensed under the Apache License, Version 2.0 (the "License");
17
# you may not use this file except in compliance with the License.
18
# You may obtain a copy of the License at
19
#
20
# http://www.apache.org/licenses/LICENSE-2.0
21
#
22
# Unless required by applicable law or agreed to in writing, software
23
# distributed under the License is distributed on an "AS IS" BASIS,
24
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
# See the License for the specific language governing permissions and
26
# limitations under the License.
27

    
28
if [ -z "${IMAGES_FINAL_DIR}" -o "${IMAGES_FINAL_DIR}" = "/" ]; then
29
	echo "IMAGES_FINAL_DIR is not defined"
30
	print_error_pfS
31
fi
32

    
33
kldload filemon >/dev/null 2>&1
34

    
35
lc() {
36
	echo "${1}" | tr '[[:upper:]]' '[[:lower:]]'
37
}
38

    
39
git_last_commit() {
40
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
41
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
42
	echo ">>> Last known commit $CURRENT_AUTHOR - $CURRENT_COMMIT"
43
	echo "$CURRENT_COMMIT" > $SCRATCHDIR/build_commit_info.txt
44
}
45

    
46
# Create core pkg repository
47
core_pkg_create_repo() {
48
	if [ ! -d "${CORE_PKG_REAL_PATH}/All" ]; then
49
		return
50
	fi
51

    
52
	############ ATTENTION ##############
53
	#
54
	# For some reason pkg-repo fail without / in the end of directory name
55
	# so removing it will break command
56
	#
57
	# https://github.com/freebsd/pkg/issues/1364
58
	#
59
	echo -n ">>> Creating core packages repository... "
60
	if pkg repo -q "${CORE_PKG_REAL_PATH}/"; then
61
		echo "Done!"
62
	else
63
		echo "Failed!"
64
		print_error_pfS
65
	fi
66

    
67
	# Use the same directory structure as poudriere does to avoid
68
	# breaking snapshot repositories during rsync
69
	ln -sf $(basename ${CORE_PKG_REAL_PATH}) ${CORE_PKG_PATH}/.latest
70
	ln -sf .latest/All ${CORE_PKG_ALL_PATH}
71
	ln -sf .latest/digests.txz ${CORE_PKG_PATH}/digests.txz
72
	ln -sf .latest/meta.txz ${CORE_PKG_PATH}/meta.txz
73
	ln -sf .latest/packagesite.txz ${CORE_PKG_PATH}/packagesite.txz
74
}
75

    
76
# Create core pkg (base, kernel)
77
core_pkg_create() {
78
	local _template="${1}"
79
	local _flavor="${2}"
80
	local _version="${3}"
81
	local _root="${4}"
82
	local _findroot="${5}"
83
	local _filter="${6}"
84

    
85
	local _template_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
86

    
87
	# Use default pkg repo to obtain ABI and ALTABI
88
	local _abi=$(sed -e "s/%%ARCH%%/${TARGET_ARCH}/g" \
89
	    ${PKG_REPO_DEFAULT%%.conf}.abi)
90
	local _altabi_arch=$(get_altabi_arch ${TARGET_ARCH})
91
	local _altabi=$(sed -e "s/%%ARCH%%/${_altabi_arch}/g" \
92
	    ${PKG_REPO_DEFAULT%%.conf}.altabi)
93

    
94
	${BUILDER_SCRIPTS}/create_core_pkg.sh \
95
		-t "${_template_path}" \
96
		-f "${_flavor}" \
97
		-v "${_version}" \
98
		-r "${_root}" \
99
		-s "${_findroot}" \
100
		-F "${_filter}" \
101
		-d "${CORE_PKG_REAL_PATH}/All" \
102
		-a "${_abi}" \
103
		-A "${_altabi}" \
104
		|| print_error_pfS
105
}
106

    
107
# This routine will output that something went wrong
108
print_error_pfS() {
109
	echo
110
	echo "####################################"
111
	echo "Something went wrong, check errors!" >&2
112
	echo "####################################"
113
	echo
114
	echo "NOTE: a lot of times you can run './build.sh --clean-builder' to resolve."
115
	echo
116
	[ -n "${LOGFILE}" -a -f "${LOGFILE}" ] && \
117
		echo "Log saved on ${LOGFILE}" && \
118
	echo
119
	kill $$
120
	exit 1
121
}
122

    
123
# This routine will verify that the kernel has been
124
# installed OK to the staging area.
125
ensure_kernel_exists() {
126
	if [ ! -f "$1/boot/kernel/kernel.gz" ]; then
127
		echo ">>> ERROR: Could not locate $1/boot/kernel.gz"
128
		print_error_pfS
129
	fi
130
	KERNEL_SIZE=$(stat -f "%z" $1/boot/kernel/kernel.gz)
131
	if [ "$KERNEL_SIZE" -lt 3500 ]; then
132
		echo ">>> ERROR: Kernel $1/boot/kernel.gz appears to be smaller than it should be: $KERNEL_SIZE"
133
		print_error_pfS
134
	fi
135
}
136

    
137
get_pkg_name() {
138
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
139
}
140

    
141
# This routine builds all related kernels
142
build_all_kernels() {
143
	# Set KERNEL_BUILD_PATH if it has not been set
144
	if [ -z "${KERNEL_BUILD_PATH}" ]; then
145
		KERNEL_BUILD_PATH=$SCRATCHDIR/kernels
146
		echo ">>> KERNEL_BUILD_PATH has not been set. Setting to ${KERNEL_BUILD_PATH}!"
147
	fi
148

    
149
	[ -d "${KERNEL_BUILD_PATH}" ] \
150
		&& rm -rf ${KERNEL_BUILD_PATH}
151

    
152
	# Build embedded kernel
153
	for BUILD_KERNEL in $BUILD_KERNELS; do
154
		unset KERNCONF
155
		unset KERNEL_DESTDIR
156
		unset KERNEL_NAME
157
		export KERNCONF=$BUILD_KERNEL
158
		export KERNEL_DESTDIR="$KERNEL_BUILD_PATH/$BUILD_KERNEL"
159
		export KERNEL_NAME=${BUILD_KERNEL}
160

    
161
		LOGFILE="${BUILDER_LOGS}/kernel.${KERNCONF}.${TARGET}.log"
162
		echo ">>> Building $BUILD_KERNEL kernel."  | tee -a ${LOGFILE}
163

    
164
		if [ -n "${NO_BUILDKERNEL}" -a -f "${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz" ]; then
165
			echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
166
			continue
167
		fi
168

    
169
		buildkernel
170

    
171
		echo ">>> Staging $BUILD_KERNEL kernel..." | tee -a ${LOGFILE}
172
		installkernel
173

    
174
		ensure_kernel_exists $KERNEL_DESTDIR
175

    
176
		echo ">>> Creating pkg of $KERNEL_NAME-debug kernel to staging area..."  | tee -a ${LOGFILE}
177
		core_pkg_create kernel-debug ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} \
178
		    "./usr/lib/debug/boot" \*.debug
179
		rm -rf ${KERNEL_DESTDIR}/usr
180

    
181
		echo ">>> Creating pkg of $KERNEL_NAME kernel to staging area..."  | tee -a ${LOGFILE}
182
		core_pkg_create kernel ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} "./boot/kernel ./boot/modules"
183

    
184
		rm -rf $KERNEL_DESTDIR 2>&1 1>/dev/null
185
	done
186
}
187

    
188
install_default_kernel() {
189
	if [ -z "${1}" ]; then
190
		echo ">>> ERROR: install_default_kernel called without a kernel config name"| tee -a ${LOGFILE}
191
		print_error_pfS
192
	fi
193

    
194
	export KERNEL_NAME="${1}"
195

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

    
198
	# Copy kernel package to chroot, otherwise pkg won't find it to install
199
	if ! pkg_chroot_add ${FINAL_CHROOT_DIR} kernel-${KERNEL_NAME}; then
200
		echo ">>> ERROR: Error installing kernel package $(get_pkg_name kernel-${KERNEL_NAME}).txz" | tee -a ${LOGFILE}
201
		print_error_pfS
202
	fi
203

    
204
	# Set kernel pkg as vital to avoid user end up removing it for any reason
205
	pkg_chroot ${FINAL_CHROOT_DIR} set -v 1 -y $(get_pkg_name kernel-${KERNEL_NAME})
206

    
207
	if [ ! -f $FINAL_CHROOT_DIR/boot/kernel/kernel.gz ]; then
208
		echo ">>> ERROR: No kernel installed on $FINAL_CHROOT_DIR and the resulting image will be unusable. STOPPING!" | tee -a ${LOGFILE}
209
		print_error_pfS
210
	fi
211
	mkdir -p $FINAL_CHROOT_DIR/pkgs
212
	if [ -z "${2}" -o -n "${INSTALL_EXTRA_KERNELS}" ]; then
213
		cp ${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz $FINAL_CHROOT_DIR/pkgs
214
		if [ -n "${INSTALL_EXTRA_KERNELS}" ]; then
215
			for _EXTRA_KERNEL in $INSTALL_EXTRA_KERNELS; do
216
				_EXTRA_KERNEL_PATH=${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${_EXTRA_KERNEL}).txz
217
				if [ -f "${_EXTRA_KERNEL_PATH}" ]; then
218
					echo -n ". adding ${_EXTRA_KERNEL_PATH} on image /pkgs folder"
219
					cp ${_EXTRA_KERNEL_PATH} $FINAL_CHROOT_DIR/pkgs
220
				else
221
					echo ">>> ERROR: Requested kernel $(get_pkg_name kernel-${_EXTRA_KERNEL}).txz was not found to be put on image /pkgs folder!"
222
					print_error_pfS
223
				fi
224
			done
225
		fi
226
	fi
227
	echo "Done." | tee -a ${LOGFILE}
228

    
229
	unset KERNEL_NAME
230
}
231

    
232
# This builds FreeBSD (make buildworld)
233
# Imported from FreeSBIE
234
make_world() {
235
	LOGFILE=${BUILDER_LOGS}/buildworld.${TARGET}
236
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
237
	if [ -n "${NO_BUILDWORLD}" ]; then
238
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
239
		return
240
	fi
241

    
242
	echo ">>> $(LC_ALL=C date) - Starting build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
243
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -K -s ${FREEBSD_SRC_DIR} \
244
		|| print_error_pfS
245
	echo ">>> $(LC_ALL=C date) - Finished build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
246

    
247
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
248
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
249

    
250
	[ -d "${INSTALLER_CHROOT_DIR}" ] \
251
		|| mkdir -p ${INSTALLER_CHROOT_DIR}
252

    
253
	echo ">>> Installing world with bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
254
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -i -K \
255
		-s ${FREEBSD_SRC_DIR} \
256
		-d ${INSTALLER_CHROOT_DIR} \
257
		|| print_error_pfS
258

    
259
	# Copy additional installer scripts
260
	install -o root -g wheel -m 0755 ${BUILDER_TOOLS}/installer/*.sh \
261
		${INSTALLER_CHROOT_DIR}/root
262

    
263
	# XXX set root password since we don't have nullok enabled
264
	pw -R ${INSTALLER_CHROOT_DIR} usermod root -w yes
265

    
266
	echo ">>> Installing world without bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
267
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -K \
268
		-s ${FREEBSD_SRC_DIR} \
269
		-d ${STAGE_CHROOT_DIR} \
270
		|| print_error_pfS
271

    
272
	# Use the builder cross compiler from obj to produce the final binary.
273
	BUILD_CC="${MAKEOBJDIRPREFIX}${FREEBSD_SRC_DIR}/${TARGET}.${TARGET_ARCH}/tmp/usr/bin/cc"
274

    
275
	[ -f "${BUILD_CC}" ] || print_error_pfS
276

    
277
	# XXX It must go to the scripts
278
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
279
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
280
	makeargs="CC=${BUILD_CC} DESTDIR=${STAGE_CHROOT_DIR}"
281
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
282
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
283
	# XXX FIX IT
284
#	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
285
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
286

    
287
	if [ "${PRODUCT_NAME}" = "pfSense" -a -n "${GNID_REPO_BASE}" ]; then
288
		echo ">>> Building gnid... " | tee -a ${LOGFILE}
289
		(\
290
			cd ${GNID_SRC_DIR} && \
291
			make \
292
				CC=${BUILD_CC} \
293
				INCLUDE_DIR=${GNID_INCLUDE_DIR} \
294
				LIBCRYPTO_DIR=${GNID_LIBCRYPTO_DIR} \
295
			clean gnid \
296
		) || print_error_pfS
297
		install -o root -g wheel -m 0700 ${GNID_SRC_DIR}/gnid \
298
			${STAGE_CHROOT_DIR}/usr/sbin \
299
			|| print_error_pfS
300
		install -o root -g wheel -m 0700 ${GNID_SRC_DIR}/gnid \
301
			${INSTALLER_CHROOT_DIR}/usr/sbin \
302
			|| print_error_pfS
303
	fi
304

    
305
	unset makeargs
306
}
307

    
308
# This routine creates a ova image that contains
309
# a ovf and vmdk file. These files can be imported
310
# right into vmware or virtual box.
311
# (and many other emulation platforms)
312
# http://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
313
create_ova_image() {
314
	# XXX create a .ovf php creator that you can pass:
315
	#     1. populatedSize
316
	#     2. license
317
	#     3. product name
318
	#     4. version
319
	#     5. number of network interface cards
320
	#     6. allocationUnits
321
	#     7. capacity
322
	#     8. capacityAllocationUnits
323

    
324
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
325

    
326
	local _mntdir=${OVA_TMP}/mnt
327

    
328
	if [ -d "${_mntdir}" ]; then
329
		local _dev
330
		# XXX Root cause still didn't found but it doesn't umount
331
		#     properly on looped builds and then require this extra
332
		#     check
333
		while true; do
334
			_dev=$(mount -p ${_mntdir} 2>/dev/null | awk '{print $1}')
335
			[ $? -ne 0 -o -z "${_dev}" ] \
336
				&& break
337
			umount -f ${_mntdir}
338
			mdconfig -d -u ${_dev#/dev/}
339
		done
340
		chflags -R noschg ${OVA_TMP}
341
		rm -rf ${OVA_TMP}
342
	fi
343

    
344
	mkdir -p $(dirname ${OVAPATH})
345

    
346
	mkdir -p ${_mntdir}
347

    
348
	if [ -z "${OVA_SWAP_PART_SIZE_IN_GB}" -o "${OVA_SWAP_PART_SIZE_IN_GB}" = "0" ]; then
349
		# first partition size (freebsd-ufs)
350
		local OVA_FIRST_PART_SIZE_IN_GB=${VMDK_DISK_CAPACITY_IN_GB}
351
		# Calculate real first partition size, removing 256 blocks (131072 bytes) beginning/loader
352
		local OVA_FIRST_PART_SIZE=$((${OVA_FIRST_PART_SIZE_IN_GB}*1024*1024*1024-131072))
353
		# Unset swap partition size variable
354
		unset OVA_SWAP_PART_SIZE
355
		# Parameter used by mkimg
356
		unset OVA_SWAP_PART_PARAM
357
	else
358
		# first partition size (freebsd-ufs)
359
		local OVA_FIRST_PART_SIZE_IN_GB=$((VMDK_DISK_CAPACITY_IN_GB-OVA_SWAP_PART_SIZE_IN_GB))
360
		# Use first partition size in g
361
		local OVA_FIRST_PART_SIZE="${OVA_FIRST_PART_SIZE_IN_GB}g"
362
		# Calculate real swap size, removing 256 blocks (131072 bytes) beginning/loader
363
		local OVA_SWAP_PART_SIZE=$((${OVA_SWAP_PART_SIZE_IN_GB}*1024*1024*1024-131072))
364
		# Parameter used by mkimg
365
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
366
	fi
367

    
368
	# Prepare folder to be put in image
369
	customize_stagearea_for_image "ova"
370
	install_default_kernel ${DEFAULT_KERNEL} "no"
371

    
372
	# Fill fstab
373
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
374
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
375
	if [ -n "${OVA_SWAP_PART_SIZE}" ]; then
376
		echo "/dev/gpt/swap0	none	swap	sw	0	0" >> ${FINAL_CHROOT_DIR}/etc/fstab
377
	fi
378

    
379
	# Create / partition
380
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
381
	truncate -s ${OVA_FIRST_PART_SIZE} ${OVA_TMP}/${OVFUFS}
382
	local _md=$(mdconfig -a -f ${OVA_TMP}/${OVFUFS})
383
	trap "mdconfig -d -u ${_md}; return" 1 2 15 EXIT
384

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

    
387
	if ! mount /dev/${_md} ${_mntdir} 2>&1 >>${LOGFILE}; then
388
		echo "Failed!" | tee -a ${LOGFILE}
389
		echo ">>> ERROR: Error mounting temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
390
		print_error_pfS
391
	fi
392
	trap "sync; sleep 3; umount ${_mntdir} || umount -f ${_mntdir}; mdconfig -d -u ${_md}; return" 1 2 15 EXIT
393

    
394
	echo "Done!" | tee -a ${LOGFILE}
395

    
396
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
397

    
398
	sync
399
	sleep 3
400
	umount ${_mntdir} || umount -f ${_mntdir} >>${LOGFILE} 2>&1
401
	mdconfig -d -u ${_md}
402
	trap "-" 1 2 15 EXIT
403

    
404
	# Create raw disk
405
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
406
	mkimg \
407
		-s gpt \
408
		-f raw \
409
		-b ${FINAL_CHROOT_DIR}/boot/pmbr \
410
		-p freebsd-boot:=${FINAL_CHROOT_DIR}/boot/gptboot \
411
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
412
		${OVA_SWAP_PART_PARAM} \
413
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
414

    
415
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
416
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
417
			rm -f ${OVA_TMP}/${OVFUFS}
418
		fi
419
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
420
			rm -f ${OVA_TMP}/${OVFRAW}
421
		fi
422
		echo "Failed!" | tee -a ${LOGFILE}
423
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
424
		print_error_pfS
425
	fi
426
	echo "Done!" | tee -a ${LOGFILE}
427

    
428
	# We don't need it anymore
429
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
430

    
431
	# Convert raw to vmdk
432
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
433
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
434

    
435
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
436
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
437
			rm -f ${OVA_TMP}/${OVFRAW}
438
		fi
439
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
440
			rm -f ${OVA_TMP}/${OVFVMDK}
441
		fi
442
		echo "Failed!" | tee -a ${LOGFILE}
443
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
444
		print_error_pfS
445
	fi
446
	echo "Done!" | tee -a ${LOGFILE}
447

    
448
	rm -f ${OVA_TMP}/${OVFRAW}
449

    
450
	ova_setup_ovf_template
451

    
452
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
453
	# Create OVA file for vmware
454
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
455
	echo "Done!" | tee -a ${LOGFILE}
456
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
457

    
458
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
459
}
460

    
461
# called from create_ova_image
462
ova_setup_ovf_template() {
463
	if [ ! -f ${OVFTEMPLATE} ]; then
464
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
465
		print_error_pfS
466
	fi
467

    
468
	#  OperatingSystemSection (${PRODUCT_NAME}.ovf)
469
	#  42   FreeBSD 32-Bit
470
	#  78   FreeBSD 64-Bit
471
	if [ "${TARGET}" = "amd64" ]; then
472
		local _os_id="78"
473
		local _os_type="freebsd64Guest"
474
		local _os_descr="FreeBSD 64-Bit"
475
	else
476
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
477
		print_error_pfS
478
	fi
479

    
480
	local POPULATED_SIZE=$(du -d0 -k $FINAL_CHROOT_DIR | cut -f1)
481
	local POPULATED_SIZE_IN_BYTES=$((${POPULATED_SIZE}*1024))
482
	local VMDK_FILE_SIZE=$(stat -f "%z" ${OVA_TMP}/${OVFVMDK})
483

    
484
	sed \
485
		-e "s,%%VMDK_FILE_SIZE%%,${VMDK_FILE_SIZE},g" \
486
		-e "s,%%VMDK_DISK_CAPACITY_IN_GB%%,${VMDK_DISK_CAPACITY_IN_GB},g" \
487
		-e "s,%%POPULATED_SIZE_IN_BYTES%%,${POPULATED_SIZE_IN_BYTES},g" \
488
		-e "s,%%OS_ID%%,${_os_id},g" \
489
		-e "s,%%OS_TYPE%%,${_os_type},g" \
490
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
491
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
492
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
493
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
494
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
495
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
496
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
497
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/LICENSE" \
498
		-e "/^%%PRODUCT_LICENSE%%/d" \
499
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
500
}
501

    
502
# Cleans up previous builds
503
clean_builder() {
504
	# Clean out directories
505
	echo ">>> Cleaning up previous build environment...Please wait!"
506

    
507
	staginareas_clean_each_run
508

    
509
	if [ -d "${STAGE_CHROOT_DIR}" ]; then
510
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR}... "
511
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
512
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
513
		echo "Done."
514
	fi
515

    
516
	if [ -d "${INSTALLER_CHROOT_DIR}" ]; then
517
		echo -n ">>> Cleaning ${INSTALLER_CHROOT_DIR}... "
518
		chflags -R noschg ${INSTALLER_CHROOT_DIR} 2>&1 >/dev/null
519
		rm -rf ${INSTALLER_CHROOT_DIR}/* 2>/dev/null
520
		echo "Done."
521
	fi
522

    
523
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
524
		OBJTREE=$(make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
525
		if [ -d "${OBJTREE}" ]; then
526
			echo -n ">>> Cleaning FreeBSD objects dir staging..."
527
			echo -n "."
528
			chflags -R noschg ${OBJTREE} 2>&1 >/dev/null
529
			echo -n "."
530
			rm -rf ${OBJTREE}/*
531
			echo "Done!"
532
		fi
533
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
534
			echo -n ">>> Cleaning previously built kernel stage area..."
535
			rm -rf $KERNEL_BUILD_PATH/*
536
			echo "Done!"
537
		fi
538
	fi
539
	mkdir -p $KERNEL_BUILD_PATH
540

    
541
	echo -n ">>> Cleaning previously built images..."
542
	rm -rf $IMAGES_FINAL_DIR/*
543
	echo "Done!"
544

    
545
	echo -n ">>> Cleaning previous builder logs..."
546
	if [ -d "$BUILDER_LOGS" ]; then
547
		rm -rf ${BUILDER_LOGS}
548
	fi
549
	mkdir -p ${BUILDER_LOGS}
550

    
551
	echo "Done!"
552

    
553
	echo ">>> Cleaning of builder environment has finished."
554
}
555

    
556
clone_directory_contents() {
557
	if [ ! -e "$2" ]; then
558
		mkdir -p "$2"
559
	fi
560
	if [ ! -d "$1" -o ! -d "$2" ]; then
561
		if [ -z "${LOGFILE}" ]; then
562
			echo ">>> ERROR: Argument $1 supplied is not a directory!"
563
		else
564
			echo ">>> ERROR: Argument $1 supplied is not a directory!" | tee -a ${LOGFILE}
565
		fi
566
		print_error_pfS
567
	fi
568
	echo -n ">>> Using TAR to clone $1 to $2 ..."
569
	tar -C ${1} -c -f - . | tar -C ${2} -x -p -f -
570
	echo "Done!"
571
}
572

    
573
clone_to_staging_area() {
574
	# Clone everything to the final staging area
575
	echo -n ">>> Cloning everything to ${STAGE_CHROOT_DIR} staging area..."
576
	LOGFILE=${BUILDER_LOGS}/cloning.${TARGET}.log
577

    
578
	tar -C ${PRODUCT_SRC} -c -f - . | \
579
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
580

    
581
	if [ "${PRODUCT_NAME}" != "pfSense" ]; then
582
		mv ${STAGE_CHROOT_DIR}/usr/local/sbin/pfSense-upgrade \
583
			${STAGE_CHROOT_DIR}/usr/local/sbin/${PRODUCT_NAME}-upgrade
584
	fi
585

    
586
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
587
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
588
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
589
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
590
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
591
	fi
592

    
593
	## Add buildtime and lastcommit information
594
	# This is used for detecting updates.
595
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
596
	# Record last commit info if it is available.
597
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
598
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
599
	fi
600

    
601
	local _exclude_files="${SCRATCHDIR}/base_exclude_files"
602
	sed \
603
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
604
		-e "s,%%VERSION%%,${_version},g" \
605
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
606
		> ${_exclude_files}
607

    
608
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
609

    
610
	# Include a sample pkg stable conf to base
611
	setup_pkg_repo \
612
		${PKG_REPO_DEFAULT} \
613
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
614
		${TARGET} \
615
		${TARGET_ARCH}
616

    
617
	mtree \
618
		-c \
619
		-k uid,gid,mode,size,flags,sha256digest \
620
		-p ${STAGE_CHROOT_DIR} \
621
		-X ${_exclude_files} \
622
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
623
	tar \
624
		-C ${STAGE_CHROOT_DIR} \
625
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
626
		-X ${_exclude_files} \
627
		.
628

    
629
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
630
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
631
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
632

    
633
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
634

    
635
	# Save current WAN and LAN if value
636
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
637
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
638

    
639
	# Change default interface names to match vmware driver
640
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
641
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
642
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
643

    
644
	# Restore default values to be used by serial package
645
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
646
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
647

    
648
	# Activate serial console in config.xml
649
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
650
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
651
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
652
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
653
	rm -f ${DEFAULTCONF}.tmp
654

    
655
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
656

    
657
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
658

    
659
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
660
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
661

    
662
	# Make sure pkg is present
663
	pkg_bootstrap ${STAGE_CHROOT_DIR}
664

    
665
	# Make sure correct repo is available on tmp dir
666
	mkdir -p ${STAGE_CHROOT_DIR}/tmp/pkg/pkg-repos
667
	setup_pkg_repo \
668
		${PKG_REPO_BUILD} \
669
		${STAGE_CHROOT_DIR}/tmp/pkg/pkg-repos/repo.conf \
670
		${TARGET} \
671
		${TARGET_ARCH} \
672
		staging \
673
		${STAGE_CHROOT_DIR}/tmp/pkg/pkg.conf
674

    
675
	echo "Done!"
676
}
677

    
678
create_final_staging_area() {
679
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
680
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
681
		print_error_pfS
682
	fi
683

    
684
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
685
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
686
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
687
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
688
		echo "Done." | tee -a ${LOGFILE}
689
	fi
690

    
691
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
692
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
693
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
694

    
695
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
696
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
697
		print_error_pfS
698
	fi
699
}
700

    
701
customize_stagearea_for_image() {
702
	local _image_type="$1"
703
	local _default_config="" # filled with $2 below
704
	local _image_variant="$3"
705

    
706
	if [ -n "$2" ]; then
707
		_default_config="$2"
708
	elif [ "${_image_type}" = "memstickserial" -o \
709
	     "${_image_type}" = "memstickadi" ]; then
710
		_default_config="default-config-serial"
711
	elif [ "${_image_type}" = "ova" ]; then
712
		_default_config="default-config-vmware"
713
	else
714
		_default_config="default-config"
715
	fi
716

    
717
	# Prepare final stage area
718
	create_final_staging_area
719

    
720
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
721
	pkg_chroot_add ${FINAL_CHROOT_DIR} base
722

    
723
	# Set base/rc pkgs as vital to avoid user end up removing it for any reason
724
	pkg_chroot ${FINAL_CHROOT_DIR} set -v 1 -y $(get_pkg_name rc)
725
	pkg_chroot ${FINAL_CHROOT_DIR} set -v 1 -y $(get_pkg_name base)
726

    
727
	if [ "${_image_type}" = "iso" -o \
728
	     "${_image_type}" = "memstick" -o \
729
	     "${_image_type}" = "memstickserial" -o \
730
	     "${_image_type}" = "memstickadi" ]; then
731
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
732
		cp ${CORE_PKG_ALL_PATH}/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
733
	fi
734

    
735
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
736

    
737
	# XXX: Workaround to avoid pkg to complain regarding release
738
	#      repo on first boot since packages are installed from
739
	#      staging server during build phase
740
	if [ -n "${USE_PKG_REPO_STAGING}" ]; then
741
		_read_cmd="select value from repodata where key='packagesite'"
742
		if [ -n "${_IS_RELEASE}" -o -n "${_IS_RC}" ]; then
743
			local _tgt_server="${PKG_REPO_SERVER_RELEASE}"
744
		else
745
			local _tgt_server="${PKG_REPO_SERVER_DEVEL}"
746
		fi
747
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
748
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
749
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER_STAGING},${_tgt_server},")
750
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
751
		done
752
	fi
753

    
754
	if [ -n "$_image_variant" -a \
755
	    -d ${BUILDER_TOOLS}/templates/custom_logos/${_image_variant} ]; then
756
		mkdir -p ${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
757
		cp -f \
758
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.svg \
759
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
760
		cp -f \
761
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.css \
762
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
763
	fi
764

    
765
	# Remove temporary repo conf
766
	rm -rf ${FINAL_CHROOT_DIR}/tmp/pkg
767
}
768

    
769
create_distribution_tarball() {
770
	mkdir -p ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist
771

    
772
	echo -n ">>> Creating distribution tarball... " | tee -a ${LOGFILE}
773
	tar -C ${FINAL_CHROOT_DIR} --exclude ./pkgs \
774
		-cJf ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/base.txz .
775
	echo "Done!" | tee -a ${LOGFILE}
776

    
777
	echo -n ">>> Creating manifest... " | tee -a ${LOGFILE}
778
	(cd ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist && \
779
		sh ${FREEBSD_SRC_DIR}/release/scripts/make-manifest.sh base.txz) \
780
		> ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/MANIFEST
781
	echo "Done!" | tee -a ${LOGFILE}
782
}
783

    
784
create_iso_image() {
785
	local _variant="$1"
786

    
787
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
788

    
789
	if [ -z "${ISOPATH}" ]; then
790
		echo ">>> ISOPATH is empty skipping generation of ISO image!" | tee -a ${LOGFILE}
791
		return
792
	fi
793

    
794
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
795

    
796
	mkdir -p $(dirname ${ISOPATH})
797

    
798
	local _image_path=${ISOPATH}
799
	if [ -n "${_variant}" ]; then
800
		_image_path=$(echo "$_image_path" | \
801
			sed "s/${PRODUCT_NAME_SUFFIX}-/&${_variant}-/")
802
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
803
	fi
804

    
805
	customize_stagearea_for_image "iso" "" $_variant
806
	install_default_kernel ${DEFAULT_KERNEL}
807

    
808
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
809
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
810

    
811
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
812
	echo 'autoboot_delay="3"' > ${LOADERCONF}
813
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
814
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
815

    
816
	create_distribution_tarball
817

    
818
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
819

    
820
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/mkisoimages.sh -b \
821
		${FSLABEL} \
822
		${_image_path} \
823
		${INSTALLER_CHROOT_DIR}
824

    
825
	if [ ! -f "${_image_path}" ]; then
826
		echo "ERROR! ISO image was not built"
827
		print_error_pfS
828
	fi
829

    
830
	gzip -qf $_image_path &
831
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
832

    
833
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
834
}
835

    
836
create_memstick_image() {
837
	local _variant="$1"
838

    
839
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
840
	if [ "${MEMSTICKPATH}" = "" ]; then
841
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
842
		return
843
	fi
844

    
845
	mkdir -p $(dirname ${MEMSTICKPATH})
846

    
847
	local _image_path=${MEMSTICKPATH}
848
	if [ -n "${_variant}" ]; then
849
		_image_path=$(echo "$_image_path" | \
850
			sed "s/-memstick-/-memstick-${_variant}-/")
851
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
852
	fi
853

    
854
	customize_stagearea_for_image "memstick" "" $_variant
855
	install_default_kernel ${DEFAULT_KERNEL}
856

    
857
	echo ">>> Creating memstick to ${_image_path}." 2>&1 | tee -a ${LOGFILE}
858

    
859
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
860
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
861

    
862
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
863

    
864
	echo 'autoboot_delay="3"' > ${LOADERCONF}
865
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
866
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
867

    
868
	create_distribution_tarball
869

    
870
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
871

    
872
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/mkisoimages.sh -b \
873
		${FSLABEL} \
874
		${_image_path} \
875
		${INSTALLER_CHROOT_DIR}
876

    
877
	if [ ! -f "${_image_path}" ]; then
878
		echo "ERROR! memstick image was not built"
879
		print_error_pfS
880
	fi
881

    
882
	gzip -qf $_image_path &
883
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
884

    
885
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
886
}
887

    
888
create_memstick_serial_image() {
889
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
890
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
891
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
892
		return
893
	fi
894

    
895
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
896

    
897
	customize_stagearea_for_image "memstickserial"
898
	install_default_kernel ${DEFAULT_KERNEL}
899

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

    
902
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
903
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
904

    
905
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
906
	echo "-S115200 -D" > ${BOOTCONF}
907

    
908
	# Activate serial console+video console in loader.conf
909
	echo 'autoboot_delay="3"' > ${LOADERCONF}
910
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
911
	echo 'boot_multicons="YES"' >> ${LOADERCONF}
912
	echo 'boot_serial="YES"' >> ${LOADERCONF}
913
	echo 'console="comconsole,vidconsole"' >> ${LOADERCONF}
914
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
915

    
916
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
917
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
918

    
919
	create_distribution_tarball
920

    
921
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
922
		${INSTALLER_CHROOT_DIR} \
923
		${MEMSTICKSERIALPATH}
924

    
925
	if [ ! -f "${MEMSTICKSERIALPATH}" ]; then
926
		echo "ERROR! memstick serial image was not built"
927
		print_error_pfS
928
	fi
929

    
930
	gzip -qf $MEMSTICKSERIALPATH &
931
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
932

    
933
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
934
}
935

    
936
create_memstick_adi_image() {
937
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
938
	if [ "${MEMSTICKADIPATH}" = "" ]; then
939
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
940
		return
941
	fi
942

    
943
	mkdir -p $(dirname ${MEMSTICKADIPATH})
944

    
945
	customize_stagearea_for_image "memstickadi"
946
	install_default_kernel ${DEFAULT_KERNEL}
947

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

    
950
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
951
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
952

    
953
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
954
	echo "-S115200 -h" > ${BOOTCONF}
955

    
956
	# Activate serial console+video console in loader.conf
957
	echo 'autoboot_delay="3"' > ${LOADERCONF}
958
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
959
	echo 'boot_serial="YES"' >> ${LOADERCONF}
960
	echo 'console="comconsole"' >> ${LOADERCONF}
961
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
962
	echo 'comconsole_port="0x2F8"' >> ${LOADERCONF}
963
	echo 'hint.uart.0.flags="0x00"' >> ${LOADERCONF}
964
	echo 'hint.uart.1.flags="0x10"' >> ${LOADERCONF}
965

    
966
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
967
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
968

    
969
	create_distribution_tarball
970

    
971
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
972
		${INSTALLER_CHROOT_DIR} \
973
		${MEMSTICKADIPATH}
974

    
975
	if [ ! -f "${MEMSTICKADIPATH}" ]; then
976
		echo "ERROR! memstick ADI image was not built"
977
		print_error_pfS
978
	fi
979

    
980
	gzip -qf $MEMSTICKADIPATH &
981
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
982

    
983
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
984
}
985

    
986
get_altabi_arch() {
987
	local _target_arch="$1"
988

    
989
	if [ "${_target_arch}" = "amd64" ]; then
990
		echo "x86:64"
991
	elif [ "${_target_arch}" = "i386" ]; then
992
		echo "x86:32"
993
	elif [ "${_target_arch}" = "armv7" ]; then
994
		echo "32:el:eabi:softfp"
995
	else
996
		echo ">>> ERROR: Invalid arch"
997
		print_error_pfS
998
	fi
999
}
1000

    
1001
# Create pkg conf on desired place with desired arch/branch
1002
setup_pkg_repo() {
1003
	if [ -z "${4}" ]; then
1004
		return
1005
	fi
1006

    
1007
	local _template="${1}"
1008
	local _target="${2}"
1009
	local _arch="${3}"
1010
	local _target_arch="${4}"
1011
	local _staging="${5}"
1012
	local _pkg_conf="${6}"
1013

    
1014
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
1015
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
1016
		print_error_pfS
1017
	fi
1018

    
1019
	if [ -n "${_staging}" -a -n "${USE_PKG_REPO_STAGING}" ]; then
1020
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_STAGING}
1021
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_STAGING}
1022
		local _pkg_repo_server_release=${PKG_REPO_SERVER_STAGING}
1023
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_STAGING}
1024
	else
1025
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_DEVEL}
1026
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_DEVEL}
1027
		local _pkg_repo_server_release=${PKG_REPO_SERVER_RELEASE}
1028
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_RELEASE}
1029
	fi
1030

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

    
1033
	sed \
1034
		-e "s/%%ARCH%%/${_target_arch}/" \
1035
		-e "s/%%PKG_REPO_BRANCH_DEVEL%%/${_pkg_repo_branch_devel}/g" \
1036
		-e "s/%%PKG_REPO_BRANCH_RELEASE%%/${_pkg_repo_branch_release}/g" \
1037
		-e "s,%%PKG_REPO_SERVER_DEVEL%%,${_pkg_repo_server_devel},g" \
1038
		-e "s,%%PKG_REPO_SERVER_RELEASE%%,${_pkg_repo_server_release},g" \
1039
		-e "s,%%POUDRIERE_PORTS_NAME%%,${POUDRIERE_PORTS_NAME},g" \
1040
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
1041
		-e "s/%%REPO_BRANCH_PREFIX%%/${REPO_BRANCH_PREFIX}/g" \
1042
		${_template} \
1043
		> ${_target}
1044

    
1045
	local ALTABI_ARCH=$(get_altabi_arch ${_target_arch})
1046

    
1047
	ABI=$(cat ${_template%%.conf}.abi 2>/dev/null \
1048
	    | sed -e "s/%%ARCH%%/${_target_arch}/g")
1049
	ALTABI=$(cat ${_template%%.conf}.altabi 2>/dev/null \
1050
	    | sed -e "s/%%ARCH%%/${ALTABI_ARCH}/g")
1051

    
1052
	if [ -n "${_pkg_conf}" -a -n "${ABI}" -a -n "${ALTABI}" ]; then
1053
		mkdir -p $(dirname ${_pkg_conf})
1054
		echo "ABI=${ABI}" > ${_pkg_conf}
1055
		echo "ALTABI=${ALTABI}" >> ${_pkg_conf}
1056
	fi
1057
}
1058

    
1059
depend_check() {
1060
	for _pkg in ${BUILDER_PKG_DEPENDENCIES}; do
1061
		if ! pkg info -e ${_pkg}; then
1062
			echo "Missing dependency (${_pkg})."
1063
			print_error_pfS
1064
		fi
1065
	done
1066
}
1067

    
1068
# This routine ensures any ports / binaries that the builder
1069
# system needs are on disk and ready for execution.
1070
builder_setup() {
1071
	# If Product-builder is already installed, just leave
1072
	if pkg info -e -q ${PRODUCT_NAME}-builder; then
1073
		return
1074
	fi
1075

    
1076
	if [ ! -f ${PKG_REPO_PATH} ]; then
1077
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
1078
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
1079

    
1080
		update_freebsd_sources
1081

    
1082
		local _arch=$(uname -m)
1083
		setup_pkg_repo \
1084
			${PKG_REPO_BUILD} \
1085
			${PKG_REPO_PATH} \
1086
			${_arch} \
1087
			${_arch} \
1088
			"staging"
1089

    
1090
		# Use fingerprint keys from repo
1091
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
1092
			${PKG_REPO_PATH}
1093
	fi
1094

    
1095
	pkg install ${PRODUCT_NAME}-builder
1096
}
1097

    
1098
# Updates FreeBSD sources
1099
update_freebsd_sources() {
1100
	if [ "${1}" = "full" ]; then
1101
		local _full=1
1102
		local _clone_params=""
1103
	else
1104
		local _full=0
1105
		local _clone_params="--depth 1 --single-branch"
1106
	fi
1107

    
1108
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
1109
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
1110
		return
1111
	fi
1112

    
1113
	echo ">>> Obtaining FreeBSD sources (${FREEBSD_BRANCH})..."
1114
	${BUILDER_SCRIPTS}/git_checkout.sh \
1115
		-r ${FREEBSD_REPO_BASE} \
1116
		-d ${FREEBSD_SRC_DIR} \
1117
		-b ${FREEBSD_BRANCH}
1118

    
1119
	if [ $? -ne 0 -o ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1120
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1121
		print_error_pfS
1122
	fi
1123

    
1124
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1125
		echo -n ">>> Checking out desired commit (${GIT_FREEBSD_COSHA1})... "
1126
		( git -C  ${FREEBSD_SRC_DIR} checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | \
1127
			grep -C3 -i -E 'error|fatal'
1128
		echo "Done!"
1129
	fi
1130

    
1131
	if [ "${PRODUCT_NAME}" = "pfSense" -a -n "${GNID_REPO_BASE}" ]; then
1132
		echo ">>> Obtaining gnid sources..."
1133
		${BUILDER_SCRIPTS}/git_checkout.sh \
1134
			-r ${GNID_REPO_BASE} \
1135
			-d ${GNID_SRC_DIR} \
1136
			-b ${GNID_BRANCH}
1137
	fi
1138
}
1139

    
1140
pkg_chroot() {
1141
	local _root="${1}"
1142
	shift
1143

    
1144
	if [ $# -eq 0 ]; then
1145
		return -1
1146
	fi
1147

    
1148
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1149
		return -1
1150
	fi
1151

    
1152
	mkdir -p \
1153
		${SCRATCHDIR}/pkg_cache \
1154
		${_root}/var/cache/pkg \
1155
		${_root}/dev
1156

    
1157
	/sbin/mount -t nullfs ${SCRATCHDIR}/pkg_cache ${_root}/var/cache/pkg
1158
	/sbin/mount -t devfs devfs ${_root}/dev
1159
	cp -f /etc/resolv.conf ${_root}/etc/resolv.conf
1160
	touch ${BUILDER_LOGS}/install_pkg_install_ports.txt
1161
	local _params=""
1162
	if [ -f "${_root}/tmp/pkg/pkg-repos/repo.conf" ]; then
1163
		_params="--repo-conf-dir /tmp/pkg/pkg-repos "
1164
	fi
1165
	if [ -f "${_root}/tmp/pkg/pkg.conf" ]; then
1166
		_params="${_params} --config /tmp/pkg/pkg.conf "
1167
	fi
1168
	script -aq ${BUILDER_LOGS}/install_pkg_install_ports.txt \
1169
		chroot ${_root} pkg ${_params}$@ >/dev/null 2>&1
1170
	local result=$?
1171
	rm -f ${_root}/etc/resolv.conf
1172
	/sbin/umount -f ${_root}/dev
1173
	/sbin/umount -f ${_root}/var/cache/pkg
1174

    
1175
	return $result
1176
}
1177

    
1178

    
1179
pkg_chroot_add() {
1180
	if [ -z "${1}" -o -z "${2}" ]; then
1181
		return 1
1182
	fi
1183

    
1184
	local _target="${1}"
1185
	local _pkg="$(get_pkg_name ${2}).txz"
1186

    
1187
	if [ ! -d "${_target}" ]; then
1188
		echo ">>> ERROR: Target dir ${_target} not found"
1189
		print_error_pfS
1190
	fi
1191

    
1192
	if [ ! -f ${CORE_PKG_ALL_PATH}/${_pkg} ]; then
1193
		echo ">>> ERROR: Package ${_pkg} not found"
1194
		print_error_pfS
1195
	fi
1196

    
1197
	cp ${CORE_PKG_ALL_PATH}/${_pkg} ${_target}
1198
	pkg_chroot ${_target} add /${_pkg}
1199
	rm -f ${_target}/${_pkg}
1200
}
1201

    
1202
pkg_bootstrap() {
1203
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1204

    
1205
	setup_pkg_repo \
1206
		${PKG_REPO_BUILD} \
1207
		${_root}${PKG_REPO_PATH} \
1208
		${TARGET} \
1209
		${TARGET_ARCH} \
1210
		"staging"
1211

    
1212
	pkg_chroot ${_root} bootstrap -f
1213
}
1214

    
1215
# This routine assists with installing various
1216
# freebsd ports files into the pfsense-fs staging
1217
# area.
1218
install_pkg_install_ports() {
1219
	local MAIN_PKG="${1}"
1220

    
1221
	if [ -z "${MAIN_PKG}" ]; then
1222
		MAIN_PKG=${PRODUCT_NAME}
1223
	fi
1224

    
1225
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1226

    
1227
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1228
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1229

    
1230
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1231
		mkdir -p ${SCRATCHDIR}/pkg_cache
1232

    
1233
	echo -n ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... "
1234
	# First mark all packages as automatically installed
1235
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 1 -a
1236
	# Install all necessary packages
1237
	if ! pkg_chroot ${STAGE_CHROOT_DIR} install ${MAIN_PKG} ${custom_package_list}; then
1238
		echo "Failed!"
1239
		print_error_pfS
1240
	fi
1241
	# Make sure required packages are set as non-automatic
1242
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 0 pkg ${MAIN_PKG} ${custom_package_list}
1243
	# pkg and MAIN_PKG are vital
1244
	pkg_chroot ${STAGE_CHROOT_DIR} set -y -v 1 pkg ${MAIN_PKG}
1245
	# Remove unnecessary packages
1246
	pkg_chroot ${STAGE_CHROOT_DIR} autoremove
1247
	echo "Done!"
1248
}
1249

    
1250
staginareas_clean_each_run() {
1251
	echo -n ">>> Cleaning build directories: "
1252
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1253
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1254
		echo -n "$BASENAME "
1255
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1256
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1257
	fi
1258
	echo "Done!"
1259
}
1260

    
1261
# Imported from FreeSBIE
1262
buildkernel() {
1263
	local _kernconf=${1:-${KERNCONF}}
1264

    
1265
	if [ -n "${NO_BUILDKERNEL}" ]; then
1266
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1267
		return
1268
	fi
1269

    
1270
	if [ -z "${_kernconf}" ]; then
1271
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1272
		print_error_pfS
1273
	fi
1274

    
1275
	local _old_kernconf=${KERNCONF}
1276
	export KERNCONF=${_kernconf}
1277

    
1278
	echo ">>> $(LC_ALL=C date) - Starting build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1279
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -W -s ${FREEBSD_SRC_DIR} \
1280
		|| print_error_pfS
1281
	echo ">>> $(LC_ALL=C date) - Finished build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1282

    
1283
	export KERNCONF=${_old_kernconf}
1284
}
1285

    
1286
# Imported from FreeSBIE
1287
installkernel() {
1288
	local _destdir=${1:-${KERNEL_DESTDIR}}
1289
	local _kernconf=${2:-${KERNCONF}}
1290

    
1291
	if [ -z "${_kernconf}" ]; then
1292
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1293
		print_error_pfS
1294
	fi
1295

    
1296
	local _old_kernconf=${KERNCONF}
1297
	export KERNCONF=${_kernconf}
1298

    
1299
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1300
	echo ">>> Installing kernel (${_kernconf}) for ${TARGET} architecture..." | tee -a ${LOGFILE}
1301
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -W -D -z \
1302
		-s ${FREEBSD_SRC_DIR} \
1303
		-d ${_destdir} \
1304
		|| print_error_pfS
1305

    
1306
	export KERNCONF=${_old_kernconf}
1307
}
1308

    
1309
# Launch is ran first to setup a few variables that we need
1310
# Imported from FreeSBIE
1311
launch() {
1312
	if [ "$(id -u)" != "0" ]; then
1313
		echo "Sorry, this must be done as root."
1314
	fi
1315

    
1316
	echo ">>> Operation $0 has started at $(date)"
1317
}
1318

    
1319
finish() {
1320
	echo ">>> Operation $0 has ended at $(date)"
1321
}
1322

    
1323
pkg_repo_rsync() {
1324
	local _repo_path_param="${1}"
1325
	local _ignore_final_rsync="${2}"
1326

    
1327
	if [ -z "${_repo_path_param}" -o ! -d "${_repo_path_param}" ]; then
1328
		return
1329
	fi
1330

    
1331
	if [ -n "${SKIP_FINAL_RSYNC}" ]; then
1332
		_ignore_final_rsync="1"
1333
	fi
1334

    
1335
	# Sanitize path
1336
	_repo_path=$(realpath ${_repo_path_param})
1337

    
1338
	# Trigger file to be used to sync files to S3
1339
	touch ${_repo_path}/.sync_to_s3
1340

    
1341
	local _repo_dir=$(dirname ${_repo_path})
1342
	local _repo_base=$(basename ${_repo_path})
1343

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

    
1347
	if [ -z "${LOGFILE}" ]; then
1348
		local _logfile="/dev/null"
1349
	else
1350
		local _logfile="${LOGFILE}"
1351
	fi
1352

    
1353
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" -a -z "${DO_NOT_SIGN_PKG_REPO}" ]; then
1354
		# Detect poudriere directory structure
1355
		if [ -L "${_repo_path}/.latest" ]; then
1356
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1357
		else
1358
			local _real_repo_path=${_repo_path}
1359
		fi
1360

    
1361
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1362
		############ ATTENTION ##############
1363
		#
1364
		# For some reason pkg-repo fail without / in the end of directory name
1365
		# so removing it will break command
1366
		#
1367
		# https://github.com/freebsd/pkg/issues/1364
1368
		#
1369
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1370
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1371
			echo "Done!" | tee -a ${_logfile}
1372
		else
1373
			echo "Failed!" | tee -a ${_logfile}
1374
			echo ">>> ERROR: An error occurred trying to sign repo"
1375
			print_error_pfS
1376
		fi
1377

    
1378
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1379
		if [ -e ${_pkgfile} ]; then
1380
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1381

    
1382
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1383
			    > ${_pkgfile}.sig 2>/dev/null; then
1384
				echo "Done!" | tee -a ${_logfile}
1385
			else
1386
				echo "Failed!" | tee -a ${_logfile}
1387
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1388
				print_error_pfS
1389
			fi
1390
		fi
1391
	fi
1392

    
1393
	if [ -z "${UPLOAD}" ]; then
1394
		return
1395
	fi
1396

    
1397
	for _pkg_rsync_hostname in ${PKG_RSYNC_HOSTNAME}; do
1398
		# Make sure destination directory exist
1399
		ssh -p ${PKG_RSYNC_SSH_PORT} \
1400
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} \
1401
			"mkdir -p ${PKG_RSYNC_DESTDIR}"
1402

    
1403
		echo -n ">>> Sending updated repository to ${_pkg_rsync_hostname}... " | tee -a ${_logfile}
1404
		if script -aq ${_logfile} rsync -Have "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1405
			--timeout=60 --delete-delay ${_repo_path} \
1406
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1407
		then
1408
			echo "Done!" | tee -a ${_logfile}
1409
		else
1410
			echo "Failed!" | tee -a ${_logfile}
1411
			echo ">>> ERROR: An error occurred sending repo to remote hostname"
1412
			print_error_pfS
1413
		fi
1414

    
1415
		if [ -z "${USE_PKG_REPO_STAGING}" -o -n "${_ignore_final_rsync}" ]; then
1416
			return
1417
		fi
1418

    
1419
		if [ -n "${_IS_RELEASE}" -o "${_repo_path_param}" = "${CORE_PKG_PATH}" ]; then
1420
			for _pkg_final_rsync_hostname in ${PKG_FINAL_RSYNC_HOSTNAME}; do
1421
				# Send .real* directories first to prevent having a broken repo while transfer happens
1422
				local _cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1423
					--timeout=60 ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1424
					--include=\"/*\" --include=\"*/.real*\" --include=\"*/.real*/***\" \
1425
					--exclude=\"*\" \
1426
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1427

    
1428
				echo -n ">>> Sending updated packages to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1429
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1430
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1431
					echo "Done!" | tee -a ${_logfile}
1432
				else
1433
					echo "Failed!" | tee -a ${_logfile}
1434
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1435
					print_error_pfS
1436
				fi
1437

    
1438
				_cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1439
					--timeout=60 --delete-delay ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1440
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1441

    
1442
				echo -n ">>> Sending updated repositories metadata to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1443
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1444
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1445
					echo "Done!" | tee -a ${_logfile}
1446
				else
1447
					echo "Failed!" | tee -a ${_logfile}
1448
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1449
					print_error_pfS
1450
				fi
1451

    
1452
				if [ -z "${PKG_POST_RSYNC_COMMAND}" ]; then
1453
					continue
1454
				fi
1455

    
1456
				echo -n ">>> Running post rsync command at ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1457
				if script -aq ${_logfile} ssh -p ${PKG_FINAL_RSYNC_SSH_PORT} \
1458
				    ${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname} "${PKG_POST_RSYNC_COMMAND}"; then
1459
					echo "Done!" | tee -a ${_logfile}
1460
				else
1461
					echo "Failed!" | tee -a ${_logfile}
1462
					echo ">>> ERROR: An error occurred executing post command at pkg final hostname"
1463
					print_error_pfS
1464
				fi
1465
			done
1466
		fi
1467
	done
1468
}
1469

    
1470
poudriere_possible_archs() {
1471
	local _arch=$(uname -m)
1472
	local _archs=""
1473

    
1474
	# If host is amd64, we'll create both repos, and if possible armv7
1475
	if [ "${_arch}" = "amd64" ]; then
1476
		_archs="amd64.amd64"
1477

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

    
1482
			if binmiscctl lookup armv7 >/dev/null 2>&1; then
1483
				_archs="${_archs} arm.armv7"
1484
			fi
1485
		fi
1486
	fi
1487

    
1488
	if [ -n "${ARCH_LIST}" ]; then
1489
		local _found=0
1490
		for _desired_arch in ${ARCH_LIST}; do
1491
			_found=0
1492
			for _possible_arch in ${_archs}; do
1493
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1494
					_found=1
1495
					break
1496
				fi
1497
			done
1498
			if [ ${_found} -eq 0 ]; then
1499
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1500
				print_error_pfS
1501
			fi
1502
		done
1503
		_archs="${ARCH_LIST}"
1504
	fi
1505

    
1506
	echo ${_archs}
1507
}
1508

    
1509
poudriere_jail_name() {
1510
	local _jail_arch="${1}"
1511

    
1512
	if [ -z "${_jail_arch}" ]; then
1513
		return 1
1514
	fi
1515

    
1516
	# Remove arch
1517
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1518
}
1519

    
1520
poudriere_rename_ports() {
1521
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1522
		return;
1523
	fi
1524

    
1525
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1526

    
1527
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1528

    
1529
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1530
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1531
		local _pdir=$(dirname ${d})
1532
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1533
		local _plist=""
1534

    
1535
		if [ -e ${_pdir}/${_pname} ]; then
1536
			rm -rf ${_pdir}/${_pname}
1537
		fi
1538

    
1539
		cp -r ${d} ${_pdir}/${_pname}
1540

    
1541
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1542
			_plist=${_pdir}/${_pname}/pkg-plist
1543
		fi
1544

    
1545
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1546
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1547
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1548
			${_pdir}/${_pname}/Makefile \
1549
			${_pdir}/${_pname}/pkg-descr ${_plist}
1550

    
1551
		# PHP module is special
1552
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1553
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1554
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1555
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1556
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1557
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1558
				${_pdir}/${_pname}/files/config.m4
1559

    
1560
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1561
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1562
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1563
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1564
				${_pdir}/${_pname}/files/pfSense.c \
1565
				${_pdir}/${_pname}/files/php_pfSense.h
1566
		fi
1567

    
1568
		if [ -d ${_pdir}/${_pname}/files ]; then
1569
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1570
				local _fddir=$(dirname ${fd})
1571
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1572

    
1573
				mv ${fd} ${_fddir}/${_fdname}
1574
			done
1575
		fi
1576
	done
1577
	echo "Done!" | tee -a ${LOGFILE}
1578
}
1579

    
1580
poudriere_create_ports_tree() {
1581
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1582

    
1583
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1584
		local _branch=""
1585
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1586
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1587
			print_error_pfS
1588
		fi
1589
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1590
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1591
		fi
1592
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1593
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git -U ${POUDRIERE_PORTS_GIT_URL} ${_branch} >/dev/null 2>&1; then
1594
			echo "" | tee -a ${LOGFILE}
1595
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1596
			print_error_pfS
1597
		fi
1598
		echo "Done!" | tee -a ${LOGFILE}
1599
		poudriere_rename_ports
1600
	fi
1601
}
1602

    
1603
poudriere_init() {
1604
	local _error=0
1605
	local _archs=$(poudriere_possible_archs)
1606

    
1607
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1608

    
1609
	# Sanity checks
1610
	if [ -z "${ZFS_TANK}" ]; then
1611
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1612
		error=1
1613
	fi
1614

    
1615
	if [ -z "${ZFS_ROOT}" ]; then
1616
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1617
		error=1
1618
	fi
1619

    
1620
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1621
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1622
		error=1
1623
	fi
1624

    
1625
	if [ ${_error} -eq 1 ]; then
1626
		print_error_pfS
1627
	fi
1628

    
1629
	# Check if zpool exists
1630
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
1631
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
1632
		print_error_pfS
1633
	fi
1634

    
1635
	# Check if zfs rootfs exists
1636
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1637
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
1638
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
1639
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1640
			echo "Done!"
1641
		else
1642
			echo "Failed!"
1643
			print_error_pfS
1644
		fi
1645
	fi
1646

    
1647
	# Make sure poudriere is installed
1648
	if [ ! -f /usr/local/bin/poudriere ]; then
1649
		echo ">>> Installing poudriere..." | tee -a ${LOGFILE}
1650
		if ! pkg install poudriere >/dev/null 2>&1; then
1651
			echo ">>> ERROR: poudriere was not installed, aborting..." | tee -a ${LOGFILE}
1652
			print_error_pfS
1653
		fi
1654
	fi
1655

    
1656
	# Create poudriere.conf
1657
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1658
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1659
		print_error_pfS
1660
	fi
1661
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
1662
	cat <<EOF >/usr/local/etc/poudriere.conf
1663
ZPOOL=${ZFS_TANK}
1664
ZROOTFS=${ZFS_ROOT}
1665
RESOLV_CONF=/etc/resolv.conf
1666
BASEFS=/usr/local/poudriere
1667
USE_PORTLINT=no
1668
USE_TMPFS=yes
1669
NOLINUX=yes
1670
DISTFILES_CACHE=/usr/ports/distfiles
1671
CHECK_CHANGED_OPTIONS=yes
1672
CHECK_CHANGED_DEPS=yes
1673
ATOMIC_PACKAGE_REPOSITORY=yes
1674
COMMIT_PACKAGES_ON_FAILURE=no
1675
KEEP_OLD_PACKAGES=yes
1676
KEEP_OLD_PACKAGES_COUNT=5
1677
ALLOW_MAKE_JOBS=yes
1678
PARALLEL_JOBS=8
1679
EOF
1680

    
1681
	if pkg info -e ccache; then
1682
	cat <<EOF >>/usr/local/etc/poudriere.conf
1683
CCACHE_DIR=/var/cache/ccache
1684
EOF
1685
	fi
1686

    
1687
	# Create specific items conf
1688
	[ ! -d /usr/local/etc/poudriere.d ] \
1689
		&& mkdir -p /usr/local/etc/poudriere.d
1690

    
1691
	# Create DISTFILES_CACHE if it doesn't exist
1692
	if [ ! -d /usr/ports/distfiles ]; then
1693
		mkdir -p /usr/ports/distfiles
1694
	fi
1695

    
1696
	# Remove old jails
1697
	for jail_arch in ${_archs}; do
1698
		jail_name=$(poudriere_jail_name ${jail_arch})
1699

    
1700
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1701
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
1702
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
1703
		fi
1704
	done
1705

    
1706
	# Remove old ports tree
1707
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1708
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
1709
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
1710
	fi
1711

    
1712
	local native_xtools=""
1713
	# Now we are ready to create jails
1714
	for jail_arch in ${_archs}; do
1715
		jail_name=$(poudriere_jail_name ${jail_arch})
1716

    
1717
		if [ "${jail_arch}" = "arm.armv7" ]; then
1718
			native_xtools="-x"
1719
		else
1720
			native_xtools=""
1721
		fi
1722

    
1723
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1724
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
1725
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
1726
			echo "" | tee -a ${LOGFILE}
1727
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1728
			print_error_pfS
1729
		fi
1730
		echo "Done!" | tee -a ${LOGFILE}
1731
	done
1732

    
1733
	poudriere_create_ports_tree
1734

    
1735
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
1736
}
1737

    
1738
poudriere_update_jails() {
1739
	local _archs=$(poudriere_possible_archs)
1740

    
1741
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1742

    
1743
	local native_xtools=""
1744
	for jail_arch in ${_archs}; do
1745
		jail_name=$(poudriere_jail_name ${jail_arch})
1746

    
1747
		local _create_or_update="-u"
1748
		local _create_or_update_text="Updating"
1749
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1750
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
1751
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
1752
			_create_or_update_text="Creating"
1753
		fi
1754

    
1755
		if [ "${jail_arch}" = "arm.armv7" ]; then
1756
			native_xtools="-x"
1757
		else
1758
			native_xtools=""
1759
		fi
1760

    
1761
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1762
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
1763
			echo "" | tee -a ${LOGFILE}
1764
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1765
			print_error_pfS
1766
		fi
1767
		echo "Done!" | tee -a ${LOGFILE}
1768
	done
1769
}
1770

    
1771
poudriere_update_ports() {
1772
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1773

    
1774
	# Create ports tree if necessary
1775
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1776
		poudriere_create_ports_tree
1777
	else
1778
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1779
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
1780
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
1781
		echo "Done!" | tee -a ${LOGFILE}
1782
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1783
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
1784
		echo "Done!" | tee -a ${LOGFILE}
1785
		poudriere_rename_ports
1786
	fi
1787
}
1788

    
1789
poudriere_bulk() {
1790
	local _archs=$(poudriere_possible_archs)
1791
	local _makeconf
1792

    
1793
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1794

    
1795
	if [ -n "${UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
1796
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
1797
		print_error_pfS
1798
	fi
1799

    
1800
	rm -f ${LOGFILE}
1801

    
1802
	poudriere_create_ports_tree
1803

    
1804
	[ -d /usr/local/etc/poudriere.d ] || \
1805
		mkdir -p /usr/local/etc/poudriere.d
1806

    
1807
	_makeconf=/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1808
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
1809
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ${_makeconf}
1810
	fi
1811

    
1812
	cat <<EOF >>/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1813
PKG_REPO_BRANCH_DEVEL=${PKG_REPO_BRANCH_DEVEL}
1814
PKG_REPO_BRANCH_RELEASE=${PKG_REPO_BRANCH_RELEASE}
1815
PKG_REPO_SERVER_DEVEL=${PKG_REPO_SERVER_DEVEL}
1816
PKG_REPO_SERVER_RELEASE=${PKG_REPO_SERVER_RELEASE}
1817
POUDRIERE_PORTS_NAME=${POUDRIERE_PORTS_NAME}
1818
PFSENSE_DEFAULT_REPO=${PFSENSE_DEFAULT_REPO}
1819
PRODUCT_NAME=${PRODUCT_NAME}
1820
REPO_BRANCH_PREFIX=${REPO_BRANCH_PREFIX}
1821
EOF
1822

    
1823
	local _value=""
1824
	for jail_arch in ${_archs}; do
1825
		eval "_value=\${PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}}"
1826
		if [ -n "${_value}" ]; then
1827
			echo "PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}=${_value}" \
1828
				>> ${_makeconf}
1829
		fi
1830
		eval "_value=\${PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}}"
1831
		if [ -n "${_value}" ]; then
1832
			echo "PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}=${_value}" \
1833
				>> ${_makeconf}
1834
		fi
1835
		eval "_value=\${PKG_REPO_SERVER_DEVEL_${jail_arch##*.}}"
1836
		if [ -n "${_value}" ]; then
1837
			echo "PKG_REPO_SERVER_DEVEL_${jail_arch##*.}=${_value}" \
1838
				>> ${_makeconf}
1839
		fi
1840
		eval "_value=\${PKG_REPO_SERVER_RELEASE_${jail_arch##*.}}"
1841
		if [ -n "${_value}" ]; then
1842
			echo "PKG_REPO_SERVER_RELEASE_${jail_arch##*.}=${_value}" \
1843
				>> ${_makeconf}
1844
		fi
1845
	done
1846

    
1847
	# Change version of pfSense meta ports for snapshots
1848
	if [ -z "${_IS_RELEASE}" ]; then
1849
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
1850
		sed -i '' \
1851
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
1852
			-e "/^PORTREVISION=/d" \
1853
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile \
1854
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/Makefile
1855
	fi
1856

    
1857
	# Copy over pkg repo templates to pfSense-repo
1858
	mkdir -p /usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1859
	cp -f ${PKG_REPO_BASE}/* \
1860
		/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1861

    
1862
	for jail_arch in ${_archs}; do
1863
		jail_name=$(poudriere_jail_name ${jail_arch})
1864

    
1865
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1866
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
1867
			continue
1868
		fi
1869

    
1870
		_ref_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.ref.${jail_arch}
1871
		rm -rf ${_ref_bulk} ${_ref_bulk}.tmp
1872
		touch ${_ref_bulk}.tmp
1873
		if [ -f "${POUDRIERE_BULK}.${jail_arch#*.}" ]; then
1874
			cat "${POUDRIERE_BULK}.${jail_arch#*.}" >> ${_ref_bulk}.tmp
1875
		fi
1876
		if [ -f "${POUDRIERE_BULK}" ]; then
1877
			cat "${POUDRIERE_BULK}" >> ${_ref_bulk}.tmp
1878
		fi
1879
		cat ${_ref_bulk}.tmp | sort -u > ${_ref_bulk}
1880

    
1881
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.${jail_arch}
1882
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
1883

    
1884
		local _exclude_bulk="${POUDRIERE_BULK}.exclude.${jail_arch}"
1885
		if [ -f "${_exclude_bulk}" ]; then
1886
			mv ${_bulk} ${_bulk}.tmp
1887
			sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_exclude_bulk} > ${_bulk}.exclude
1888
			cat ${_bulk}.tmp ${_bulk}.exclude | sort | uniq -u > ${_bulk}
1889
			rm -f ${_bulk}.tmp ${_bulk}.exclude
1890
		fi
1891

    
1892
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
1893
			echo ">>> ERROR: Something went wrong..."
1894
			print_error_pfS
1895
		fi
1896

    
1897
		echo ">>> Cleaning up old packages from repo..."
1898
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
1899
			echo ">>> ERROR: Something went wrong..."
1900
			print_error_pfS
1901
		fi
1902

    
1903
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
1904
	done
1905
}
1906

    
1907
# This routine is called to write out to stdout
1908
# a string. The string is appended to $SNAPSHOTSLOGFILE
1909
snapshots_update_status() {
1910
	if [ -z "$1" ]; then
1911
		return
1912
	fi
1913
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
1914
		return
1915
	fi
1916
	echo "$*"
1917
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
1918
}
1919

    
1920
create_sha256() {
1921
	local _file="${1}"
1922

    
1923
	if [ ! -f "${_file}" ]; then
1924
		return 1
1925
	fi
1926

    
1927
	( \
1928
		cd $(dirname ${_file}) && \
1929
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
1930
	)
1931
}
1932

    
1933
snapshots_create_latest_symlink() {
1934
	local _image="${1}"
1935

    
1936
	if [ -z "${_image}" ]; then
1937
		return
1938
	fi
1939

    
1940
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
1941
		return
1942
	fi
1943

    
1944
	if [ ! -f "${_image}" ]; then
1945
		return
1946
	fi
1947

    
1948
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
1949
	ln -sf $(basename ${_image}) ${_symlink}
1950
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
1951
}
1952

    
1953
snapshots_create_sha256() {
1954
	local _img=""
1955

    
1956
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
1957
		if [ -f "${_img}.gz" ]; then
1958
			_img="${_img}.gz"
1959
		fi
1960
		if [ ! -f "${_img}" ]; then
1961
			continue
1962
		fi
1963
		create_sha256 ${_img}
1964
		snapshots_create_latest_symlink ${_img}
1965
	done
1966
}
1967

    
1968
snapshots_scp_files() {
1969
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
1970
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60"
1971
	fi
1972

    
1973
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
1974
	pkg_repo_rsync "${CORE_PKG_PATH}"
1975
	snapshots_update_status ">>> Finished copying core pkg repo"
1976

    
1977
	for _rsyncip in ${RSYNCIP}; do
1978
		snapshots_update_status ">>> Copying files to ${_rsyncip}"
1979

    
1980
		# Ensure directory(s) are available
1981
		ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/installer"
1982
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1983
			ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/virtualization"
1984
		fi
1985
		# ensure permissions are correct for r+w
1986
		ssh ${RSYNCUSER}@${_rsyncip} "chmod -R ug+rw ${RSYNCPATH}/."
1987
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
1988
			${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/installer/
1989
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1990
			rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
1991
				${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/virtualization/
1992
		fi
1993

    
1994
		snapshots_update_status ">>> Finished copying files."
1995
	done
1996
}
(2-2/3)