Project

General

Profile

Download (55.2 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 81299b5c Renato Botelho
# Copyright (c) 2004-2016 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 a9ae8dac Renato Botelho
	local _filter="${5}"
81 6f73c362 Renato Botelho
82 1217cd7a Renato Botelho
	local _template_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
83
84 cdf2b5f8 Renato Botelho
	${BUILDER_SCRIPTS}/create_core_pkg.sh \
85 1217cd7a Renato Botelho
		-t "${_template_path}" \
86
		-f "${_flavor}" \
87
		-v "${_version}" \
88
		-r "${_root}" \
89
		-F "${_filter}" \
90
		-d "${CORE_PKG_REAL_PATH}/All" \
91
		|| print_error_pfS
92 6f73c362 Renato Botelho
}
93
94
# This routine will output that something went wrong
95
print_error_pfS() {
96
	echo
97
	echo "####################################"
98
	echo "Something went wrong, check errors!" >&2
99
	echo "####################################"
100
	echo
101
	echo "NOTE: a lot of times you can run './build.sh --clean-builder' to resolve."
102
	echo
103 d4c6029e Renato Botelho
	[ -n "${LOGFILE}" -a -f "${LOGFILE}" ] && \
104 6f73c362 Renato Botelho
		echo "Log saved on ${LOGFILE}" && \
105
	echo
106
	kill $$
107
	exit 1
108
}
109
110
# This routine will verify that the kernel has been
111
# installed OK to the staging area.
112
ensure_kernel_exists() {
113
	if [ ! -f "$1/boot/kernel/kernel.gz" ]; then
114
		echo ">>> ERROR: Could not locate $1/boot/kernel.gz"
115
		print_error_pfS
116
	fi
117
	KERNEL_SIZE=$(stat -f "%z" $1/boot/kernel/kernel.gz)
118
	if [ "$KERNEL_SIZE" -lt 3500 ]; then
119
		echo ">>> ERROR: Kernel $1/boot/kernel.gz appears to be smaller than it should be: $KERNEL_SIZE"
120
		print_error_pfS
121
	fi
122
}
123
124
get_pkg_name() {
125
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
126
}
127
128
# This routine builds all related kernels
129
build_all_kernels() {
130
	# Set KERNEL_BUILD_PATH if it has not been set
131
	if [ -z "${KERNEL_BUILD_PATH}" ]; then
132
		KERNEL_BUILD_PATH=$SCRATCHDIR/kernels
133
		echo ">>> KERNEL_BUILD_PATH has not been set. Setting to ${KERNEL_BUILD_PATH}!"
134
	fi
135
136
	[ -d "${KERNEL_BUILD_PATH}" ] \
137
		&& rm -rf ${KERNEL_BUILD_PATH}
138
139
	# Build embedded kernel
140
	for BUILD_KERNEL in $BUILD_KERNELS; do
141
		unset KERNCONF
142
		unset KERNEL_DESTDIR
143
		unset KERNEL_NAME
144
		export KERNCONF=$BUILD_KERNEL
145
		export KERNEL_DESTDIR="$KERNEL_BUILD_PATH/$BUILD_KERNEL"
146
		export KERNEL_NAME=${BUILD_KERNEL}
147
148
		LOGFILE="${BUILDER_LOGS}/kernel.${KERNCONF}.${TARGET}.log"
149
		echo ">>> Building $BUILD_KERNEL kernel."  | tee -a ${LOGFILE}
150
151 91039db4 Renato Botelho
		if [ -n "${NO_BUILDKERNEL}" -a -f "${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz" ]; then
152 6f73c362 Renato Botelho
			echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
153
			continue
154
		fi
155
156
		buildkernel
157
158
		echo ">>> Staging $BUILD_KERNEL kernel..." | tee -a ${LOGFILE}
159
		installkernel
160
161
		ensure_kernel_exists $KERNEL_DESTDIR
162
163 044cf5bd Renato Botelho
		echo ">>> Creating pkg of $KERNEL_NAME-debug kernel to staging area..."  | tee -a ${LOGFILE}
164 a9ae8dac Renato Botelho
		core_pkg_create kernel-debug ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} \*.symbols
165
		find ${KERNEL_DESTDIR} -name '*.symbols' -type f -delete
166
167 044cf5bd Renato Botelho
		echo ">>> Creating pkg of $KERNEL_NAME kernel to staging area..."  | tee -a ${LOGFILE}
168 6f73c362 Renato Botelho
		core_pkg_create kernel ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR}
169
170
		rm -rf $KERNEL_DESTDIR 2>&1 1>/dev/null
171
	done
172
}
173
174
install_default_kernel() {
175
	if [ -z "${1}" ]; then
176
		echo ">>> ERROR: install_default_kernel called without a kernel config name"| tee -a ${LOGFILE}
177
		print_error_pfS
178
	fi
179
180
	export KERNEL_NAME="${1}"
181
182
	echo -n ">>> Installing kernel to be used by image ${KERNEL_NAME}..." | tee -a ${LOGFILE}
183
184
	# Copy kernel package to chroot, otherwise pkg won't find it to install
185
	if ! pkg_chroot_add ${FINAL_CHROOT_DIR} kernel-${KERNEL_NAME}; then
186
		echo ">>> ERROR: Error installing kernel package $(get_pkg_name kernel-${KERNEL_NAME}).txz" | tee -a ${LOGFILE}
187
		print_error_pfS
188
	fi
189
190
	# Lock kernel to avoid user end up removing it for any reason
191
	pkg_chroot ${FINAL_CHROOT_DIR} lock -q -y $(get_pkg_name kernel-${KERNEL_NAME})
192
193
	if [ ! -f $FINAL_CHROOT_DIR/boot/kernel/kernel.gz ]; then
194
		echo ">>> ERROR: No kernel installed on $FINAL_CHROOT_DIR and the resulting image will be unusable. STOPPING!" | tee -a ${LOGFILE}
195
		print_error_pfS
196
	fi
197
	mkdir -p $FINAL_CHROOT_DIR/pkgs
198
	if [ -z "${2}" -o -n "${INSTALL_EXTRA_KERNELS}" ]; then
199 91039db4 Renato Botelho
		cp ${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${KERNEL_NAME}).txz $FINAL_CHROOT_DIR/pkgs
200 6f73c362 Renato Botelho
		if [ -n "${INSTALL_EXTRA_KERNELS}" ]; then
201
			for _EXTRA_KERNEL in $INSTALL_EXTRA_KERNELS; do
202 91039db4 Renato Botelho
				_EXTRA_KERNEL_PATH=${CORE_PKG_ALL_PATH}/$(get_pkg_name kernel-${_EXTRA_KERNEL}).txz
203 6f73c362 Renato Botelho
				if [ -f "${_EXTRA_KERNEL_PATH}" ]; then
204
					echo -n ". adding ${_EXTRA_KERNEL_PATH} on image /pkgs folder"
205
					cp ${_EXTRA_KERNEL_PATH} $FINAL_CHROOT_DIR/pkgs
206
				else
207
					echo ">>> ERROR: Requested kernel $(get_pkg_name kernel-${_EXTRA_KERNEL}).txz was not found to be put on image /pkgs folder!"
208
					print_error_pfS
209
				fi
210
			done
211
		fi
212
	fi
213
	echo "Done." | tee -a ${LOGFILE}
214
215
	unset KERNEL_NAME
216
}
217
218
# This builds FreeBSD (make buildworld)
219
# Imported from FreeSBIE
220
make_world() {
221
	LOGFILE=${BUILDER_LOGS}/buildworld.${TARGET}
222 ab7ead15 Renato Botelho
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
223 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" ]; then
224 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
225
		return
226
	fi
227
228 29cdd776 Renato Botelho
	echo ">>> $(LC_ALL=C date) - Starting build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
229 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -K -s ${FREEBSD_SRC_DIR} \
230 29cdd776 Renato Botelho
		|| print_error_pfS
231
	echo ">>> $(LC_ALL=C date) - Finished build world for ${TARGET} architecture..." | tee -a ${LOGFILE}
232 6f73c362 Renato Botelho
233
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
234
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
235 729c0c7a Renato Botelho
236
	[ -d "${INSTALLER_CHROOT_DIR}" ] \
237
		|| mkdir -p ${INSTALLER_CHROOT_DIR}
238
239 468f236d Renato Botelho
	echo ">>> Installing world with bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
240 49de5a2e Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -i -K \
241 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
242
		-d ${INSTALLER_CHROOT_DIR} \
243
		|| print_error_pfS
244
245 92db4492 Renato Botelho
	# XXX set root password since we don't have nullok enabled
246
	pw -R ${INSTALLER_CHROOT_DIR} usermod root -w yes
247
248 468f236d Renato Botelho
	echo ">>> Installing world without bsdinstall for ${TARGET} architecture..." | tee -a ${LOGFILE}
249 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -K \
250 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
251
		-d ${STAGE_CHROOT_DIR} \
252
		|| print_error_pfS
253 6f73c362 Renato Botelho
254 468f236d Renato Botelho
	# XXX It must go to the scripts
255 6f73c362 Renato Botelho
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
256
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
257 468f236d Renato Botelho
	makeargs="DESTDIR=${STAGE_CHROOT_DIR}"
258 6f73c362 Renato Botelho
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
259 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}
260 a0b4972b Renato Botelho
	# XXX FIX IT
261
#	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
262 6f73c362 Renato Botelho
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
263
264
	unset makeargs
