Project

General

Profile

Download (61.6 KB) Statistics
| Branch: | Tag: | Revision:
1 6f73c362 Renato Botelho
#!/bin/sh
2
#
3
# builder_common.sh
4
#
5 ac24dc24 Renato Botelho
# part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
# Copyright (c) 2004-2013 BSD Perimeter
7
# Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
# Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 6f73c362 Renato Botelho
# 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 b12ea3fb Renato Botelho
# 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 6f73c362 Renato Botelho
#
20 b12ea3fb Renato Botelho
# http://www.apache.org/licenses/LICENSE-2.0
21 6f73c362 Renato Botelho
#
22 b12ea3fb Renato Botelho
# 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 6f73c362 Renato Botelho
28 f26731b0 Renato Botelho
if [ -z "${IMAGES_FINAL_DIR}" -o "${IMAGES_FINAL_DIR}" = "/" ]; then
29 04992be2 Renato Botelho
	echo "IMAGES_FINAL_DIR is not defined"
30 6f73c362 Renato Botelho
	print_error_pfS
31
fi
32
33 a81faa85 Renato Botelho
kldload filemon >/dev/null 2>&1
34
35 6f73c362 Renato Botelho
lc() {
36
	echo "${1}" | tr '[[:upper:]]' '[[:lower:]]'
37
}
38
39
git_last_commit() {
40 0b0ef57e Renato Botelho
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
41
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
42 78b0f246 Renato Botelho
	echo ">>> Last known commit $CURRENT_AUTHOR - $CURRENT_COMMIT"
43
	echo "$CURRENT_COMMIT" > $SCRATCHDIR/build_commit_info.txt
44 6f73c362 Renato Botelho
}
45
46 98978a36 Renato Botelho
# Create core pkg repository
47
core_pkg_create_repo() {
48 da781042 Renato Botelho
	if [ ! -d "${CORE_PKG_REAL_PATH}/All" ]; then
49 98978a36 Renato Botelho
		return
50
	fi
51
52 da781042 Renato Botelho
	############ 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 98978a36 Renato Botelho
	echo -n ">>> Creating core packages repository... "
60 da781042 Renato Botelho
	if pkg repo -q "${CORE_PKG_REAL_PATH}/"; then
61 98978a36 Renato Botelho
		echo "Done!"
62
	else
63
		echo "Failed!"
64
		print_error_pfS
65
	fi
66 da781042 Renato Botelho
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 91039db4 Renato Botelho
	ln -sf .latest/All ${CORE_PKG_ALL_PATH}
71 da781042 Renato Botelho
	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 98978a36 Renato Botelho
}
75
76 6f73c362 Renato Botelho
# 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 318a7b77 Luiz Souza
	local _findroot="${5}"
83
	local _filter="${6}"
84 6f73c362 Renato Botelho
85 1217cd7a Renato Botelho
	local _template_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
86
87 dff2bf9c Renato Botelho
	# 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 cdf2b5f8 Renato Botelho
	${BUILDER_SCRIPTS}/create_core_pkg.sh \
95 1217cd7a Renato Botelho
		-t "${_template_path}" \
96
		-f "${_flavor}" \
97
		-v "${_version}" \
98
		-r "${_root}" \
99 318a7b77 Luiz Souza
		-s "${_findroot}" \
100 1217cd7a Renato Botelho
		-F "${_filter}" \
101
		-d "${CORE_PKG_REAL_PATH}/All" \
102 dff2bf9c Renato Botelho
		-a "${_abi}" \
103
		-A "${_altabi}" \
104 1217cd7a Renato Botelho
		|| print_error_pfS
105 6f73c362 Renato Botelho
}
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 d4c6029e Renato Botelho
	[ -n "${LOGFILE}" -a -f "${LOGFILE}" ] && \
117 6f73c362 Renato Botelho
		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 91039db4 Renato Botelho
		if [ -n "${NO_BUILDKERNEL}" -a -f "${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz" ]; then
165 6f73c362 Renato Botelho
			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 044cf5bd Renato Botelho
		echo ">>> Creating pkg of $KERNEL_NAME-debug kernel to staging area..."  | tee -a ${LOGFILE}
177 1ec6f217 Luiz Souza
		core_pkg_create kernel-debug ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} \
178 fd349773 Renato Botelho
		    "./usr/lib/debug/boot" \*.debug
179 72e2fb75 Renato Botelho
		rm -rf ${KERNEL_DESTDIR}/usr
180 a9ae8dac Renato Botelho
181 044cf5bd Renato Botelho
		echo ">>> Creating pkg of $KERNEL_NAME kernel to staging area..."  | tee -a ${LOGFILE}
182 1ec6f217 Luiz Souza
		core_pkg_create kernel ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} "./boot/kernel ./boot/modules"
