Project

General

Profile

Download (60.1 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-2018 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# FreeSBIE portions of the code
10
# Copyright (c) 2005 Dario Freni
11
# and copied from FreeSBIE project
12
# All rights reserved.
13
#
14
# Licensed under the Apache License, Version 2.0 (the "License");
15
# you may not use this file except in compliance with the License.
16
# You may obtain a copy of the License at
17
#
18
# http://www.apache.org/licenses/LICENSE-2.0
19
#
20
# Unless required by applicable law or agreed to in writing, software
21
# distributed under the License is distributed on an "AS IS" BASIS,
22
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
# See the License for the specific language governing permissions and
24
# limitations under the License.
25

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

    
31
kldload filemon >/dev/null 2>&1
32

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

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

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

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

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

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

    
83
	local _template_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
84

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

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

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

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

    
135
get_pkg_name() {
136
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
137
}
138

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

    
147
	[ -d "${KERNEL_BUILD_PATH}" ] \
148
		&& rm -rf ${KERNEL_BUILD_PATH}
149

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

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

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

    
167
		buildkernel
168

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

    
172
		ensure_kernel_exists $KERNEL_DESTDIR
173

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

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

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

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

    
192
	export KERNEL_NAME="${1}"
193

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

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

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

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

    
227
	unset KERNEL_NAME
228
}
229

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

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

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

    
248
	[ -d "${INSTALLER_CHROOT_DIR}" ] \
249
		|| mkdir -p ${INSTALLER_CHROOT_DIR}
250

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

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

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

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

    
270
	# Use the builder cross compiler from obj to produce the final binary.
271
	if [ "${TARGET_ARCH}" == "$(uname -p)" ]; then
272
		BUILD_CC="${MAKEOBJDIRPREFIX}/${FREEBSD_SRC_DIR}/tmp/usr/bin/cc"
273
	else
274
		BUILD_CC="${MAKEOBJDIRPREFIX}/${TARGET}.${TARGET_ARCH}${FREEBSD_SRC_DIR}/tmp/usr/bin/cc"
275
	fi
276

    
277
	[ -f "${BUILD_CC}" ] || print_error_pfS
278

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

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

    
307
	unset makeargs
308
}
309

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

    
326
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
327

    
328
	local _mntdir=${OVA_TMP}/mnt
329

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

    
346
	mkdir -p $(dirname ${OVAPATH})
347

    
348
	mkdir -p ${_mntdir}
349

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

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

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

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

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

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

    
396
	echo "Done!" | tee -a ${LOGFILE}
397

    
398
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
399

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

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

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

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

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

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

    
450
	rm -f ${OVA_TMP}/${OVFRAW}
451

    
452
	ova_setup_ovf_template
453

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

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

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

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

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

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

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

    
509
	staginareas_clean_each_run
510

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

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

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

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

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

    
553
	echo "Done!"
554

    
555
	echo ">>> Cleaning of builder environment has finished."
556
}
557

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

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

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

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

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

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

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

    
610
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
611

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

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

    
631
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
632
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
633
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
634

    
635
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
636

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

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

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

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

    
657
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
658

    
659
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
660

    
661
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
662
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
663

    
664
	# Make sure pkg is present
665
	pkg_bootstrap ${STAGE_CHROOT_DIR}
666

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

    
677
	echo "Done!"
678
}
679

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

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

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

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

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

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

    
719
	# Prepare final stage area
720
	create_final_staging_area
721

    
722
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
723
	pkg_chroot_add ${FINAL_CHROOT_DIR} base