265
}
266
267
# This routine creates a ova image that contains
268
# a ovf and vmdk file. These files can be imported
269
# right into vmware or virtual box.
270
# (and many other emulation platforms)
271
# http://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
272
create_ova_image() {
273
	# XXX create a .ovf php creator that you can pass:
274
	#     1. populatedSize
275
	#     2. license
276
	#     3. product name
277
	#     4. version
278
	#     5. number of network interface cards
279
	#     6. allocationUnits
280
	#     7. capacity
281
	#     8. capacityAllocationUnits
282
283
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
284
285 e4de9720 Renato Botelho
	if [ -d "${OVA_TMP}" ]; then
286
		chflags -R noschg ${OVA_TMP}
287
		rm -rf ${OVA_TMP}
288
	fi
289 89a72b59 Renato Botelho
290 f26731b0 Renato Botelho
	mkdir -p $(dirname ${OVAPATH})
291
292 5de5a708 Renato Botelho
	local _mntdir=${OVA_TMP}/mnt
293
	mkdir -p ${_mntdir}
294 89a72b59 Renato Botelho
295 2006b940 Renato Botelho
	if [ -z "${OVA_SWAP_PART_SIZE_IN_GB}" -o "${OVA_SWAP_PART_SIZE_IN_GB}" = "0" ]; then
296
		# first partition size (freebsd-ufs)
297
		local OVA_FIRST_PART_SIZE_IN_GB=${VMDK_DISK_CAPACITY_IN_GB}
298
		# Calculate real first partition size, removing 128 blocks (65536 bytes) beginning/loader
299
		local OVA_FIRST_PART_SIZE=$((${OVA_FIRST_PART_SIZE_IN_GB}*1024*1024*1024-65536))
300
		# Unset swap partition size variable
301
		unset OVA_SWAP_PART_SIZE
302
		# Parameter used by mkimg
303
		unset OVA_SWAP_PART_PARAM
304
	else
305
		# first partition size (freebsd-ufs)
306
		local OVA_FIRST_PART_SIZE_IN_GB=$((VMDK_DISK_CAPACITY_IN_GB-OVA_SWAP_PART_SIZE_IN_GB))
307
		# Use first partition size in g
308
		local OVA_FIRST_PART_SIZE="${OVA_FIRST_PART_SIZE_IN_GB}g"
309
		# Calculate real swap size, removing 128 blocks (65536 bytes) beginning/loader
310
		local OVA_SWAP_PART_SIZE=$((${OVA_SWAP_PART_SIZE_IN_GB}*1024*1024*1024-65536))
311
		# Parameter used by mkimg
312
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
313
	fi
314 35104e03 Renato Botelho
315 6f73c362 Renato Botelho
	# Prepare folder to be put in image
316
	customize_stagearea_for_image "ova"
317
	install_default_kernel ${DEFAULT_KERNEL} "no"
318
319 89a72b59 Renato Botelho
	# Fill fstab
320
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
321 c9b36088 Renato Botelho
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
322 2006b940 Renato Botelho
	if [ -n "${OVA_SWAP_PART_SIZE}" ]; then
323
		echo "/dev/gpt/swap0	none	swap	sw	0	0" >> ${FINAL_CHROOT_DIR}/etc/fstab
324
	fi
325 89a72b59 Renato Botelho
326
	# Create / partition
327 7743b4bd Renato Botelho
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
328 5de5a708 Renato Botelho
	truncate -s ${OVA_FIRST_PART_SIZE} ${OVA_TMP}/${OVFUFS}
329 eeddd261 Renato Botelho
	local _md=$(mdconfig -a -f ${OVA_TMP}/${OVFUFS})
330 5de5a708 Renato Botelho
	trap "mdconfig -d -u ${_md}; return" 1 2 15 EXIT
331
332
	newfs -L ${PRODUCT_NAME} -j /dev/${_md} 2>&1 >>${LOGFILE}
333 89a72b59 Renato Botelho
334 5de5a708 Renato Botelho
	if ! mount /dev/${_md} ${_mntdir} 2>&1 >>${LOGFILE}; then
335 85bc1c63 Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
336 5de5a708 Renato Botelho
		echo ">>> ERROR: Error mounting temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
337 85bc1c63 Renato Botelho
		print_error_pfS
338
	fi
339 32918935 Renato Botelho
	trap "sync; sleep 3; umount ${_mntdir} || umount -f ${_mntdir}; mdconfig -d -u ${_md}; return" 1 2 15 EXIT
340 5de5a708 Renato Botelho
341 85bc1c63 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
342
343 5de5a708 Renato Botelho
	clone_directory_contents ${FINAL_CHROOT_DIR} ${_mntdir}
344
345
	sync
346 b2ee641c Renato Botelho
	sleep 3
347 32918935 Renato Botelho
	umount ${_mntdir} || umount -f ${_mntdir} >>${LOGFILE} 2>&1
348 5de5a708 Renato Botelho
	mdconfig -d -u ${_md}
349
	trap "-" 1 2 15 EXIT
350
351 7743b4bd Renato Botelho
	# Create raw disk
352
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
353 89a72b59 Renato Botelho
	mkimg \
354
		-s gpt \
355 679b5a66 Renato Botelho
		-f raw \
356 8f05bbd6 Renato Botelho
		-b ${FINAL_CHROOT_DIR}/boot/pmbr \
357
		-p freebsd-boot:=${FINAL_CHROOT_DIR}/boot/gptboot \
358 89a72b59 Renato Botelho
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
359 2006b940 Renato Botelho
		${OVA_SWAP_PART_PARAM} \
360 679b5a66 Renato Botelho
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
361 89a72b59 Renato Botelho
362 679b5a66 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
363 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
364
			rm -f ${OVA_TMP}/${OVFUFS}
365
		fi
366 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
367
			rm -f ${OVA_TMP}/${OVFRAW}
368 502db0bd Renato Botelho
		fi
369 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
370 502db0bd Renato Botelho
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
371
		print_error_pfS
372
	fi
373 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
374 502db0bd Renato Botelho
375
	# We don't need it anymore
376
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
377
378 679b5a66 Renato Botelho
	# Convert raw to vmdk
379 7743b4bd Renato Botelho
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
380 beb07890 Renato Botelho
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
381 502db0bd Renato Botelho
382 2dc195f9 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
383 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
384
			rm -f ${OVA_TMP}/${OVFRAW}
385 502db0bd Renato Botelho
		fi
386 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
387
			rm -f ${OVA_TMP}/${OVFVMDK}
388
		fi
389 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
390 89a72b59 Renato Botelho
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
391
		print_error_pfS
392 6f73c362 Renato Botelho
	fi
393 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
394 6f73c362 Renato Botelho
395 3437fab0 Renato Botelho
	rm -f ${OVA_TMP}/${OVFRAW}
396 6f73c362 Renato Botelho
397 89a72b59 Renato Botelho
	ova_setup_ovf_template
398 6f73c362 Renato Botelho
399 7743b4bd Renato Botelho
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
400
	# Create OVA file for vmware
401 89a72b59 Renato Botelho
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
402 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
403 89a72b59 Renato Botelho
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
404
405
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
406 6f73c362 Renato Botelho
}
407
408
# called from create_ova_image
409 a82b9f43 Renato Botelho
ova_setup_ovf_template() {
410 fc48ef54 Renato Botelho
	if [ ! -f ${OVFTEMPLATE} ]; then
411 173fa93f Renato Botelho
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
412
		print_error_pfS
413 6f73c362 Renato Botelho
	fi
414
415 fc48ef54 Renato Botelho
	#  OperatingSystemSection (${PRODUCT_NAME}.ovf)
416
	#  42   FreeBSD 32-Bit
417
	#  78   FreeBSD 64-Bit
418
	if [ "${TARGET}" = "amd64" ]; then
419
		local _os_id="78"
420 502db0bd Renato Botelho
		local _os_type="freebsd64Guest"
421
		local _os_descr="FreeBSD 64-Bit"
422 fc48ef54 Renato Botelho
	else
423
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
424
		print_error_pfS
425
	fi
426 50dcadff Renato Botelho
427 502db0bd Renato Botelho
	local POPULATED_SIZE=$(du -d0 -k $FINAL_CHROOT_DIR | cut -f1)
428
	local POPULATED_SIZE_IN_BYTES=$((${POPULATED_SIZE}*1024))
429
	local VMDK_FILE_SIZE=$(stat -f "%z" ${OVA_TMP}/${OVFVMDK})
430 6f73c362 Renato Botelho
431 fc48ef54 Renato Botelho
	sed \
432 502db0bd Renato Botelho
		-e "s,%%VMDK_FILE_SIZE%%,${VMDK_FILE_SIZE},g" \
433
		-e "s,%%VMDK_DISK_CAPACITY_IN_GB%%,${VMDK_DISK_CAPACITY_IN_GB},g" \
434
		-e "s,%%POPULATED_SIZE_IN_BYTES%%,${POPULATED_SIZE_IN_BYTES},g" \
435 fc48ef54 Renato Botelho
		-e "s,%%OS_ID%%,${_os_id},g" \
436
		-e "s,%%OS_TYPE%%,${_os_type},g" \
437 502db0bd Renato Botelho
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
438 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
439 f2d83cfb Renato Botelho
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
440 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
441
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
442 0e334a37 Renato Botelho
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
443
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
444 b12ea3fb Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/LICENSE" \
445 fc48ef54 Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/d" \
446
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
447 6f73c362 Renato Botelho
}
448
449
# Cleans up previous builds
450 7e6ab3ed Renato Botelho
clean_builder() {
451 6f73c362 Renato Botelho
	# Clean out directories
452
	echo ">>> Cleaning up previous build environment...Please wait!"
453
454
	staginareas_clean_each_run
455
456
	if [ -d "${STAGE_CHROOT_DIR}" ]; then
457 729c0c7a Renato Botelho
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR}... "
458 6f73c362 Renato Botelho
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
459 f546e6ca Renato Botelho
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
460 6f73c362 Renato Botelho
		echo "Done."
