Project

General

Profile

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