Project

General

Profile

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