461
	fi
462
463 729c0c7a Renato Botelho
	if [ -d "${INSTALLER_CHROOT_DIR}" ]; then
464
		echo -n ">>> Cleaning ${INSTALLER_CHROOT_DIR}... "
465
		chflags -R noschg ${INSTALLER_CHROOT_DIR} 2>&1 >/dev/null
466
		rm -rf ${INSTALLER_CHROOT_DIR}/* 2>/dev/null
467
		echo "Done."
468
	fi
469
470 2abc3e80 Renato Botelho
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
471 edfe9e76 Renato Botelho
		OBJTREE=$(make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
472 6f73c362 Renato Botelho
		if [ -d "${OBJTREE}" ]; then
473
			echo -n ">>> Cleaning FreeBSD objects dir staging..."
474
			echo -n "."
475
			chflags -R noschg ${OBJTREE} 2>&1 >/dev/null
476
			echo -n "."
477
			rm -rf ${OBJTREE}/*
478
			echo "Done!"
479
		fi
480 7e6ab3ed Renato Botelho
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
481 6f73c362 Renato Botelho
			echo -n ">>> Cleaning previously built kernel stage area..."
482
			rm -rf $KERNEL_BUILD_PATH/*
483
			echo "Done!"
484
		fi
485
	fi
486
	mkdir -p $KERNEL_BUILD_PATH
487
488
	echo -n ">>> Cleaning previously built images..."
489 04992be2 Renato Botelho
	rm -rf $IMAGES_FINAL_DIR/*
490 6f73c362 Renato Botelho
	echo "Done!"
491
492
	echo -n ">>> Cleaning previous builder logs..."
493
	if [ -d "$BUILDER_LOGS" ]; then
494
		rm -rf ${BUILDER_LOGS}
495
	fi
496
	mkdir -p ${BUILDER_LOGS}
497
498
	echo "Done!"
499
500
	echo ">>> Cleaning of builder environment has finished."
501
}
502
503
clone_directory_contents() {
504 25c5455d Renato Botelho
	if [ ! -e "$2" ]; then
505
		mkdir -p "$2"
506
	fi
507 6f73c362 Renato Botelho
	if [ ! -d "$1" -o ! -d "$2" ]; then
508
		if [ -z "${LOGFILE}" ]; then
509
			echo ">>> ERROR: Argument $1 supplied is not a directory!"
510
		else
511
			echo ">>> ERROR: Argument $1 supplied is not a directory!" | tee -a ${LOGFILE}
512
		fi
513
		print_error_pfS
514
	fi
515
	echo -n ">>> Using TAR to clone $1 to $2 ..."
516
	tar -C ${1} -c -f - . | tar -C ${2} -x -p -f -
517
	echo "Done!"
518
}
519
520
clone_to_staging_area() {
521
	# Clone everything to the final staging area
522
	echo -n ">>> Cloning everything to ${STAGE_CHROOT_DIR} staging area..."
523
	LOGFILE=${BUILDER_LOGS}/cloning.${TARGET}.log
524
525 694028d6 Renato Botelho
	tar -C ${PRODUCT_SRC} -c -f - . | \
526 6f73c362 Renato Botelho
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
527
528 a3bb036b Renato Botelho
	if [ "${PRODUCT_NAME}" != "pfSense" ]; then
529
		mv ${STAGE_CHROOT_DIR}/usr/local/sbin/pfSense-upgrade \
530
			${STAGE_CHROOT_DIR}/usr/local/sbin/${PRODUCT_NAME}-upgrade
531
	fi
532
533 6f73c362 Renato Botelho
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
534
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
535
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
536
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
537
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
538
	fi
539
540
	## Add buildtime and lastcommit information
541
	# This is used for detecting updates.
542
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
543
	# Record last commit info if it is available.
544
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
545
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
546
	fi
547
548 0d67c726 Renato Botelho
	local _exclude_files="${SCRATCHDIR}/base_exclude_files"
549 6f73c362 Renato Botelho
	sed \
550
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
551
		-e "s,%%VERSION%%,${_version},g" \
552 0ce2a7f3 Renato Botelho
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
553 6f73c362 Renato Botelho
		> ${_exclude_files}
554
555 651f440c Renato Botelho
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
556 10742be9 Renato Botelho
557
	# Include a sample pkg stable conf to base
558
	setup_pkg_repo \
559 db8621d8 Renato Botelho
		${PKG_REPO_DEFAULT} \
560 651f440c Renato Botelho
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
561 10742be9 Renato Botelho
		${TARGET} \
562 74a4eefb Renato Botelho
		${TARGET_ARCH}
563 10742be9 Renato Botelho
564 6f73c362 Renato Botelho
	mtree \
565
		-c \
566
		-k uid,gid,mode,size,flags,sha256digest \
567
		-p ${STAGE_CHROOT_DIR} \
568
		-X ${_exclude_files} \
569 651f440c Renato Botelho
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
570 6f73c362 Renato Botelho
	tar \
571
		-C ${STAGE_CHROOT_DIR} \
572 651f440c Renato Botelho
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
573 6f73c362 Renato Botelho
		-X ${_exclude_files} \
574
		.
575
576 dab31392 Renato Botelho
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
577 6f73c362 Renato Botelho
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
578
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
579
580
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
581 c4968f42 Renato Botelho
582 2f6260c5 Renato Botelho
	# Save current WAN and LAN if value
583 168c162a Renato Botelho
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
584
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
585 2f6260c5 Renato Botelho
586 c4968f42 Renato Botelho
	# Change default interface names to match vmware driver
587 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
588
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
589 fbd4dfc0 Renato Botelho
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
590 c4968f42 Renato Botelho
591
	# Restore default values to be used by serial package
592 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
593
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
594 c4968f42 Renato Botelho
595 6f73c362 Renato Botelho
	# Activate serial console in config.xml
596 2f6260c5 Renato Botelho
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
597
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
598
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
599
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
600
	rm -f ${DEFAULTCONF}.tmp
601 6f73c362 Renato Botelho
602
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
603
604
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
605
606
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
607
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
608
609
	# Make sure pkg is present
610
	pkg_bootstrap ${STAGE_CHROOT_DIR}
611
612 3e80d64e Renato Botelho
	# Make sure correct repo is available on tmp dir
613
	mkdir -p ${STAGE_CHROOT_DIR}/tmp/pkg-repos
614
	setup_pkg_repo \
615
		${PKG_REPO_DEFAULT} \
616
		${STAGE_CHROOT_DIR}/tmp/pkg-repos/repo.conf \
617
		${TARGET} \
618
		${TARGET_ARCH} \
619
		staging
620
621 6f73c362 Renato Botelho
	echo "Done!"
622
}
623
624
create_final_staging_area() {
625
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
626
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
627
		print_error_pfS
628
	fi
629
630
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
631
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
632
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
633
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
634
		echo "Done." | tee -a ${LOGFILE}
635
	fi
636
637
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
638
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
639
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
640
641
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
642
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
643
		print_error_pfS
644
	fi
645
}
646
647
customize_stagearea_for_image() {
648 b2fbacdc Renato Botelho
	local _image_type="$1"
649 b0d0498c Renato Botelho
	local _default_config="" # filled with $2 below
650
	local _image_variant="$3"
651 75e28295 Renato Botelho
652
	if [ -n "$2" ]; then
653
		_default_config="$2"
654 2ac4be3a Renato Botelho
	elif [ "${_image_type}" = "memstickserial" -o \
655 75e28295 Renato Botelho
	     "${_image_type}" = "memstickadi" ]; then
656
		_default_config="default-config-serial"
657
	elif [ "${_image_type}" = "ova" ]; then
658
		_default_config="default-config-vmware"
659
	else
660
		_default_config="default-config"
661
	fi
662 b2fbacdc Renato Botelho
663 6f73c362 Renato Botelho
	# Prepare final stage area
664
	create_final_staging_area
665
666 dab31392 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
667 2ac4be3a Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} base
668 6ee167de Renato Botelho
669 b2fbacdc Renato Botelho
	if [ "${_image_type}" = "iso" -o \
670
	     "${_image_type}" = "memstick" -o \
671
	     "${_image_type}" = "memstickserial" -o \
672
	     "${_image_type}" = "memstickadi" ]; then
673 6f73c362 Renato Botelho
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
674 91039db4 Renato Botelho
		cp ${CORE_PKG_ALL_PATH}/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
675 6f73c362 Renato Botelho
	fi
676
677 75e28295 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
678 052f4bdb Renato Botelho
679
	# XXX: Workaround to avoid pkg to complain regarding release
680
	#      repo on first boot since packages are installed from
681
	#      staging server during build phase
682 74a4eefb Renato Botelho
	if [ -n "${USE_PKG_REPO_STAGING}" ]; then
683 052f4bdb Renato Botelho
		_read_cmd="select value from repodata where key='packagesite'"
684 71002df8 Renato Botelho
		if [ -n "${_IS_RELEASE}" ]; then
685
			local _tgt_server="${PKG_REPO_SERVER_RELEASE}"
686
		else
687
			local _tgt_server="${PKG_REPO_SERVER_DEVEL}"
688
		fi
689 052f4bdb Renato Botelho
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
690
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
691 71002df8 Renato Botelho
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER_STAGING},${_tgt_server},")
692 052f4bdb Renato Botelho
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
693
		done
694
	fi
695 b0d0498c Renato Botelho
696
	if [ -n "$_image_variant" -a \
697
	    -d ${BUILDER_TOOLS}/templates/custom_logos/${_image_variant} ]; then
698
		mkdir -p ${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
699
		cp -f \
700
			${BUILDER_TOOLS}/templates/custom_logos/${_image_variant}/*.png \
701
			${FINAL_CHROOT_DIR}/usr/local/share/${PRODUCT_NAME}/custom_logos
702
	fi
703 3e80d64e Renato Botelho
704
	# Remove temporary repo conf
705
	rm -rf ${FINAL_CHROOT_DIR}/tmp/pkg-repos
706 6f73c362 Renato Botelho
}
707
708
create_distribution_tarball() {
709 729c0c7a Renato Botelho
	mkdir -p ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist
710
711 cd618e85 Renato Botelho
	echo -n ">>> Creating distribution tarball... " | tee -a ${LOGFILE}
712 c0ac85e7 Renato Botelho
	tar -C ${FINAL_CHROOT_DIR} --exclude ./pkgs \
713 729c0c7a Renato Botelho
		-cJf ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/base.txz .
714 cd618e85 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
715 6f73c362 Renato Botelho
716 cd618e85 Renato Botelho
	echo -n ">>> Creating manifest... " | tee -a ${LOGFILE}
717 729c0c7a Renato Botelho
	(cd ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist && \
718
		sh ${FREEBSD_SRC_DIR}/release/scripts/make-manifest.sh base.txz) \
719
		> ${INSTALLER_CHROOT_DIR}/usr/freebsd-dist/MANIFEST
720 cd618e85 Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
721 6f73c362 Renato Botelho
}
722
723
create_iso_image() {
724
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
725 102e7265 Renato Botelho
726 00dc6b2a Renato Botelho
	if [ -z "${ISOPATH}" ]; then
727 5df6d76a Renato Botelho
		echo ">>> ISOPATH is empty skipping generation of ISO image!" | tee -a ${LOGFILE}
728
		return
729 6f73c362 Renato Botelho
	fi
730
731 5df6d76a Renato Botelho
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
732
733 f26731b0 Renato Botelho
	mkdir -p $(dirname ${ISOPATH})
734
735 6f73c362 Renato Botelho
	customize_stagearea_for_image "iso"
736
	install_default_kernel ${DEFAULT_KERNEL}
737
738 5df6d76a Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
739
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
740 6f73c362 Renato Botelho
741 5df6d76a Renato Botelho
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
742 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
743
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
744 6f73c362 Renato Botelho
745
	create_distribution_tarball
746
747 5df6d76a Renato Botelho
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
748
749 102e7265 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/mkisoimages.sh -b \
750 5df6d76a Renato Botelho
		${FSLABEL} \
751
		${ISOPATH} \
752
		${INSTALLER_CHROOT_DIR}
753 6f73c362 Renato Botelho
754 044ff383 Renato Botelho
	if [ ! -f "${ISOPATH}" ]; then
755
		echo "ERROR! ISO image was not built"
756
		print_error_pfS
757
	fi
758
759 6f73c362 Renato Botelho
	gzip -qf $ISOPATH &
760 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
761 6f73c362 Renato Botelho
762
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
763
}
764
765
create_memstick_image() {
766 b0d0498c Renato Botelho
	local _variant="$1"
767
768 6f73c362 Renato Botelho
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
769
	if [ "${MEMSTICKPATH}" = "" ]; then
770
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
771
		return
772
	fi
773
774 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKPATH})
775
776 b0d0498c Renato Botelho
	local _image_path=${MEMSTICKPATH}
777
	if [ -n "${_variant}" ]; then
778
		_image_path=$(echo "$_image_path" | \
779
			sed "s/-memstick-/-memstick-${_variant}-/")
780
		VARIANTIMAGES="${VARIANTIMAGES}${VARIANTIMAGES:+ }${_image_path}"
781
	fi
782
783
	customize_stagearea_for_image "memstick" "" $_variant
784 624376f6 Renato Botelho
	install_default_kernel ${DEFAULT_KERNEL}
785 6f73c362 Renato Botelho
786 b0d0498c Renato Botelho
	echo ">>> Creating memstick to ${_image_path}." 2>&1 | tee -a ${LOGFILE}
787 96428027 Renato Botelho
	echo "kern.cam.boot_delay=10000" >> ${INSTALLER_CHROOT_DIR}/boot/loader.conf.local
788 6f73c362 Renato Botelho
789 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
790
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
791
792
	rm -f ${LOADERCONF} ${BOOTCONF} >/dev/null 2>&1
793
794 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
795
	cat ${LOADERCONF} > ${FINAL_CHROOT_DIR}/boot/loader.conf
796 6b5779e9 Renato Botelho
797 6f73c362 Renato Botelho
	create_distribution_tarball
798
799 729c0c7a Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
800
		${INSTALLER_CHROOT_DIR} \
801
		${_image_path}
802
803 044ff383 Renato Botelho
	if [ ! -f "${_image_path}" ]; then
804
		echo "ERROR! memstick image was not built"
805
		print_error_pfS
806
	fi
807
808 b0d0498c Renato Botelho
	gzip -qf $_image_path &
809 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
810 6f73c362 Renato Botelho
811
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
812
}
813
814
create_memstick_serial_image() {
815
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
816
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
817
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
818
		return
819
	fi
820
821 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKSERIALPATH})
822
823 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickserial"
824
	install_default_kernel ${DEFAULT_KERNEL}
825 6f73c362 Renato Botelho
826
	echo ">>> Creating serial memstick to ${MEMSTICKSERIALPATH}." 2>&1 | tee -a ${LOGFILE}
827 96428027 Renato Botelho
	echo "kern.cam.boot_delay=10000" >> ${INSTALLER_CHROOT_DIR}/boot/loader.conf.local
828 6f73c362 Renato Botelho
829 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
830
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
831 6f73c362 Renato Botelho
832 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
833 6b5779e9 Renato Botelho
	echo "-S115200 -D" > ${BOOTCONF}
834 6f73c362 Renato Botelho
835
	# Activate serial console+video console in loader.conf
836 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
837
	echo 'boot_multicons="YES"' >> ${LOADERCONF}
838 6f73c362 Renato Botelho
	echo 'boot_serial="YES"' >> ${LOADERCONF}
839
	echo 'console="comconsole,vidconsole"' >> ${LOADERCONF}
840
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
841
842 535f8a75 Renato Botelho
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
843
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
844 6b5779e9 Renato Botelho
845 6f73c362 Renato Botelho
	create_distribution_tarball
846
847 6b5779e9 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
848
		${INSTALLER_CHROOT_DIR} \
849 8de27a69 Renato Botelho
		${MEMSTICKSERIALPATH}
850 6b5779e9 Renato Botelho
851 044ff383 Renato Botelho
	if [ ! -f "${MEMSTICKSERIALPATH}" ]; then
852
		echo "ERROR! memstick serial image was not built"
853
		print_error_pfS
854
	fi
855
856 6f73c362 Renato Botelho
	gzip -qf $MEMSTICKSERIALPATH &
857 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
858 6f73c362 Renato Botelho
859
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
860
}
861
862
create_memstick_adi_image() {
863 462cbb5a Renato Botelho
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
864 6f73c362 Renato Botelho
	if [ "${MEMSTICKADIPATH}" = "" ]; then
865
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
866
		return
867
	fi
868
869 f26731b0 Renato Botelho
	mkdir -p $(dirname ${MEMSTICKADIPATH})
870
871 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickadi"
872
	install_default_kernel ${DEFAULT_KERNEL}
873 6f73c362 Renato Botelho
874
	echo ">>> Creating serial memstick to ${MEMSTICKADIPATH}." 2>&1 | tee -a ${LOGFILE}
875 96428027 Renato Botelho
	echo "kern.cam.boot_delay=10000" >> ${INSTALLER_CHROOT_DIR}/boot/loader.conf.local
876 6f73c362 Renato Botelho
877 6b5779e9 Renato Botelho
	BOOTCONF=${INSTALLER_CHROOT_DIR}/boot.config
878
	LOADERCONF=${INSTALLER_CHROOT_DIR}/boot/loader.conf
879 6f73c362 Renato Botelho
880 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
881 6b5779e9 Renato Botelho
	echo "-S115200 -h" > ${BOOTCONF}
882 6f73c362 Renato Botelho
883
	# Activate serial console+video console in loader.conf
884 788f1c3b Renato Botelho
	echo 'autoboot_delay="3"' > ${LOADERCONF}
885
	echo 'boot_serial="YES"' >> ${LOADERCONF}
886 6f73c362 Renato Botelho
	echo 'console="comconsole"' >> ${LOADERCONF}
887
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
888
	echo 'comconsole_port="0x2F8"' >> ${LOADERCONF}
889
	echo 'hint.uart.0.flags="0x00"' >> ${LOADERCONF}
890
	echo 'hint.uart.1.flags="0x10"' >> ${LOADERCONF}
891
892 6b5779e9 Renato Botelho
	cat ${BOOTCONF} >> ${FINAL_CHROOT_DIR}/boot.config
893
	cat ${LOADERCONF} >> ${FINAL_CHROOT_DIR}/boot/loader.conf
894
895 6f73c362 Renato Botelho
	create_distribution_tarball
896
897 6b5779e9 Renato Botelho
	sh ${FREEBSD_SRC_DIR}/release/${TARGET}/make-memstick.sh \
898
		${INSTALLER_CHROOT_DIR} \
899 8de27a69 Renato Botelho
		${MEMSTICKADIPATH}
900 6b5779e9 Renato Botelho
901 044ff383 Renato Botelho
	if [ ! -f "${MEMSTICKADIPATH}" ]; then
902
		echo "ERROR! memstick ADI image was not built"
903
		print_error_pfS
904
	fi
905
906 6f73c362 Renato Botelho
	gzip -qf $MEMSTICKADIPATH &
907 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
908 6f73c362 Renato Botelho
909
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
910
}
911
912
# Create pkg conf on desired place with desired arch/branch
913
setup_pkg_repo() {
914 74a4eefb Renato Botelho
	if [ -z "${4}" ]; then
915 6f73c362 Renato Botelho
		return
916
	fi
917
918 db8621d8 Renato Botelho
	local _template="${1}"
919
	local _target="${2}"
920
	local _arch="${3}"
921
	local _target_arch="${4}"
922 74a4eefb Renato Botelho
	local _staging="${5}"
923 52efc840 Renato Botelho
924
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
925
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
926
		print_error_pfS
927
	fi
928 6f73c362 Renato Botelho
929 74a4eefb Renato Botelho
	if [ -n "${_staging}" -a -n "${USE_PKG_REPO_STAGING}" ]; then
930 fb4d75d9 Renato Botelho
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_STAGING}
931
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_STAGING}
932
		local _pkg_repo_server_release=${PKG_REPO_SERVER_STAGING}
933
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_STAGING}
934 74a4eefb Renato Botelho
	else
935 fb4d75d9 Renato Botelho
		local _pkg_repo_server_devel=${PKG_REPO_SERVER_DEVEL}
936
		local _pkg_repo_branch_devel=${PKG_REPO_BRANCH_DEVEL}
937
		local _pkg_repo_server_release=${PKG_REPO_SERVER_RELEASE}
938
		local _pkg_repo_branch_release=${PKG_REPO_BRANCH_RELEASE}
939 2c27ff24 Renato Botelho
	fi
940 97fd32ba Renato Botelho
941 6f73c362 Renato Botelho
	mkdir -p $(dirname ${_target}) >/dev/null 2>&1
942
943
	sed \
944 ab225f2b Renato Botelho
		-e "s/%%ARCH%%/${_target_arch}/" \
945 fb4d75d9 Renato Botelho
		-e "s/%%PKG_REPO_BRANCH_DEVEL%%/${_pkg_repo_branch_devel}/g" \
946
		-e "s/%%PKG_REPO_BRANCH_RELEASE%%/${_pkg_repo_branch_release}/g" \
947
		-e "s,%%PKG_REPO_SERVER_DEVEL%%,${_pkg_repo_server_devel},g" \
948
		-e "s,%%PKG_REPO_SERVER_RELEASE%%,${_pkg_repo_server_release},g" \
949 f8a36c30 Renato Botelho
		-e "s,%%POUDRIERE_PORTS_NAME%%,${POUDRIERE_PORTS_NAME},g" \
950 6f73c362 Renato Botelho
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
951 52efc840 Renato Botelho
		${_template} \
952 6f73c362 Renato Botelho
		> ${_target}
953
}
954
955
# This routine ensures any ports / binaries that the builder
956
# system needs are on disk and ready for execution.
957
builder_setup() {
958
	# If Product-builder is already installed, just leave
959
	if pkg info -e -q ${PRODUCT_NAME}-builder; then
960
		return
961
	fi
962
963 52efc840 Renato Botelho
	if [ ! -f ${PKG_REPO_PATH} ]; then
964
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
965
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
966 6f73c362 Renato Botelho
967 d2f163a6 Renato Botelho
		update_freebsd_sources
968
969 6f73c362 Renato Botelho
		local _arch=$(uname -m)
970 0c096dad Renato Botelho
		setup_pkg_repo \
971 db8621d8 Renato Botelho
			${PKG_REPO_DEFAULT} \
972 52efc840 Renato Botelho
			${PKG_REPO_PATH} \
973 0c096dad Renato Botelho
			${_arch} \
974 67f2556a Renato Botelho
			${_arch} \
975
			"staging"
976 569929ef Renato Botelho
977
		# Use fingerprint keys from repo
978
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
979
			${PKG_REPO_PATH}
980 6f73c362 Renato Botelho
	fi
981
982
	pkg install ${PRODUCT_NAME}-builder
983
}
984
985
# Updates FreeBSD sources
986
update_freebsd_sources() {
987
	if [ "${1}" = "full" ]; then
988
		local _full=1
989
		local _clone_params=""
990
	else
991
		local _full=0
992
		local _clone_params="--depth 1 --single-branch"
993
	fi
994
995 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
996 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
997
		return
998
	fi
999
1000 a10a0e70 Renato Botelho
	echo ">>> Obtaining FreeBSD sources (${FREEBSD_BRANCH})..."
1001 cdf2b5f8 Renato Botelho
	${BUILDER_SCRIPTS}/git_checkout.sh \
1002 a10a0e70 Renato Botelho
		-r ${FREEBSD_REPO_BASE} \
1003
		-d ${FREEBSD_SRC_DIR} \
1004
		-b ${FREEBSD_BRANCH}
1005 6f73c362 Renato Botelho
1006 a10a0e70 Renato Botelho
	if [ $? -ne 0 -o ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1007 6f73c362 Renato Botelho
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1008
		print_error_pfS
1009
	fi
1010
1011
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1012 a10a0e70 Renato Botelho
		echo -n ">>> Checking out desired commit (${GIT_FREEBSD_COSHA1})... "
1013
		( git -C  ${FREEBSD_SRC_DIR} checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | \
1014
			grep -C3 -i -E 'error|fatal'
1015
		echo "Done!"
1016 6f73c362 Renato Botelho
	fi
1017
}
1018
1019
pkg_chroot() {
1020
	local _root="${1}"
1021
	shift
1022
1023
	if [ $# -eq 0 ]; then
1024
		return -1
1025
	fi
1026
1027
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1028
		return -1
1029
	fi
1030
1031
	mkdir -p \
1032
		${SCRATCHDIR}/pkg_cache \
1033
		${_root}/var/cache/pkg \
1034
		${_root}/dev
1035
1036
	/sbin/mount -t nullfs ${SCRATCHDIR}/pkg_cache ${_root}/var/cache/pkg
1037
	/sbin/mount -t devfs devfs ${_root}/dev
1038
	cp -f /etc/resolv.conf ${_root}/etc/resolv.conf
1039
	touch ${BUILDER_LOGS}/install_pkg_install_ports.txt
1040 3e80d64e Renato Botelho
	local _params=""
1041
	if [ -f "${_root}/tmp/pkg-repos/repo.conf" ]; then
1042
		_params="--repo-conf-dir /tmp/pkg-repos "
1043
	fi
1044
	script -aq ${BUILDER_LOGS}/install_pkg_install_ports.txt \
1045
		pkg -c ${_root} ${_params}$@ >/dev/null 2>&1
1046 10f26956 Renato Botelho
	local result=$?
1047 6f73c362 Renato Botelho
	rm -f ${_root}/etc/resolv.conf
1048 5084361d Renato Botelho
	/sbin/umount -f ${_root}/dev
1049
	/sbin/umount -f ${_root}/var/cache/pkg
1050 10f26956 Renato Botelho
1051
	return $result
1052 6f73c362 Renato Botelho
}
1053
1054
1055
pkg_chroot_add() {
1056
	if [ -z "${1}" -o -z "${2}" ]; then
1057
		return 1
1058
	fi
1059
1060
	local _target="${1}"
1061
	local _pkg="$(get_pkg_name ${2}).txz"
1062
1063
	if [ ! -d "${_target}" ]; then
1064
		echo ">>> ERROR: Target dir ${_target} not found"
1065
		print_error_pfS
1066
	fi
1067
1068 91039db4 Renato Botelho
	if [ ! -f ${CORE_PKG_ALL_PATH}/${_pkg} ]; then
1069 6f73c362 Renato Botelho
		echo ">>> ERROR: Package ${_pkg} not found"
1070
		print_error_pfS
1071
	fi
1072
1073 91039db4 Renato Botelho
	cp ${CORE_PKG_ALL_PATH}/${_pkg} ${_target}
1074 6f73c362 Renato Botelho
	pkg_chroot ${_target} add /${_pkg}
1075
	rm -f ${_target}/${_pkg}
1076
}
1077
1078
pkg_bootstrap() {
1079
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1080
1081 0ada33a9 Renato Botelho
	setup_pkg_repo \
1082 db8621d8 Renato Botelho
		${PKG_REPO_DEFAULT} \
1083 52efc840 Renato Botelho
		${_root}${PKG_REPO_PATH} \
1084 0ada33a9 Renato Botelho
		${TARGET} \
1085
		${TARGET_ARCH} \
1086 74a4eefb Renato Botelho
		"staging"
1087 6f73c362 Renato Botelho
1088
	pkg_chroot ${_root} bootstrap -f
1089
}
1090
1091
# This routine assists with installing various
1092 97bc6c78 Phil Davis
# freebsd ports files into the pfsense-fs staging
1093 6f73c362 Renato Botelho
# area.
1094
install_pkg_install_ports() {
1095
	local MAIN_PKG="${1}"
1096
1097
	if [ -z "${MAIN_PKG}" ]; then
1098
		MAIN_PKG=${PRODUCT_NAME}
1099
	fi
1100
1101
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1102
1103
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1104
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1105
1106
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1107
		mkdir -p ${SCRATCHDIR}/pkg_cache
1108
1109 10f26956 Renato Botelho
	echo -n ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... "
1110 cdac78f6 Renato Botelho
	# First mark all packages as automatically installed
1111
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 1 -a
1112
	# Install all necessary packages
1113 10f26956 Renato Botelho
	if ! pkg_chroot ${STAGE_CHROOT_DIR} install ${MAIN_PKG} ${custom_package_list}; then
1114
		echo "Failed!"
1115
		print_error_pfS
1116
	fi
1117 cdac78f6 Renato Botelho
	# Make sure required packages are set as non-automatic
1118 4b0f15b6 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 0 pkg ${MAIN_PKG} ${custom_package_list}
1119 cdac78f6 Renato Botelho
	# Remove unnecessary packages
1120 6f73c362 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} autoremove
1121 10f26956 Renato Botelho
	echo "Done!"
1122 6f73c362 Renato Botelho
}
1123
1124
staginareas_clean_each_run() {
1125
	echo -n ">>> Cleaning build directories: "
1126
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1127
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1128
		echo -n "$BASENAME "
1129
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1130 f546e6ca Renato Botelho
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1131 6f73c362 Renato Botelho
	fi
1132
	echo "Done!"
1133
}
1134
1135
# Imported from FreeSBIE
1136
buildkernel() {
1137 f41f9c0c Renato Botelho
	local _kernconf=${1:-${KERNCONF}}
1138
1139 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDKERNEL}" ]; then
1140 6f73c362 Renato Botelho
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1141
		return
1142
	fi
1143
1144 f41f9c0c Renato Botelho
	if [ -z "${_kernconf}" ]; then
1145 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1146
		print_error_pfS
1147
	fi
1148
1149 f41f9c0c Renato Botelho
	local _old_kernconf=${KERNCONF}
1150
	export KERNCONF=${_kernconf}
1151 6f73c362 Renato Botelho
1152 29cdd776 Renato Botelho
	echo ">>> $(LC_ALL=C date) - Starting build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1153 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/build_freebsd.sh -W -s ${FREEBSD_SRC_DIR} \
1154 29cdd776 Renato Botelho
		|| print_error_pfS
1155
	echo ">>> $(LC_ALL=C date) - Finished build kernel for ${TARGET} architecture..." | tee -a ${LOGFILE}
1156 f41f9c0c Renato Botelho
1157
	export KERNCONF=${_old_kernconf}
1158 6f73c362 Renato Botelho
}
1159
1160
# Imported from FreeSBIE
1161
installkernel() {
1162 7d460897 Renato Botelho
	local _destdir=${1:-${KERNEL_DESTDIR}}
1163 f41f9c0c Renato Botelho
	local _kernconf=${2:-${KERNCONF}}
1164 7d460897 Renato Botelho
1165 f41f9c0c Renato Botelho
	if [ -z "${_kernconf}" ]; then
1166 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1167
		print_error_pfS
1168
	fi
1169
1170 f41f9c0c Renato Botelho
	local _old_kernconf=${KERNCONF}
1171
	export KERNCONF=${_kernconf}
1172 6f73c362 Renato Botelho
1173
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1174 f41f9c0c Renato Botelho
	echo ">>> Installing kernel (${_kernconf}) for ${TARGET} architecture..." | tee -a ${LOGFILE}
1175 cdf2b5f8 Renato Botelho
	script -aq $LOGFILE ${BUILDER_SCRIPTS}/install_freebsd.sh -W -D -z \
1176 468f236d Renato Botelho
		-s ${FREEBSD_SRC_DIR} \
1177
		-d ${_destdir} \
1178
		|| print_error_pfS
1179 f41f9c0c Renato Botelho
1180
	export KERNCONF=${_old_kernconf}
1181 6f73c362 Renato Botelho
}
1182
1183
# Launch is ran first to setup a few variables that we need
1184
# Imported from FreeSBIE
1185
launch() {
1186
	if [ "$(id -u)" != "0" ]; then
1187
		echo "Sorry, this must be done as root."
1188
	fi
1189
1190
	echo ">>> Operation $0 has started at $(date)"
1191
}
1192
1193
finish() {
1194
	echo ">>> Operation $0 has ended at $(date)"
1195
}
1196
1197 184b39e1 Renato Botelho
pkg_repo_rsync() {
1198 15333972 Renato Botelho
	local _repo_path_param="${1}"
1199 92a1044d Renato Botelho
	local _ignore_final_rsync="${2}"
1200 184b39e1 Renato Botelho
1201 15333972 Renato Botelho
	if [ -z "${_repo_path_param}" -o ! -d "${_repo_path_param}" ]; then
1202 184b39e1 Renato Botelho
		return
1203
	fi
1204
1205 92a1044d Renato Botelho
	if [ -n "${SKIP_FINAL_RSYNC}" ]; then
1206
		_ignore_final_rsync="1"
1207
	fi
1208
1209 61b63ac0 Renato Botelho
	# Sanitize path
1210 15333972 Renato Botelho
	_repo_path=$(realpath ${_repo_path_param})
1211 61b63ac0 Renato Botelho
1212
	local _repo_dir=$(dirname ${_repo_path})
1213
	local _repo_base=$(basename ${_repo_path})
1214
1215
	# Add ./ it's an rsync trick to make it chdir to directory before sending it
1216
	_repo_path="${_repo_dir}/./${_repo_base}"
1217
1218 184b39e1 Renato Botelho
	if [ -z "${LOGFILE}" ]; then
1219
		local _logfile="/dev/null"
1220
	else
1221
		local _logfile="${LOGFILE}"
1222
	fi
1223
1224 94f7df8f Renato Botelho
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" -a -z "${DO_NOT_SIGN_PKG_REPO}" ]; then
1225 efcf5e2a Renato Botelho
		# Detect poudriere directory structure
1226
		if [ -L "${_repo_path}/.latest" ]; then
1227
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1228
		else
1229
			local _real_repo_path=${_repo_path}
1230
		fi
1231
1232 f4575189 Renato Botelho
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1233 efcf5e2a Renato Botelho
		############ ATTENTION ##############
1234
		#
1235
		# For some reason pkg-repo fail without / in the end of directory name
1236
		# so removing it will break command
1237
		#
1238 c2af1487 Renato Botelho
		# https://github.com/freebsd/pkg/issues/1364
1239
		#
1240 efcf5e2a Renato Botelho
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1241 f4575189 Renato Botelho
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1242
			echo "Done!" | tee -a ${_logfile}
1243
		else
1244
			echo "Failed!" | tee -a ${_logfile}
1245
			echo ">>> ERROR: An error occurred trying to sign repo"
1246
			print_error_pfS
1247
		fi
1248
1249
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1250
		if [ -e ${_pkgfile} ]; then
1251
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1252
1253
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1254
			    > ${_pkgfile}.sig 2>/dev/null; then
1255
				echo "Done!" | tee -a ${_logfile}
1256
			else
1257
				echo "Failed!" | tee -a ${_logfile}
1258
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1259
				print_error_pfS
1260
			fi
1261
		fi
1262
	fi
1263
1264
	if [ -n "${DO_NOT_UPLOAD}" ]; then
1265
		return
1266
	fi
1267
1268 5163c3ca Renato Botelho
	for _pkg_rsync_hostname in ${PKG_RSYNC_HOSTNAME}; do
1269
		# Make sure destination directory exist
1270
		ssh -p ${PKG_RSYNC_SSH_PORT} \
1271
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} \
1272
			"mkdir -p ${PKG_RSYNC_DESTDIR}"
1273
1274
		echo -n ">>> Sending updated repository to ${_pkg_rsync_hostname}... " | tee -a ${_logfile}
1275
		if script -aq ${_logfile} rsync -Have "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1276
			--timeout=60 --delete-delay ${_repo_path} \
1277
			${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1278
		then
1279 2c056b08 Renato Botelho
			echo "Done!" | tee -a ${_logfile}
1280
		else
1281
			echo "Failed!" | tee -a ${_logfile}
1282 5163c3ca Renato Botelho
			echo ">>> ERROR: An error occurred sending repo to remote hostname"
1283 2c056b08 Renato Botelho
			print_error_pfS
1284
		fi
1285
1286 5163c3ca Renato Botelho
		if [ -z "${USE_PKG_REPO_STAGING}" -o -n "${_ignore_final_rsync}" ]; then
1287
			return
1288
		fi
1289 15333972 Renato Botelho
1290 5163c3ca Renato Botelho
		if [ -n "${_IS_RELEASE}" -o "${_repo_path_param}" = "${CORE_PKG_PATH}" ]; then
1291
			for _pkg_final_rsync_hostname in ${PKG_FINAL_RSYNC_HOSTNAME}; do
1292
				# Send .real* directories first to prevent having a broken repo while transfer happens
1293
				local _cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1294
					--timeout=60 ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1295
					--include=\"/*\" --include=\"*/.real*\" --include=\"*/.real*/***\" \
1296
					--exclude=\"*\" \
1297
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1298
1299
				echo -n ">>> Sending updated packages to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1300
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1301
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1302
					echo "Done!" | tee -a ${_logfile}