724

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

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

    
737
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
738

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

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

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

    
771
create_distribution_tarball() {
772
	mkdir -p ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist
773

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

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

    
786
create_iso_image() {
787
	local _variant="$1"
788

    
789
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
790

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

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

    
798
	mkdir -p $(dirname ${ISOPATH})
799

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

    
807
	customize_stagearea_for_image "iso" "" $_variant
808
	install_default_kernel ${DEFAULT_KERNEL}
809

    
810
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
811
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
812

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

    
818
	create_distribution_tarball
819

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

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

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

    
832
	gzip -qf $_image_path &
833
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
834

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

    
838
create_memstick_image() {
839
	local _variant="$1"
840

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

    
847
	mkdir -p $(dirname ${MEMSTICKPATH})
848

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

    
856
	customize_stagearea_for_image "memstick" "" $_variant
857
	install_default_kernel ${DEFAULT_KERNEL}
858

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

    
861
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
862
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
863

    
864
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
865

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

    
870
	create_distribution_tarball
871

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

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

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

    
884
	gzip -qf $_image_path &
885
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
886

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

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

    
897
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
898

    
899
	customize_stagearea_for_image "memstickserial"
900
	install_default_kernel ${DEFAULT_KERNEL}
901

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

    
904
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
905
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
906

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

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

    
918
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
919
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
920

    
921
	create_distribution_tarball
922

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

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

    
932
	gzip -qf $MEMSTICKSERIALPATH &
933
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
934

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

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

    
945
	mkdir -p $(dirname ${MEMSTICKADIPATH})
946

    
947
	customize_stagearea_for_image "memstickadi"
948
	install_default_kernel ${DEFAULT_KERNEL}
949

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

    
952
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
953
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
954

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

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

    
968
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
969
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
970

    
971
	create_distribution_tarball
972

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

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

    
982
	gzip -qf $MEMSTICKADIPATH &
983
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
984

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

    
988
get_altabi_arch() {
989
	local _target_arch="$1"
990

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

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

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

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

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

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

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

    
1047
	local ALTABI_ARCH=$(get_altabi_arch ${_target_arch})
1048

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

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

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

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

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

    
1082
		update_freebsd_sources
1083

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

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

    
1097
	pkg install ${PRODUCT_NAME}-builder
1098
}
1099

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

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

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

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

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

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

    
1142
pkg_chroot() {
1143
	local _root="${1}"
1144
	shift
1145

    
1146
	if [ $# -eq 0 ]; then
1147
		return -1
1148
	fi
1149

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

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

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

    
1177
	return $result
1178
}
1179

    
1180

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

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

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

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

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

    
1204
pkg_bootstrap() {
1205
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1206

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

    
1214
	pkg_chroot ${_root} bootstrap -f
1215
}
1216

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

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

    
1227
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1228

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

    
1232
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1233
		mkdir -p ${SCRATCHDIR}/pkg_cache
1234

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

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

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

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

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

    
1277
	local _old_kernconf=${KERNCONF}
1278
	export KERNCONF=${_kernconf}
1279

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

    
1285
	export KERNCONF=${_old_kernconf}
1286
}
1287

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

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

    
1298
	local _old_kernconf=${KERNCONF}
1299
	export KERNCONF=${_kernconf}
1300

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

    
1308
	export KERNCONF=${_old_kernconf}
1309
}
1310

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

    
1318
	echo ">>> Operation $0 has started at $(date)"
1319
}
1320

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

    
1325
pkg_repo_rsync() {
1326
	local _repo_path_param="${1}"
1327
	local _ignore_final_rsync="${2}"
1328

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

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

    
1337
	# Sanitize path
1338
	_repo_path=$(realpath ${_repo_path_param})
1339

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1455
poudriere_possible_archs() {
1456
	local _arch=$(uname -m)
1457
	local _archs=""
1458

    
1459
	# If host is amd64, we'll create both repos, and if possible armv6
1460
	if [ "${_arch}" = "amd64" ]; then
1461
		_archs="amd64.amd64"
1462

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

    
1467
			if binmiscctl lookup armv6 >/dev/null 2>&1; then
1468
				_archs="${_archs} arm.armv6"
1469
			fi
1470
		fi
1471
	fi
1472

    
1473
	if [ -n "${ARCH_LIST}" ]; then
1474
		local _found=0
1475
		for _desired_arch in ${ARCH_LIST}; do
1476
			_found=0
1477
			for _possible_arch in ${_archs}; do
1478
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1479
					_found=1
1480
					break
1481
				fi
1482
			done
1483
			if [ ${_found} -eq 0 ]; then
1484
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1485
				print_error_pfS
1486
			fi
1487
		done
1488
		_archs="${ARCH_LIST}"
1489
	fi
1490

    
1491
	echo ${_archs}
1492
}
1493

    
1494
poudriere_jail_name() {
1495
	local _jail_arch="${1}"
1496

    
1497
	if [ -z "${_jail_arch}" ]; then
1498
		return 1
1499
	fi
1500

    
1501
	# Remove arch
1502
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1503
}
1504

    
1505
poudriere_rename_ports() {
1506
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1507
		return;
1508
	fi
1509

    
1510
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1511

    
1512
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1513

    
1514
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1515
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1516
		local _pdir=$(dirname ${d})
1517
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1518
		local _plist=""
1519

    
1520
		if [ -e ${_pdir}/${_pname} ]; then
1521
			rm -rf ${_pdir}/${_pname}
1522
		fi
1523

    
1524
		cp -r ${d} ${_pdir}/${_pname}
1525

    
1526
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1527
			_plist=${_pdir}/${_pname}/pkg-plist
1528
		fi
1529

    
1530
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1531
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1532
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1533
			${_pdir}/${_pname}/Makefile \
1534
			${_pdir}/${_pname}/pkg-descr ${_plist}
1535

    
1536
		# PHP module is special
1537
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1538
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1539
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1540
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1541
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1542
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1543
				${_pdir}/${_pname}/files/config.m4
1544

    
1545
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1546
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1547
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1548
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1549
				${_pdir}/${_pname}/files/pfSense.c \
1550
				${_pdir}/${_pname}/files/php_pfSense.h
1551
		fi
1552

    
1553
		if [ -d ${_pdir}/${_pname}/files ]; then
1554
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1555
				local _fddir=$(dirname ${fd})
1556
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1557

    
1558
				mv ${fd} ${_fddir}/${_fdname}
1559
			done
1560
		fi
1561
	done
1562
	echo "Done!" | tee -a ${LOGFILE}
1563
}
1564

    
1565
poudriere_create_ports_tree() {
1566
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1567

    
1568
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1569
		local _branch=""
1570
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1571
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1572
			print_error_pfS
1573
		fi
1574
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1575
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1576
		fi
1577
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1578
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git -U ${POUDRIERE_PORTS_GIT_URL} ${_branch} >/dev/null 2>&1; then
1579
			echo "" | tee -a ${LOGFILE}
1580
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1581
			print_error_pfS
1582
		fi
1583
		echo "Done!" | tee -a ${LOGFILE}
1584
		poudriere_rename_ports
1585
	fi
1586
}
1587

    
1588
poudriere_init() {
1589
	local _error=0
1590
	local _archs=$(poudriere_possible_archs)
1591

    
1592
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1593

    
1594
	# Sanity checks
1595
	if [ -z "${ZFS_TANK}" ]; then
1596
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1597
		error=1
1598
	fi
1599

    
1600
	if [ -z "${ZFS_ROOT}" ]; then
1601
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1602
		error=1
1603
	fi
1604

    
1605
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1606
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1607
		error=1
1608
	fi
1609

    
1610
	if [ ${_error} -eq 1 ]; then
1611
		print_error_pfS
1612
	fi
1613

    
1614
	# Check if zpool exists
1615
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
1616
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
1617
		print_error_pfS
1618
	fi
1619

    
1620
	# Check if zfs rootfs exists
1621
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1622
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
1623
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
1624
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1625
			echo "Done!"
1626
		else
1627
			echo "Failed!"
1628
			print_error_pfS
1629
		fi
1630
	fi
1631

    
1632
	# Make sure poudriere is installed
1633
	if [ ! -f /usr/local/bin/poudriere ]; then
1634
		echo ">>> Installing poudriere..." | tee -a ${LOGFILE}
1635
		if ! pkg install poudriere >/dev/null 2>&1; then
1636
			echo ">>> ERROR: poudriere was not installed, aborting..." | tee -a ${LOGFILE}
1637
			print_error_pfS
1638
		fi
1639
	fi
1640

    
1641
	# Create poudriere.conf
1642
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1643
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1644
		print_error_pfS
1645
	fi
1646
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
1647
	cat <<EOF >/usr/local/etc/poudriere.conf
1648
ZPOOL=${ZFS_TANK}
1649
ZROOTFS=${ZFS_ROOT}
1650
RESOLV_CONF=/etc/resolv.conf
1651
BASEFS=/usr/local/poudriere
1652
USE_PORTLINT=no
1653
USE_TMPFS=yes
1654
NOLINUX=yes
1655
CHECK_CHANGED_OPTIONS=yes
1656
CHECK_CHANGED_DEPS=yes
1657
ATOMIC_PACKAGE_REPOSITORY=yes
1658
COMMIT_PACKAGES_ON_FAILURE=no
1659
KEEP_OLD_PACKAGES=yes
1660
KEEP_OLD_PACKAGES_COUNT=5
1661
EOF
1662

    
1663
	if pkg info -e ccache; then
1664
	cat <<EOF >>/usr/local/etc/poudriere.conf
1665
CCACHE_DIR=/var/cache/ccache
1666
EOF
1667
	fi
1668

    
1669
	# Create specific items conf
1670
	[ ! -d /usr/local/etc/poudriere.d ] \
1671
		&& mkdir -p /usr/local/etc/poudriere.d
1672

    
1673
	# Remove old jails
1674
	for jail_arch in ${_archs}; do
1675
		jail_name=$(poudriere_jail_name ${jail_arch})
1676

    
1677
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1678
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
1679
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
1680
		fi
1681
	done
1682

    
1683
	# Remove old ports tree
1684
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1685
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
1686
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
1687
	fi
1688

    
1689
	local native_xtools=""
1690
	# Now we are ready to create jails
1691
	for jail_arch in ${_archs}; do
1692
		jail_name=$(poudriere_jail_name ${jail_arch})
1693

    
1694
		if [ "${jail_arch}" = "arm.armv6" ]; then
1695
			native_xtools="-x"
1696
		else
1697
			native_xtools=""
1698
		fi
1699

    
1700
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1701
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
1702
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
1703
			echo "" | tee -a ${LOGFILE}
1704
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1705
			print_error_pfS
1706
		fi
1707
		echo "Done!" | tee -a ${LOGFILE}
1708
	done
1709

    
1710
	poudriere_create_ports_tree
1711

    
1712
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
1713
}
1714

    
1715
poudriere_update_jails() {
1716
	local _archs=$(poudriere_possible_archs)
1717

    
1718
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1719

    
1720
	local native_xtools=""
1721
	for jail_arch in ${_archs}; do
1722
		jail_name=$(poudriere_jail_name ${jail_arch})
1723

    
1724
		local _create_or_update="-u"
1725
		local _create_or_update_text="Updating"
1726
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1727
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
1728
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
1729
			_create_or_update_text="Creating"
1730
		fi
1731

    
1732
		if [ "${jail_arch}" = "arm.armv6" ]; then
1733
			native_xtools="-x"
1734
		else
1735
			native_xtools=""
1736
		fi
1737

    
1738
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1739
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
1740
			echo "" | tee -a ${LOGFILE}
1741
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1742
			print_error_pfS
1743
		fi
1744
		echo "Done!" | tee -a ${LOGFILE}
1745
	done
1746
}
1747

    
1748
poudriere_update_ports() {
1749
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1750

    
1751
	# Create ports tree if necessary
1752
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1753
		poudriere_create_ports_tree
1754
	else
1755
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1756
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
1757
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
1758
		echo "Done!" | tee -a ${LOGFILE}
1759
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1760
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
1761
		echo "Done!" | tee -a ${LOGFILE}
1762
		poudriere_rename_ports
1763
	fi
1764
}
1765

    
1766
poudriere_bulk() {
1767
	local _archs=$(poudriere_possible_archs)
1768
	local _makeconf
1769

    
1770
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1771

    
1772
	if [ -n "${UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
1773
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
1774
		print_error_pfS
1775
	fi
1776

    
1777
	rm -f ${LOGFILE}
1778

    
1779
	poudriere_create_ports_tree
1780

    
1781
	[ -d /usr/local/etc/poudriere.d ] || \
1782
		mkdir -p /usr/local/etc/poudriere.d
1783

    
1784
	_makeconf=/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1785
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
1786
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ${_makeconf}
1787
	fi
1788

    
1789
	cat <<EOF >>/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1790
PKG_REPO_BRANCH_DEVEL=${PKG_REPO_BRANCH_DEVEL}
1791
PKG_REPO_BRANCH_RELEASE=${PKG_REPO_BRANCH_RELEASE}
1792
PKG_REPO_SERVER_DEVEL=${PKG_REPO_SERVER_DEVEL}
1793
PKG_REPO_SERVER_RELEASE=${PKG_REPO_SERVER_RELEASE}
1794
POUDRIERE_PORTS_NAME=${POUDRIERE_PORTS_NAME}
1795
PFSENSE_DEFAULT_REPO=${PFSENSE_DEFAULT_REPO}
1796
PRODUCT_NAME=${PRODUCT_NAME}
1797
REPO_BRANCH_PREFIX=${REPO_BRANCH_PREFIX}
1798
EOF
1799

    
1800
	local _value=""
1801
	for jail_arch in ${_archs}; do
1802
		eval "_value=\${PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}}"
1803
		if [ -n "${_value}" ]; then
1804
			echo "PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}=${_value}" \
1805
				>> ${_makeconf}
1806
		fi
1807
		eval "_value=\${PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}}"
1808
		if [ -n "${_value}" ]; then
1809
			echo "PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}=${_value}" \
1810
				>> ${_makeconf}
1811
		fi
1812
		eval "_value=\${PKG_REPO_SERVER_DEVEL_${jail_arch##*.}}"
1813
		if [ -n "${_value}" ]; then
1814
			echo "PKG_REPO_SERVER_DEVEL_${jail_arch##*.}=${_value}" \
1815
				>> ${_makeconf}
1816
		fi
1817
		eval "_value=\${PKG_REPO_SERVER_RELEASE_${jail_arch##*.}}"
1818
		if [ -n "${_value}" ]; then
1819
			echo "PKG_REPO_SERVER_RELEASE_${jail_arch##*.}=${_value}" \
1820
				>> ${_makeconf}
1821
		fi
1822
	done
1823

    
1824
	# Change version of pfSense meta ports for snapshots
1825
	if [ -z "${_IS_RELEASE}" ]; then
1826
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
1827
		sed -i '' \
1828
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
1829
			-e "/^PORTREVISION=/d" \
1830
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile \
1831
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/Makefile
1832
	fi
1833

    
1834
	# Copy over pkg repo templates to pfSense-repo
1835
	mkdir -p /usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1836
	cp -f ${PKG_REPO_BASE}/* \
1837
		/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1838

    
1839
	for jail_arch in ${_archs}; do
1840
		jail_name=$(poudriere_jail_name ${jail_arch})
1841

    
1842
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1843
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
1844
			continue
1845
		fi
1846

    
1847
		_ref_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.ref.${jail_arch}
1848
		rm -rf ${_ref_bulk} ${_ref_bulk}.tmp
1849
		touch ${_ref_bulk}.tmp
1850
		if [ -f "${POUDRIERE_BULK}.${jail_arch#*.}" ]; then
1851
			cat "${POUDRIERE_BULK}.${jail_arch#*.}" >> ${_ref_bulk}.tmp
1852
		fi
1853
		if [ -f "${POUDRIERE_BULK}" ]; then
1854
			cat "${POUDRIERE_BULK}" >> ${_ref_bulk}.tmp
1855
		fi
1856
		cat ${_ref_bulk}.tmp | sort -u > ${_ref_bulk}
1857

    
1858
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.${jail_arch}
1859
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
1860

    
1861
		local _exclude_bulk="${POUDRIERE_BULK}.exclude.${jail_arch}"
1862
		if [ -f "${_exclude_bulk}" ]; then
1863
			mv ${_bulk} ${_bulk}.tmp
1864
			sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_exclude_bulk} > ${_bulk}.exclude
1865
			cat ${_bulk}.tmp ${_bulk}.exclude | sort | uniq -u > ${_bulk}
1866
			rm -f ${_bulk}.tmp ${_bulk}.exclude
1867
		fi
1868

    
1869
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
1870
			echo ">>> ERROR: Something went wrong..."
1871
			print_error_pfS
1872
		fi
1873

    
1874
		echo ">>> Cleaning up old packages from repo..."
1875
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
1876
			echo ">>> ERROR: Something went wrong..."
1877
			print_error_pfS
1878
		fi
1879

    
1880
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
1881
	done
1882
}
1883

    
1884
# This routine is called to write out to stdout
1885
# a string. The string is appended to $SNAPSHOTSLOGFILE
1886
snapshots_update_status() {
1887
	if [ -z "$1" ]; then
1888
		return
1889
	fi
1890
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
1891
		return
1892
	fi
1893
	echo "$*"
1894
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
1895
}
1896

    
1897
create_sha256() {
1898
	local _file="${1}"
1899

    
1900
	if [ ! -f "${_file}" ]; then
1901
		return 1
1902
	fi
1903

    
1904
	( \
1905
		cd $(dirname ${_file}) && \
1906
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
1907
	)
1908
}
1909

    
1910
snapshots_create_latest_symlink() {
1911
	local _image="${1}"
1912

    
1913
	if [ -z "${_image}" ]; then
1914
		return
1915
	fi
1916

    
1917
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
1918
		return
1919
	fi
1920

    
1921
	if [ ! -f "${_image}" ]; then
1922
		return
1923
	fi
1924

    
1925
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
1926
	ln -sf $(basename ${_image}) ${_symlink}
1927
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
1928
}
1929

    
1930
snapshots_create_sha256() {
1931
	local _img=""
1932

    
1933
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
1934
		if [ -f "${_img}.gz" ]; then
1935
			_img="${_img}.gz"
1936
		fi
1937
		if [ ! -f "${_img}" ]; then
1938
			continue
1939
		fi
1940
		create_sha256 ${_img}
1941
		snapshots_create_latest_symlink ${_img}
1942
	done
1943
}
1944

    
1945
snapshots_scp_files() {
1946
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
1947
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60"
1948
	fi
1949

    
1950
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
1951
	pkg_repo_rsync "${CORE_PKG_PATH}"
1952
	snapshots_update_status ">>> Finished copying core pkg repo"
1953

    
1954
	for _rsyncip in ${RSYNCIP}; do
1955
		snapshots_update_status ">>> Copying files to ${_rsyncip}"
1956

    
1957
		# Ensure directory(s) are available
1958
		ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/installer"
1959
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1960
			ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/virtualization"
1961
		fi
1962
		# ensure permissions are correct for r+w
1963
		ssh ${RSYNCUSER}@${_rsyncip} "chmod -R ug+rw ${RSYNCPATH}/."
1964
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
1965
			${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/installer/
1966
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1967
			rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
1968
				${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/virtualization/
1969
		fi
1970

    
1971
		snapshots_update_status ">>> Finished copying files."
1972
	done
1973
}
(2-2/3)