183 6f73c362 Renato Botelho
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 d071acf5 Renato Botelho
	# 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 6f73c362 Renato Botelho
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 91039db4 Renato Botelho
		cp ${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz $FINAL_CHROOT_DIR/pkgs
214 6f73c362 Renato Botelho
		if [ -n "${INSTALL_EXTRA_KERNELS}" ]; then
215
			for _EXTRA_KERNEL in $INSTALL_EXTRA_KERNELS; do
216 91039db4 Renato Botelho
				_EXTRA_KERNEL_PATH=${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${_EXTRA_KERNEL}).txz
217 6f73c362 Renato Botelho
				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 ab7ead15 Renato Botelho
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
237 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" ]; then
238 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
239
		return
240
	fi
241
242 29cdd776 Renato Botelho
	echo ">>> $(LC_ALL=C date) - Starting build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
243 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -K -s ${FREEBSD_SRC_DIR} \
244 29cdd776 Renato Botelho
		|| print_error_pfS
245
	echo ">>> $(LC_ALL=C date) - Finished build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
246 6f73c362 Renato Botelho
247
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
248
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
249 729c0c7a Renato Botelho
250
	[ -d "${INSTALLER_CHROOT_DIR}" ] \
251
		|| mkdir -p ${INSTALLER_CHROOT_DIR}
252
253 468f236d Renato Botelho
	echo ">>> Installing world with bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
254 49de5a2e Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -i -K \
255 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
256
		-d ${INSTALLER_CHROOT_DIR} \
257
		|| print_error_pfS
258
259 e5aeaeb6 jim-p
	# Copy additional installer scripts
260
	install -o root -g wheel -m 0755 ${BUILDER_TOOLS}/installer/*.sh \
261
		${INSTALLER_CHROOT_DIR}/root
262
263 92db4492 Renato Botelho
	# XXX set root password since we don't have nullok enabled
264
	pw -R ${INSTALLER_CHROOT_DIR} usermod root -w yes
265
266 468f236d Renato Botelho
	echo ">>> Installing world without bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
267 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -K \
268 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
269
		-d ${STAGE_CHROOT_DIR} \
270
		|| print_error_pfS
271 6f73c362 Renato Botelho
272 06c13973 Renato Botelho
	# Use the builder cross compiler from obj to produce the final binary.
273 295b0d2b Renato Botelho
	BUILD_CC="${MAKEOBJDIRPREFIX}${FREEBSD_SRC_DIR}/${TARGET}.${TARGET_ARCH}/tmp/usr/bin/cc"
274 06c13973 Renato Botelho
275
	[ -f "${BUILD_CC}" ] || print_error_pfS
276
277 468f236d Renato Botelho
	# XXX It must go to the scripts
278 6f73c362 Renato Botelho
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
279
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
280 06c13973 Renato Botelho
	makeargs="CC=${BUILD_CC} DESTDIR=${STAGE_CHROOT_DIR}"
281 6f73c362 Renato Botelho
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
282 d4c6029e Renato Botelho
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
283 a0b4972b Renato Botelho
	# 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 6f73c362 Renato Botelho
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
286
287 1af9fab9 Renato Botelho
	if [ "${PRODUCT_NAME}" = "pfSense" -a -n "${GNID_REPO_BASE}" ]; then
288
		echo ">>> Building gnid... " | tee -a ${LOGFILE}
289 0874c8f5 Renato Botelho
		(\
290
			cd ${GNID_SRC_DIR} && \
291 06c13973 Renato Botelho
			make \
292
				CC=${BUILD_CC} \
293
				INCLUDE_DIR=${GNID_INCLUDE_DIR} \
294
				LIBCRYPTO_DIR=${GNID_LIBCRYPTO_DIR} \
295
			clean gnid \
296 0874c8f5 Renato Botelho
		) || print_error_pfS
297 1af9fab9 Renato Botelho
		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 6f73c362 Renato Botelho
	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 f25c08c9 Renato Botelho
	local _mntdir=${OVA_TMP}/mnt
327
328
	if [ -d "${_mntdir}" ]; then
329 ca0f9142 Renato Botelho
		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 f25c08c9 Renato Botelho
			_dev=$(mount -p ${_mntdir} 2>/dev/null | awk '{print $1}')
335 ca0f9142 Renato Botelho
			[ $? -ne 0 -o -z "${_dev}" ] \
336
				&& break
337 f25c08c9 Renato Botelho
			umount -f ${_mntdir}
338 ca0f9142 Renato Botelho
			mdconfig -d -u ${_dev#/dev/}
339
		done
340 e4de9720 Renato Botelho
		chflags -R noschg ${OVA_TMP}
341
		rm -rf ${OVA_TMP}
342
	fi
343 89a72b59 Renato Botelho
344 f26731b0 Renato Botelho
	mkdir -p $(dirname ${OVAPATH})
345
346 5de5a708 Renato Botelho
	mkdir -p ${_mntdir}
347 89a72b59 Renato Botelho
348 2006b940 Renato Botelho
	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 c56630d7 Renato Botelho
		# 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 2006b940 Renato Botelho
		# 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 c56630d7 Renato Botelho
		# 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 2006b940 Renato Botelho
		# Parameter used by mkimg
365
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
366
	fi
367 35104e03 Renato Botelho
368 6f73c362 Renato Botelho
	# Prepare folder to be put in image
369
	customize_stagearea_for_image "ova"
370
	install_default_kernel ${DEFAULT_KERNEL} "no"
371
372 89a72b59 Renato Botelho
	# Fill fstab
373
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
374 c9b36088 Renato Botelho
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
375 2006b940 Renato Botelho
	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 89a72b59 Renato Botelho
379
	# Create / partition
380 7743b4bd Renato Botelho
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
381 5de5a708 Renato Botelho
	truncate -s ${OVA_FIRST_PART_SIZE} ${OVA_TMP}/${OVFUFS}
382 eeddd261 Renato Botelho
	local _md=$(mdconfig -a -f ${OVA_TMP}/${OVFUFS})
383 5de5a708 Renato Botelho
	trap "mdconfig -d -u ${_md}; return" 1 2 15 EXIT
384
385
	newfs -L ${PRODUCT_NAME} -j /dev/${_md} 2>&1 >>${LOGFILE}
386 89a72b59 Renato Botelho
387 5de5a708 Renato Botelho
	if ! mount /dev/${_md} ${_mntdir} 2>&1 >>${LOGFILE}; then
388 85bc1c63 Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
389 5de5a708 Renato Botelho
		echo ">>> ERROR: Error mounting temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
390 85bc1c63 Renato Botelho
		print_error_pfS
391
	fi
392 32918935 Renato Botelho
	trap "sync; sleep 3; umount ${_mntdir} || umount -f ${_mntdir}; mdconfig -d -u ${_md}; return" 1 2 15 EXIT
393 5de5a708 Renato Botelho
394 85bc1c63 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
395
396 5de5a708 Renato Botelho
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
397
398
	sync
399 b2ee641c Renato Botelho
	sleep 3
400 32918935 Renato Botelho
	umount ${_mntdir} || umount -f ${_mntdir} >>${LOGFILE} 2>&1
401 5de5a708 Renato Botelho
	mdconfig -d -u ${_md}
402
	trap "-" 1 2 15 EXIT
403
404 7743b4bd Renato Botelho
	# Create raw disk
405
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
406 89a72b59 Renato Botelho
	mkimg \
407
		-s gpt \
408 679b5a66 Renato Botelho
		-f raw \
409 8f05bbd6 Renato Botelho
		-b ${FINAL_CHROOT_DIR}/boot/pmbr \
410
		-p freebsd-boot:=${FINAL_CHROOT_DIR}/boot/gptboot \
411 89a72b59 Renato Botelho
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
412 2006b940 Renato Botelho
		${OVA_SWAP_PART_PARAM} \
413 679b5a66 Renato Botelho
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
414 89a72b59 Renato Botelho
415 679b5a66 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
416 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
417
			rm -f ${OVA_TMP}/${OVFUFS}
418
		fi
419 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
420
			rm -f ${OVA_TMP}/${OVFRAW}
421 502db0bd Renato Botelho
		fi
422 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
423 502db0bd Renato Botelho
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
424
		print_error_pfS
425
	fi
426 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
427 502db0bd Renato Botelho
428
	# We don't need it anymore
429
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
430
431 679b5a66 Renato Botelho
	# Convert raw to vmdk
432 7743b4bd Renato Botelho
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
433 beb07890 Renato Botelho
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
434 502db0bd Renato Botelho
435 2dc195f9 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
436 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
437
			rm -f ${OVA_TMP}/${OVFRAW}
438 502db0bd Renato Botelho
		fi
439 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
440
			rm -f ${OVA_TMP}/${OVFVMDK}
441
		fi
442 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
443 89a72b59 Renato Botelho
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
444
		print_error_pfS
445 6f73c362 Renato Botelho
	fi
446 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
447 6f73c362 Renato Botelho
448 3437fab0 Renato Botelho
	rm -f ${OVA_TMP}/${OVFRAW}
449 6f73c362 Renato Botelho
450 89a72b59 Renato Botelho
	ova_setup_ovf_template
451 6f73c362 Renato Botelho
452 7743b4bd Renato Botelho
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
453
	# Create OVA file for vmware
454 89a72b59 Renato Botelho
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
455 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
456 89a72b59 Renato Botelho
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
457
458
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
459 6f73c362 Renato Botelho
}
460
461
# called from create_ova_image
462 a82b9f43 Renato Botelho
ova_setup_ovf_template() {
463 fc48ef54 Renato Botelho
	if [ ! -f ${OVFTEMPLATE} ]; then
464 173fa93f Renato Botelho
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
465
		print_error_pfS
466 6f73c362 Renato Botelho
	fi
467
468 fc48ef54 Renato Botelho
	#  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 502db0bd Renato Botelho
		local _os_type="freebsd64Guest"
474
		local _os_descr="FreeBSD 64-Bit"
475 fc48ef54 Renato Botelho
	else
476
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
477
		print_error_pfS
478
	fi
479 50dcadff Renato Botelho
480 502db0bd Renato Botelho
	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 6f73c362 Renato Botelho
484 fc48ef54 Renato Botelho
	sed \
485 502db0bd Renato Botelho
		-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 fc48ef54 Renato Botelho
		-e "s,%%OS_ID%%,${_os_id},g" \
489
		-e "s,%%OS_TYPE%%,${_os_type},g" \
490 502db0bd Renato Botelho
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
491 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
492 f2d83cfb Renato Botelho
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
493 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
494
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
495 0e334a37 Renato Botelho
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
496
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
497 b12ea3fb Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/LICENSE" \
498 fc48ef54 Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/d" \
499
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
500 6f73c362 Renato Botelho
}
501
502
# Cleans up previous builds
503 7e6ab3ed Renato Botelho
clean_builder() {
504 6f73c362 Renato Botelho
	# 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 729c0c7a Renato Botelho
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR}... "
511 6f73c362 Renato Botelho
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
512 f546e6ca Renato Botelho
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
513 6f73c362 Renato Botelho
		echo "Done."
514
	fi
515
516 729c0c7a Renato Botelho
	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 2abc3e80 Renato Botelho
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
524 edfe9e76 Renato Botelho
		OBJTREE=$(make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
525 6f73c362 Renato Botelho
		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 7e6ab3ed Renato Botelho
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
534 6f73c362 Renato Botelho
			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 04992be2 Renato Botelho
	rm -rf $IMAGES_FINAL_DIR/*
543 6f73c362 Renato Botelho
	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 25c5455d Renato Botelho
	if [ ! -e "$2" ]; then
558
		mkdir -p "$2"
559
	fi
560 6f73c362 Renato Botelho
	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 694028d6 Renato Botelho
	tar -C ${PRODUCT_SRC} -c -f - . | \
579 6f73c362 Renato Botelho
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
580
581
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
582
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
583
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
584
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
585
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
586
	fi
587
588
	## Add buildtime and lastcommit information
589
	# This is used for detecting updates.
590
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
591
	# Record last commit info if it is available.
592
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
593
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
594
	fi
595
596 0d67c726 Renato Botelho
	local _exclude_files="${SCRATCHDIR}/base_exclude_files"
597 6f73c362 Renato Botelho
	sed \
598
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
599
		-e "s,%%VERSION%%,${_version},g" \
600 0ce2a7f3 Renato Botelho
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
601 6f73c362 Renato Botelho
		> ${_exclude_files}
602
603 651f440c Renato Botelho
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
604 10742be9 Renato Botelho
605
	# Include a sample pkg stable conf to base
606
	setup_pkg_repo \
607 db8621d8 Renato Botelho
		${PKG_REPO_DEFAULT} \
608 651f440c Renato Botelho
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
609 10742be9 Renato Botelho
		${TARGET} \
610 74a4eefb Renato Botelho
		${TARGET_ARCH}
611 10742be9 Renato Botelho
612 6f73c362 Renato Botelho
	mtree \
613
		-c \
614
		-k uid,gid,mode,size,flags,sha256digest \
615
		-p ${STAGE_CHROOT_DIR} \
616
		-X ${_exclude_files} \
617 651f440c Renato Botelho
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
618 6f73c362 Renato Botelho
	tar \
619
		-C ${STAGE_CHROOT_DIR} \
620 651f440c Renato Botelho
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
621 6f73c362 Renato Botelho
		-X ${_exclude_files} \
622
		.
623
624 dab31392 Renato Botelho
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
625 6f73c362 Renato Botelho
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
626
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
627
628
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
629 c4968f42 Renato Botelho
630 2f6260c5 Renato Botelho
	# Save current WAN and LAN if value
631 168c162a Renato Botelho
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
632
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
633 2f6260c5 Renato Botelho
634 c4968f42 Renato Botelho
	# Change default interface names to match vmware driver
635 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
636
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
637 fbd4dfc0 Renato Botelho
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
638 c4968f42 Renato Botelho
639
	# Restore default values to be used by serial package
640 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
641
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
642 c4968f42 Renato Botelho
643 6f73c362 Renato Botelho
	# Activate serial console in config.xml
644 2f6260c5 Renato Botelho
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
645
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
646
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
647
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
648
	rm -f ${DEFAULTCONF}.tmp
649 6f73c362 Renato Botelho
650
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
651
652
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
653
654
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
655
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
656
657
	# Make sure pkg is present
658
	pkg_bootstrap ${STAGE_CHROOT_DIR}
659
660 3e80d64e Renato Botelho
	# Make sure correct repo is available on tmp dir
661 a76b4810 Renato Botelho
	mkdir -p ${STAGE_CHROOT_DIR}/tmp/pkg/pkg-repos
662 3e80d64e Renato Botelho
	setup_pkg_repo \
663 dbd615c4 Renato Botelho
		${PKG_REPO_BUILD} \
664 a76b4810 Renato Botelho
		${STAGE_CHROOT_DIR}/tmp/pkg/pkg-repos/repo.conf \
665 3e80d64e Renato Botelho
		${TARGET} \
666
		${TARGET_ARCH} \
667 a76b4810 Renato Botelho
		staging \
668
		${STAGE_CHROOT_DIR}/tmp/pkg/pkg.conf
669 3e80d64e Renato Botelho
670 6f73c362 Renato Botelho
	echo "Done!"
671
}
672
673
create_final_staging_area() {
674
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
675
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
676
		print_error_pfS
677
	fi
678
679
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
680
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
681
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
682
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
683
		echo "Done." | tee -a ${LOGFILE}
684
	fi
685
686
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
687
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
688
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
689
690
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
691
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
692
		print_error_pfS
693
	fi
694
}
695
696
customize_stagearea_for_image() {
697 b2fbacdc Renato Botelho
	local _image_type="$1"
698 b0d0498c Renato Botelho
	local _default_config="" # filled with $2 below
699
	local _image_variant="$3"
700 75e28295 Renato Botelho
701
	if [ -n "$2" ]; then
702
		_default_config="$2"
703 2ac4be3a Renato Botelho
	elif [ "${_image_type}" = "memstickserial" -o \
704 75e28295 Renato Botelho
	     "${_image_type}" = "memstickadi" ]; then
705
		_default_config="default-config-serial"
706
	elif [ "${_image_type}" = "ova" ]; then
707
		_default_config="default-config-vmware"
708
	else
709
		_default_config="default-config"
710
	fi
711 b2fbacdc Renato Botelho
712 6f73c362 Renato Botelho
	# Prepare final stage area
713
	create_final_staging_area
714
715 dab31392 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
716 2ac4be3a Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} base
717 6ee167de Renato Botelho
718 d071acf5 Renato Botelho
	# Set base/rc pkgs as vital to avoid user end up removing it for any reason
719
	pkg_chroot ${FINAL_CHROOT_DIR} set -v 1 -y $(get_pkg_name rc)
720
	pkg_chroot ${FINAL_CHROOT_DIR} set -v 1 -y $(get_pkg_name base)
721
722 b2fbacdc Renato Botelho
	if [ "${_image_type}" = "iso" -o \
723
	     "${_image_type}" = "memstick" -o \
724
	     "${_image_type}" = "memstickserial" -o \
725
	     "${_image_type}" = "memstickadi" ]; then
726 6f73c362 Renato Botelho
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
727 91039db4 Renato Botelho
		cp ${CORE_PKG_ALL_PATH}/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
728 6f73c362 Renato Botelho
	fi
729
730 75e28295 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
731 052f4bdb Renato Botelho
732
	# XXX: Workaround to avoid pkg to complain regarding release
733
	#      repo on first boot since packages are installed from
734
	#      staging server during build phase
735 74a4eefb Renato Botelho
	if [ -n "${USE_PKG_REPO_STAGING}" ]; then
736 052f4bdb Renato Botelho
		_read_cmd="select value from repodata where key='packagesite'"
737 f3800174 Renato Botelho
		if [ -n "${_IS_RELEASE}" -o -n "${_IS_RC}" ]; then
738 71002df8 Renato Botelho
			local _tgt_server="${PKG_REPO_SERVER_RELEASE}"
739
		else
740
			local _tgt_server="${PKG_REPO_SERVER_DEVEL}"
741
		fi
742 052f4bdb Renato Botelho
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
743
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
744 71002df8 Renato Botelho
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER_STAGING},${_tgt_server},")
745 052f4bdb Renato Botelho
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
746
		done
747
	fi
748 b0d0498c Renato Botelho
749
	if [ -n "$_image_variant" -a \
750
	    -d ${BUILDER_TOOLS}/templates/custom_logos/${_image_variant} ]; then
751
		mkdir -p ${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
752
		cp -f \
753 c8735982 Renato Botelho
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.svg \
754
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
755
		cp -f \
756
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.css \
757 b0d0498c Renato Botelho
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
758
	fi
759 3e80d64e Renato Botelho
760
	# Remove temporary repo conf
761 a76b4810 Renato Botelho
	rm -rf ${FINAL_CHROOT_DIR}/tmp/pkg
762 6f73c362 Renato Botelho
}
763
764
create_distribution_tarball() {
765 729c0c7a Renato Botelho
	mkdir -p ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist
766
767 cd618e85 Renato Botelho
	echo -n ">>> Creating distribution tarball... " | tee -a ${LOGFILE}
768 c0ac85e7 Renato Botelho
	tar -C ${FINAL_CHROOT_DIR} --exclude ./pkgs \
769 729c0c7a Renato Botelho
		-cJf ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/base.txz .
770 cd618e85 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
771 6f73c362 Renato Botelho
772 cd618e85 Renato Botelho
	echo -n ">>> Creating manifest... " | tee -a ${LOGFILE}
773 729c0c7a Renato Botelho
	(cd ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist && \
774
		sh ${FREEBSD_SRC_DIR}/release/scripts/make-manifest.sh base.txz) \
775
		> ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/MANIFEST
776 cd618e85 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
777 6f73c362 Renato Botelho
}
778
779
create_iso_image() {
780 b17d47b6 Renato Botelho
	local _variant="$1"
781
782 6f73c362 Renato Botelho
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
783 102e7265 Renato Botelho
784 00dc6b2a Renato Botelho
	if [ -z "${ISOPATH}" ]; then
785 5df6d76a Renato Botelho
		echo ">>> ISOPATH is empty skipping generation of ISO image!" | tee -a ${LOGFILE}
786
		return
787 6f73c362 Renato Botelho
	fi
788
789 5df6d76a Renato Botelho
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
790
791 f26731b0 Renato Botelho
	mkdir -p $(dirname ${ISOPATH})
792
793 b17d47b6 Renato Botelho
	local _image_path=${ISOPATH}
794
	if [ -n "${_variant}" ]; then
795
		_image_path=$(echo "$_image_path" | \
796
			sed "s/${PRODUCT_NAME_SUFFIX}-/&${_variant}-/")
797
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
798
	fi
799
800
	customize_stagearea_for_image "iso" "" $_variant
801 6f73c362 Renato Botelho
	install_default_kernel ${DEFAULT_KERNEL}
802
803 5df6d76a Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
804
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
805 6f73c362 Renato Botelho
806 5df6d76a Renato Botelho
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
807 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
808 286ec1c7 Renato Botelho
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
809 788f1c3b Renato Botelho
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
810 6f73c362 Renato Botelho
811
	create_distribution_tarball
812
813 5df6d76a Renato Botelho
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
814
815 102e7265 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/mkisoimages.sh -b \
816 5df6d76a Renato Botelho
		${FSLABEL} \
817 b17d47b6 Renato Botelho
		${_image_path} \
818 5df6d76a Renato Botelho
		${INSTALLER_CHROOT_DIR}
819 6f73c362 Renato Botelho
820 b17d47b6 Renato Botelho
	if [ ! -f "${_image_path}" ]; then
821 044ff383 Renato Botelho
		echo "ERROR! ISO image was not built"
822
		print_error_pfS
823
	fi
824
825 b17d47b6 Renato Botelho
	gzip -qf $_image_path &
826 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
827 6f73c362 Renato Botelho
828
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
829
}
830
831
create_memstick_image() {
832 b0d0498c Renato Botelho
	local _variant="$1"
833
834 6f73c362 Renato Botelho
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
835
	if [ "${MEMSTICKPATH}" = "" ]; then
836
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
837
		return
838
	fi
839
840 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKPATH})
841
842 b0d0498c Renato Botelho
	local _image_path=${MEMSTICKPATH}
843
	if [ -n "${_variant}" ]; then
844
		_image_path=$(echo "$_image_path" | \
845
			sed "s/-memstick-/-memstick-${_variant}-/")
846
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
847
	fi
848
849
	customize_stagearea_for_image "memstick" "" $_variant
850 624376f6 Renato Botelho
	install_default_kernel ${DEFAULT_KERNEL}
851 6f73c362 Renato Botelho
852 b0d0498c Renato Botelho
	echo ">>> Creating memstick to ${_image_path}." 2>&1 | tee -a ${LOGFILE}
853 6f73c362 Renato Botelho
854 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
855
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
856
857
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
858
859 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
860 286ec1c7 Renato Botelho
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
861 4511fe40 Renato Botelho do Couto
	echo 'boot_serial="NO"' >> ${LOADERCONF}
862 788f1c3b Renato Botelho
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
863 6b5779e9 Renato Botelho
864 6f73c362 Renato Botelho
	create_distribution_tarball
865
866 b66b246e Renato Botelho
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
867
868
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/mkisoimages.sh -b \
869
		${FSLABEL} \
870
		${_image_path} \
871
		${INSTALLER_CHROOT_DIR}
872 729c0c7a Renato Botelho
873 044ff383 Renato Botelho
	if [ ! -f "${_image_path}" ]; then
874
		echo "ERROR! memstick image was not built"
875
		print_error_pfS
876
	fi
877
878 b0d0498c Renato Botelho
	gzip -qf $_image_path &
879 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
880 6f73c362 Renato Botelho
881
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
882
}
883
884
create_memstick_serial_image() {
885
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
886
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
887
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
888
		return
889
	fi
890
891 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
892
893 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickserial"
894
	install_default_kernel ${DEFAULT_KERNEL}
895 6f73c362 Renato Botelho
896
	echo ">>> Creating serial memstick to ${MEMSTICKSERIALPATH}." 2>&1 | tee -a ${LOGFILE}
897
898 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
899
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
900 6f73c362 Renato Botelho
901 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
902 6b5779e9 Renato Botelho
	echo "-S115200 -D" > ${BOOTCONF}
903 6f73c362 Renato Botelho
904
	# Activate serial console+video console in loader.conf
905 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
906 286ec1c7 Renato Botelho
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
907 788f1c3b Renato Botelho
	echo 'boot_multicons="YES"' >> ${LOADERCONF}
908 6f73c362 Renato Botelho
	echo 'boot_serial="YES"' >> ${LOADERCONF}
909
	echo 'console="comconsole,vidconsole"' >> ${LOADERCONF}
910
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
911
912 535f8a75 Renato Botelho
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
913
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
914 6b5779e9 Renato Botelho
915 6f73c362 Renato Botelho
	create_distribution_tarball
916
917 d84eec80 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
918
		${INSTALLER_CHROOT_DIR} \
919
		${MEMSTICKSERIALPATH}
920 6b5779e9 Renato Botelho
921 044ff383 Renato Botelho
	if [ ! -f "${MEMSTICKSERIALPATH}" ]; then
922
		echo "ERROR! memstick serial image was not built"
923
		print_error_pfS
924
	fi
925
926 6f73c362 Renato Botelho
	gzip -qf $MEMSTICKSERIALPATH &
927 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
928 6f73c362 Renato Botelho
929
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
930
}
931
932
create_memstick_adi_image() {
933 462cbb5a Renato Botelho
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
934 6f73c362 Renato Botelho
	if [ "${MEMSTICKADIPATH}" = "" ]; then
935
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
936
		return
937
	fi
938
939 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKADIPATH})
940
941 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickadi"
942
	install_default_kernel ${DEFAULT_KERNEL}
943 6f73c362 Renato Botelho
944
	echo ">>> Creating serial memstick to ${MEMSTICKADIPATH}." 2>&1 | tee -a ${LOGFILE}
945
946 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
947
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
948 6f73c362 Renato Botelho
949 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
950 6b5779e9 Renato Botelho
	echo "-S115200 -h" > ${BOOTCONF}
951 6f73c362 Renato Botelho
952
	# Activate serial console+video console in loader.conf
953 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
954 286ec1c7 Renato Botelho
	echo 'kern.cam.boot_delay=10000' >> ${LOADERCONF}
955 788f1c3b Renato Botelho
	echo 'boot_serial="YES"' >> ${LOADERCONF}
956 6f73c362 Renato Botelho
	echo 'console="comconsole"' >> ${LOADERCONF}
957
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
958
	echo 'comconsole_port="0x2F8"' >> ${LOADERCONF}
959
	echo 'hint.uart.0.flags="0x00"' >> ${LOADERCONF}
960
	echo 'hint.uart.1.flags="0x10"' >> ${LOADERCONF}
961
962 6b5779e9 Renato Botelho
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
963
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
964
965 6f73c362 Renato Botelho
	create_distribution_tarball
966
967 d84eec80 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
968
		${INSTALLER_CHROOT_DIR} \
969
		${MEMSTICKADIPATH}
970 6b5779e9 Renato Botelho
971 044ff383 Renato Botelho
	if [ ! -f "${MEMSTICKADIPATH}" ]; then
972
		echo "ERROR! memstick ADI image was not built"
973
		print_error_pfS
974
	fi
975
976 6f73c362 Renato Botelho
	gzip -qf $MEMSTICKADIPATH &
977 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
978 6f73c362 Renato Botelho
979
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
980
}
981
982 dff2bf9c Renato Botelho
get_altabi_arch() {
983
	local _target_arch="$1"
984
985
	if [ "${_target_arch}" = "amd64" ]; then
986
		echo "x86:64"
987
	elif [ "${_target_arch}" = "i386" ]; then
988
		echo "x86:32"
989 82b59cee Renato Botelho
	elif [ "${_target_arch}" = "armv7" ]; then
990 e200e241 Renato Botelho
		echo "32:el:eabi:softfp"
991 dff2bf9c Renato Botelho
	else
992
		echo ">>> ERROR: Invalid arch"
993
		print_error_pfS
994
	fi
995
}
996
997 6f73c362 Renato Botelho
# Create pkg conf on desired place with desired arch/branch
998
setup_pkg_repo() {
999 74a4eefb Renato Botelho
	if [ -z "${4}" ]; then
1000 6f73c362 Renato Botelho
		return
1001
	fi
1002
1003 db8621d8 Renato Botelho
	local _template="${1}"
1004
	local _target="${2}"
1005
	local _arch="${3}"
1006
	local _target_arch="${4}"
1007 74a4eefb Renato Botelho
	local _staging="${5}"
1008 a76b4810 Renato Botelho
	local _pkg_conf="${6}"
1009 52efc840 Renato Botelho
1010
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
1011
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
1012
		print_error_pfS
1013
	fi
1014 6f73c362 Renato Botelho
1015 74a4eefb Renato Botelho
	if [ -n "${_staging}" -a -n "${USE_PKG_REPO_STAGING}" ]; then
1016 fb4d75d9 Renato Botelho
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_STAGING}
1017
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_STAGING}
1018
		local _pkg_repo_server_release=${PKG_REPO_SERVER_STAGING}
1019
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_STAGING}
1020 74a4eefb Renato Botelho
	else
1021 fb4d75d9 Renato Botelho
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_DEVEL}
1022
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_DEVEL}
1023
		local _pkg_repo_server_release=${PKG_REPO_SERVER_RELEASE}
1024
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_RELEASE}
1025 2c27ff24 Renato Botelho
	fi
1026 97fd32ba Renato Botelho
1027 6f73c362 Renato Botelho
	mkdir -p $(dirname ${_target}) >/dev/null 2>&1
1028
1029
	sed \
1030 ab225f2b Renato Botelho
		-e "s/%%ARCH%%/${_target_arch}/" \
1031 fb4d75d9 Renato Botelho
		-e "s/%%PKG_REPO_BRANCH_DEVEL%%/${_pkg_repo_branch_devel}/g" \
1032
		-e "s/%%PKG_REPO_BRANCH_RELEASE%%/${_pkg_repo_branch_release}/g" \
1033
		-e "s,%%PKG_REPO_SERVER_DEVEL%%,${_pkg_repo_server_devel},g" \
1034
		-e "s,%%PKG_REPO_SERVER_RELEASE%%,${_pkg_repo_server_release},g" \
1035 f8a36c30 Renato Botelho
		-e "s,%%POUDRIERE_PORTS_NAME%%,${POUDRIERE_PORTS_NAME},g" \
1036 6f73c362 Renato Botelho
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
1037 743cc0cc Renato Botelho
		-e "s/%%REPO_BRANCH_PREFIX%%/${REPO_BRANCH_PREFIX}/g" \
1038 52efc840 Renato Botelho
		${_template} \
1039 6f73c362 Renato Botelho
		> ${_target}
1040 a76b4810 Renato Botelho
1041 dff2bf9c Renato Botelho
	local ALTABI_ARCH=$(get_altabi_arch ${_target_arch})
1042 a76b4810 Renato Botelho
1043
	ABI=$(cat ${_template%%.conf}.abi 2>/dev/null \
1044
	    | sed -e "s/%%ARCH%%/${_target_arch}/g")
1045
	ALTABI=$(cat ${_template%%.conf}.altabi 2>/dev/null \
1046
	    | sed -e "s/%%ARCH%%/${ALTABI_ARCH}/g")
1047
1048
	if [ -n "${_pkg_conf}" -a -n "${ABI}" -a -n "${ALTABI}" ]; then
1049
		mkdir -p $(dirname ${_pkg_conf})
1050
		echo "ABI=${ABI}" > ${_pkg_conf}
1051
		echo "ALTABI=${ALTABI}" >> ${_pkg_conf}
1052
	fi
1053 6f73c362 Renato Botelho
}
1054
1055 1b40e1a6 Renato Botelho
depend_check() {
1056 ffeac248 Renato Botelho
	for _pkg in ${BUILDER_PKG_DEPENDENCIES}; do
1057 1b40e1a6 Renato Botelho
		if ! pkg info -e ${_pkg}; then
1058
			echo "Missing dependency (${_pkg})."
1059
			print_error_pfS
1060
		fi
1061
	done
1062
}
1063
1064 6f73c362 Renato Botelho
# This routine ensures any ports / binaries that the builder
1065
# system needs are on disk and ready for execution.
1066
builder_setup() {
1067
	# If Product-builder is already installed, just leave
1068
	if pkg info -e -q ${PRODUCT_NAME}-builder; then
1069
		return
1070
	fi
1071
1072 52efc840 Renato Botelho
	if [ ! -f ${PKG_REPO_PATH} ]; then
1073
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
1074
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
1075 6f73c362 Renato Botelho
1076 d2f163a6 Renato Botelho
		update_freebsd_sources
1077
1078 6f73c362 Renato Botelho
		local _arch=$(uname -m)
1079 0c096dad Renato Botelho
		setup_pkg_repo \
1080 dbd615c4 Renato Botelho
			${PKG_REPO_BUILD} \
1081 52efc840 Renato Botelho
			${PKG_REPO_PATH} \
1082 0c096dad Renato Botelho
			${_arch} \
1083 67f2556a Renato Botelho
			${_arch} \
1084
			"staging"
1085 569929ef Renato Botelho
1086
		# Use fingerprint keys from repo
1087
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
1088
			${PKG_REPO_PATH}
1089 6f73c362 Renato Botelho
	fi
1090
1091
	pkg install ${PRODUCT_NAME}-builder
1092
}
1093
1094
# Updates FreeBSD sources
1095
update_freebsd_sources() {
1096
	if [ "${1}" = "full" ]; then
1097
		local _full=1
1098
		local _clone_params=""
1099
	else
1100
		local _full=0
1101
		local _clone_params="--depth 1 --single-branch"
1102
	fi
1103
1104 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
1105 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
1106
		return
1107
	fi
1108
1109 a10a0e70 Renato Botelho
	echo ">>> Obtaining FreeBSD sources (${FREEBSD_BRANCH})..."
1110 cdf2b5f8 Renato Botelho
	${BUILDER_SCRIPTS}/git_checkout.sh \
1111 a10a0e70 Renato Botelho
		-r ${FREEBSD_REPO_BASE} \
1112
		-d ${FREEBSD_SRC_DIR} \
1113
		-b ${FREEBSD_BRANCH}
1114 6f73c362 Renato Botelho
1115 a10a0e70 Renato Botelho
	if [ $? -ne 0 -o ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1116 6f73c362 Renato Botelho
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1117
		print_error_pfS
1118
	fi
1119
1120
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1121 a10a0e70 Renato Botelho
		echo -n ">>> Checking out desired commit (${GIT_FREEBSD_COSHA1})... "
1122
		( git -C  ${FREEBSD_SRC_DIR} checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | \
1123
			grep -C3 -i -E 'error|fatal'
1124
		echo "Done!"
1125 6f73c362 Renato Botelho
	fi
1126 1af9fab9 Renato Botelho
1127
	if [ "${PRODUCT_NAME}" = "pfSense" -a -n "${GNID_REPO_BASE}" ]; then
1128
		echo ">>> Obtaining gnid sources..."
1129
		${BUILDER_SCRIPTS}/git_checkout.sh \
1130
			-r ${GNID_REPO_BASE} \
1131
			-d ${GNID_SRC_DIR} \
1132
			-b ${GNID_BRANCH}
1133
	fi
1134 6f73c362 Renato Botelho
}
1135
1136
pkg_chroot() {
1137
	local _root="${1}"
1138
	shift
1139
1140
	if [ $# -eq 0 ]; then
1141
		return -1
1142
	fi
1143
1144
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1145
		return -1
1146
	fi
1147
1148
	mkdir -p \
1149
		${SCRATCHDIR}/pkg_cache \
1150
		${_root}/var/cache/pkg \
1151
		${_root}/dev
1152
1153
	/sbin/mount -t nullfs ${SCRATCHDIR}/pkg_cache ${_root}/var/cache/pkg
1154
	/sbin/mount -t devfs devfs ${_root}/dev
1155
	cp -f /etc/resolv.conf ${_root}/etc/resolv.conf
1156
	touch ${BUILDER_LOGS}/install_pkg_install_ports.txt
1157 3e80d64e Renato Botelho
	local _params=""
1158 a76b4810 Renato Botelho
	if [ -f "${_root}/tmp/pkg/pkg-repos/repo.conf" ]; then
1159
		_params="--repo-conf-dir /tmp/pkg/pkg-repos "
1160
	fi
1161
	if [ -f "${_root}/tmp/pkg/pkg.conf" ]; then
1162
		_params="${_params} --config /tmp/pkg/pkg.conf "
1163 3e80d64e Renato Botelho
	fi
1164
	script -aq ${BUILDER_LOGS}/install_pkg_install_ports.txt \
1165 5f7c49a4 Renato Botelho
		chroot ${_root} pkg ${_params}$@ >/dev/null 2>&1
1166 10f26956 Renato Botelho
	local result=$?
1167 6f73c362 Renato Botelho
	rm -f ${_root}/etc/resolv.conf
1168 5084361d Renato Botelho
	/sbin/umount -f ${_root}/dev
1169
	/sbin/umount -f ${_root}/var/cache/pkg
1170 10f26956 Renato Botelho
1171
	return $result
1172 6f73c362 Renato Botelho
}
1173
1174
1175
pkg_chroot_add() {
1176
	if [ -z "${1}" -o -z "${2}" ]; then
1177
		return 1
1178
	fi
1179
1180
	local _target="${1}"
1181
	local _pkg="$(get_pkg_name ${2}).txz"
1182
1183
	if [ ! -d "${_target}" ]; then
1184
		echo ">>> ERROR: Target dir ${_target} not found"
1185
		print_error_pfS
1186
	fi
1187
1188 91039db4 Renato Botelho
	if [ ! -f ${CORE_PKG_ALL_PATH}/${_pkg} ]; then
1189 6f73c362 Renato Botelho
		echo ">>> ERROR: Package ${_pkg} not found"
1190
		print_error_pfS
1191
	fi
1192
1193 91039db4 Renato Botelho
	cp ${CORE_PKG_ALL_PATH}/${_pkg} ${_target}
1194 6f73c362 Renato Botelho
	pkg_chroot ${_target} add /${_pkg}
1195
	rm -f ${_target}/${_pkg}
1196
}
1197
1198
pkg_bootstrap() {
1199
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1200
1201 0ada33a9 Renato Botelho
	setup_pkg_repo \
1202 dbd615c4 Renato Botelho
		${PKG_REPO_BUILD} \
1203 52efc840 Renato Botelho
		${_root}${PKG_REPO_PATH} \
1204 0ada33a9 Renato Botelho
		${TARGET} \
1205
		${TARGET_ARCH} \
1206 74a4eefb Renato Botelho
		"staging"
1207 6f73c362 Renato Botelho
1208
	pkg_chroot ${_root} bootstrap -f
1209
}
1210
1211
# This routine assists with installing various
1212 97bc6c78 Phil Davis
# freebsd ports files into the pfsense-fs staging
1213 6f73c362 Renato Botelho
# area.
1214
install_pkg_install_ports() {
1215
	local MAIN_PKG="${1}"
1216
1217
	if [ -z "${MAIN_PKG}" ]; then
1218
		MAIN_PKG=${PRODUCT_NAME}
1219
	fi
1220
1221
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1222
1223
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1224
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1225
1226
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1227
		mkdir -p ${SCRATCHDIR}/pkg_cache
1228
1229 10f26956 Renato Botelho
	echo -n ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... "
1230 cdac78f6 Renato Botelho
	# First mark all packages as automatically installed
1231
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 1 -a
1232
	# Install all necessary packages
1233 10f26956 Renato Botelho
	if ! pkg_chroot ${STAGE_CHROOT_DIR} install ${MAIN_PKG} ${custom_package_list}; then
1234
		echo "Failed!"
1235
		print_error_pfS
1236
	fi
1237 cdac78f6 Renato Botelho
	# Make sure required packages are set as non-automatic
1238 4b0f15b6 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 0 pkg ${MAIN_PKG} ${custom_package_list}
1239 d071acf5 Renato Botelho
	# pkg and MAIN_PKG are vital
1240
	pkg_chroot ${STAGE_CHROOT_DIR} set -y -v 1 pkg ${MAIN_PKG}
1241 cdac78f6 Renato Botelho
	# Remove unnecessary packages
1242 6f73c362 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} autoremove
1243 10f26956 Renato Botelho
	echo "Done!"
1244 6f73c362 Renato Botelho
}
1245
1246
staginareas_clean_each_run() {
1247
	echo -n ">>> Cleaning build directories: "
1248
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1249
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1250
		echo -n "$BASENAME "
1251
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1252 f546e6ca Renato Botelho
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1253 6f73c362 Renato Botelho
	fi
1254
	echo "Done!"
1255
}
1256
1257
# Imported from FreeSBIE
1258
buildkernel() {
1259 f41f9c0c Renato Botelho
	local _kernconf=${1:-${KERNCONF}}
1260
1261 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDKERNEL}" ]; then
1262 6f73c362 Renato Botelho
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1263
		return
1264
	fi
1265
1266 f41f9c0c Renato Botelho
	if [ -z "${_kernconf}" ]; then
1267 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1268
		print_error_pfS
1269
	fi
1270
1271 f41f9c0c Renato Botelho
	local _old_kernconf=${KERNCONF}
1272
	export KERNCONF=${_kernconf}
1273 6f73c362 Renato Botelho
1274 29cdd776 Renato Botelho
	echo ">>> $(LC_ALL=C date) - Starting build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1275 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -W -s ${FREEBSD_SRC_DIR} \
1276 29cdd776 Renato Botelho
		|| print_error_pfS
1277
	echo ">>> $(LC_ALL=C date) - Finished build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1278 f41f9c0c Renato Botelho
1279
	export KERNCONF=${_old_kernconf}
1280 6f73c362 Renato Botelho
}
1281
1282
# Imported from FreeSBIE
1283
installkernel() {
1284 7d460897 Renato Botelho
	local _destdir=${1:-${KERNEL_DESTDIR}}
1285 f41f9c0c Renato Botelho
	local _kernconf=${2:-${KERNCONF}}
1286 7d460897 Renato Botelho
1287 f41f9c0c Renato Botelho
	if [ -z "${_kernconf}" ]; then
1288 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1289
		print_error_pfS
1290
	fi
1291
1292 f41f9c0c Renato Botelho
	local _old_kernconf=${KERNCONF}
1293
	export KERNCONF=${_kernconf}
1294 6f73c362 Renato Botelho
1295
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1296 f41f9c0c Renato Botelho
	echo ">>> Installing kernel (${_kernconf}) for ${TARGET} architecture..." | tee -a ${LOGFILE}
1297 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -W -D -z \
1298 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
1299
		-d ${_destdir} \
1300
		|| print_error_pfS
1301 f41f9c0c Renato Botelho
1302
	export KERNCONF=${_old_kernconf}
1303 6f73c362 Renato Botelho
}
1304
1305
# Launch is ran first to setup a few variables that we need
1306
# Imported from FreeSBIE
1307
launch() {
1308
	if [ "$(id -u)" != "0" ]; then
1309
		echo "Sorry, this must be done as root."
1310
	fi
1311
1312
	echo ">>> Operation $0 has started at $(date)"
1313
}
1314
1315
finish() {
1316
	echo ">>> Operation $0 has ended at $(date)"
1317
}
1318
1319 184b39e1 Renato Botelho
pkg_repo_rsync() {
1320 15333972 Renato Botelho
	local _repo_path_param="${1}"
1321 92a1044d Renato Botelho
	local _ignore_final_rsync="${2}"
1322 5c0d30fb Renato Botelho do Couto
	local _aws_sync_cmd="aws s3 sync --quiet --exclude '.real*/*' --exclude '.latest/*'"
1323 184b39e1 Renato Botelho
1324 15333972 Renato Botelho
	if [ -z "${_repo_path_param}" -o ! -d "${_repo_path_param}" ]; then
1325 184b39e1 Renato Botelho
		return
1326
	fi
1327
1328 92a1044d Renato Botelho
	if [ -n "${SKIP_FINAL_RSYNC}" ]; then
1329
		_ignore_final_rsync="1"
1330
	fi
1331
1332 61b63ac0 Renato Botelho
	# Sanitize path
1333 15333972 Renato Botelho
	_repo_path=$(realpath ${_repo_path_param})
1334 61b63ac0 Renato Botelho
1335
	local _repo_dir=$(dirname ${_repo_path})
1336
	local _repo_base=$(basename ${_repo_path})
1337
1338
	# Add ./ it's an rsync trick to make it chdir to directory before sending it
1339
	_repo_path="${_repo_dir}/./${_repo_base}"
1340
1341 184b39e1 Renato Botelho
	if [ -z "${LOGFILE}" ]; then
1342
		local _logfile="/dev/null"
1343
	else
1344
		local _logfile="${LOGFILE}"
1345
	fi
1346
1347 94f7df8f Renato Botelho
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" -a -z "${DO_NOT_SIGN_PKG_REPO}" ]; then
1348 efcf5e2a Renato Botelho
		# Detect poudriere directory structure
1349
		if [ -L "${_repo_path}/.latest" ]; then
1350
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1351
		else
1352
			local _real_repo_path=${_repo_path}
1353
		fi
1354
1355 f4575189 Renato Botelho
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1356 efcf5e2a Renato Botelho
		############ ATTENTION ##############
1357
		#
1358
		# For some reason pkg-repo fail without / in the end of directory name
1359
		# so removing it will break command
1360
		#
1361 c2af1487 Renato Botelho
		# https://github.com/freebsd/pkg/issues/1364
1362
		#
1363 efcf5e2a Renato Botelho
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1364 f4575189 Renato Botelho
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1365
			echo "Done!" | tee -a ${_logfile}
1366
		else
1367
			echo "Failed!" | tee -a ${_logfile}
1368
			echo ">>> ERROR: An error occurred trying to sign repo"
1369
			print_error_pfS
1370
		fi
1371
1372
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1373
		if [ -e ${_pkgfile} ]; then
1374
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1375
1376
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1377
			    > ${_pkgfile}.sig 2>/dev/null; then
1378
				echo "Done!" | tee -a ${_logfile}
1379
			else
1380
				echo "Failed!" | tee -a ${_logfile}
1381
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1382
				print_error_pfS
1383
			fi
1384
		fi
1385
	fi
1386
1387 f6c83d05 Renato Botelho
	if [ -z "${UPLOAD}" ]; then
1388 f4575189 Renato Botelho
		return
1389
	fi
1390
1391 5163c3ca Renato Botelho
	for _pkg_rsync_hostname in ${PKG_RSYNC_HOSTNAME}; do
1392
		# Make sure destination directory exist
1393
		ssh -p ${PKG_RSYNC_SSH_PORT} \
1394
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} \
1395
			"mkdir -p ${PKG_RSYNC_DESTDIR}"
1396
1397
		echo -n ">>> Sending updated repository to ${_pkg_rsync_hostname}... " | tee -a ${_logfile}
1398
		if script -aq ${_logfile} rsync -Have "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1399
			--timeout=60 --delete-delay ${_repo_path} \
1400
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1401
		then
1402 2c056b08 Renato Botelho
			echo "Done!" | tee -a ${_logfile}
1403
		else
1404
			echo "Failed!" | tee -a ${_logfile}
1405 5163c3ca Renato Botelho
			echo ">>> ERROR: An error occurred sending repo to remote hostname"
1406 2c056b08 Renato Botelho
			print_error_pfS
1407
		fi
1408
1409 5163c3ca Renato Botelho
		if [ -z "${USE_PKG_REPO_STAGING}" -o -n "${_ignore_final_rsync}" ]; then
1410
			return
1411
		fi
1412 15333972 Renato Botelho
1413 5163c3ca Renato Botelho
		if [ -n "${_IS_RELEASE}" -o "${_repo_path_param}" = "${CORE_PKG_PATH}" ]; then
1414
			for _pkg_final_rsync_hostname in ${PKG_FINAL_RSYNC_HOSTNAME}; do
1415
				# Send .real* directories first to prevent having a broken repo while transfer happens
1416
				local _cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1417
					--timeout=60 ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1418
					--include=\"/*\" --include=\"*/.real*\" --include=\"*/.real*/***\" \
1419
					--exclude=\"*\" \
1420
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1421
1422
				echo -n ">>> Sending updated packages to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1423
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1424
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1425
					echo "Done!" | tee -a ${_logfile}
1426
				else
1427
					echo "Failed!" | tee -a ${_logfile}
1428
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1429
					print_error_pfS
1430
				fi
1431
1432
				_cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1433
					--timeout=60 --delete-delay ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1434
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1435
1436
				echo -n ">>> Sending updated repositories metadata to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1437
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1438
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1439
					echo "Done!" | tee -a ${_logfile}
1440
				else
1441
					echo "Failed!" | tee -a ${_logfile}
1442
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1443
					print_error_pfS
1444
				fi
1445 457573e2 Renato Botelho
1446 31c07400 Renato Botelho do Couto
				if [ -z "${PKG_FINAL_S3_PATH}" ]; then
1447 457573e2 Renato Botelho
					continue
1448
				fi
1449
1450 31c07400 Renato Botelho do Couto
				local _repos=$(ssh -p ${PKG_FINAL_RSYNC_SSH_PORT} \
1451
				    ${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname} \
1452
				    "ls -1d ${PKG_FINAL_RSYNC_DESTDIR}/${_repo_base%%-core}*")
1453
				for _repo in ${_repos}; do
1454
					echo -n ">>> Sending updated packages to AWS ${PKG_FINAL_S3_PATH}... " | tee -a ${_logfile}
1455
					if script -aq ${_logfile} ssh -p ${PKG_FINAL_RSYNC_SSH_PORT} \
1456
					    ${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname} \
1457 5c0d30fb Renato Botelho do Couto
					    "${_aws_sync_cmd} ${_repo} ${PKG_FINAL_S3_PATH}/$(basename ${_repo})"; then
1458
						echo "Done!" | tee -a ${_logfile}
1459
					else
1460
						echo "Failed!" | tee -a ${_logfile}
1461
						echo ">>> ERROR: An error occurred sending files to AWS S3"
1462
						print_error_pfS
1463
					fi
1464
					echo -n ">>> Cleaning up packages at AWS ${PKG_FINAL_S3_PATH}... " | tee -a ${_logfile}
1465
					if script -aq ${_logfile} ssh -p ${PKG_FINAL_RSYNC_SSH_PORT} \
1466
					    ${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname} \
1467
					    "${_aws_sync_cmd} --delete ${_repo} ${PKG_FINAL_S3_PATH}/$(basename ${_repo})"; then
1468 31c07400 Renato Botelho do Couto
						echo "Done!" | tee -a ${_logfile}
1469
					else
1470
						echo "Failed!" | tee -a ${_logfile}
1471
						echo ">>> ERROR: An error occurred sending files to AWS S3"
1472
						print_error_pfS
1473
					fi
1474
				done
1475 5163c3ca Renato Botelho
			done
1476 15333972 Renato Botelho
		fi
1477 5163c3ca Renato Botelho
	done
1478 184b39e1 Renato Botelho
}
1479
1480 6f73c362 Renato Botelho
poudriere_possible_archs() {
1481
	local _arch=$(uname -m)
1482 44b30d59 Renato Botelho
	local _archs=""
1483 6f73c362 Renato Botelho
1484 82b59cee Renato Botelho
	# If host is amd64, we'll create both repos, and if possible armv7
1485 6f73c362 Renato Botelho
	if [ "${_arch}" = "amd64" ]; then
1486 44b30d59 Renato Botelho
		_archs="amd64.amd64"
1487 6f73c362 Renato Botelho
1488
		if [ -f /usr/local/bin/qemu-arm-static ]; then
1489
			# Make sure binmiscctl is ok
1490
			/usr/local/etc/rc.d/qemu_user_static forcestart >/dev/null 2>&1
1491
1492 82b59cee Renato Botelho
			if binmiscctl lookup armv7 >/dev/null 2>&1; then
1493
				_archs="${_archs} arm.armv7"
1494 6f73c362 Renato Botelho
			fi
1495
		fi
1496
	fi
1497
1498 fb192227 Renato Botelho
	if [ -n "${ARCH_LIST}" ]; then
1499
		local _found=0
1500 4010811b Renato Botelho
		for _desired_arch in ${ARCH_LIST}; do
1501 fb192227 Renato Botelho
			_found=0
1502 4010811b Renato Botelho
			for _possible_arch in ${_archs}; do
1503 fb192227 Renato Botelho
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1504
					_found=1
1505
					break
1506
				fi
1507
			done
1508
			if [ ${_found} -eq 0 ]; then
1509
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1510
				print_error_pfS
1511
			fi
1512
		done
1513
		_archs="${ARCH_LIST}"
1514
	fi
1515
1516 6f73c362 Renato Botelho
	echo ${_archs}
1517
}
1518
1519
poudriere_jail_name() {
1520
	local _jail_arch="${1}"
1521
1522
	if [ -z "${_jail_arch}" ]; then
1523
		return 1
1524
	fi
1525
1526 0449a11f Renato Botelho
	# Remove arch
1527 83354dea Renato Botelho
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1528 6f73c362 Renato Botelho
}
1529
1530 a3bb036b Renato Botelho
poudriere_rename_ports() {
1531
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1532
		return;
1533
	fi
1534
1535
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1536
1537
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1538
1539
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1540
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1541
		local _pdir=$(dirname ${d})
1542
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1543 c7e1c621 Renato Botelho
		local _plist=""
1544 a3bb036b Renato Botelho
1545
		if [ -e ${_pdir}/${_pname} ]; then
1546
			rm -rf ${_pdir}/${_pname}
1547
		fi
1548
1549
		cp -r ${d} ${_pdir}/${_pname}
1550
1551 c7e1c621 Renato Botelho
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1552
			_plist=${_pdir}/${_pname}/pkg-plist
1553
		fi
1554
1555 a3bb036b Renato Botelho
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1556
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1557
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1558
			${_pdir}/${_pname}/Makefile \
1559 c7e1c621 Renato Botelho
			${_pdir}/${_pname}/pkg-descr ${_plist}
1560 a3bb036b Renato Botelho
1561
		# PHP module is special
1562
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1563
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1564
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1565
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1566
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1567
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1568
				${_pdir}/${_pname}/files/config.m4
1569
1570
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1571
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1572
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1573
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1574
				${_pdir}/${_pname}/files/pfSense.c \
1575
				${_pdir}/${_pname}/files/php_pfSense.h
1576
		fi
1577
1578
		if [ -d ${_pdir}/${_pname}/files ]; then
1579
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1580
				local _fddir=$(dirname ${fd})
1581
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1582
1583
				mv ${fd} ${_fddir}/${_fdname}
1584
			done
1585
		fi
1586
	done
1587
	echo "Done!" | tee -a ${LOGFILE}
1588
}
1589
1590 6f73c362 Renato Botelho
poudriere_create_ports_tree() {
1591
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1592
1593
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1594
		local _branch=""
1595
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1596
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1597
			print_error_pfS
1598
		fi
1599
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1600
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1601
		fi
1602
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1603 ac464524 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git -U ${POUDRIERE_PORTS_GIT_URL} ${_branch} >/dev/null 2>&1; then
1604 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1605
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1606
			print_error_pfS
1607
		fi
1608
		echo "Done!" | tee -a ${LOGFILE}
1609 a3bb036b Renato Botelho
		poudriere_rename_ports
1610 6f73c362 Renato Botelho
	fi
1611
}
1612
1613
poudriere_init() {
1614
	local _error=0
1615
	local _archs=$(poudriere_possible_archs)
1616
1617
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1618
1619
	# Sanity checks
1620
	if [ -z "${ZFS_TANK}" ]; then
1621
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1622
		error=1
1623
	fi
1624
1625
	if [ -z "${ZFS_ROOT}" ]; then
1626
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1627
		error=1
1628
	fi
1629
1630
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1631
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1632
		error=1
1633
	fi
1634
1635
	if [ ${_error} -eq 1 ]; then
1636
		print_error_pfS
1637
	fi
1638
1639
	# Check if zpool exists
1640
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
1641
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
1642
		print_error_pfS
1643
	fi
1644
1645
	# Check if zfs rootfs exists
1646
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1647 01541076 Renato Botelho
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
1648 52ec3f66 Renato Botelho
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
1649
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1650 01541076 Renato Botelho
			echo "Done!"
1651
		else
1652
			echo "Failed!"
1653
			print_error_pfS
1654
		fi
1655 6f73c362 Renato Botelho
	fi
1656
1657
	# Make sure poudriere is installed
1658 6743ed95 Renato Botelho
	if [ ! -f /usr/local/bin/poudriere ]; then
1659
		echo ">>> Installing poudriere..." | tee -a ${LOGFILE}
1660
		if ! pkg install poudriere >/dev/null 2>&1; then
1661
			echo ">>> ERROR: poudriere was not installed, aborting..." | tee -a ${LOGFILE}
1662 6f73c362 Renato Botelho
			print_error_pfS
1663
		fi
1664
	fi
1665
1666
	# Create poudriere.conf
1667
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1668
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1669
		print_error_pfS
1670
	fi
1671 0e491fe2 Renato Botelho do Couto
1672
	# PARALLEL_JOBS us ncpu / 4 for best performance
1673
	local _parallel_jobs=$(sysctl -qn hw.ncpu)
1674
	_parallel_jobs=$((_parallel_jobs / 4))
1675
1676 6f73c362 Renato Botelho
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
1677
	cat <<EOF >/usr/local/etc/poudriere.conf
1678
ZPOOL=${ZFS_TANK}
1679
ZROOTFS=${ZFS_ROOT}
1680
RESOLV_CONF=/etc/resolv.conf
1681
BASEFS=/usr/local/poudriere
1682
USE_PORTLINT=no
1683
USE_TMPFS=yes
1684
NOLINUX=yes
1685 382c5ba6 Renato Botelho
DISTFILES_CACHE=/usr/ports/distfiles
1686 6f73c362 Renato Botelho
CHECK_CHANGED_OPTIONS=yes
1687
CHECK_CHANGED_DEPS=yes
1688
ATOMIC_PACKAGE_REPOSITORY=yes
1689
COMMIT_PACKAGES_ON_FAILURE=no
1690 4fd3649f Renato Botelho
KEEP_OLD_PACKAGES=yes
1691
KEEP_OLD_PACKAGES_COUNT=5
1692 2d0b7245 Renato Botelho
ALLOW_MAKE_JOBS=yes
1693 0e491fe2 Renato Botelho do Couto
PARALLEL_JOBS=${_parallel_jobs}
1694 5f64e2ca Renato Botelho
EOF
1695
1696 fe31dc35 Renato Botelho
	if pkg info -e ccache; then
1697
	cat <<EOF >>/usr/local/etc/poudriere.conf
1698
CCACHE_DIR=/var/cache/ccache
1699
EOF
1700
	fi
1701
1702 5f64e2ca Renato Botelho
	# Create specific items conf
1703
	[ ! -d /usr/local/etc/poudriere.d ] \
1704
		&& mkdir -p /usr/local/etc/poudriere.d
1705
1706 382c5ba6 Renato Botelho
	# Create DISTFILES_CACHE if it doesn't exist
1707
	if [ ! -d /usr/ports/distfiles ]; then
1708
		mkdir -p /usr/ports/distfiles
1709
	fi
1710
1711 6f73c362 Renato Botelho
	# Remove old jails
1712
	for jail_arch in ${_archs}; do
1713
		jail_name=$(poudriere_jail_name ${jail_arch})
1714
1715
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1716
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
1717
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
1718
		fi
1719
	done
1720
1721
	# Remove old ports tree
1722
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1723
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
1724
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
1725
	fi
1726
1727
	local native_xtools=""
1728
	# Now we are ready to create jails
1729
	for jail_arch in ${_archs}; do
1730
		jail_name=$(poudriere_jail_name ${jail_arch})
1731
1732 82b59cee Renato Botelho
		if [ "${jail_arch}" = "arm.armv7" ]; then
1733 6f73c362 Renato Botelho
			native_xtools="-x"
1734
		else
1735
			native_xtools=""
1736
		fi
1737
1738
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1739 ac464524 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
1740
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
1741 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1742
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1743
			print_error_pfS
1744
		fi
1745
		echo "Done!" | tee -a ${LOGFILE}
1746
	done
1747
1748
	poudriere_create_ports_tree
1749
1750
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
1751
}
1752
1753
poudriere_update_jails() {
1754
	local _archs=$(poudriere_possible_archs)
1755
1756
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1757
1758
	local native_xtools=""
1759
	for jail_arch in ${_archs}; do
1760
		jail_name=$(poudriere_jail_name ${jail_arch})
1761
1762 c136dde1 Renato Botelho
		local _create_or_update="-u"
1763 43f6b081 Renato Botelho
		local _create_or_update_text="Updating"
1764 6f73c362 Renato Botelho
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1765 c136dde1 Renato Botelho
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
1766 ac464524 Renato Botelho
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
1767 43f6b081 Renato Botelho
			_create_or_update_text="Creating"
1768 6f73c362 Renato Botelho
		fi
1769
1770 82b59cee Renato Botelho
		if [ "${jail_arch}" = "arm.armv7" ]; then
1771 6f73c362 Renato Botelho
			native_xtools="-x"
1772
		else
1773
			native_xtools=""
1774
		fi
1775
1776 43f6b081 Renato Botelho
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1777 ac464524 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
1778 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1779 43f6b081 Renato Botelho
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1780 6f73c362 Renato Botelho
			print_error_pfS
1781
		fi
1782
		echo "Done!" | tee -a ${LOGFILE}
1783
	done
1784
}
1785
1786
poudriere_update_ports() {
1787
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1788
1789
	# Create ports tree if necessary
1790
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1791
		poudriere_create_ports_tree
1792
	else
1793 97bc6c78 Phil Davis
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1794 d94da298 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
1795 7d26baf3 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
1796 d94da298 Renato Botelho
		echo "Done!" | tee -a ${LOGFILE}
1797 6f73c362 Renato Botelho
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1798
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
1799
		echo "Done!" | tee -a ${LOGFILE}
1800 a3bb036b Renato Botelho
		poudriere_rename_ports
1801 6f73c362 Renato Botelho
	fi
1802
}
1803
1804
poudriere_bulk() {
1805
	local _archs=$(poudriere_possible_archs)
1806 61de46d7 Renato Botelho
	local _makeconf
1807 6f73c362 Renato Botelho
1808
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1809
1810 f6c83d05 Renato Botelho
	if [ -n "${UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
1811 e295b113 Renato Botelho
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
1812
		print_error_pfS
1813
	fi
1814
1815 26f1087f Renato Botelho
	rm -f ${LOGFILE}
1816
1817 6f73c362 Renato Botelho
	poudriere_create_ports_tree
1818
1819
	[ -d /usr/local/etc/poudriere.d ] || \
1820
		mkdir -p /usr/local/etc/poudriere.d
1821
1822 61de46d7 Renato Botelho
	_makeconf=/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1823 edef37f6 Renato Botelho
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
1824 61de46d7 Renato Botelho
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ${_makeconf}
1825 6f73c362 Renato Botelho
	fi
1826
1827 39f2cfd1 Renato Botelho
	cat <<EOF >>/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1828 3209e782 Renato Botelho do Couto
1829 39f2cfd1 Renato Botelho
PKG_REPO_BRANCH_DEVEL=${PKG_REPO_BRANCH_DEVEL}
1830
PKG_REPO_BRANCH_RELEASE=${PKG_REPO_BRANCH_RELEASE}
1831
PKG_REPO_SERVER_DEVEL=${PKG_REPO_SERVER_DEVEL}
1832
PKG_REPO_SERVER_RELEASE=${PKG_REPO_SERVER_RELEASE}
1833
POUDRIERE_PORTS_NAME=${POUDRIERE_PORTS_NAME}
1834 15f12d8e Renato Botelho
PFSENSE_DEFAULT_REPO=${PFSENSE_DEFAULT_REPO}
1835 39f2cfd1 Renato Botelho
PRODUCT_NAME=${PRODUCT_NAME}
1836 c1f18417 Renato Botelho
REPO_BRANCH_PREFIX=${REPO_BRANCH_PREFIX}
1837 39f2cfd1 Renato Botelho
EOF
1838
1839 61de46d7 Renato Botelho
	local _value=""
1840 f7961bd1 Renato Botelho
	for jail_arch in ${_archs}; do
1841 f2bd5e35 Renato Botelho
		eval "_value=\${PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}}"
1842 61de46d7 Renato Botelho
		if [ -n "${_value}" ]; then
1843 f2bd5e35 Renato Botelho
			echo "PKG_REPO_BRANCH_DEVEL_${jail_arch##*.}=${_value}" \
1844 61de46d7 Renato Botelho
				>> ${_makeconf}
1845 f7961bd1 Renato Botelho
		fi
1846 f2bd5e35 Renato Botelho
		eval "_value=\${PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}}"
1847 61de46d7 Renato Botelho
		if [ -n "${_value}" ]; then
1848 f2bd5e35 Renato Botelho
			echo "PKG_REPO_BRANCH_RELEASE_${jail_arch##*.}=${_value}" \
1849 61de46d7 Renato Botelho
				>> ${_makeconf}
1850 f7961bd1 Renato Botelho
		fi
1851 f2bd5e35 Renato Botelho
		eval "_value=\${PKG_REPO_SERVER_DEVEL_${jail_arch##*.}}"
1852 61de46d7 Renato Botelho
		if [ -n "${_value}" ]; then
1853 f2bd5e35 Renato Botelho
			echo "PKG_REPO_SERVER_DEVEL_${jail_arch##*.}=${_value}" \
1854 61de46d7 Renato Botelho
				>> ${_makeconf}
1855 f7961bd1 Renato Botelho
		fi
1856 f2bd5e35 Renato Botelho
		eval "_value=\${PKG_REPO_SERVER_RELEASE_${jail_arch##*.}}"
1857 61de46d7 Renato Botelho
		if [ -n "${_value}" ]; then
1858 f2bd5e35 Renato Botelho
			echo "PKG_REPO_SERVER_RELEASE_${jail_arch##*.}=${_value}" \
1859 61de46d7 Renato Botelho
				>> ${_makeconf}
1860 f7961bd1 Renato Botelho
		fi
1861
	done
1862
1863 d94da298 Renato Botelho
	# Change version of pfSense meta ports for snapshots
1864
	if [ -z "${_IS_RELEASE}" ]; then
1865 b00400ef Renato Botelho
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
1866 02ec5bd3 Renato Botelho
		sed -i '' \
1867
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
1868
			-e "/^PORTREVISION=/d" \
1869 39f2cfd1 Renato Botelho
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile \
1870
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/Makefile
1871 d94da298 Renato Botelho
	fi
1872
1873 39f2cfd1 Renato Botelho
	# Copy over pkg repo templates to pfSense-repo
1874
	mkdir -p /usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1875 a546d014 Renato Botelho
	cp -f ${PKG_REPO_BASE}/* \
1876 39f2cfd1 Renato Botelho
		/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1877
1878 6f73c362 Renato Botelho
	for jail_arch in ${_archs}; do
1879
		jail_name=$(poudriere_jail_name ${jail_arch})
1880
1881
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1882
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
1883
			continue
1884
		fi
1885
1886 deb5f1da Luiz Souza
		_ref_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.ref.${jail_arch}
1887
		rm -rf ${_ref_bulk} ${_ref_bulk}.tmp
1888
		touch ${_ref_bulk}.tmp
1889 bd936941 Luiz Souza
		if [ -f "${POUDRIERE_BULK}.${jail_arch#*.}" ]; then
1890
			cat "${POUDRIERE_BULK}.${jail_arch#*.}" >> ${_ref_bulk}.tmp
1891 deb5f1da Luiz Souza
		fi
1892
		if [ -f "${POUDRIERE_BULK}" ]; then
1893
			cat "${POUDRIERE_BULK}" >> ${_ref_bulk}.tmp
1894 6f73c362 Renato Botelho
		fi
1895 deb5f1da Luiz Souza
		cat ${_ref_bulk}.tmp | sort -u > ${_ref_bulk}
1896 6f73c362 Renato Botelho
1897 deb5f1da Luiz Souza
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}.${jail_arch}
1898 6f73c362 Renato Botelho
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
1899
1900 f72d162f Renato Botelho
		local _exclude_bulk="${POUDRIERE_BULK}.exclude.${jail_arch}"
1901
		if [ -f "${_exclude_bulk}" ]; then
1902
			mv ${_bulk} ${_bulk}.tmp
1903
			sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_exclude_bulk} > ${_bulk}.exclude
1904 855d9774 Renato Botelho
			cat ${_bulk}.tmp ${_bulk}.exclude | sort | uniq -u > ${_bulk}
1905
			rm -f ${_bulk}.tmp ${_bulk}.exclude
1906 f72d162f Renato Botelho
		fi
1907
1908 6f73c362 Renato Botelho
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
1909
			echo ">>> ERROR: Something went wrong..."
1910
			print_error_pfS
1911
		fi
1912
1913
		echo ">>> Cleaning up old packages from repo..."
1914
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
1915
			echo ">>> ERROR: Something went wrong..."
1916
			print_error_pfS
1917
		fi
1918
1919 61b63ac0 Renato Botelho
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
1920 6f73c362 Renato Botelho
	done
1921
}
1922 ab943fc9 Renato Botelho
1923
# This routine is called to write out to stdout
1924
# a string. The string is appended to $SNAPSHOTSLOGFILE
1925
snapshots_update_status() {
1926 919c8486 Renato Botelho
	if [ -z "$1" ]; then
1927
		return
1928
	fi
1929
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
1930 ab943fc9 Renato Botelho
		return
1931
	fi
1932 0cc93b74 Renato Botelho
	echo "$*"
1933
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
1934 ab943fc9 Renato Botelho
}
1935
1936 97f7a154 Renato Botelho
create_sha256() {
1937
	local _file="${1}"
1938
1939
	if [ ! -f "${_file}" ]; then
1940
		return 1
1941
	fi
1942
1943
	( \
1944
		cd $(dirname ${_file}) && \
1945
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
1946
	)
1947
}
1948
1949 e28305f4 Renato Botelho
snapshots_create_latest_symlink() {
1950
	local _image="${1}"
1951
1952
	if [ -z "${_image}" ]; then
1953
		return
1954
	fi
1955
1956
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
1957
		return
1958
	fi
1959
1960 5a944dd5 Renato Botelho
	if [ ! -f "${_image}" ]; then
1961 a4a336b2 Renato Botelho
		return
1962 e28305f4 Renato Botelho
	fi
1963
1964 695ba439 Renato Botelho
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
1965
	ln -sf $(basename ${_image}) ${_symlink}
1966 e28305f4 Renato Botelho
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
1967
}
1968
1969 f26731b0 Renato Botelho
snapshots_create_sha256() {
1970 87925fd1 Renato Botelho
	local _img=""
1971 ab943fc9 Renato Botelho
1972 b0d0498c Renato Botelho
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
1973 f26731b0 Renato Botelho
		if [ -f "${_img}.gz" ]; then
1974
			_img="${_img}.gz"
1975
		fi
1976
		if [ ! -f "${_img}" ]; then
1977 87925fd1 Renato Botelho
			continue
1978
		fi
1979 97f7a154 Renato Botelho
		create_sha256 ${_img}
1980 f26731b0 Renato Botelho
		snapshots_create_latest_symlink ${_img}
1981 87925fd1 Renato Botelho
	done
1982 ab943fc9 Renato Botelho
}
1983
1984
snapshots_scp_files() {
1985 d4c6029e Renato Botelho
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
1986 820b17e1 Renato Botelho
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60"
1987 ab943fc9 Renato Botelho
	fi
1988 48396120 Renato Botelho
1989
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
1990 61b63ac0 Renato Botelho
	pkg_repo_rsync "${CORE_PKG_PATH}"
1991 48396120 Renato Botelho
	snapshots_update_status ">>> Finished copying core pkg repo"
1992
1993 5163c3ca Renato Botelho
	for _rsyncip in ${RSYNCIP}; do
1994
		snapshots_update_status ">>> Copying files to ${_rsyncip}"
1995 ab943fc9 Renato Botelho
1996 5163c3ca Renato Botelho
		# Ensure directory(s) are available
1997
		ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/installer"
1998
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1999
			ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/virtualization"
2000
		fi
2001
		# ensure permissions are correct for r+w
2002
		ssh ${RSYNCUSER}@${_rsyncip} "chmod -R ug+rw ${RSYNCPATH}/."
2003
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
2004
			${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/installer/
2005
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
2006
			rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
2007
				${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/virtualization/
2008
		fi
2009 ab943fc9 Renato Botelho
2010 5163c3ca Renato Botelho
		snapshots_update_status ">>> Finished copying files."
2011
	done
2012 ab943fc9 Renato Botelho
}