1303
				else
1304
					echo "Failed!" | tee -a ${_logfile}
1305
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1306
					print_error_pfS
1307
				fi
1308
1309
				_cmd="rsync -Have \"ssh -p ${PKG_FINAL_RSYNC_SSH_PORT}\" \
1310
					--timeout=60 --delete-delay ${PKG_RSYNC_DESTDIR}/./${_repo_base%%-core}* \
1311
					${PKG_FINAL_RSYNC_USERNAME}@${_pkg_final_rsync_hostname}:${PKG_FINAL_RSYNC_DESTDIR}"
1312
1313
				echo -n ">>> Sending updated repositories metadata to ${_pkg_final_rsync_hostname}... " | tee -a ${_logfile}
1314
				if script -aq ${_logfile} ssh -p ${PKG_RSYNC_SSH_PORT} \
1315
					${PKG_RSYNC_USERNAME}@${_pkg_rsync_hostname} ${_cmd} >/dev/null 2>&1; then
1316
					echo "Done!" | tee -a ${_logfile}
1317
				else
1318
					echo "Failed!" | tee -a ${_logfile}
1319
					echo ">>> ERROR: An error occurred sending repo to final hostname"
1320
					print_error_pfS
1321
				fi
1322
			done
1323 15333972 Renato Botelho
		fi
1324 5163c3ca Renato Botelho
	done
1325 184b39e1 Renato Botelho
}
1326
1327 6f73c362 Renato Botelho
poudriere_possible_archs() {
1328
	local _arch=$(uname -m)
1329 44b30d59 Renato Botelho
	local _archs=""
1330 6f73c362 Renato Botelho
1331 97bc6c78 Phil Davis
	# If host is amd64, we'll create both repos, and if possible armv6
1332 6f73c362 Renato Botelho
	if [ "${_arch}" = "amd64" ]; then
1333 44b30d59 Renato Botelho
		_archs="amd64.amd64"
1334 6f73c362 Renato Botelho
1335
		if [ -f /usr/local/bin/qemu-arm-static ]; then
1336
			# Make sure binmiscctl is ok
1337
			/usr/local/etc/rc.d/qemu_user_static forcestart >/dev/null 2>&1
1338
1339
			if binmiscctl lookup armv6 >/dev/null 2>&1; then
1340
				_archs="${_archs} arm.armv6"
1341
			fi
1342
		fi
1343
	fi
1344
1345 fb192227 Renato Botelho
	if [ -n "${ARCH_LIST}" ]; then
1346
		local _found=0
1347 4010811b Renato Botelho
		for _desired_arch in ${ARCH_LIST}; do
1348 fb192227 Renato Botelho
			_found=0
1349 4010811b Renato Botelho
			for _possible_arch in ${_archs}; do
1350 fb192227 Renato Botelho
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1351
					_found=1
1352
					break
1353
				fi
1354
			done
1355
			if [ ${_found} -eq 0 ]; then
1356
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1357
				print_error_pfS
1358
			fi
1359
		done
1360
		_archs="${ARCH_LIST}"
1361
	fi
1362
1363 6f73c362 Renato Botelho
	echo ${_archs}
1364
}
1365
1366
poudriere_jail_name() {
1367
	local _jail_arch="${1}"
1368
1369
	if [ -z "${_jail_arch}" ]; then
1370
		return 1
1371
	fi
1372
1373 0449a11f Renato Botelho
	# Remove arch
1374 83354dea Renato Botelho
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1375 6f73c362 Renato Botelho
}
1376
1377 a3bb036b Renato Botelho
poudriere_rename_ports() {
1378
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1379
		return;
1380
	fi
1381
1382
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1383
1384
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1385
1386
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1387
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1388
		local _pdir=$(dirname ${d})
1389
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1390 c7e1c621 Renato Botelho
		local _plist=""
1391 a3bb036b Renato Botelho
1392
		if [ -e ${_pdir}/${_pname} ]; then
1393
			rm -rf ${_pdir}/${_pname}
1394
		fi
1395
1396
		cp -r ${d} ${_pdir}/${_pname}
1397
1398 c7e1c621 Renato Botelho
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
1399
			_plist=${_pdir}/${_pname}/pkg-plist
1400
		fi
1401
1402 a3bb036b Renato Botelho
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
1403
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
1404
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
1405
			${_pdir}/${_pname}/Makefile \
1406 c7e1c621 Renato Botelho
			${_pdir}/${_pname}/pkg-descr ${_plist}
1407 a3bb036b Renato Botelho
1408
		# PHP module is special
1409
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
1410
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
1411
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
1412
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
1413
				  -e "s,pfSense,${PRODUCT_NAME},g" \
1414
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
1415
				${_pdir}/${_pname}/files/config.m4
1416
1417
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
1418
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
1419
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
1420
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
1421
				${_pdir}/${_pname}/files/pfSense.c \
1422
				${_pdir}/${_pname}/files/php_pfSense.h
1423
		fi
1424
1425
		if [ -d ${_pdir}/${_pname}/files ]; then
1426
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
1427
				local _fddir=$(dirname ${fd})
1428
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
1429
1430
				mv ${fd} ${_fddir}/${_fdname}
1431
			done
1432
		fi
1433
	done
1434
	echo "Done!" | tee -a ${LOGFILE}
1435
}
1436
1437 6f73c362 Renato Botelho
poudriere_create_ports_tree() {
1438
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1439
1440
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1441
		local _branch=""
1442
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1443
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1444
			print_error_pfS
1445
		fi
1446
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
1447
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
1448
		fi
1449
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
1450 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
1451 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1452
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
1453
			print_error_pfS
1454
		fi
1455
		echo "Done!" | tee -a ${LOGFILE}
1456 a3bb036b Renato Botelho
		poudriere_rename_ports
1457 6f73c362 Renato Botelho
	fi
1458
}
1459
1460
poudriere_init() {
1461
	local _error=0
1462
	local _archs=$(poudriere_possible_archs)
1463
1464
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1465
1466
	# Sanity checks
1467
	if [ -z "${ZFS_TANK}" ]; then
1468
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
1469
		error=1
1470
	fi
1471
1472
	if [ -z "${ZFS_ROOT}" ]; then
1473
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
1474
		error=1
1475
	fi
1476
1477
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
1478
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
1479
		error=1
1480
	fi
1481
1482
	if [ ${_error} -eq 1 ]; then
1483
		print_error_pfS
1484
	fi
1485
1486
	# Check if zpool exists
1487
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
1488
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
1489
		print_error_pfS
1490
	fi
1491
1492
	# Check if zfs rootfs exists
1493
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1494 01541076 Renato Botelho
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
1495 52ec3f66 Renato Botelho
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
1496
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
1497 01541076 Renato Botelho
			echo "Done!"
1498
		else
1499
			echo "Failed!"
1500
			print_error_pfS
1501
		fi
1502 6f73c362 Renato Botelho
	fi
1503
1504
	# Make sure poudriere is installed
1505 ac464524 Renato Botelho
	if ! pkg info --quiet poudriere-devel; then
1506
		echo ">>> Installing poudriere-devel..." | tee -a ${LOGFILE}
1507
		if ! pkg install poudriere-devel >/dev/null 2>&1; then
1508
			echo ">>> ERROR: poudriere-devel was not installed, aborting..." | tee -a ${LOGFILE}
1509 6f73c362 Renato Botelho
			print_error_pfS
1510
		fi
1511
	fi
1512
1513
	# Create poudriere.conf
1514
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
1515
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
1516
		print_error_pfS
1517
	fi
1518
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
1519
	cat <<EOF >/usr/local/etc/poudriere.conf
1520
ZPOOL=${ZFS_TANK}
1521
ZROOTFS=${ZFS_ROOT}
1522
RESOLV_CONF=/etc/resolv.conf
1523
BASEFS=/usr/local/poudriere
1524
USE_PORTLINT=no
1525
USE_TMPFS=yes
1526
NOLINUX=yes
1527 86349bee Renato Botelho
DISTFILES_CACHE=/usr/ports/distfiles
1528 6f73c362 Renato Botelho
CHECK_CHANGED_OPTIONS=yes
1529
CHECK_CHANGED_DEPS=yes
1530
ATOMIC_PACKAGE_REPOSITORY=yes
1531
COMMIT_PACKAGES_ON_FAILURE=no
1532 4fd3649f Renato Botelho
KEEP_OLD_PACKAGES=yes
1533
KEEP_OLD_PACKAGES_COUNT=5
1534 5f64e2ca Renato Botelho
EOF
1535
1536
	# Create specific items conf
1537
	[ ! -d /usr/local/etc/poudriere.d ] \
1538
		&& mkdir -p /usr/local/etc/poudriere.d
1539
1540 fd6cd92b Renato Botelho
	# Create DISTFILES_CACHE if it doesn't exist
1541
	if [ ! -d /usr/ports/distfiles ]; then
1542
		mkdir -p /usr/ports/distfiles
1543
	fi
1544
1545 6f73c362 Renato Botelho
	# Remove old jails
1546
	for jail_arch in ${_archs}; do
1547
		jail_name=$(poudriere_jail_name ${jail_arch})
1548
1549
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1550
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
1551
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
1552
		fi
1553
	done
1554
1555
	# Remove old ports tree
1556
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1557
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
1558
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
1559
	fi
1560
1561
	local native_xtools=""
1562
	# Now we are ready to create jails
1563
	for jail_arch in ${_archs}; do
1564
		jail_name=$(poudriere_jail_name ${jail_arch})
1565
1566
		if [ "${jail_arch}" = "arm.armv6" ]; then
1567
			native_xtools="-x"
1568
		else
1569
			native_xtools=""
1570
		fi
1571
1572
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1573 ac464524 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_BRANCH} \
1574
				-a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE} ${native_xtools} >/dev/null 2>&1; then
1575 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1576
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1577
			print_error_pfS
1578
		fi
1579
		echo "Done!" | tee -a ${LOGFILE}
1580
	done
1581
1582
	poudriere_create_ports_tree
1583
1584
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
1585
}
1586
1587
poudriere_update_jails() {
1588
	local _archs=$(poudriere_possible_archs)
1589
1590
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1591
1592
	local native_xtools=""
1593
	for jail_arch in ${_archs}; do
1594
		jail_name=$(poudriere_jail_name ${jail_arch})
1595
1596 c136dde1 Renato Botelho
		local _create_or_update="-u"
1597 43f6b081 Renato Botelho
		local _create_or_update_text="Updating"
1598 6f73c362 Renato Botelho
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1599 c136dde1 Renato Botelho
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
1600 ac464524 Renato Botelho
			_create_or_update="-c -v ${FREEBSD_BRANCH} -a ${jail_arch} -m git -U ${FREEBSD_REPO_BASE_POUDRIERE}"
1601 43f6b081 Renato Botelho
			_create_or_update_text="Creating"
1602 6f73c362 Renato Botelho
		fi
1603
1604
		if [ "${jail_arch}" = "arm.armv6" ]; then
1605
			native_xtools="-x"
1606
		else
1607
			native_xtools=""
1608
		fi
1609
1610 43f6b081 Renato Botelho
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
1611 ac464524 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" ${native_xtools} >/dev/null 2>&1; then
1612 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
1613 43f6b081 Renato Botelho
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
1614 6f73c362 Renato Botelho
			print_error_pfS
1615
		fi
1616
		echo "Done!" | tee -a ${LOGFILE}
1617
	done
1618
}
1619
1620
poudriere_update_ports() {
1621
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1622
1623
	# Create ports tree if necessary
1624
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
1625
		poudriere_create_ports_tree
1626
	else
1627 97bc6c78 Phil Davis
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1628 d94da298 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
1629 7d26baf3 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
1630 d94da298 Renato Botelho
		echo "Done!" | tee -a ${LOGFILE}
1631 6f73c362 Renato Botelho
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1632
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
1633
		echo "Done!" | tee -a ${LOGFILE}
1634 a3bb036b Renato Botelho
		poudriere_rename_ports
1635 6f73c362 Renato Botelho
	fi
1636
}
1637
1638
poudriere_bulk() {
1639
	local _archs=$(poudriere_possible_archs)
1640
1641
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1642
1643 e295b113 Renato Botelho
	if [ -z "${DO_NOT_UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
1644
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
1645
		print_error_pfS
1646
	fi
1647
1648 26f1087f Renato Botelho
	rm -f ${LOGFILE}
1649
1650 6f73c362 Renato Botelho
	poudriere_create_ports_tree
1651
1652
	[ -d /usr/local/etc/poudriere.d ] || \
1653
		mkdir -p /usr/local/etc/poudriere.d
1654
1655 edef37f6 Renato Botelho
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
1656 39f2cfd1 Renato Botelho
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" \
1657
			/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1658 6f73c362 Renato Botelho
	fi
1659
1660 39f2cfd1 Renato Botelho
	cat <<EOF >>/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
1661
PKG_REPO_BRANCH_DEVEL=${PKG_REPO_BRANCH_DEVEL}
1662
PKG_REPO_BRANCH_RELEASE=${PKG_REPO_BRANCH_RELEASE}
1663
PKG_REPO_SERVER_DEVEL=${PKG_REPO_SERVER_DEVEL}
1664
PKG_REPO_SERVER_RELEASE=${PKG_REPO_SERVER_RELEASE}
1665
POUDRIERE_PORTS_NAME=${POUDRIERE_PORTS_NAME}
1666
PRODUCT_NAME=${PRODUCT_NAME}
1667
EOF
1668
1669 d94da298 Renato Botelho
	# Change version of pfSense meta ports for snapshots
1670
	if [ -z "${_IS_RELEASE}" ]; then
1671 b00400ef Renato Botelho
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
1672 02ec5bd3 Renato Botelho
		sed -i '' \
1673
			-e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
1674
			-e "/^PORTREVISION=/d" \
1675 39f2cfd1 Renato Botelho
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile \
1676
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/Makefile
1677 d94da298 Renato Botelho
	fi
1678
1679 39f2cfd1 Renato Botelho
	# Copy over pkg repo templates to pfSense-repo
1680
	mkdir -p /usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1681 a546d014 Renato Botelho
	cp -f ${PKG_REPO_BASE}/* \
1682 39f2cfd1 Renato Botelho
		/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/sysutils/${PRODUCT_NAME}-repo/files
1683
1684 6f73c362 Renato Botelho
	for jail_arch in ${_archs}; do
1685
		jail_name=$(poudriere_jail_name ${jail_arch})
1686
1687
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
1688
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
1689
			continue
1690
		fi
1691
1692
		if [ -f "${POUDRIERE_BULK}.${jail_arch}" ]; then
1693
			_ref_bulk="${POUDRIERE_BULK}.${jail_arch}"
1694
		else
1695
			_ref_bulk="${POUDRIERE_BULK}"
1696
		fi
1697
1698 83354dea Renato Botelho
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}
1699 6f73c362 Renato Botelho
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
1700
1701 f72d162f Renato Botelho
		local _exclude_bulk="${POUDRIERE_BULK}.exclude.${jail_arch}"
1702
		if [ -f "${_exclude_bulk}" ]; then
1703
			mv ${_bulk} ${_bulk}.tmp
1704
			sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_exclude_bulk} > ${_bulk}.exclude
1705 855d9774 Renato Botelho
			cat ${_bulk}.tmp ${_bulk}.exclude | sort | uniq -u > ${_bulk}
1706
			rm -f ${_bulk}.tmp ${_bulk}.exclude
1707 f72d162f Renato Botelho
		fi
1708
1709 6f73c362 Renato Botelho
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
1710
			echo ">>> ERROR: Something went wrong..."
1711
			print_error_pfS
1712
		fi
1713
1714
		echo ">>> Cleaning up old packages from repo..."
1715
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
1716
			echo ">>> ERROR: Something went wrong..."
1717
			print_error_pfS
1718
		fi
1719
1720 61b63ac0 Renato Botelho
		pkg_repo_rsync "/usr/local/poudriere/data/packages/${jail_name}-${POUDRIERE_PORTS_NAME}"
1721 6f73c362 Renato Botelho
	done
1722
}
1723 ab943fc9 Renato Botelho
1724
# This routine is called to write out to stdout
1725
# a string. The string is appended to $SNAPSHOTSLOGFILE
1726
snapshots_update_status() {
1727 919c8486 Renato Botelho
	if [ -z "$1" ]; then
1728
		return
1729
	fi
1730
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
1731 ab943fc9 Renato Botelho
		return
1732
	fi
1733 0cc93b74 Renato Botelho
	echo "$*"
1734
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
1735 ab943fc9 Renato Botelho
}
1736
1737 97f7a154 Renato Botelho
create_sha256() {
1738
	local _file="${1}"
1739
1740
	if [ ! -f "${_file}" ]; then
1741
		return 1
1742
	fi
1743
1744
	( \
1745
		cd $(dirname ${_file}) && \
1746
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
1747
	)
1748
}
1749
1750 e28305f4 Renato Botelho
snapshots_create_latest_symlink() {
1751
	local _image="${1}"
1752
1753
	if [ -z "${_image}" ]; then
1754
		return
1755
	fi
1756
1757
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
1758
		return
1759
	fi
1760
1761 5a944dd5 Renato Botelho
	if [ ! -f "${_image}" ]; then
1762 a4a336b2 Renato Botelho
		return
1763 e28305f4 Renato Botelho
	fi
1764
1765 695ba439 Renato Botelho
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
1766
	ln -sf $(basename ${_image}) ${_symlink}
1767 e28305f4 Renato Botelho
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
1768
}
1769
1770 f26731b0 Renato Botelho
snapshots_create_sha256() {
1771 87925fd1 Renato Botelho
	local _img=""
1772 ab943fc9 Renato Botelho
1773 b0d0498c Renato Botelho
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH} ${OVAPATH} ${VARIANTIMAGES}; do
1774 f26731b0 Renato Botelho
		if [ -f "${_img}.gz" ]; then
1775
			_img="${_img}.gz"
1776
		fi
1777
		if [ ! -f "${_img}" ]; then
1778 87925fd1 Renato Botelho
			continue
1779
		fi
1780 97f7a154 Renato Botelho
		create_sha256 ${_img}
1781 f26731b0 Renato Botelho
		snapshots_create_latest_symlink ${_img}
1782 87925fd1 Renato Botelho
	done
1783 ab943fc9 Renato Botelho
}
1784
1785
snapshots_scp_files() {
1786 d4c6029e Renato Botelho
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
1787 820b17e1 Renato Botelho
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60"
1788 ab943fc9 Renato Botelho
	fi
1789 48396120 Renato Botelho
1790
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
1791 61b63ac0 Renato Botelho
	pkg_repo_rsync "${CORE_PKG_PATH}"
1792 48396120 Renato Botelho
	snapshots_update_status ">>> Finished copying core pkg repo"
1793
1794 5163c3ca Renato Botelho
	for _rsyncip in ${RSYNCIP}; do
1795
		snapshots_update_status ">>> Copying files to ${_rsyncip}"
1796 ab943fc9 Renato Botelho
1797 5163c3ca Renato Botelho
		# Ensure directory(s) are available
1798
		ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/installer"
1799
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1800
			ssh ${RSYNCUSER}@${_rsyncip} "mkdir -p ${RSYNCPATH}/virtualization"
1801
		fi
1802
		# ensure permissions are correct for r+w
1803
		ssh ${RSYNCUSER}@${_rsyncip} "chmod -R ug+rw ${RSYNCPATH}/."
1804
		rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/installer/* \
1805
			${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/installer/
1806
		if [ -d $IMAGES_FINAL_DIR/virtualization ]; then
1807
			rsync $RSYNC_COPY_ARGUMENTS $IMAGES_FINAL_DIR/virtualization/* \
1808
				${RSYNCUSER}@${_rsyncip}:${RSYNCPATH}/virtualization/
1809
		fi
1810 ab943fc9 Renato Botelho
1811 5163c3ca Renato Botelho
		snapshots_update_status ">>> Finished copying files."
1812
	done
1813 ab943fc9 Renato Botelho
}