Project

General

Profile

Download (81.6 KB) Statistics
| Branch: | Tag: | Revision:
1 6f73c362 Renato Botelho
#!/bin/sh
2
#
3
# builder_common.sh
4
#
5
# Copyright (c) 2004-2015 Electric Sheep Fencing, LLC
6
# Copyright (C) 2014 Ermal Luçi
7
# All rights reserved.
8
#
9
# NanoBSD portions of the code
10
# Copyright (c) 2005 Poul-Henning Kamp.
11
# and copied from nanobsd.sh
12
# All rights reserved.
13
#
14
# FreeSBIE portions of the code
15
# Copyright (c) 2005 Dario Freni
16
# and copied from FreeSBIE project
17
# All rights reserved.
18
#
19
# Redistribution and use in source and binary forms, with or without
20
# modification, are permitted provided that the following conditions
21
# are met:
22
#
23
# 1. Redistributions of source code must retain the above copyright
24
#    notice, this list of conditions and the following disclaimer.
25
#
26
# 2. Redistributions in binary form must reproduce the above copyright
27
#    notice, this list of conditions and the following disclaimer in the
28
#    documentation and/or other materials provided with the distribution.
29
#
30
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
31
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
34
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
# OF THE POSSIBILITY OF SUCH DAMAGE.
42
# Redistribution and use in source and binary forms, with or without
43
# modification, are permitted provided that the following conditions are met:
44
#
45
46 04992be2 Renato Botelho
if [ -n "${IMAGES_FINAL_DIR}" -a "${IMAGES_FINAL_DIR}" != "/" ]; then
47
	mkdir -p ${IMAGES_FINAL_DIR}
48 6f73c362 Renato Botelho
else
49 04992be2 Renato Botelho
	echo "IMAGES_FINAL_DIR is not defined"
50 6f73c362 Renato Botelho
	print_error_pfS
51
fi
52
53
lc() {
54
	echo "${1}" | tr '[[:upper:]]' '[[:lower:]]'
55
}
56
57
git_last_commit() {
58 0b0ef57e Renato Botelho
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
59
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
60 78b0f246 Renato Botelho
	echo ">>> Last known commit $CURRENT_AUTHOR - $CURRENT_COMMIT"
61
	echo "$CURRENT_COMMIT" > $SCRATCHDIR/build_commit_info.txt
62 6f73c362 Renato Botelho
}
63
64 98978a36 Renato Botelho
# Create core pkg repository
65
core_pkg_create_repo() {
66 da781042 Renato Botelho
	if [ ! -d "${CORE_PKG_REAL_PATH}/All" ]; then
67 98978a36 Renato Botelho
		return
68
	fi
69
70 da781042 Renato Botelho
	############ ATTENTION ##############
71
	#
72
	# For some reason pkg-repo fail without / in the end of directory name
73
	# so removing it will break command
74
	#
75
	# https://github.com/freebsd/pkg/issues/1364
76
	#
77 98978a36 Renato Botelho
	echo -n ">>> Creating core packages repository... "
78 da781042 Renato Botelho
	if pkg repo -q "${CORE_PKG_REAL_PATH}/"; then
79 98978a36 Renato Botelho
		echo "Done!"
80
	else
81
		echo "Failed!"
82
		print_error_pfS
83
	fi
84 da781042 Renato Botelho
85
	# Use the same directory structure as poudriere does to avoid
86
	# breaking snapshot repositories during rsync
87
	ln -sf $(basename ${CORE_PKG_REAL_PATH}) ${CORE_PKG_PATH}/.latest
88
	ln -sf .latest/All ${CORE_PKG_PATH}/All
89
	ln -sf .latest/digests.txz ${CORE_PKG_PATH}/digests.txz
90
	ln -sf .latest/meta.txz ${CORE_PKG_PATH}/meta.txz
91
	ln -sf .latest/packagesite.txz ${CORE_PKG_PATH}/packagesite.txz
92 98978a36 Renato Botelho
}
93
94 6f73c362 Renato Botelho
# Create core pkg (base, kernel)
95
core_pkg_create() {
96
	local _template="${1}"
97
	local _flavor="${2}"
98
	local _version="${3}"
99
	local _root="${4}"
100 a9ae8dac Renato Botelho
	local _filter="${5}"
101 6f73c362 Renato Botelho
102
	[ -d "${CORE_PKG_TMP}" ] \
103
		&& rm -rf ${CORE_PKG_TMP}
104
105 0ce2a7f3 Renato Botelho
	local _templates_path=${BUILDER_TOOLS}/templates/core_pkg/${_template}
106 6f73c362 Renato Botelho
	local _template_metadir=${_templates_path}/metadir
107
	local _metadir=${CORE_PKG_TMP}/${_template}_metadir
108
109
	if [ ! -d ${_template_metadir} ]; then
110
		echo "ERROR: Template dir not found for pkg ${_template}"
111
		exit
112
	fi
113
114
	mkdir -p ${CORE_PKG_TMP}
115
116
	cp -r ${_template_metadir} ${_metadir}
117
118
	local _manifest=${_metadir}/+MANIFEST
119
	local _plist=${CORE_PKG_TMP}/${_template}_plist
120
	local _exclude_plist=${CORE_PKG_TMP}/${_template}_exclude_plist
121
122
	if [ -f "${_templates_path}/pkg-plist" ]; then
123
		cp ${_templates_path}/pkg-plist ${_plist}
124
	else
125 a9ae8dac Renato Botelho
		if [ -n "${_filter}" ]; then
126
			_filter="-name ${_filter}"
127
		fi
128
		(cd ${_root} && find . ${_filter} -type f -or -type l | sed 's,^.,,' | sort -u) > ${_plist}
129 6f73c362 Renato Botelho
	fi
130
131
	if [ -f "${_templates_path}/exclude_plist" ]; then
132
		cp ${_templates_path}/exclude_plist ${_exclude_plist}
133
	else
134
		touch ${_exclude_plist}
135
	fi
136
137
	sed \
138
		-i '' \
139
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
140
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
141 a326d8a1 Renato Botelho
		-e "s,%%FLAVOR%%,${_flavor:+-}${_flavor},g" \
142 a069d03f Renato Botelho
		-e "s,%%FLAVOR_DESC%%,${_flavor:+ (${_flavor})},g" \
143 6f73c362 Renato Botelho
		-e "s,%%VERSION%%,${_version},g" \
144
		${_metadir}/* \
145
		${_plist} \
146
		${exclude_plist}
147
148
	if [ -f "${_exclude_plist}" ]; then
149
		sort -u ${_exclude_plist} > ${_plist}.exclude
150
		mv ${_plist} ${_plist}.tmp
151
		comm -23 ${_plist}.tmp ${_plist}.exclude > ${_plist}
152
		rm -f ${_plist}.tmp ${plist}.exclude
153
	fi
154
155 10983687 Renato Botelho
	# Add license information
156
	local _portname=$(sed '/^name: /!d; s,^[^"]*",,; s,",,' ${_metadir}/+MANIFEST)
157
	local _licenses_dir="/usr/local/share/licenses/${_portname}-${_version}"
158
	mkdir -p ${_root}${_licenses_dir}
159
	cp ${BUILDER_ROOT}/license.txt ${_root}${_licenses_dir}/ESF
160
	echo "This package has a single license: ESF (Electric Sheep Fencing License)." \
161
		> ${_root}${_licenses_dir}/LICENSE
162
	cat <<EOF >${_root}${_licenses_dir}/catalog.mk
163
_LICENSE=ESF
164
_LICENSE_NAME=Electric Sheep Fencing License
165
_LICENSE_PERMS=dist-mirror dist-sell pkg-mirror pkg-sell auto-accept
166
_LICENSE_GROUPS=
167
_LICENSE_DISTFILES=
168
EOF
169
	cat <<EOF >>${_plist}
170
${_licenses_dir}/catalog.mk
171
${_licenses_dir}/LICENSE
172
${_licenses_dir}/ESF
173
EOF
174
175 da781042 Renato Botelho
	mkdir -p ${CORE_PKG_REAL_PATH}/All
176
	if ! pkg create -o ${CORE_PKG_REAL_PATH}/All -p ${_plist} -r ${_root} -m ${_metadir}; then
177 6f73c362 Renato Botelho
		echo ">>> ERROR: Error building package ${_template} ${_flavor}"
178
		print_error_pfS
179
	fi
180
}
181
182
# This routine will output that something went wrong
183
print_error_pfS() {
184
	echo
185
	echo "####################################"
186
	echo "Something went wrong, check errors!" >&2
187
	echo "####################################"
188
	echo
189
	echo "NOTE: a lot of times you can run './build.sh --clean-builder' to resolve."
190
	echo
191
	if [ "$1" != "" ]; then
192
		echo $1
193
	fi
194 d4c6029e Renato Botelho
	[ -n "${LOGFILE}" -a -f "${LOGFILE}" ] && \
195 6f73c362 Renato Botelho
		echo "Log saved on ${LOGFILE}" && \
196
		tail -n20 ${LOGFILE} >&2
197
	echo
198
	kill $$
199
	exit 1
200
}
201
202
prestage_on_ram_setup() {
203 6064020a Renato Botelho
	# Do not use memory disks for release build
204
	if [ -n "${_IS_RELEASE}" ]; then
205
		return
206
	fi
207
208 f546e6ca Renato Botelho
	[ -d "${STAGE_CHROOT_DIR}" ] \
209
		|| mkdir -p ${STAGE_CHROOT_DIR}
210
	[ -d "${FINAL_CHROOT_DIR}" ] \
211
		|| mkdir -p ${FINAL_CHROOT_DIR}
212
213 6f73c362 Renato Botelho
	_AVAIL_MEM=$(($(sysctl -n hw.usermem) / 1024 / 1024))
214
	if [ $_AVAIL_MEM -lt 2000 ]; then
215
		echo ">>> Builder has less than 2GiB RAM skipping memory disks"
216
		return
217
	else
218
		echo "######################################################################################"
219
		echo
220 82c6f31a Renato Botelho
		echo ">>> Builder has more than 2GiB RAM enabling memory disks"
221 6f73c362 Renato Botelho
		echo ">>> WARNING: Remember to remove these memory disks by running $0 --disable-memorydisks"
222
		echo
223
		echo "######################################################################################"
224
	fi
225
226
	if df /dev/ufs/prestagebacking >/dev/null 2>&1; then
227
		echo ">>> Detected preexisting memory disk enabled for ${STAGE_CHROOT_DIR}."
228
	else
229
		mdconfig -a -t swap -u 10001 -s ${MEMORYDISK_SIZE}
230
		newfs -L prestagebacking -U /dev/md10001
231
		mount /dev/ufs/prestagebacking ${STAGE_CHROOT_DIR}
232
	fi
233
234
	if df /dev/ufs/finalstagebacking >/dev/null 2>&1; then
235
		echo ">>> Detected preexisting memory disk enabled for ${FINAL_CHROOT_DIR}."
236
	else
237
		mdconfig -a -t swap -u 10002 -s ${MEMORYDISK_SIZE}
238
		newfs -L finalstagebacking -U /dev/md10002
239
		mount /dev/ufs/finalstagebacking ${FINAL_CHROOT_DIR}
240
	fi
241
}
242
243
prestage_on_ram_cleanup() {
244
	if [ -c /dev/md10001 ]; then
245
		if [ -d ${STAGE_CHROOT_DIR} ]; then
246
			umount ${STAGE_CHROOT_DIR}
247
		fi
248
		mdconfig -d -u 10001
249
	fi
250
	if [ -c /dev/md10002 ]; then
251
		if [ -d ${FINAL_CHROOT_DIR} ]; then
252
			umount ${FINAL_CHROOT_DIR}
253
		fi
254
		mdconfig -d -u 10002
255
	fi
256
}
257
258
# This routine will verify that the kernel has been
259
# installed OK to the staging area.
260
ensure_kernel_exists() {
261
	if [ ! -f "$1/boot/kernel/kernel.gz" ]; then
262
		echo ">>> ERROR: Could not locate $1/boot/kernel.gz"
263
		print_error_pfS
264
	fi
265
	KERNEL_SIZE=$(stat -f "%z" $1/boot/kernel/kernel.gz)
266
	if [ "$KERNEL_SIZE" -lt 3500 ]; then
267
		echo ">>> ERROR: Kernel $1/boot/kernel.gz appears to be smaller than it should be: $KERNEL_SIZE"
268
		print_error_pfS
269
	fi
270
}
271
272
get_pkg_name() {
273
	echo "${PRODUCT_NAME}-${1}-${CORE_PKG_VERSION}"
274
}
275
276
# This routine builds all related kernels
277
build_all_kernels() {
278
	# Set KERNEL_BUILD_PATH if it has not been set
279
	if [ -z "${KERNEL_BUILD_PATH}" ]; then
280
		KERNEL_BUILD_PATH=$SCRATCHDIR/kernels
281
		echo ">>> KERNEL_BUILD_PATH has not been set. Setting to ${KERNEL_BUILD_PATH}!"
282
	fi
283
284
	[ -d "${KERNEL_BUILD_PATH}" ] \
285
		&& rm -rf ${KERNEL_BUILD_PATH}
286
287
	# Build embedded kernel
288
	for BUILD_KERNEL in $BUILD_KERNELS; do
289
		unset KERNCONF
290
		unset KERNEL_DESTDIR
291
		unset KERNELCONF
292
		unset KERNEL_NAME
293
		export KERNCONF=$BUILD_KERNEL
294
		export KERNEL_DESTDIR="$KERNEL_BUILD_PATH/$BUILD_KERNEL"
295 64f27203 Renato Botelho
		export KERNELCONF="${FREEBSD_SRC_DIR}/sys/${TARGET}/conf/$BUILD_KERNEL"
296 6f73c362 Renato Botelho
		export KERNEL_NAME=${BUILD_KERNEL}
297
298
		LOGFILE="${BUILDER_LOGS}/kernel.${KERNCONF}.${TARGET}.log"
299
		echo ">>> Building $BUILD_KERNEL kernel."  | tee -a ${LOGFILE}
300
301 64f27203 Renato Botelho
		if [ ! -e "${FREEBSD_SRC_DIR}/sys/${TARGET}/conf/${BUILD_KERNEL}" ]; then
302 6f73c362 Renato Botelho
			echo ">>> ERROR: Could not find $KERNELCONF"
303
			print_error_pfS
304
		fi
305
306 da781042 Renato Botelho
		if [ -n "${NO_BUILDKERNEL}" -a -f "${CORE_PKG_REAL_PATH}/All/$(get_pkg_name kernel-${KERNEL_NAME}).txz" ]; then
307 6f73c362 Renato Botelho
			echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
308
			continue
309
		fi
310
311
		export SRC_CONF=${SRC_CONF}
312
		buildkernel
313
314
		echo ">>> Staging $BUILD_KERNEL kernel..." | tee -a ${LOGFILE}
315
		installkernel
316
317
		ensure_kernel_exists $KERNEL_DESTDIR
318
319 a9ae8dac Renato Botelho
		echo -n ">>> Creating pkg of $KERNEL_NAME-debug kernel to staging area..."  | tee -a ${LOGFILE}
320
		core_pkg_create kernel-debug ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR} \*.symbols
321
		find ${KERNEL_DESTDIR} -name '*.symbols' -type f -delete
322
323 6f73c362 Renato Botelho
		echo -n ">>> Creating pkg of $KERNEL_NAME kernel to staging area..."  | tee -a ${LOGFILE}
324
		core_pkg_create kernel ${KERNEL_NAME} ${CORE_PKG_VERSION} ${KERNEL_DESTDIR}
325
326
		rm -rf $KERNEL_DESTDIR 2>&1 1>/dev/null
327
328
		echo ".Done" | tee -a ${LOGFILE}
329
	done
330
}
331
332
install_default_kernel() {
333
	if [ -z "${1}" ]; then
334
		echo ">>> ERROR: install_default_kernel called without a kernel config name"| tee -a ${LOGFILE}
335
		print_error_pfS
336
	fi
337
338
	export KERNEL_NAME="${1}"
339
340
	echo -n ">>> Installing kernel to be used by image ${KERNEL_NAME}..." | tee -a ${LOGFILE}
341
342
	# Copy kernel package to chroot, otherwise pkg won't find it to install
343
	if ! pkg_chroot_add ${FINAL_CHROOT_DIR} kernel-${KERNEL_NAME}; then
344
		echo ">>> ERROR: Error installing kernel package $(get_pkg_name kernel-${KERNEL_NAME}).txz" | tee -a ${LOGFILE}
345
		print_error_pfS
346
	fi
347
348
	# Lock kernel to avoid user end up removing it for any reason
349
	pkg_chroot ${FINAL_CHROOT_DIR} lock -q -y $(get_pkg_name kernel-${KERNEL_NAME})
350
351
	if [ ! -f $FINAL_CHROOT_DIR/boot/kernel/kernel.gz ]; then
352
		echo ">>> ERROR: No kernel installed on $FINAL_CHROOT_DIR and the resulting image will be unusable. STOPPING!" | tee -a ${LOGFILE}
353
		print_error_pfS
354
	fi
355
	mkdir -p $FINAL_CHROOT_DIR/pkgs
356
	if [ -z "${2}" -o -n "${INSTALL_EXTRA_KERNELS}" ]; then
357 da781042 Renato Botelho
		cp ${CORE_PKG_REAL_PATH}/All/$(get_pkg_name kernel-${KERNEL_NAME}).txz $FINAL_CHROOT_DIR/pkgs
358 6f73c362 Renato Botelho
		if [ -n "${INSTALL_EXTRA_KERNELS}" ]; then
359
			for _EXTRA_KERNEL in $INSTALL_EXTRA_KERNELS; do
360 da781042 Renato Botelho
				_EXTRA_KERNEL_PATH=${CORE_PKG_REAL_PATH}/All/$(get_pkg_name kernel-${_EXTRA_KERNEL}).txz
361 6f73c362 Renato Botelho
				if [ -f "${_EXTRA_KERNEL_PATH}" ]; then
362
					echo -n ". adding ${_EXTRA_KERNEL_PATH} on image /pkgs folder"
363
					cp ${_EXTRA_KERNEL_PATH} $FINAL_CHROOT_DIR/pkgs
364
				else
365
					echo ">>> ERROR: Requested kernel $(get_pkg_name kernel-${_EXTRA_KERNEL}).txz was not found to be put on image /pkgs folder!"
366
					print_error_pfS
367
				fi
368
			done
369
		fi
370
	fi
371
	echo "Done." | tee -a ${LOGFILE}
372
373
	unset KERNEL_NAME
374
}
375
376
# Creates a full update file
377
create_Full_update_tarball() {
378
	mkdir -p $UPDATESDIR
379
380
	customize_stagearea_for_image "fullupdate"
381
	install_default_kernel ${DEFAULT_KERNEL}
382
383
	rm -rf ${FINAL_CHROOT_DIR}/cf
384
	rm -rf ${FINAL_CHROOT_DIR}/conf
385 e02a5d8c Renato Botelho
	rm -f ${FINAL_CHROOT_DIR}/etc/dh-parameters.*
386 6f73c362 Renato Botelho
	rm -f ${FINAL_CHROOT_DIR}/etc/rc.conf
387
	rm -f ${FINAL_CHROOT_DIR}/etc/pwd.db 2>/dev/null
388
	rm -f ${FINAL_CHROOT_DIR}/etc/group 2>/dev/null
389
	rm -f ${FINAL_CHROOT_DIR}/etc/spwd.db 2>/dev/null
390
	rm -f ${FINAL_CHROOT_DIR}/etc/passwd 2>/dev/null
391
	rm -f ${FINAL_CHROOT_DIR}/etc/master.passwd 2>/dev/null
392
	rm -f ${FINAL_CHROOT_DIR}/etc/fstab 2>/dev/null
393
	rm -f ${FINAL_CHROOT_DIR}/etc/bogons 2>/dev/null
394
	# Remove loader.conf and friends.  Ticket #560
395
	rm ${FINAL_CHROOT_DIR}/boot/loader.conf 2>/dev/null
396
	rm ${FINAL_CHROOT_DIR}/boot/loader.conf.local 2>/dev/null
397
398 f3fff72f Renato Botelho
	# Old systems will run (pre|post)_upgrade_command from /tmp
399 651f440c Renato Botelho
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command ]; then
400 20680ab0 Renato Botelho
		cp -p \
401 651f440c Renato Botelho
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/pre_upgrade_command \
402 20680ab0 Renato Botelho
			${FINAL_CHROOT_DIR}/tmp
403
	fi
404 651f440c Renato Botelho
	if [ -f ${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command ]; then
405 f3fff72f Renato Botelho
		cp -p \
406 651f440c Renato Botelho
			${FINAL_CHROOT_DIR}${PRODUCT_SHARE_DIR}/post_upgrade_command \
407 f3fff72f Renato Botelho
			${FINAL_CHROOT_DIR}/tmp
408
	fi
409 20680ab0 Renato Botelho
410 6f73c362 Renato Botelho
	echo ">>> Creating ${UPDATES_TARBALL_FILENAME} ..." | tee -a ${LOGFILE}
411
	tar --exclude=./dev -czPf ${UPDATES_TARBALL_FILENAME} -C ${FINAL_CHROOT_DIR} .
412
}
413
414
# Outputs various set variables aka env
415
print_flags() {
416
417
	echo
418
	printf "             Product version: %s\n" $PRODUCT_VERSION
419
	printf "                   Stage DIR: %s\n" $STAGE_CHROOT_DIR
420
	printf "                 Updates dir: %s\n" $UPDATESDIR
421
	printf " Image Preparation Stage DIR: %s\n" $FINAL_CHROOT_DIR
422 64f27203 Renato Botelho
	printf "                  Source DIR: %s\n" $FREEBSD_SRC_DIR
423 6f73c362 Renato Botelho
	printf "          FreeBSD repository: %s\n" $FREEBSD_REPO_BASE
424
	printf "          FreeBSD-src branch: %s\n" $FREEBSD_BRANCH
425
	printf "     FreeBSD original branch: %s\n" $FREEBSD_PARENT_BRANCH
426
	printf "               BUILD_KERNELS: %s\n" $BUILD_KERNELS
427
	printf "           Git Branch or Tag: %s\n" $GIT_REPO_BRANCH_OR_TAG
428
	printf "            MODULES_OVERRIDE: %s\n" $MODULES_OVERRIDE
429 502db0bd Renato Botelho
	printf "    VMDK_DISK_CAPACITY_IN_GB: %s\n" $VMDK_DISK_CAPACITY_IN_GB
430 173fa93f Renato Botelho
	printf "                 OVFTEMPLATE: %s\n" $OVFTEMPLATE
431 6f73c362 Renato Botelho
	printf "                     OVFVMDK: %s\n" $OVFVMDK
432
	printf "                    SRC_CONF: %s\n" $SRC_CONF
433
	printf "                     ISOPATH: %s\n" $ISOPATH
434
	printf "                MEMSTICKPATH: %s\n" $MEMSTICKPATH
435
	printf "    UPDATES_TARBALL_FILENAME: %s\n" $UPDATES_TARBALL_FILENAME
436
if [ -n "$SHOW_ENV" ]; then
437
	for LINE in $(env | egrep -v '(terminal|PASS|NAME|USER|SSH|GROUP|HOST)'); do
438
		echo "SHOW_ENV: $LINE"
439
	done
440
fi
441
	echo
442
}
443
444
# This builds FreeBSD (make buildworld)
445
# Imported from FreeSBIE
446
make_world() {
447
448
	LOGFILE=${BUILDER_LOGS}/buildworld.${TARGET}
449 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" ]; then
450 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD set, skipping build" | tee -a ${LOGFILE}
451
		return
452
	fi
453
454
	# Set SRC_CONF variable if it's not already set.
455 d4c6029e Renato Botelho
	if [ -z "${SRC_CONF}" ]; then
456 6f73c362 Renato Botelho
		echo ">>> SRC_CONF is unset make sure this is what you want!" | tee -a ${LOGFILE}
457
	else
458
		echo ">>> Setting SRC_CONF to $SRC_CONF" | tee -a ${LOGFILE}
459
	fi
460
461
	# Set default parameters
462 d4c6029e Renato Botelho
	export MAKE_ARGS="${MAKEJ_WORLD} __MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF} TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
463 6f73c362 Renato Botelho
464
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
465
	makeargs="${MAKE_ARGS}"
466
	echo ">>> Building world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
467 d4c6029e Renato Botelho
	echo ">>> Builder is running the command: env LOCAL_ITOOLS=\"${EXTRA_TOOLS}\" script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} -DNO_CLEAN ${makeargs} buildworld" | tee -a ${LOGFILE}
468
	(env LOCAL_ITOOLS="${EXTRA_TOOLS}" script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} -DNO_CLEAN ${makeargs} buildworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
469 6f73c362 Renato Botelho
	echo ">>> Building world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
470
471
	LOGFILE=${BUILDER_LOGS}/installworld.${TARGET}
472
	echo ">>> LOGFILE set to $LOGFILE." | tee -a ${LOGFILE}
473
	# Create if cleaned up
474
	makeargs="${MAKE_ARGS} DESTDIR=${STAGE_CHROOT_DIR} WITHOUT_TOOLCHAIN=1"
475
	echo ">>> Installing world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
476 d4c6029e Renato Botelho
	echo ">>> Builder is running the command: env LOCAL_ITOOLS=\"${EXTRA_TOOLS}\" script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld" | tee -a ${LOGFILE}
477
	(env LOCAL_ITOOLS="${EXTRA_TOOLS}" script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installworld || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
478 6f73c362 Renato Botelho
	echo ">>> Installing world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
479
480
	makeargs="${MAKE_ARGS} DESTDIR=${STAGE_CHROOT_DIR}"
481
	echo ">>> Distribution world for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
482 d4c6029e Renato Botelho
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution " | tee -a ${LOGFILE}
483
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} distribution  || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
484 6f73c362 Renato Botelho
	echo ">>> Distribution world for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
485
486
	[ -d "${STAGE_CHROOT_DIR}/usr/local/bin" ] \
487
		|| mkdir -p ${STAGE_CHROOT_DIR}/usr/local/bin
488
	makeargs="${MAKE_ARGS} DESTDIR=${STAGE_CHROOT_DIR}"
489
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Starting - $(LC_ALL=C date))" | tee -a ${LOGFILE}
490 d4c6029e Renato Botelho
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install " | tee -a ${LOGFILE}
491
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/crypto ${makeargs} clean all install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
492
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean" | tee -a ${LOGFILE}
493
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} clean || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
494
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} all" | tee -a ${LOGFILE}
495
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} all || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
496
	echo ">>> Builder is running the command: script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} install" | tee -a ${LOGFILE}
497
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR}/tools/tools/ath/athstats ${makeargs} install || print_error_pfS;) | egrep '^>>>' | tee -a ${LOGFILE}
498 6f73c362 Renato Botelho
	echo ">>> Building and installing crypto tools and athstats for ${TARGET} architecture... (Finished - $(LC_ALL=C date))" | tee -a ${LOGFILE}
499
500
	unset makeargs
501
}
502
503 acd5c0cd Renato Botelho
nanobsd_image_filename() {
504
	local _size="$1"
505
	local _type="$2"
506
	local _upgrade="$3"
507
508 96f22d87 Renato Botelho
	if [ -z "$_upgrade" ]; then
509 acd5c0cd Renato Botelho
		local _template=${NANOBSD_IMG_TEMPLATE}
510
	else
511
		local _template=${NANOBSD_UPGRADE_TEMPLATE}
512
	fi
513
514
	echo "$_template" | sed \
515
		-e "s,%%SIZE%%,${_size},g" \
516
		-e "s,%%TYPE%%,${_type},g"
517
518
	return 0
519
}
520
521 6f73c362 Renato Botelho
# This routine originated in nanobsd.sh
522
nanobsd_set_flash_details () {
523
	a1=$(echo $1 | tr '[:upper:]' '[:lower:]')
524
525
	# Source:
526
	#	SanDisk CompactFlash Memory Card
527
	#	Product Manual
528
	#	Version 10.9
529
	#	Document No. 20-10-00038
530
	#	April 2005
531
	# Table 2-7
532
	# NB: notice math error in SDCFJ-4096-388 line.
533
	#
534
	case "${a1}" in
535
		2048|2048m|2048mb|2g)
536
			NANO_MEDIASIZE=$((1989999616/512))
537
			;;
538
		4096|4096m|4096mb|4g)
539
			NANO_MEDIASIZE=$((3989999616/512))
540
			;;
541
		8192|8192m|8192mb|8g)
542
			NANO_MEDIASIZE=$((7989999616/512))
543
			;;
544
		16384|16384m|16384mb|16g)
545
			NANO_MEDIASIZE=$((15989999616/512))
546
			;;
547
		*)
548
			echo "Unknown Flash capacity"
549
			exit 2
550
			;;
551
	esac
552
553
	NANO_HEADS=16
554
	NANO_SECTS=63
555
556
	echo ">>> [nanoo] $1"
557
	echo ">>> [nanoo] NANO_MEDIASIZE: $NANO_MEDIASIZE"
558
	echo ">>> [nanoo] NANO_HEADS: $NANO_HEADS"
559
	echo ">>> [nanoo] NANO_SECTS: $NANO_SECTS"
560
	echo ">>> [nanoo] NANO_BOOT0CFG: $NANO_BOOT0CFG"
561
}
562
563
# This routine originated in nanobsd.sh
564
create_nanobsd_diskimage () {
565
	if [ -z "${1}" ]; then
566
		echo ">>> ERROR: Type of image has not been specified"
567
		print_error_pfS
568
	fi
569
	if [ -z "${2}" ]; then
570
		echo ">>> ERROR: Size of image has not been specified"
571
		print_error_pfS
572
	fi
573
574
	if [ "${1}" = "nanobsd" ]; then
575
		# It's serial
576
		export NANO_BOOTLOADER="boot/boot0sio"
577
	elif [ "${1}" = "nanobsd-vga" ]; then
578
		# It's vga
579
		export NANO_BOOTLOADER="boot/boot0"
580
	else
581
		echo ">>> ERROR: Type of image to create unknown"
582
		print_error_pfS
583
	fi
584
585
	if [ -z "${2}" ]; then
586
		echo ">>> ERROR: Media size(s) not specified."
587
		print_error_pfS
588
	fi
589
590
	if [ -z "${2}" ]; then
591
		echo ">>> ERROR: FLASH_SIZE is not set."
592
		print_error_pfS
593
	fi
594
595
	LOGFILE=${BUILDER_LOGS}/${1}.${TARGET}
596
	# Prepare folder to be put in image
597
	customize_stagearea_for_image "${1}"
598
	install_default_kernel ${DEFAULT_KERNEL} "no"
599
600 92135bfd Renato Botelho
	echo ">>> Fixing up NanoBSD Specific items..." | tee -a ${LOGFILE}
601
602
	echo "nanobsd" > $FINAL_CHROOT_DIR/etc/platform
603
604
	local BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
605
	local LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
606
607
	if [ "${1}" = "nanobsd" ]; then
608
		# Tell loader to use serial console early.
609
		echo "-S115200 -h" >> ${BOOTCONF}
610
611 9dcb68a4 Renato Botelho
		# Remove old console options if present.
612
		[ -f "${LOADERCONF}" ] \
613
			&& sed -i "" -Ee "/(console|boot_multicons|boot_serial|hint.uart)/d" ${LOADERCONF}
614
		# Activate serial console+video console in loader.conf
615
		echo 'loader_color="NO"' >> ${LOADERCONF}
616
		echo 'beastie_disable="YES"' >> ${LOADERCONF}
617
		echo 'boot_serial="YES"' >> ${LOADERCONF}
618
		echo 'console="comconsole"' >> ${LOADERCONF}
619
		echo 'comconsole_speed="115200"' >> ${LOADERCONF}
620
	fi
621 a9ba7be8 Renato Botelho
	echo 'autoboot_delay="5"' >> ${LOADERCONF}
622 6f73c362 Renato Botelho
623
	for _NANO_MEDIASIZE in ${2}; do
624
		if [ -z "${_NANO_MEDIASIZE}" ]; then
625
			continue;
626
		fi
627
628
		echo ">>> building NanoBSD(${1}) disk image with size ${_NANO_MEDIASIZE} for platform (${TARGET})..." | tee -a ${LOGFILE}
629
		echo "" > $BUILDER_LOGS/nanobsd_cmds.sh
630
631 acd5c0cd Renato Botelho
		IMG="${IMAGES_FINAL_DIR}/$(nanobsd_image_filename ${_NANO_MEDIASIZE} ${1})"
632
		IMGUPDATE="${IMAGES_FINAL_DIR}/$(nanobsd_image_filename ${_NANO_MEDIASIZE} ${1} 1)"
633 6f73c362 Renato Botelho
634
		nanobsd_set_flash_details ${_NANO_MEDIASIZE}
635
636
		# These are defined in FlashDevice and on builder_default.sh
637
		echo $NANO_MEDIASIZE \
638
			$NANO_IMAGES \
639
			$NANO_SECTS \
640
			$NANO_HEADS \
641
			$NANO_CODESIZE \
642
			$NANO_CONFSIZE \
643
			$NANO_DATASIZE |
644
awk '
645
{
646
	printf "# %s\n", $0
647
648
	# size of cylinder in sectors
649
	cs = $3 * $4
650
651
	# number of full cylinders on media
652
	cyl = int ($1 / cs)
653
654
	# output fdisk geometry spec, truncate cyls to 1023
655
	if (cyl <= 1023)
656
		print "g c" cyl " h" $4 " s" $3
657
	else
658
		print "g c" 1023 " h" $4 " s" $3
659
660
	if ($7 > 0) {
661
		# size of data partition in full cylinders
662
		dsl = int (($7 + cs - 1) / cs)
663
	} else {
664
		dsl = 0;
665
	}
666
667
	# size of config partition in full cylinders
668
	csl = int (($6 + cs - 1) / cs)
669
670
	if ($5 == 0) {
671
		# size of image partition(s) in full cylinders
672
		isl = int ((cyl - dsl - csl) / $2)
673
	} else {
674
		isl = int (($5 + cs - 1) / cs)
675
	}
676
677
	# First image partition start at second track
678
	print "p 1 165 " $3, isl * cs - $3
679
	c = isl * cs;
680
681
	# Second image partition (if any) also starts offset one
682
	# track to keep them identical.
683
	if ($2 > 1) {
684
		print "p 2 165 " $3 + c, isl * cs - $3
685
		c += isl * cs;
686
	}
687
688
	# Config partition starts at cylinder boundary.
689
	print "p 3 165 " c, csl * cs
690
	c += csl * cs
691
692
	# Data partition (if any) starts at cylinder boundary.
693
	if ($7 > 0) {
694
		print "p 4 165 " c, dsl * cs
695
	} else if ($7 < 0 && $1 > c) {
696
		print "p 4 165 " c, $1 - c
697
	} else if ($1 < c) {
698
		print "Disk space overcommitted by", \
699
		    c - $1, "sectors" > "/dev/stderr"
700
		exit 2
701
	}
702
703
	# Force slice 1 to be marked active. This is necessary
704
	# for booting the image from a USB device to work.
705
	print "a 1"
706
}
707 04992be2 Renato Botelho
	' > ${IMAGES_FINAL_DIR}/_.fdisk
708 6f73c362 Renato Botelho
709 04992be2 Renato Botelho
		MNT=${IMAGES_FINAL_DIR}/_.mnt
710 6f73c362 Renato Botelho
		mkdir -p ${MNT}
711
712
		dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
713
			count=0 seek=$((${NANO_MEDIASIZE}/${NANO_SECTS})) 2>&1 >> ${LOGFILE}
714
715
		MD=$(mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} -y ${NANO_HEADS})
716
		trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
717
718 04992be2 Renato Botelho
		fdisk -i -f ${IMAGES_FINAL_DIR}/_.fdisk ${MD} 2>&1 >> ${LOGFILE}
719 6f73c362 Renato Botelho
		fdisk ${MD} 2>&1 >> ${LOGFILE}
720
721 151d1591 Chris Buechler
		boot0cfg -t 100 -B -b ${FINAL_CHROOT_DIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 2>&1 >> ${LOGFILE}
722 6f73c362 Renato Botelho
723
		# Create first image
724
		bsdlabel -m i386 -w -B -b ${FINAL_CHROOT_DIR}/boot/boot ${MD}s1 2>&1 >> ${LOGFILE}
725
		bsdlabel -m i386 ${MD}s1 2>&1 >> ${LOGFILE}
726
		local _label=$(lc ${PRODUCT_NAME})
727
		newfs -L ${_label}0 ${NANO_NEWFS} /dev/${MD}s1a 2>&1 >> ${LOGFILE}
728
		mount /dev/ufs/${_label}0 ${MNT}
729
		if [ $? -ne 0 ]; then
730
			echo ">>> ERROR: Something wrong happened during mount of first slice image creation. STOPPING!" | tee -a ${LOGFILE}
731
			print_error_pfS
732
		fi
733
		# Consider the unmounting as well
734
		trap "umount /dev/ufs/${_label}0; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
735
736
		clone_directory_contents ${FINAL_CHROOT_DIR} ${MNT}
737
738
		# Set NanoBSD image size
739
		echo "${_NANO_MEDIASIZE}" > ${MNT}/etc/nanosize.txt
740
741
		echo "/dev/ufs/${_label}0 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
742
		if [ $NANO_CONFSIZE -gt 0 ] ; then
743
			echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
744
		fi
745
746
		umount ${MNT}
747
		# Restore the original trap
748
		trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
749
750
		# Setting NANO_IMAGES to 1 and NANO_INIT_IMG2 will tell
751
		# NanoBSD to only create one partition.  We default to 2
752
		# partitions in case anything happens to the first the
753
		# operator can boot from the 2nd and should be OK.
754
755
		# Before just going to use dd for duplicate think!
756
		# The images are created as sparse so lets take advantage
757
		# of that by just exec some commands.
758
		if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
759
			# Duplicate to second image (if present)
760
			echo ">>> Creating NanoBSD second slice by duplicating first slice." | tee -a ${LOGFILE}
761
			# Create second image
762
			dd if=/dev/${MD}s1 of=/dev/${MD}s2 conv=sparse bs=64k 2>&1 >> ${LOGFILE}
763
			tunefs -L ${_label}1 /dev/${MD}s2a 2>&1 >> ${LOGFILE}
764
			mount /dev/ufs/${_label}1 ${MNT}
765
			if [ $? -ne 0 ]; then
766
				echo ">>> ERROR: Something wrong happened during mount of second slice image creation. STOPPING!" | tee -a ${LOGFILE}
767
				print_error_pfS
768
			fi
769
			# Consider the unmounting as well
770
			trap "umount /dev/ufs/${_label}1; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
771
772
			echo "/dev/ufs/${_label}1 / ufs ro,sync,noatime 1 1" > ${MNT}/etc/fstab
773
			if [ $NANO_CONFSIZE -gt 0 ] ; then
774
				echo "/dev/ufs/cf /cf ufs ro,sync,noatime 1 1" >> ${MNT}/etc/fstab
775
			fi
776
777
			umount ${MNT}
778
			# Restore the trap back
779
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
780
		fi
781
782
		# Create Data slice, if any.
783
		# Note the changing of the variable to NANO_CONFSIZE
784
		# from NANO_DATASIZE.  We also added glabel support
785
		# and populate the Product configuration from the /cf
786
		# directory located in FINAL_CHROOT_DIR
787
		if [ $NANO_CONFSIZE -gt 0 ] ; then
788
			echo ">>> Creating /cf area to hold config.xml"
789
			newfs -L cf ${NANO_NEWFS} /dev/${MD}s3 2>&1 >> ${LOGFILE}
790
			# Mount data partition and copy contents of /cf
791
			# Can be used later to create custom default config.xml while building
792
			mount /dev/ufs/cf ${MNT}
793
			if [ $? -ne 0 ]; then
794
				echo ">>> ERROR: Something wrong happened during mount of cf slice image creation. STOPPING!" | tee -a ${LOGFILE}
795
				print_error_pfS
796
			fi
797
			# Consider the unmounting as well
798
			trap "umount /dev/ufs/cf; mdconfig -d -u ${MD}; return" 1 2 15 EXIT
799
800
			clone_directory_contents ${FINAL_CHROOT_DIR}/cf ${MNT}
801
802
			umount ${MNT}
803
			# Restore the trap back
804
			trap "mdconfig -d -u ${MD}; return" 1 2 15 EXIT
805
		else
806
			">>> [nanoo] NANO_CONFSIZE is not set. Not adding a /conf partition.. You sure about this??" | tee -a ${LOGFILE}
807
		fi
808
809
		echo ">>> [nanoo] Creating NanoBSD upgrade file from first slice..." | tee -a ${LOGFILE}
810
		dd if=/dev/${MD}s1 of=$IMGUPDATE conv=sparse bs=64k 2>&1 >> ${LOGFILE}
811
812
		mdconfig -d -u $MD
813
		# Restore default action
814
		trap "-" 1 2 15 EXIT
815
816
		# Check each image and ensure that they are over
817
		# 3 megabytes.  If either image is under 20 megabytes
818
		# in size then error out.
819
		IMGSIZE=$(stat -f "%z" ${IMG})
820
		IMGUPDATESIZE=$(stat -f "%z" ${IMGUPDATE})
821
		CHECKSIZE="20040710"
822
		if [ "$IMGSIZE" -lt "$CHECKSIZE" ]; then
823
			echo ">>> ERROR: Something went wrong when building NanoBSD.  The image size is under 20 megabytes!" | tee -a ${LOGFILE}
824
			print_error_pfS
825
		fi
826
		if [ "$IMGUPDATESIZE" -lt "$CHECKSIZE" ]; then
827
			echo ">>> ERROR: Something went wrong when building NanoBSD upgrade image.  The image size is under 20 megabytes!" | tee -a ${LOGFILE}
828
			print_error_pfS
829
		fi
830
831
		# Wrap up the show, Johnny
832
		echo ">>> NanoBSD Image completed for size: $_NANO_MEDIASIZE." | tee -a ${LOGFILE}
833
834 39ef49f6 Renato Botelho
		gzip -qf $IMG &
835
		_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
836
		gzip -qf $IMGUPDATE &
837
		_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
838 6f73c362 Renato Botelho
	done
839
840
	unset IMG
841
	unset IMGUPDATE
842
	unset IMGUPDATESIZE
843
	unset IMGSIZE
844
845 04992be2 Renato Botelho
	ls -lah $IMAGES_FINAL_DIR
846 6f73c362 Renato Botelho
}
847
848
# This routine creates a ova image that contains
849
# a ovf and vmdk file. These files can be imported
850
# right into vmware or virtual box.
851
# (and many other emulation platforms)
852
# http://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
853
create_ova_image() {
854
	# XXX create a .ovf php creator that you can pass:
855
	#     1. populatedSize
856
	#     2. license
857
	#     3. product name
858
	#     4. version
859
	#     5. number of network interface cards
860
	#     6. allocationUnits
861
	#     7. capacity
862
	#     8. capacityAllocationUnits
863
864
	LOGFILE=${BUILDER_LOGS}/ova.${TARGET}.log
865
866 89a72b59 Renato Botelho
	[ -d "${OVA_TMP}" ] \
867
		&& rm -rf ${OVA_TMP}
868
869
	mkdir -p ${OVA_TMP}
870
871 2006b940 Renato Botelho
	if [ -z "${OVA_SWAP_PART_SIZE_IN_GB}" -o "${OVA_SWAP_PART_SIZE_IN_GB}" = "0" ]; then
872
		# first partition size (freebsd-ufs)
873
		local OVA_FIRST_PART_SIZE_IN_GB=${VMDK_DISK_CAPACITY_IN_GB}
874
		# Calculate real first partition size, removing 128 blocks (65536 bytes) beginning/loader
875
		local OVA_FIRST_PART_SIZE=$((${OVA_FIRST_PART_SIZE_IN_GB}*1024*1024*1024-65536))
876
		# Unset swap partition size variable
877
		unset OVA_SWAP_PART_SIZE
878
		# Parameter used by mkimg
879
		unset OVA_SWAP_PART_PARAM
880
	else
881
		# first partition size (freebsd-ufs)
882
		local OVA_FIRST_PART_SIZE_IN_GB=$((VMDK_DISK_CAPACITY_IN_GB-OVA_SWAP_PART_SIZE_IN_GB))
883
		# Use first partition size in g
884
		local OVA_FIRST_PART_SIZE="${OVA_FIRST_PART_SIZE_IN_GB}g"
885
		# Calculate real swap size, removing 128 blocks (65536 bytes) beginning/loader
886
		local OVA_SWAP_PART_SIZE=$((${OVA_SWAP_PART_SIZE_IN_GB}*1024*1024*1024-65536))
887
		# Parameter used by mkimg
888
		local OVA_SWAP_PART_PARAM="-p freebsd-swap/swap0::${OVA_SWAP_PART_SIZE}"
889
	fi
890 35104e03 Renato Botelho
891 6f73c362 Renato Botelho
	# Prepare folder to be put in image
892
	customize_stagearea_for_image "ova"
893
	install_default_kernel ${DEFAULT_KERNEL} "no"
894
895 89a72b59 Renato Botelho
	# Fill fstab
896
	echo ">>> Installing platform specific items..." | tee -a ${LOGFILE}
897 c9b36088 Renato Botelho
	echo "/dev/gpt/${PRODUCT_NAME}	/	ufs		rw	1	1" > ${FINAL_CHROOT_DIR}/etc/fstab
898 2006b940 Renato Botelho
	if [ -n "${OVA_SWAP_PART_SIZE}" ]; then
899
		echo "/dev/gpt/swap0	none	swap	sw	0	0" >> ${FINAL_CHROOT_DIR}/etc/fstab
900
	fi
901 89a72b59 Renato Botelho
902
	# Create / partition
903 7743b4bd Renato Botelho
	echo -n ">>> Creating / partition... " | tee -a ${LOGFILE}
904 89a72b59 Renato Botelho
	makefs \
905
		-B little \
906 85bc1c63 Renato Botelho
		-o label=${PRODUCT_NAME},version=2 \
907 2006b940 Renato Botelho
		-s ${OVA_FIRST_PART_SIZE} \
908 89a72b59 Renato Botelho
		${OVA_TMP}/${OVFUFS} \
909
		${FINAL_CHROOT_DIR} 2>&1 >> ${LOGFILE}
910
911
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFUFS} ]; then
912
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
913
			rm -f ${OVA_TMP}/${OVFUFS}
914
		fi
915 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
916 89a72b59 Renato Botelho
		echo ">>> ERROR: Error creating vmdk / partition. STOPPING!" | tee -a ${LOGFILE}
917
		print_error_pfS
918 6f73c362 Renato Botelho
	fi
919 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
920 89a72b59 Renato Botelho
921 85bc1c63 Renato Botelho
	echo -n ">>> Enabling SUJ on recently created disk... " | tee -a ${LOGFILE}
922
	if ! tunefs -j enable ${OVA_TMP}/${OVFUFS} 2>&1 >>${LOGFILE}; then
923
		echo "Failed!" | tee -a ${LOGFILE}
924
		echo ">>> ERROR: Error enabling SUJ on disk. STOPPING!" | tee -a ${LOGFILE}
925
		print_error_pfS
926
	fi
927
	echo "Done!" | tee -a ${LOGFILE}
928
929 7743b4bd Renato Botelho
	# Create raw disk
930
	echo -n ">>> Creating raw disk... " | tee -a ${LOGFILE}
931 89a72b59 Renato Botelho
	mkimg \
932
		-s gpt \
933 679b5a66 Renato Botelho
		-f raw \
934 89a72b59 Renato Botelho
		-b /boot/pmbr \
935
		-p freebsd-boot:=/boot/gptboot \
936
		-p freebsd-ufs/${PRODUCT_NAME}:=${OVA_TMP}/${OVFUFS} \
937 2006b940 Renato Botelho
		${OVA_SWAP_PART_PARAM} \
938 679b5a66 Renato Botelho
		-o ${OVA_TMP}/${OVFRAW} 2>&1 >> ${LOGFILE}
939 89a72b59 Renato Botelho
940 679b5a66 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFRAW} ]; then
941 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFUFS} ]; then
942
			rm -f ${OVA_TMP}/${OVFUFS}
943
		fi
944 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
945
			rm -f ${OVA_TMP}/${OVFRAW}
946 502db0bd Renato Botelho
		fi
947 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
948 502db0bd Renato Botelho
		echo ">>> ERROR: Error creating temporary vmdk image. STOPPING!" | tee -a ${LOGFILE}
949
		print_error_pfS
950
	fi
951 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
952 502db0bd Renato Botelho
953
	# We don't need it anymore
954
	rm -f ${OVA_TMP}/${OVFUFS} >/dev/null 2>&1
955
956 679b5a66 Renato Botelho
	# Convert raw to vmdk
957 7743b4bd Renato Botelho
	echo -n ">>> Creating vmdk disk... " | tee -a ${LOGFILE}
958 beb07890 Renato Botelho
	vmdktool -z9 -v ${OVA_TMP}/${OVFVMDK} ${OVA_TMP}/${OVFRAW}
959 502db0bd Renato Botelho
960 2dc195f9 Renato Botelho
	if [ $? -ne 0 -o ! -f ${OVA_TMP}/${OVFVMDK} ]; then
961 679b5a66 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFRAW} ]; then
962
			rm -f ${OVA_TMP}/${OVFRAW}
963 502db0bd Renato Botelho
		fi
964 89a72b59 Renato Botelho
		if [ -f ${OVA_TMP}/${OVFVMDK} ]; then
965
			rm -f ${OVA_TMP}/${OVFVMDK}
966
		fi
967 7743b4bd Renato Botelho
		echo "Failed!" | tee -a ${LOGFILE}
968 89a72b59 Renato Botelho
		echo ">>> ERROR: Error creating vmdk image. STOPPING!" | tee -a ${LOGFILE}
969
		print_error_pfS
970 6f73c362 Renato Botelho
	fi
971 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
972 6f73c362 Renato Botelho
973 679b5a66 Renato Botelho
	rm -f ${OVA_TMP}/i${OVFRAW}
974 6f73c362 Renato Botelho
975 89a72b59 Renato Botelho
	ova_setup_ovf_template
976 6f73c362 Renato Botelho
977 7743b4bd Renato Botelho
	echo -n ">>> Writing final ova image... " | tee -a ${LOGFILE}
978
	# Create OVA file for vmware
979 89a72b59 Renato Botelho
	gtar -C ${OVA_TMP} -cpf ${OVAPATH} ${PRODUCT_NAME}.ovf ${OVFVMDK}
980 7743b4bd Renato Botelho
	echo "Done!" | tee -a ${LOGFILE}
981 89a72b59 Renato Botelho
	rm -f ${OVA_TMP}/${OVFVMDK} >/dev/null 2>&1
982
983
	echo ">>> OVA created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
984 6f73c362 Renato Botelho
}
985
986
# called from create_ova_image
987 a82b9f43 Renato Botelho
ova_setup_ovf_template() {
988 fc48ef54 Renato Botelho
	if [ ! -f ${OVFTEMPLATE} ]; then
989 173fa93f Renato Botelho
		echo ">>> ERROR: OVF template file (${OVFTEMPLATE}) not found."
990
		print_error_pfS
991 6f73c362 Renato Botelho
	fi
992
993 fc48ef54 Renato Botelho
	#  OperatingSystemSection (${PRODUCT_NAME}.ovf)
994
	#  42   FreeBSD 32-Bit
995
	#  78   FreeBSD 64-Bit
996
	if [ "${TARGET}" = "amd64" ]; then
997
		local _os_id="78"
998 502db0bd Renato Botelho
		local _os_type="freebsd64Guest"
999
		local _os_descr="FreeBSD 64-Bit"
1000 fc48ef54 Renato Botelho
	elif [ "${TARGET}" = "i386" ]; then
1001
		local _os_id="42"
1002 502db0bd Renato Botelho
		local _os_type="freebsdGuest"
1003
		local _os_descr="FreeBSD"
1004 fc48ef54 Renato Botelho
	else
1005
		echo ">>> ERROR: Platform not supported for OVA (${TARGET})"
1006
		print_error_pfS
1007
	fi
1008 50dcadff Renato Botelho
1009 502db0bd Renato Botelho
	local POPULATED_SIZE=$(du -d0 -k $FINAL_CHROOT_DIR | cut -f1)
1010
	local POPULATED_SIZE_IN_BYTES=$((${POPULATED_SIZE}*1024))
1011
	local VMDK_FILE_SIZE=$(stat -f "%z" ${OVA_TMP}/${OVFVMDK})
1012 6f73c362 Renato Botelho
1013 fc48ef54 Renato Botelho
	sed \
1014 502db0bd Renato Botelho
		-e "s,%%VMDK_FILE_SIZE%%,${VMDK_FILE_SIZE},g" \
1015
		-e "s,%%VMDK_DISK_CAPACITY_IN_GB%%,${VMDK_DISK_CAPACITY_IN_GB},g" \
1016
		-e "s,%%POPULATED_SIZE_IN_BYTES%%,${POPULATED_SIZE_IN_BYTES},g" \
1017 fc48ef54 Renato Botelho
		-e "s,%%OS_ID%%,${_os_id},g" \
1018
		-e "s,%%OS_TYPE%%,${_os_type},g" \
1019 502db0bd Renato Botelho
		-e "s,%%OS_DESCR%%,${_os_descr},g" \
1020 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
1021 f2d83cfb Renato Botelho
		-e "s,%%PRODUCT_NAME_SUFFIX%%,${PRODUCT_NAME_SUFFIX},g" \
1022 fc48ef54 Renato Botelho
		-e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION},g" \
1023
		-e "s,%%PRODUCT_URL%%,${PRODUCT_URL},g" \
1024 0e334a37 Renato Botelho
		-e "s#%%VENDOR_NAME%%#${VENDOR_NAME}#g" \
1025
		-e "s#%%OVF_INFO%%#${OVF_INFO}#g" \
1026 7e35e0bd Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/r ${BUILDER_ROOT}/license.txt" \
1027 fc48ef54 Renato Botelho
		-e "/^%%PRODUCT_LICENSE%%/d" \
1028
		${OVFTEMPLATE} > ${OVA_TMP}/${PRODUCT_NAME}.ovf
1029 6f73c362 Renato Botelho
}
1030
1031
# Cleans up previous builds
1032 7e6ab3ed Renato Botelho
clean_builder() {
1033 6f73c362 Renato Botelho
	# Clean out directories
1034
	echo ">>> Cleaning up previous build environment...Please wait!"
1035
1036
	staginareas_clean_each_run
1037
1038
	if [ -d "${STAGE_CHROOT_DIR}" ]; then
1039
		BASENAME=$(basename ${STAGE_CHROOT_DIR})
1040
		echo -n ">>> Cleaning ${STAGE_CHROOT_DIR} ..."
1041
		chflags -R noschg ${STAGE_CHROOT_DIR} 2>&1 >/dev/null
1042 f546e6ca Renato Botelho
		rm -rf ${STAGE_CHROOT_DIR}/* 2>/dev/null
1043 6f73c362 Renato Botelho
		echo "Done."
1044
	fi
1045
1046 2abc3e80 Renato Botelho
	if [ -z "${NO_CLEAN_FREEBSD_OBJ}" -a -d "${FREEBSD_SRC_DIR}" ]; then
1047 64f27203 Renato Botelho
		OBJTREE=$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} make -C ${FREEBSD_SRC_DIR} -V OBJTREE)
1048 6f73c362 Renato Botelho
		if [ -d "${OBJTREE}" ]; then
1049
			echo -n ">>> Cleaning FreeBSD objects dir staging..."
1050
			echo -n "."
1051
			chflags -R noschg ${OBJTREE} 2>&1 >/dev/null
1052
			echo -n "."
1053
			rm -rf ${OBJTREE}/*
1054
			echo "Done!"
1055
		fi
1056 7e6ab3ed Renato Botelho
		if [ -d "${KERNEL_BUILD_PATH}" ]; then
1057 6f73c362 Renato Botelho
			echo -n ">>> Cleaning previously built kernel stage area..."
1058
			rm -rf $KERNEL_BUILD_PATH/*
1059
			echo "Done!"
1060
		fi
1061
	fi
1062
	mkdir -p $KERNEL_BUILD_PATH
1063
1064
	echo -n ">>> Cleaning previously built images..."
1065 04992be2 Renato Botelho
	rm -rf $IMAGES_FINAL_DIR/*
1066 ab943fc9 Renato Botelho
	rm -rf $STAGINGAREA/*
1067 6f73c362 Renato Botelho
	echo "Done!"
1068
1069 2abc3e80 Renato Botelho
	if [ -z "${NO_CLEAN_FREEBSD_SRC}" ]; then
1070 64f27203 Renato Botelho
		if [ -d "$FREEBSD_SRC_DIR" ]; then
1071
			echo -n ">>> Ensuring $FREEBSD_SRC_DIR is clean..."
1072
			rm -rf ${FREEBSD_SRC_DIR}
1073 6f73c362 Renato Botelho
			echo "Done!"
1074
		fi
1075
	fi
1076
1077
	echo -n ">>> Cleaning previous builder logs..."
1078
	if [ -d "$BUILDER_LOGS" ]; then
1079
		rm -rf ${BUILDER_LOGS}
1080
	fi
1081
	mkdir -p ${BUILDER_LOGS}
1082
1083
	echo "Done!"
1084
1085
	echo ">>> Cleaning of builder environment has finished."
1086
}
1087
1088
clone_directory_contents() {
1089 25c5455d Renato Botelho
	if [ ! -e "$2" ]; then
1090
		mkdir -p "$2"
1091
	fi
1092 6f73c362 Renato Botelho
	if [ ! -d "$1" -o ! -d "$2" ]; then
1093
		if [ -z "${LOGFILE}" ]; then
1094
			echo ">>> ERROR: Argument $1 supplied is not a directory!"
1095
		else
1096
			echo ">>> ERROR: Argument $1 supplied is not a directory!" | tee -a ${LOGFILE}
1097
		fi
1098
		print_error_pfS
1099
	fi
1100
	echo -n ">>> Using TAR to clone $1 to $2 ..."
1101
	tar -C ${1} -c -f - . | tar -C ${2} -x -p -f -
1102
	echo "Done!"
1103
}
1104
1105
clone_to_staging_area() {
1106
	# Clone everything to the final staging area
1107
	echo -n ">>> Cloning everything to ${STAGE_CHROOT_DIR} staging area..."
1108
	LOGFILE=${BUILDER_LOGS}/cloning.${TARGET}.log
1109
1110 694028d6 Renato Botelho
	tar -C ${PRODUCT_SRC} -c -f - . | \
1111 6f73c362 Renato Botelho
		tar -C ${STAGE_CHROOT_DIR} -x -p -f -
1112
1113 a3bb036b Renato Botelho
	if [ "${PRODUCT_NAME}" != "pfSense" ]; then
1114
		mv ${STAGE_CHROOT_DIR}/usr/local/sbin/pfSense-upgrade \
1115
			${STAGE_CHROOT_DIR}/usr/local/sbin/${PRODUCT_NAME}-upgrade
1116
	fi
1117
1118 6f73c362 Renato Botelho
	if [ -f ${STAGE_CHROOT_DIR}/etc/master.passwd ]; then
1119
		chroot ${STAGE_CHROOT_DIR} pwd_mkdb /etc/master.passwd
1120
	fi
1121
	mkdir -p ${STAGE_CHROOT_DIR}/etc/mtree
1122
	mtree -Pcp ${STAGE_CHROOT_DIR}/var > ${STAGE_CHROOT_DIR}/etc/mtree/var.dist
1123
	mtree -Pcp ${STAGE_CHROOT_DIR}/etc > ${STAGE_CHROOT_DIR}/etc/mtree/etc.dist
1124
	if [ -d ${STAGE_CHROOT_DIR}/usr/local/etc ]; then
1125
		mtree -Pcp ${STAGE_CHROOT_DIR}/usr/local/etc > ${STAGE_CHROOT_DIR}/etc/mtree/localetc.dist
1126
	fi
1127
1128
	## Add buildtime and lastcommit information
1129
	# This is used for detecting updates.
1130
	echo "$BUILTDATESTRING" > $STAGE_CHROOT_DIR/etc/version.buildtime
1131
	# Record last commit info if it is available.
1132
	if [ -f $SCRATCHDIR/build_commit_info.txt ]; then
1133
		cp $SCRATCHDIR/build_commit_info.txt $STAGE_CHROOT_DIR/etc/version.lastcommit
1134
	fi
1135
1136
	local _exclude_files="${CORE_PKG_TMP}/base_exclude_files"
1137
	sed \
1138
		-e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" \
1139
		-e "s,%%VERSION%%,${_version},g" \
1140 0ce2a7f3 Renato Botelho
		${BUILDER_TOOLS}/templates/core_pkg/base/exclude_files \
1141 6f73c362 Renato Botelho
		> ${_exclude_files}
1142
1143 651f440c Renato Botelho
	mkdir -p ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR} >/dev/null 2>&1
1144 10742be9 Renato Botelho
1145
	# Include a sample pkg stable conf to base
1146
	setup_pkg_repo \
1147 db8621d8 Renato Botelho
		${PKG_REPO_DEFAULT} \
1148 651f440c Renato Botelho
		${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/${PRODUCT_NAME}-repo.conf \
1149 10742be9 Renato Botelho
		${TARGET} \
1150
		${TARGET_ARCH} \
1151 147ab4fd Renato Botelho
		${PKG_REPO_CONF_BRANCH}
1152 10742be9 Renato Botelho
1153 6f73c362 Renato Botelho
	mtree \
1154
		-c \
1155
		-k uid,gid,mode,size,flags,sha256digest \
1156
		-p ${STAGE_CHROOT_DIR} \
1157
		-X ${_exclude_files} \
1158 651f440c Renato Botelho
		> ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.mtree
1159 6f73c362 Renato Botelho
	tar \
1160
		-C ${STAGE_CHROOT_DIR} \
1161 651f440c Renato Botelho
		-cJf ${STAGE_CHROOT_DIR}${PRODUCT_SHARE_DIR}/base.txz \
1162 6f73c362 Renato Botelho
		-X ${_exclude_files} \
1163
		.
1164
1165 db8621d8 Renato Botelho
	local _share_repos_path="${SCRATCHDIR}/repo-tmp/${PRODUCT_SHARE_DIR}/pkg/repos"
1166
	rm -rf ${SCRATCHDIR}/repo-tmp >/dev/null 2>&1
1167
	mkdir -p ${_share_repos_path} >/dev/null 2>&1
1168 97fd32ba Renato Botelho
1169
	setup_pkg_repo \
1170
		${PKG_REPO_DEFAULT} \
1171
		${_share_repos_path}/${PRODUCT_NAME}-repo.conf \
1172
		${TARGET} \
1173
		${TARGET_ARCH} \
1174
		${PKG_REPO_CONF_BRANCH} \
1175
		${PKG_REPO_SERVER_RELEASE}
1176
1177 db8621d8 Renato Botelho
	cp -f ${PKG_REPO_DEFAULT%%.conf}.descr ${_share_repos_path}
1178 52efc840 Renato Botelho
1179 db8621d8 Renato Botelho
	# Add additional repos
1180
	for _template in ${PKG_REPO_BASE}/${PRODUCT_NAME}-repo-*.conf; do
1181
		_template_filename=$(basename ${_template})
1182
		setup_pkg_repo \
1183
			${_template} \
1184
			${_share_repos_path}/${_template_filename} \
1185
			${TARGET} \
1186
			${TARGET_ARCH} \
1187 97fd32ba Renato Botelho
			${PKG_REPO_CONF_BRANCH} \
1188
			${PKG_REPO_SERVER_RELEASE}
1189 db8621d8 Renato Botelho
		cp -f ${_template%%.conf}.descr ${_share_repos_path}
1190
	done
1191 10742be9 Renato Botelho
1192 db8621d8 Renato Botelho
	core_pkg_create repo "" ${CORE_PKG_VERSION} ${SCRATCHDIR}/repo-tmp
1193 52efc840 Renato Botelho
1194 dab31392 Renato Botelho
	core_pkg_create rc "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1195 6f73c362 Renato Botelho
	core_pkg_create base "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1196 1f5207bc Renato Botelho
	core_pkg_create base-nanobsd "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1197 6f73c362 Renato Botelho
	core_pkg_create default-config "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1198
1199
	local DEFAULTCONF=${STAGE_CHROOT_DIR}/conf.default/config.xml
1200 c4968f42 Renato Botelho
1201 2f6260c5 Renato Botelho
	# Save current WAN and LAN if value
1202 168c162a Renato Botelho
	local _old_wan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/wan/if" ${DEFAULTCONF})
1203
	local _old_lan_if=$(xml sel -t -v "${XML_ROOTOBJ}/interfaces/lan/if" ${DEFAULTCONF})
1204 2f6260c5 Renato Botelho
1205 c4968f42 Renato Botelho
	# Change default interface names to match vmware driver
1206 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "vmx0" ${DEFAULTCONF}
1207
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "vmx1" ${DEFAULTCONF}
1208 fbd4dfc0 Renato Botelho
	core_pkg_create default-config "vmware" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1209 c4968f42 Renato Botelho
1210
	# Restore default values to be used by serial package
1211 2f6260c5 Renato Botelho
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/wan/if" -v "${_old_wan_if}" ${DEFAULTCONF}
1212
	xml ed -P -L -u "${XML_ROOTOBJ}/interfaces/lan/if" -v "${_old_lan_if}" ${DEFAULTCONF}
1213 c4968f42 Renato Botelho
1214 6f73c362 Renato Botelho
	# Activate serial console in config.xml
1215 2f6260c5 Renato Botelho
	xml ed -L -P -d "${XML_ROOTOBJ}/system/enableserial" ${DEFAULTCONF}
1216
	xml ed -P -s "${XML_ROOTOBJ}/system" -t elem -n "enableserial" \
1217
		${DEFAULTCONF} > ${DEFAULTCONF}.tmp
1218
	xml fo -t ${DEFAULTCONF}.tmp > ${DEFAULTCONF}
1219
	rm -f ${DEFAULTCONF}.tmp
1220 6f73c362 Renato Botelho
1221
	echo force > ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1222
1223
	core_pkg_create default-config-serial "" ${CORE_PKG_VERSION} ${STAGE_CHROOT_DIR}
1224
1225
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/enableserial_force
1226
	rm -f ${STAGE_CHROOT_DIR}/cf/conf/config.xml
1227
1228
	# Make sure pkg is present
1229
	pkg_bootstrap ${STAGE_CHROOT_DIR}
1230
1231
	echo "Done!"
1232
}
1233
1234
create_final_staging_area() {
1235
	if [ -z "${FINAL_CHROOT_DIR}" ]; then
1236
		echo ">>> ERROR: FINAL_CHROOT_DIR is not set, cannot continue!" | tee -a ${LOGFILE}
1237
		print_error_pfS
1238
	fi
1239
1240
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1241
		echo -n ">>> Previous ${FINAL_CHROOT_DIR} detected cleaning up..." | tee -a ${LOGFILE}
1242
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 1>/dev/null
1243
		rm -rf ${FINAL_CHROOT_DIR}/* 2>&1 1>/dev/null
1244
		echo "Done." | tee -a ${LOGFILE}
1245
	fi
1246
1247
	echo ">>> Preparing Final image staging area: $(LC_ALL=C date)" 2>&1 | tee -a ${LOGFILE}
1248
	echo ">>> Cloning ${STAGE_CHROOT_DIR} to ${FINAL_CHROOT_DIR}" 2>&1 | tee -a ${LOGFILE}
1249
	clone_directory_contents ${STAGE_CHROOT_DIR} ${FINAL_CHROOT_DIR}
1250
1251
	if [ ! -f $FINAL_CHROOT_DIR/sbin/init ]; then
1252
		echo ">>> ERROR: Something went wrong during cloning -- Please verify!" 2>&1 | tee -a ${LOGFILE}
1253
		print_error_pfS
1254
	fi
1255
}
1256
1257
customize_stagearea_for_image() {
1258 b2fbacdc Renato Botelho
	local _image_type="$1"
1259 75e28295 Renato Botelho
	local _default_config=""
1260
1261
	if [ -n "$2" ]; then
1262
		_default_config="$2"
1263
	elif [ "${_image_type}" = "nanobsd" -o \
1264
	     "${_image_type}" = "memstickserial" -o \
1265
	     "${_image_type}" = "memstickadi" ]; then
1266
		_default_config="default-config-serial"
1267
	elif [ "${_image_type}" = "ova" ]; then
1268
		_default_config="default-config-vmware"
1269
	else
1270
		_default_config="default-config"
1271
	fi
1272 b2fbacdc Renato Botelho
1273 6f73c362 Renato Botelho
	# Prepare final stage area
1274
	create_final_staging_area
1275
1276 dab31392 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} rc
1277 5dea3b46 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} repo
1278 dab31392 Renato Botelho
1279 b2fbacdc Renato Botelho
	if [ "${_image_type}" = "nanobsd" -o \
1280
	     "${_image_type}" = "nanobsd-vga" ]; then
1281 48b79a60 Renato Botelho
1282
		mkdir -p ${FINAL_CHROOT_DIR}/root/var/db \
1283
			 ${FINAL_CHROOT_DIR}/root/var/cache \
1284
			 ${FINAL_CHROOT_DIR}/var/db/pkg \
1285
			 ${FINAL_CHROOT_DIR}/var/cache/pkg
1286
		mv -f ${FINAL_CHROOT_DIR}/var/db/pkg ${FINAL_CHROOT_DIR}/root/var/db
1287
		mv -f ${FINAL_CHROOT_DIR}/var/cache/pkg ${FINAL_CHROOT_DIR}/root/var/cache
1288
		ln -sf ../../root/var/db/pkg ${FINAL_CHROOT_DIR}/var/db/pkg
1289
		ln -sf ../../root/var/cache/pkg ${FINAL_CHROOT_DIR}/var/cache/pkg
1290
1291 1f5207bc Renato Botelho
		pkg_chroot_add ${FINAL_CHROOT_DIR} base-nanobsd
1292
	else
1293
		pkg_chroot_add ${FINAL_CHROOT_DIR} base
1294
	fi
1295 6ee167de Renato Botelho
1296 b2fbacdc Renato Botelho
	if [ "${_image_type}" = "iso" -o \
1297
	     "${_image_type}" = "memstick" -o \
1298
	     "${_image_type}" = "memstickserial" -o \
1299
	     "${_image_type}" = "memstickadi" ]; then
1300 6f73c362 Renato Botelho
		install_bsdinstaller
1301
		mkdir -p ${FINAL_CHROOT_DIR}/pkgs
1302 da781042 Renato Botelho
		cp ${CORE_PKG_REAL_PATH}/All/*default-config*.txz ${FINAL_CHROOT_DIR}/pkgs
1303 6f73c362 Renato Botelho
	fi
1304
1305 75e28295 Renato Botelho
	pkg_chroot_add ${FINAL_CHROOT_DIR} ${_default_config}
1306 052f4bdb Renato Botelho
1307
	# XXX: Workaround to avoid pkg to complain regarding release
1308
	#      repo on first boot since packages are installed from
1309
	#      staging server during build phase
1310
	if [ "${PKG_REPO_SERVER}" != "${PKG_REPO_SERVER_RELEASE}" ]; then
1311
		_read_cmd="select value from repodata where key='packagesite'"
1312
		for _db in ${FINAL_CHROOT_DIR}/var/db/pkg/repo-*sqlite; do
1313
			_cur=$(/usr/local/bin/sqlite3 ${_db} "${_read_cmd}")
1314
			_new=$(echo "${_cur}" | sed -e "s,^${PKG_REPO_SERVER},${PKG_REPO_SERVER_RELEASE},")
1315
			/usr/local/bin/sqlite3 ${_db} "update repodata set value='${_new}' where key='packagesite'"
1316
		done
1317
	fi
1318 6f73c362 Renato Botelho
}
1319
1320
create_distribution_tarball() {
1321
	mkdir -p ${FINAL_CHROOT_DIR}/install
1322
1323
	tar -C ${FINAL_CHROOT_DIR} --exclude ./install --exclude ./pkgs -cJf ${FINAL_CHROOT_DIR}/install/${PRODUCT_NAME}.txz .
1324
}
1325
1326
create_iso_image() {
1327
	LOGFILE=${BUILDER_LOGS}/isoimage.${TARGET}
1328
	echo ">>> Building bootable ISO image for ${TARGET}" | tee -a ${LOGFILE}
1329
	if [ -z "${DEFAULT_KERNEL}" ]; then
1330
		echo ">>> ERROR: Could not identify DEFAULT_KERNEL to install on image!" | tee -a ${LOGFILE}
1331
		print_error_pfS
1332
	fi
1333
1334
	customize_stagearea_for_image "iso"
1335
	install_default_kernel ${DEFAULT_KERNEL}
1336
1337
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1338
1339
	FSLABEL=$(echo ${PRODUCT_NAME} | tr '[:lower:]' '[:upper:]')
1340
	echo "/dev/iso9660/${FSLABEL} / cd9660 ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1341
1342
	# This check is for supporting create memstick/ova images
1343
	echo -n ">>> Running command: script -aq $LOGFILE makefs -t cd9660 -o bootimage=\"i386;${FINAL_CHROOT_DIR}/boot/cdboot \"-o no-emul-boot -o rockridge " | tee -a ${LOGFILE}
1344
	echo "-o label=${FSLABEL} -o publisher=\"${PRODUCT_NAME} project.\" $ISOPATH ${FINAL_CHROOT_DIR}" | tee -a ${LOGFILE}
1345
1346
	create_distribution_tarball
1347
1348
	# Remove /rescue from iso since cd9660 cannot deal with hardlinks
1349
	rm -rf ${FINAL_CHROOT_DIR}/rescue
1350
1351
	makefs -t cd9660 -o bootimage="i386;${FINAL_CHROOT_DIR}/boot/cdboot" -o no-emul-boot -o rockridge \
1352
		-o label=${FSLABEL} -o publisher="${PRODUCT_NAME} project." $ISOPATH ${FINAL_CHROOT_DIR} 2>&1 >> ${LOGFILE}
1353
	if [ $? -ne 0 -o ! -f $ISOPATH ]; then
1354
		if [ -f ${ISOPATH} ]; then
1355
			rm -f $ISOPATH
1356
		fi
1357
		echo ">>> ERROR: Something wrong happened during ISO image creation. STOPPING!" | tee -a ${LOGFILE}
1358
		print_error_pfS
1359
	fi
1360
	gzip -qf $ISOPATH &
1361 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1362 6f73c362 Renato Botelho
1363
	echo ">>> ISO created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1364
}
1365
1366
create_memstick_image() {
1367
1368
	LOGFILE=${BUILDER_LOGS}/memstick.${TARGET}
1369
	if [ "${MEMSTICKPATH}" = "" ]; then
1370
		echo ">>> MEMSTICKPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1371
		return
1372
	fi
1373
1374 624376f6 Renato Botelho
	customize_stagearea_for_image "memstick"
1375
	install_default_kernel ${DEFAULT_KERNEL}
1376 6f73c362 Renato Botelho
1377
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1378
1379
	echo ">>> Creating memstick to ${MEMSTICKPATH}." 2>&1 | tee -a ${LOGFILE}
1380
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1381
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1382
1383
	create_distribution_tarball
1384
1385 3080d476 Renato Botelho
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${MEMSTICKPATH} ${FINAL_CHROOT_DIR}
1386 6f73c362 Renato Botelho
	if [ $? -ne 0 ]; then
1387
		if [ -f ${MEMSTICKPATH} ]; then
1388
			rm -f $MEMSTICKPATH
1389
		fi
1390
		echo ">>> ERROR: Something wrong happened during MEMSTICK image creation. STOPPING!" | tee -a ${LOGFILE}
1391
		print_error_pfS
1392
	fi
1393
	MD=$(mdconfig -a -t vnode -f $MEMSTICKPATH)
1394
	# Just in case
1395
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1396 d977046e Renato Botelho
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1397 6f73c362 Renato Botelho
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1398
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1399
	trap "-" 1 2 15 EXIT
1400
	mdconfig -d -u ${MD} 2>&1 | tee -a ${LOGFILE}
1401
	gzip -qf $MEMSTICKPATH &
1402 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1403 6f73c362 Renato Botelho
1404
	echo ">>> MEMSTICK created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1405
}
1406
1407
create_memstick_serial_image() {
1408
	LOGFILE=${BUILDER_LOGS}/memstickserial.${TARGET}
1409
	if [ "${MEMSTICKSERIALPATH}" = "" ]; then
1410
		echo ">>> MEMSTICKSERIALPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1411
		return
1412
	fi
1413
1414 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickserial"
1415
	install_default_kernel ${DEFAULT_KERNEL}
1416 6f73c362 Renato Botelho
1417
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1418
1419
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1420
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1421
1422
	echo ">>> Creating serial memstick to ${MEMSTICKSERIALPATH}." 2>&1 | tee -a ${LOGFILE}
1423
1424
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1425
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1426
1427 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
1428 6f73c362 Renato Botelho
	# Activate serial console in boot.config
1429
	if [ -f ${BOOTCONF} ]; then
1430
		sed -i "" '/-D$/d' ${BOOTCONF}
1431
	fi
1432
	echo "-S115200 -D" >> ${BOOTCONF}
1433
1434
	# Remove old console options if present.
1435
	[ -f "${LOADERCONF}" ] \
1436
		&& sed -i "" -Ee "/(console|boot_multicons|boot_serial)/d" ${LOADERCONF}
1437
	# Activate serial console+video console in loader.conf
1438
	echo 'boot_multicons="YES"' >>  ${LOADERCONF}
1439
	echo 'boot_serial="YES"' >> ${LOADERCONF}
1440
	echo 'console="comconsole,vidconsole"' >> ${LOADERCONF}
1441
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
1442
1443
	create_distribution_tarball
1444
1445 3080d476 Renato Botelho
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${MEMSTICKSERIALPATH} ${FINAL_CHROOT_DIR}
1446 6f73c362 Renato Botelho
	if [ $? -ne 0 ]; then
1447
		if [ -f ${MEMSTICKSERIALPATH} ]; then
1448
			rm -f $MEMSTICKSERIALPATH
1449
		fi
1450
		echo ">>> ERROR: Something wrong happened during MEMSTICKSERIAL image creation. STOPPING!" | tee -a ${LOGFILE}
1451
		print_error_pfS
1452
	fi
1453
	MD=$(mdconfig -a -t vnode -f $MEMSTICKSERIALPATH)
1454
	# Just in case
1455
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1456
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1457
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1458
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1459
	trap "-" 1 2 15 EXIT
1460
	mdconfig -d -u ${MD} 2>&1 >> ${LOGFILE}
1461
	gzip -qf $MEMSTICKSERIALPATH &
1462 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1463 6f73c362 Renato Botelho
1464
	echo ">>> MEMSTICKSERIAL created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1465
}
1466
1467
create_memstick_adi_image() {
1468 462cbb5a Renato Botelho
	LOGFILE=${BUILDER_LOGS}/memstickadi.${TARGET}
1469 6f73c362 Renato Botelho
	if [ "${MEMSTICKADIPATH}" = "" ]; then
1470
		echo ">>> MEMSTICKADIPATH is empty skipping generation of memstick image!" | tee -a ${LOGFILE}
1471
		return
1472
	fi
1473
1474 624376f6 Renato Botelho
	customize_stagearea_for_image "memstickadi"
1475
	install_default_kernel ${DEFAULT_KERNEL}
1476 6f73c362 Renato Botelho
1477
	echo cdrom > $FINAL_CHROOT_DIR/etc/platform
1478
1479
	echo "/dev/ufs/${PRODUCT_NAME} / ufs ro 0 0" > ${FINAL_CHROOT_DIR}/etc/fstab
1480
	echo "kern.cam.boot_delay=10000" >> ${FINAL_CHROOT_DIR}/boot/loader.conf.local
1481
1482
	echo ">>> Creating serial memstick to ${MEMSTICKADIPATH}." 2>&1 | tee -a ${LOGFILE}
1483
1484
	BOOTCONF=${FINAL_CHROOT_DIR}/boot.config
1485
	LOADERCONF=${FINAL_CHROOT_DIR}/boot/loader.conf
1486
1487 230372b6 Renato Botelho
	echo ">>> Activating serial console..." 2>&1 | tee -a ${LOGFILE}
1488 6f73c362 Renato Botelho
	# Activate serial console in boot.config
1489
	if [ -f ${BOOTCONF} ]; then
1490
		sed -i "" '/-[Dh]$/d' ${BOOTCONF}
1491
	fi
1492
	echo "-S115200 -h" >> ${BOOTCONF}
1493
1494
	# Remove old console options if present.
1495
	[ -f "${LOADERCONF}" ] \
1496
		&& sed -i "" -Ee "/(console|boot_multicons|boot_serial|hint.uart)/d" ${LOADERCONF}
1497
	# Activate serial console+video console in loader.conf
1498
	echo 'boot_serial="YES"' >> ${LOADERCONF}
1499
	echo 'console="comconsole"' >> ${LOADERCONF}
1500
	echo 'comconsole_speed="115200"' >> ${LOADERCONF}
1501
	echo 'comconsole_port="0x2F8"' >> ${LOADERCONF}
1502
	echo 'hint.uart.0.flags="0x00"' >> ${LOADERCONF}
1503
	echo 'hint.uart.1.flags="0x10"' >> ${LOADERCONF}
1504
1505
	create_distribution_tarball
1506
1507 3080d476 Renato Botelho
	makefs -B little -o label=${PRODUCT_NAME},version=2 ${MEMSTICKADIPATH} ${FINAL_CHROOT_DIR}
1508 6f73c362 Renato Botelho
	if [ $? -ne 0 ]; then
1509
		if [ -f ${MEMSTICKADIPATH} ]; then
1510
			rm -f $MEMSTICKADIPATH
1511
		fi
1512
		echo ">>> ERROR: Something wrong happened during MEMSTICKADI image creation. STOPPING!" | tee -a ${LOGFILE}
1513
		print_error_pfS
1514
	fi
1515
	MD=$(mdconfig -a -t vnode -f $MEMSTICKADIPATH)
1516
	# Just in case
1517
	trap "mdconfig -d -u ${MD}" 1 2 15 EXIT
1518
	gpart create -s BSD ${MD} 2>&1 >> ${LOGFILE}
1519
	gpart bootcode -b ${FINAL_CHROOT_DIR}/boot/boot ${MD} 2>&1 >> ${LOGFILE}
1520
	gpart add -t freebsd-ufs ${MD} 2>&1 >> ${LOGFILE}
1521
	trap "-" 1 2 15 EXIT
1522
	mdconfig -d -u ${MD} 2>&1 >> ${LOGFILE}
1523
	gzip -qf $MEMSTICKADIPATH &
1524 39ef49f6 Renato Botelho
	_bg_pids="${_bg_pids}${_bg_pids:+ }$!"
1525 6f73c362 Renato Botelho
1526
	echo ">>> MEMSTICKADI created: $(LC_ALL=C date)" | tee -a ${LOGFILE}
1527
}
1528
1529
# Create pkg conf on desired place with desired arch/branch
1530
setup_pkg_repo() {
1531 ebf99088 Renato Botelho
	if [ -z "${5}" ]; then
1532 6f73c362 Renato Botelho
		return
1533
	fi
1534
1535 db8621d8 Renato Botelho
	local _template="${1}"
1536
	local _target="${2}"
1537
	local _arch="${3}"
1538
	local _target_arch="${4}"
1539
	local _branch="${5}"
1540 97fd32ba Renato Botelho
	local _pkg_repo_server="${6}"
1541 52efc840 Renato Botelho
1542
	if [ -z "${_template}" -o ! -f "${_template}" ]; then
1543
		echo ">>> ERROR: It was not possible to find pkg conf template ${_template}"
1544
		print_error_pfS
1545
	fi
1546 6f73c362 Renato Botelho
1547 a44dea83 Renato Botelho
	if [ -z "${_pkg_repo_server}" ]; then
1548 97fd32ba Renato Botelho
		_pkg_repo_server=${PKG_REPO_SERVER}
1549 2c27ff24 Renato Botelho
	fi
1550 97fd32ba Renato Botelho
1551 6f73c362 Renato Botelho
	mkdir -p $(dirname ${_target}) >/dev/null 2>&1
1552
1553
	sed \
1554 ab225f2b Renato Botelho
		-e "s/%%ARCH%%/${_target_arch}/" \
1555 6f73c362 Renato Botelho
		-e "s/%%GIT_REPO_BRANCH_OR_TAG%%/${_branch}/g" \
1556 97fd32ba Renato Botelho
		-e "s,%%PKG_REPO_SERVER%%,${_pkg_repo_server},g" \
1557 6f73c362 Renato Botelho
		-e "s/%%PRODUCT_NAME%%/${PRODUCT_NAME}/g" \
1558 52efc840 Renato Botelho
		${_template} \
1559 6f73c362 Renato Botelho
		> ${_target}
1560
}
1561
1562
# This routine ensures any ports / binaries that the builder
1563
# system needs are on disk and ready for execution.
1564
builder_setup() {
1565
	# If Product-builder is already installed, just leave
1566
	if pkg info -e -q ${PRODUCT_NAME}-builder; then
1567
		return
1568
	fi
1569
1570 52efc840 Renato Botelho
	if [ ! -f ${PKG_REPO_PATH} ]; then
1571
		[ -d $(dirname ${PKG_REPO_PATH}) ] \
1572
			|| mkdir -p $(dirname ${PKG_REPO_PATH})
1573 6f73c362 Renato Botelho
1574 d2f163a6 Renato Botelho
		update_freebsd_sources
1575
1576 6f73c362 Renato Botelho
		local _arch=$(uname -m)
1577 0c096dad Renato Botelho
		setup_pkg_repo \
1578 db8621d8 Renato Botelho
			${PKG_REPO_DEFAULT} \
1579 52efc840 Renato Botelho
			${PKG_REPO_PATH} \
1580 0c096dad Renato Botelho
			${_arch} \
1581
			${_arch} \
1582 db8621d8 Renato Botelho
			${PKG_REPO_CONF_BRANCH}
1583 569929ef Renato Botelho
1584
		# Use fingerprint keys from repo
1585
		sed -i '' -e "/fingerprints:/ s,\"/,\"${BUILDER_ROOT}/src/," \
1586
			${PKG_REPO_PATH}
1587 6f73c362 Renato Botelho
	fi
1588
1589
	pkg install ${PRODUCT_NAME}-builder
1590
}
1591
1592
# Updates FreeBSD sources
1593
update_freebsd_sources() {
1594
	if [ "${1}" = "full" ]; then
1595
		local _full=1
1596
		local _clone_params=""
1597
	else
1598
		local _full=0
1599
		local _clone_params="--depth 1 --single-branch"
1600
	fi
1601
1602 64f27203 Renato Botelho
	if [ ! -d "${FREEBSD_SRC_DIR}" ]; then
1603
		mkdir -p ${FREEBSD_SRC_DIR}
1604 6f73c362 Renato Botelho
	fi
1605
1606 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDWORLD}" -a -n "${NO_BUILDKERNEL}" ]; then
1607 6f73c362 Renato Botelho
		echo ">>> NO_BUILDWORLD and NO_BUILDKERNEL set, skipping update of freebsd sources" | tee -a ${LOGFILE}
1608
		return
1609
	fi
1610
1611
	echo -n ">>> Obtaining FreeBSD sources ${FREEBSD_BRANCH}..."
1612
	local _FREEBSD_BRANCH=${FREEBSD_BRANCH:-"devel"}
1613
	local _CLONE=1
1614
1615 64f27203 Renato Botelho
	if [ -d "${FREEBSD_SRC_DIR}/.git" ]; then
1616
		CUR_BRANCH=$(cd ${FREEBSD_SRC_DIR} && git branch | grep '^\*' | cut -d' ' -f2)
1617 6f73c362 Renato Botelho
		if [ ${_full} -eq 0 -a "${CUR_BRANCH}" = "${_FREEBSD_BRANCH}" ]; then
1618
			_CLONE=0
1619 7d26baf3 Renato Botelho
			( cd ${FREEBSD_SRC_DIR} && git clean -fd; git fetch origin; git reset --hard origin/${_FREEBSD_BRANCH} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1620 6f73c362 Renato Botelho
		else
1621 64f27203 Renato Botelho
			rm -rf ${FREEBSD_SRC_DIR}
1622 6f73c362 Renato Botelho
		fi
1623
	fi
1624
1625
	if [ ${_CLONE} -eq 1 ]; then
1626 64f27203 Renato Botelho
		( git clone --branch ${_FREEBSD_BRANCH} ${_clone_params} ${FREEBSD_REPO_BASE} ${FREEBSD_SRC_DIR} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1627 6f73c362 Renato Botelho
	fi
1628
1629 64f27203 Renato Botelho
	if [ ! -d "${FREEBSD_SRC_DIR}/.git" ]; then
1630 6f73c362 Renato Botelho
		echo ">>> ERROR: It was not possible to clone FreeBSD src repo"
1631
		print_error_pfS
1632
	fi
1633
1634
	if [ -n "${GIT_FREEBSD_COSHA1}" ]; then
1635 64f27203 Renato Botelho
		( cd ${FREEBSD_SRC_DIR} && git checkout ${GIT_FREEBSD_COSHA1} ) 2>&1 | grep -C3 -i -E 'error|fatal'
1636 6f73c362 Renato Botelho
	fi
1637
	echo "Done!"
1638
}
1639
1640
pkg_chroot() {
1641
	local _root="${1}"
1642
	shift
1643
1644
	if [ $# -eq 0 ]; then
1645
		return -1
1646
	fi
1647
1648
	if [ -z "${_root}" -o "${_root}" = "/" -o ! -d "${_root}" ]; then
1649
		return -1
1650
	fi
1651
1652
	mkdir -p \
1653
		${SCRATCHDIR}/pkg_cache \
1654
		${_root}/var/cache/pkg \
1655
		${_root}/dev
1656
1657
	/sbin/mount -t nullfs ${SCRATCHDIR}/pkg_cache ${_root}/var/cache/pkg
1658
	/sbin/mount -t devfs devfs ${_root}/dev
1659
	cp -f /etc/resolv.conf ${_root}/etc/resolv.conf
1660
	touch ${BUILDER_LOGS}/install_pkg_install_ports.txt
1661
	script -aq ${BUILDER_LOGS}/install_pkg_install_ports.txt pkg -c ${_root} $@ >/dev/null 2>&1
1662
	rm -f ${_root}/etc/resolv.conf
1663
	/sbin/umount -f ${_root}/dev
1664
	/sbin/umount -f ${_root}/var/cache/pkg
1665
}
1666
1667
1668
pkg_chroot_add() {
1669
	if [ -z "${1}" -o -z "${2}" ]; then
1670
		return 1
1671
	fi
1672
1673
	local _target="${1}"
1674
	local _pkg="$(get_pkg_name ${2}).txz"
1675
1676
	if [ ! -d "${_target}" ]; then
1677
		echo ">>> ERROR: Target dir ${_target} not found"
1678
		print_error_pfS
1679
	fi
1680
1681 da781042 Renato Botelho
	if [ ! -f ${CORE_PKG_REAL_PATH}/All/${_pkg} ]; then
1682 6f73c362 Renato Botelho
		echo ">>> ERROR: Package ${_pkg} not found"
1683
		print_error_pfS
1684
	fi
1685
1686 da781042 Renato Botelho
	cp ${CORE_PKG_REAL_PATH}/All/${_pkg} ${_target}
1687 6f73c362 Renato Botelho
	pkg_chroot ${_target} add /${_pkg}
1688
	rm -f ${_target}/${_pkg}
1689
}
1690
1691
pkg_bootstrap() {
1692
	local _root=${1:-"${STAGE_CHROOT_DIR}"}
1693
1694 0ada33a9 Renato Botelho
	setup_pkg_repo \
1695 db8621d8 Renato Botelho
		${PKG_REPO_DEFAULT} \
1696 52efc840 Renato Botelho
		${_root}${PKG_REPO_PATH} \
1697 0ada33a9 Renato Botelho
		${TARGET} \
1698
		${TARGET_ARCH} \
1699 db8621d8 Renato Botelho
		${PKG_REPO_CONF_BRANCH}
1700 6f73c362 Renato Botelho
1701
	pkg_chroot ${_root} bootstrap -f
1702
}
1703
1704
# This routine assists with installing various
1705 97bc6c78 Phil Davis
# freebsd ports files into the pfsense-fs staging
1706 6f73c362 Renato Botelho
# area.
1707
install_pkg_install_ports() {
1708
	local MAIN_PKG="${1}"
1709
1710
	if [ -z "${MAIN_PKG}" ]; then
1711
		MAIN_PKG=${PRODUCT_NAME}
1712
	fi
1713
1714
	echo ">>> Installing pkg repository in chroot (${STAGE_CHROOT_DIR})..."
1715
1716
	[ -d ${STAGE_CHROOT_DIR}/var/cache/pkg ] || \
1717
		mkdir -p ${STAGE_CHROOT_DIR}/var/cache/pkg
1718
1719
	[ -d ${SCRATCHDIR}/pkg_cache ] || \
1720
		mkdir -p ${SCRATCHDIR}/pkg_cache
1721
1722
	echo ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... (starting)"
1723 cdac78f6 Renato Botelho
	# First mark all packages as automatically installed
1724
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 1 -a
1725
	# Install all necessary packages
1726 6f73c362 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} install ${MAIN_PKG} ${custom_package_list}
1727 cdac78f6 Renato Botelho
	# Make sure required packages are set as non-automatic
1728 4b0f15b6 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} set -A 0 pkg ${MAIN_PKG} ${custom_package_list}
1729 cdac78f6 Renato Botelho
	# Remove unnecessary packages
1730 6f73c362 Renato Botelho
	pkg_chroot ${STAGE_CHROOT_DIR} autoremove
1731
	echo ">>> Installing built ports (packages) in chroot (${STAGE_CHROOT_DIR})... (finshied)"
1732
}
1733
1734
install_bsdinstaller() {
1735 0f2bafab Renato Botelho
	local _params=""
1736
1737
	# Use staging repo on RELEASE
1738
	if [ -n "${_IS_RELEASE}" ]; then
1739
		mkdir -p ${FINAL_CHROOT_DIR}/tmp/pkg-repo
1740
		cp -f ${STAGE_CHROOT_DIR}${PKG_REPO_PATH} \
1741
			${FINAL_CHROOT_DIR}/tmp/pkg-repo
1742
		_params="--repo-conf-dir /tmp/pkg-repo "
1743
	fi
1744
1745 6f73c362 Renato Botelho
	echo ">>> Installing BSDInstaller in chroot (${FINAL_CHROOT_DIR})... (starting)"
1746 0f2bafab Renato Botelho
	pkg_chroot ${FINAL_CHROOT_DIR} ${_params}install -f bsdinstaller
1747 490920ef Renato Botelho
	sed -i '' -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME}," \
1748
		  -e "s,%%PRODUCT_VERSION%%,${PRODUCT_VERSION}," \
1749
		  -e "s,%%ARCH%%,${TARGET}," \
1750
		  ${FINAL_CHROOT_DIR}/usr/local/share/dfuibe_lua/conf/pfSense.lua \
1751
		  ${FINAL_CHROOT_DIR}/usr/local/share/dfuibe_lua/conf/pfSense_rescue.lua
1752 0f2bafab Renato Botelho
	if [ -n "${_IS_RELEASE}" ]; then
1753
		rm -rf ${FINAL_CHROOT_DIR}/tmp/pkg-repo
1754
	fi
1755 6f73c362 Renato Botelho
	echo ">>> Installing BSDInstaller in chroot (${FINAL_CHROOT_DIR})... (finished)"
1756
}
1757
1758
staginareas_clean_each_run() {
1759
	echo -n ">>> Cleaning build directories: "
1760
	if [ -d "${FINAL_CHROOT_DIR}" ]; then
1761
		BASENAME=$(basename ${FINAL_CHROOT_DIR})
1762
		echo -n "$BASENAME "
1763
		chflags -R noschg ${FINAL_CHROOT_DIR} 2>&1 >/dev/null
1764 f546e6ca Renato Botelho
		rm -rf ${FINAL_CHROOT_DIR}/* 2>/dev/null
1765 6f73c362 Renato Botelho
	fi
1766
	echo "Done!"
1767
}
1768
1769
# Imported from FreeSBIE
1770
buildkernel() {
1771 d4c6029e Renato Botelho
	if [ -n "${NO_BUILDKERNEL}" ]; then
1772 6f73c362 Renato Botelho
		echo ">>> NO_BUILDKERNEL set, skipping build" | tee -a ${LOGFILE}
1773
		return
1774
	fi
1775
1776 d4c6029e Renato Botelho
	if [ -z "${KERNCONF}" ]; then
1777 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1778
		print_error_pfS
1779
	fi
1780
1781 d4c6029e Renato Botelho
	if [ -n "${KERNELCONF}" ]; then
1782 6f73c362 Renato Botelho
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1783
		export KERNCONF=$(basename ${KERNELCONF})
1784
	fi
1785
1786
	SRCCONFBASENAME=$(basename ${SRC_CONF})
1787
	echo ">>> KERNCONFDIR: ${KERNCONFDIR}"
1788
	echo ">>> ARCH:        ${TARGET}"
1789
	echo ">>> SRC_CONF:    ${SRCCONFBASENAME}"
1790
1791 d4c6029e Renato Botelho
	makeargs="${MAKEJ_KERNEL} SRCCONF=${SRC_CONF} __MAKE_CONF=${MAKE_CONF} TARGET_ARCH=${TARGET_ARCH} TARGET=${TARGET}"
1792 6f73c362 Renato Botelho
	echo ">>> Builder is running the command: script -aq $LOGFILE make -DNO_KERNELCLEAN $makeargs buildkernel KERNCONF=${KERNCONF}" | tee -a $LOGFILE
1793 64f27203 Renato Botelho
	(script -q $LOGFILE make -C ${FREEBSD_SRC_DIR} -DNO_KERNELCLEAN $makeargs buildkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1794 6f73c362 Renato Botelho
}
1795
1796
# Imported from FreeSBIE
1797
installkernel() {
1798 d4c6029e Renato Botelho
	if [ -z "${KERNCONF}" ]; then
1799 6f73c362 Renato Botelho
		echo ">>> ERROR: No kernel configuration defined probably this is not what you want! STOPPING!" | tee -a ${LOGFILE}
1800
		print_error_pfS
1801
	fi
1802
1803 d4c6029e Renato Botelho
	if [ -n "${KERNELCONF}" ]; then
1804 6f73c362 Renato Botelho
		export KERNCONFDIR=$(dirname ${KERNELCONF})
1805
		export KERNCONF=$(basename ${KERNELCONF})
1806
	fi
1807
1808
	mkdir -p ${STAGE_CHROOT_DIR}/boot
1809 d4c6029e Renato Botelho
	makeargs="${MAKEJ_KERNEL} SRCCONF=${SRC_CONF} __MAKE_CONF=${MAKE_CONF} TARGET_ARCH=${TARGET_ARCH} TARGET=${TARGET} DESTDIR=${KERNEL_DESTDIR}"
1810
	echo ">>> Builder is running the command: script -aq $LOGFILE make ${makeargs} installkernel KERNCONF=${KERNCONF}"  | tee -a $LOGFILE
1811
	(script -aq $LOGFILE make -C ${FREEBSD_SRC_DIR} ${makeargs} installkernel KERNCONF=${KERNCONF} || print_error_pfS;) | egrep '^>>>'
1812 6f73c362 Renato Botelho
	gzip -f9 $KERNEL_DESTDIR/boot/kernel/kernel
1813
}
1814
1815
# Launch is ran first to setup a few variables that we need
1816
# Imported from FreeSBIE
1817
launch() {
1818
	if [ "$(id -u)" != "0" ]; then
1819
		echo "Sorry, this must be done as root."
1820
	fi
1821
1822
	echo ">>> Operation $0 has started at $(date)"
1823
}
1824
1825
finish() {
1826
	echo ">>> Operation $0 has ended at $(date)"
1827
}
1828
1829 184b39e1 Renato Botelho
pkg_repo_rsync() {
1830
	local _repo_path="${1}"
1831
1832 f4575189 Renato Botelho
	if [ -z "${_repo_path}" -o ! -d "${_repo_path}" ]; then
1833 184b39e1 Renato Botelho
		return
1834
	fi
1835
1836
	if [ -z "${LOGFILE}" ]; then
1837
		local _logfile="/dev/null"
1838
	else
1839
		local _logfile="${LOGFILE}"
1840
	fi
1841
1842 f4575189 Renato Botelho
	if [ -n "${PKG_REPO_SIGNING_COMMAND}" ]; then
1843 efcf5e2a Renato Botelho
1844
		# Detect poudriere directory structure
1845
		if [ -L "${_repo_path}/.latest" ]; then
1846
			local _real_repo_path=$(readlink -f ${_repo_path}/.latest)
1847
		else
1848
			local _real_repo_path=${_repo_path}
1849
		fi
1850
1851 f4575189 Renato Botelho
		echo -n ">>> Signing repository... " | tee -a ${_logfile}
1852 efcf5e2a Renato Botelho
		############ ATTENTION ##############
1853
		#
1854
		# For some reason pkg-repo fail without / in the end of directory name
1855
		# so removing it will break command
1856
		#
1857 c2af1487 Renato Botelho
		# https://github.com/freebsd/pkg/issues/1364
1858
		#
1859 efcf5e2a Renato Botelho
		if script -aq ${_logfile} pkg repo ${_real_repo_path}/ \
1860 f4575189 Renato Botelho
		    signing_command: ${PKG_REPO_SIGNING_COMMAND} >/dev/null 2>&1; then
1861
			echo "Done!" | tee -a ${_logfile}
1862
		else
1863
			echo "Failed!" | tee -a ${_logfile}
1864
			echo ">>> ERROR: An error occurred trying to sign repo"
1865
			print_error_pfS
1866
		fi
1867
1868
		local _pkgfile="${_repo_path}/Latest/pkg.txz"
1869
		if [ -e ${_pkgfile} ]; then
1870
			echo -n ">>> Signing Latest/pkg.txz for bootstraping... " | tee -a ${_logfile}
1871
1872
			if sha256 -q ${_pkgfile} | ${PKG_REPO_SIGNING_COMMAND} \
1873
			    > ${_pkgfile}.sig 2>/dev/null; then
1874
				echo "Done!" | tee -a ${_logfile}
1875
			else
1876
				echo "Failed!" | tee -a ${_logfile}
1877
				echo ">>> ERROR: An error occurred trying to sign Latest/pkg.txz"
1878
				print_error_pfS
1879
			fi
1880
		fi
1881
	fi
1882
1883
	if [ -n "${DO_NOT_UPLOAD}" ]; then
1884
		return
1885
	fi
1886
1887 184b39e1 Renato Botelho
	echo -n ">>> Sending updated repository to ${PKG_RSYNC_HOSTNAME}... " | tee -a ${_logfile}
1888
	if script -aq ${_logfile} rsync -ave "ssh -p ${PKG_RSYNC_SSH_PORT}" \
1889
		--timeout=60 --delete-delay ${_repo_path} \
1890
		${PKG_RSYNC_USERNAME}@${PKG_RSYNC_HOSTNAME}:${PKG_RSYNC_DESTDIR} >/dev/null 2>&1
1891
	then
1892
		echo "Done!" | tee -a ${_logfile}
1893
	else
1894
		echo "Failed!" | tee -a ${_logfile}
1895
		echo ">>> ERROR: An error occurred sending repo to remote hostname"
1896
		print_error_pfS
1897
	fi
1898
}
1899
1900 6f73c362 Renato Botelho
poudriere_create_patch() {
1901 83354dea Renato Botelho
	local _jail_patch="${SCRATCHDIR}/poudriere_jail.${POUDRIERE_BRANCH}.patch"
1902 6f73c362 Renato Botelho
1903
	if [ -z "${FREEBSD_PARENT_BRANCH}" ]; then
1904
		echo ">>> ERROR: FREEBSD_PARENT_BRANCH is not set"
1905
	fi
1906
1907
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1908
1909
	# Get FreeBSD source and apply patches
1910
	update_freebsd_sources full
1911
1912
	[ -f "${_jail_patch}" ] && \
1913
		rm -f "${_jail_patch}"
1914
1915
	# Create a big patch with all our changes to use on jail
1916
	( \
1917 64f27203 Renato Botelho
		cd ${FREEBSD_SRC_DIR} && \
1918 6f73c362 Renato Botelho
		git diff $(git merge-base origin/${FREEBSD_PARENT_BRANCH} ${FREEBSD_BRANCH}) > ${_jail_patch}
1919
	) >/dev/null 2>&1
1920
1921
	# Check if patch was created
1922
	if [ ! -s "${_jail_patch}" ]; then
1923
		echo ">>> ERROR: Patch does not exist or is empty, aborting..." | tee -a ${LOGFILE}
1924
		print_error_pfS
1925
	fi
1926
}
1927
1928
poudriere_possible_archs() {
1929
	local _arch=$(uname -m)
1930
	local _archs="i386.i386"
1931
1932 97bc6c78 Phil Davis
	# If host is amd64, we'll create both repos, and if possible armv6
1933 6f73c362 Renato Botelho
	if [ "${_arch}" = "amd64" ]; then
1934
		_archs="amd64.amd64 ${_archs}"
1935
1936
		if [ -f /usr/local/bin/qemu-arm-static ]; then
1937
			# Make sure binmiscctl is ok
1938
			/usr/local/etc/rc.d/qemu_user_static forcestart >/dev/null 2>&1
1939
1940
			if binmiscctl lookup armv6 >/dev/null 2>&1; then
1941
				_archs="${_archs} arm.armv6"
1942
			fi
1943
		fi
1944
	fi
1945
1946 fb192227 Renato Botelho
	if [ -n "${ARCH_LIST}" ]; then
1947
		local _found=0
1948 4010811b Renato Botelho
		for _desired_arch in ${ARCH_LIST}; do
1949 fb192227 Renato Botelho
			_found=0
1950 4010811b Renato Botelho
			for _possible_arch in ${_archs}; do
1951 fb192227 Renato Botelho
				if [ "${_desired_arch}" = "${_possible_arch}" ]; then
1952
					_found=1
1953
					break
1954
				fi
1955
			done
1956
			if [ ${_found} -eq 0 ]; then
1957
				echo ">>> ERROR: Impossible to build for arch: ${_desired_arch}"
1958
				print_error_pfS
1959
			fi
1960
		done
1961
		_archs="${ARCH_LIST}"
1962
	fi
1963
1964 6f73c362 Renato Botelho
	echo ${_archs}
1965
}
1966
1967
poudriere_jail_name() {
1968
	local _jail_arch="${1}"
1969
1970
	if [ -z "${_jail_arch}" ]; then
1971
		return 1
1972
	fi
1973
1974 0449a11f Renato Botelho
	# Remove arch
1975 83354dea Renato Botelho
	echo "${PRODUCT_NAME}_${POUDRIERE_BRANCH}_${_jail_arch##*.}"
1976 6f73c362 Renato Botelho
}
1977
1978 a3bb036b Renato Botelho
poudriere_rename_ports() {
1979
	if [ "${PRODUCT_NAME}" = "pfSense" ]; then
1980
		return;
1981
	fi
1982
1983
	LOGFILE=${BUILDER_LOGS}/poudriere.log
1984
1985
	local _ports_dir="/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}"
1986
1987
	echo -n ">>> Renaming product ports on ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
1988
	for d in $(find ${_ports_dir} -depth 2 -type d -name '*pfSense*'); do
1989
		local _pdir=$(dirname ${d})
1990
		local _pname=$(echo $(basename ${d}) | sed "s,pfSense,${PRODUCT_NAME},")
1991 c7e1c621 Renato Botelho
		local _plist=""
1992 a3bb036b Renato Botelho
1993
		if [ -e ${_pdir}/${_pname} ]; then
1994
			rm -rf ${_pdir}/${_pname}
1995
		fi
1996
1997
		cp -r ${d} ${_pdir}/${_pname}
1998
1999 c7e1c621 Renato Botelho
		if [ -f ${_pdir}/${_pname}/pkg-plist ]; then
2000
			_plist=${_pdir}/${_pname}/pkg-plist
2001
		fi
2002
2003 a3bb036b Renato Botelho
		sed -i '' -e "s,pfSense,${PRODUCT_NAME},g" \
2004
			  -e "s,https://www.pfsense.org,${PRODUCT_URL},g" \
2005
			  -e "/^MAINTAINER=/ s,^.*$,MAINTAINER=	${PRODUCT_EMAIL}," \
2006
			${_pdir}/${_pname}/Makefile \
2007 c7e1c621 Renato Botelho
			${_pdir}/${_pname}/pkg-descr ${_plist}
2008 a3bb036b Renato Botelho
2009
		# PHP module is special
2010
		if echo "${_pname}" | grep -q "^php[0-9]*-${PRODUCT_NAME}-module"; then
2011
			local _product_capital=$(echo ${PRODUCT_NAME} | tr '[a-z]' '[A-Z]')
2012
			sed -i '' -e "s,PHP_PFSENSE,PHP_${_product_capital},g" \
2013
				  -e "s,PFSENSE_SHARED_LIBADD,${_product_capital}_SHARED_LIBADD,g" \
2014
				  -e "s,pfSense,${PRODUCT_NAME},g" \
2015
				  -e "s,${PRODUCT_NAME}\.c,pfSense.c,g" \
2016
				${_pdir}/${_pname}/files/config.m4
2017
2018
			sed -i '' -e "s,COMPILE_DL_PFSENSE,COMPILE_DL_${_product_capital}," \
2019
				  -e "s,pfSense_module_entry,${PRODUCT_NAME}_module_entry,g" \
2020
				  -e "/ZEND_GET_MODULE/ s,pfSense,${PRODUCT_NAME}," \
2021
				  -e "/PHP_PFSENSE_WORLD_EXTNAME/ s,pfSense,${PRODUCT_NAME}," \
2022
				${_pdir}/${_pname}/files/pfSense.c \
2023
				${_pdir}/${_pname}/files/php_pfSense.h
2024
		fi
2025
2026
		if [ -d ${_pdir}/${_pname}/files ]; then
2027
			for fd in $(find ${_pdir}/${_pname}/files -type d -name '*pfSense*'); do
2028
				local _fddir=$(dirname ${fd})
2029
				local _fdname=$(echo $(basename ${fd}) | sed "s,pfSense,${PRODUCT_NAME},")
2030
2031
				mv ${fd} ${_fddir}/${_fdname}
2032
			done
2033
		fi
2034
	done
2035
	echo "Done!" | tee -a ${LOGFILE}
2036
}
2037
2038 6f73c362 Renato Botelho
poudriere_create_ports_tree() {
2039
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2040
2041
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2042
		local _branch=""
2043
		if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
2044
			echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
2045
			print_error_pfS
2046
		fi
2047
		if [ -n "${POUDRIERE_PORTS_GIT_BRANCH}" ]; then
2048
			_branch="-B ${POUDRIERE_PORTS_GIT_BRANCH}"
2049
		fi
2050
		echo -n ">>> Creating poudriere ports tree, it may take some time... " | tee -a ${LOGFILE}
2051
		if ! script -aq ${LOGFILE} poudriere ports -c -p "${POUDRIERE_PORTS_NAME}" -m git ${_branch} >/dev/null 2>&1; then
2052
			echo "" | tee -a ${LOGFILE}
2053
			echo ">>> ERROR: Error creating poudriere ports tree, aborting..." | tee -a ${LOGFILE}
2054
			print_error_pfS
2055
		fi
2056
		echo "Done!" | tee -a ${LOGFILE}
2057 a3bb036b Renato Botelho
		poudriere_rename_ports
2058 6f73c362 Renato Botelho
	fi
2059
}
2060
2061
poudriere_init() {
2062
	local _error=0
2063
	local _archs=$(poudriere_possible_archs)
2064 83354dea Renato Botelho
	local _jail_patch="${SCRATCHDIR}/poudriere_jail.${POUDRIERE_BRANCH}.patch"
2065 6f73c362 Renato Botelho
2066
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2067
2068
	# Sanity checks
2069
	if [ -z "${ZFS_TANK}" ]; then
2070
		echo ">>> ERROR: \$ZFS_TANK is empty" | tee -a ${LOGFILE}
2071
		error=1
2072
	fi
2073
2074
	if [ -z "${ZFS_ROOT}" ]; then
2075
		echo ">>> ERROR: \$ZFS_ROOT is empty" | tee -a ${LOGFILE}
2076
		error=1
2077
	fi
2078
2079
	if [ -z "${POUDRIERE_PORTS_NAME}" ]; then
2080
		echo ">>> ERROR: \$POUDRIERE_PORTS_NAME is empty" | tee -a ${LOGFILE}
2081
		error=1
2082
	fi
2083
2084
	if [ ${_error} -eq 1 ]; then
2085
		print_error_pfS
2086
	fi
2087
2088
	# Check if zpool exists
2089
	if ! zpool list ${ZFS_TANK} >/dev/null 2>&1; then
2090
		echo ">>> ERROR: ZFS tank ${ZFS_TANK} not found, please create it and try again..." | tee -a ${LOGFILE}
2091
		print_error_pfS
2092
	fi
2093
2094
	# Check if zfs rootfs exists
2095
	if ! zfs list ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
2096 01541076 Renato Botelho
		echo -n ">>> Creating ZFS filesystem ${ZFS_TANK}${ZFS_ROOT}... "
2097 52ec3f66 Renato Botelho
		if zfs create -o atime=off -o mountpoint=/usr/local${ZFS_ROOT} \
2098
		    ${ZFS_TANK}${ZFS_ROOT} >/dev/null 2>&1; then
2099 01541076 Renato Botelho
			echo "Done!"
2100
		else
2101
			echo "Failed!"
2102
			print_error_pfS
2103
		fi
2104 6f73c362 Renato Botelho
	fi
2105
2106
	# Make sure poudriere is installed
2107
	if ! pkg info --quiet poudriere; then
2108
		echo ">>> Installing poudriere..." | tee -a ${LOGFILE}
2109
		if ! pkg install poudriere >/dev/null 2>&1; then
2110
			echo ">>> ERROR: poudriere was not installed, aborting..." | tee -a ${LOGFILE}
2111
			print_error_pfS
2112
		fi
2113
	fi
2114
2115
	# Create poudriere.conf
2116
	if [ -z "${POUDRIERE_PORTS_GIT_URL}" ]; then
2117
		echo ">>> ERROR: POUDRIERE_PORTS_GIT_URL is not defined"
2118
		print_error_pfS
2119
	fi
2120
	echo ">>> Creating poudriere.conf" | tee -a ${LOGFILE}
2121
	cat <<EOF >/usr/local/etc/poudriere.conf
2122
ZPOOL=${ZFS_TANK}
2123
ZROOTFS=${ZFS_ROOT}
2124
RESOLV_CONF=/etc/resolv.conf
2125
BASEFS=/usr/local/poudriere
2126
USE_PORTLINT=no
2127
USE_TMPFS=yes
2128
NOLINUX=yes
2129 86349bee Renato Botelho
DISTFILES_CACHE=/usr/ports/distfiles
2130 6f73c362 Renato Botelho
CHECK_CHANGED_OPTIONS=yes
2131
CHECK_CHANGED_DEPS=yes
2132
ATOMIC_PACKAGE_REPOSITORY=yes
2133
COMMIT_PACKAGES_ON_FAILURE=no
2134 5f64e2ca Renato Botelho
EOF
2135
2136
	# Create specific items conf
2137
	[ ! -d /usr/local/etc/poudriere.d ] \
2138
		&& mkdir -p /usr/local/etc/poudriere.d
2139
2140
	cat <<EOF >/usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-poudriere.conf
2141 6f73c362 Renato Botelho
GIT_URL="${POUDRIERE_PORTS_GIT_URL}"
2142
EOF
2143
2144 fd6cd92b Renato Botelho
	# Create DISTFILES_CACHE if it doesn't exist
2145
	if [ ! -d /usr/ports/distfiles ]; then
2146
		mkdir -p /usr/ports/distfiles
2147
	fi
2148
2149 6f73c362 Renato Botelho
	# Remove old jails
2150
	for jail_arch in ${_archs}; do
2151
		jail_name=$(poudriere_jail_name ${jail_arch})
2152
2153
		if poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2154
			echo ">>> Poudriere jail ${jail_name} already exists, deleting it..." | tee -a ${LOGFILE}
2155
			poudriere jail -d -j "${jail_name}" >/dev/null 2>&1
2156
		fi
2157
	done
2158
2159
	# Remove old ports tree
2160
	if poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2161
		echo ">>> Poudriere ports tree ${POUDRIERE_PORTS_NAME} already exists, deleting it..." | tee -a ${LOGFILE}
2162
		poudriere ports -d -p "${POUDRIERE_PORTS_NAME}"
2163
	fi
2164
2165
	poudriere_create_patch
2166
2167
	local native_xtools=""
2168
	# Now we are ready to create jails
2169
	for jail_arch in ${_archs}; do
2170
		jail_name=$(poudriere_jail_name ${jail_arch})
2171
2172
		if [ "${jail_arch}" = "arm.armv6" ]; then
2173
			native_xtools="-x"
2174
		else
2175
			native_xtools=""
2176
		fi
2177
2178
		echo -n ">>> Creating jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2179
		# XXX: Change -m to git when it's available in poudriere
2180
		if ! script -aq ${LOGFILE} poudriere jail -c -j "${jail_name}" -v ${FREEBSD_PARENT_BRANCH} \
2181
				-a ${jail_arch} -m svn -P ${_jail_patch} ${native_xtools} >/dev/null 2>&1; then
2182
			echo "" | tee -a ${LOGFILE}
2183
			echo ">>> ERROR: Error creating jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2184
			print_error_pfS
2185
		fi
2186
		echo "Done!" | tee -a ${LOGFILE}
2187
	done
2188
2189
	poudriere_create_ports_tree
2190
2191
	echo ">>> Poudriere is now configured!" | tee -a ${LOGFILE}
2192
}
2193
2194
poudriere_update_jails() {
2195
	local _archs=$(poudriere_possible_archs)
2196 83354dea Renato Botelho
	local _jail_patch="${SCRATCHDIR}/poudriere_jail.${POUDRIERE_BRANCH}.patch"
2197 6f73c362 Renato Botelho
2198
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2199
2200
	poudriere_create_patch
2201
2202
	local native_xtools=""
2203
	for jail_arch in ${_archs}; do
2204
		jail_name=$(poudriere_jail_name ${jail_arch})
2205
2206 c136dde1 Renato Botelho
		local _create_or_update="-u"
2207 43f6b081 Renato Botelho
		local _create_or_update_text="Updating"
2208 6f73c362 Renato Botelho
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2209 c136dde1 Renato Botelho
			echo ">>> Poudriere jail ${jail_name} not found, creating..." | tee -a ${LOGFILE}
2210
			_create_or_update="-c -v ${FREEBSD_PARENT_BRANCH} -a ${jail_arch} -m svn"
2211 43f6b081 Renato Botelho
			_create_or_update_text="Creating"
2212 6f73c362 Renato Botelho
		fi
2213
2214
		if [ "${jail_arch}" = "arm.armv6" ]; then
2215
			native_xtools="-x"
2216
		else
2217
			native_xtools=""
2218
		fi
2219
2220 43f6b081 Renato Botelho
		echo -n ">>> ${_create_or_update_text} jail ${jail_name}, it may take some time... " | tee -a ${LOGFILE}
2221 c136dde1 Renato Botelho
		if ! script -aq ${LOGFILE} poudriere jail ${_create_or_update} -j "${jail_name}" -P ${_jail_patch} ${native_xtools} >/dev/null 2>&1; then
2222 6f73c362 Renato Botelho
			echo "" | tee -a ${LOGFILE}
2223 43f6b081 Renato Botelho
			echo ">>> ERROR: Error ${_create_or_update_text} jail ${jail_name}, aborting..." | tee -a ${LOGFILE}
2224 6f73c362 Renato Botelho
			print_error_pfS
2225
		fi
2226
		echo "Done!" | tee -a ${LOGFILE}
2227
	done
2228
}
2229
2230
poudriere_update_ports() {
2231
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2232
2233
	# Create ports tree if necessary
2234
	if ! poudriere ports -l | grep -q -E "^${POUDRIERE_PORTS_NAME}[[:blank:]]"; then
2235
		poudriere_create_ports_tree
2236
	else
2237 97bc6c78 Phil Davis
		echo -n ">>> Resetting local changes on ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2238 d94da298 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" reset --hard >/dev/null 2>&1
2239 7d26baf3 Renato Botelho
		script -aq ${LOGFILE} git -C "/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}" clean -fd >/dev/null 2>&1
2240 d94da298 Renato Botelho
		echo "Done!" | tee -a ${LOGFILE}
2241 6f73c362 Renato Botelho
		echo -n ">>> Updating ports tree ${POUDRIERE_PORTS_NAME}... " | tee -a ${LOGFILE}
2242
		script -aq ${LOGFILE} poudriere ports -u -p "${POUDRIERE_PORTS_NAME}" >/dev/null 2>&1
2243
		echo "Done!" | tee -a ${LOGFILE}
2244 a3bb036b Renato Botelho
		poudriere_rename_ports
2245 6f73c362 Renato Botelho
	fi
2246
}
2247
2248
poudriere_bulk() {
2249
	local _archs=$(poudriere_possible_archs)
2250
2251
	LOGFILE=${BUILDER_LOGS}/poudriere.log
2252
2253 e295b113 Renato Botelho
	if [ -z "${DO_NOT_UPLOAD}" -a -z "${PKG_RSYNC_HOSTNAME}" ]; then
2254
		echo ">>> ERROR: PKG_RSYNC_HOSTNAME is not set"
2255
		print_error_pfS
2256
	fi
2257
2258 6f73c362 Renato Botelho
	poudriere_create_ports_tree
2259
2260
	[ -d /usr/local/etc/poudriere.d ] || \
2261
		mkdir -p /usr/local/etc/poudriere.d
2262
2263 edef37f6 Renato Botelho
	if [ -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" ]; then
2264 0ce2a7f3 Renato Botelho
		cp -f "${BUILDER_TOOLS}/conf/pfPorts/make.conf" /usr/local/etc/poudriere.d/${POUDRIERE_PORTS_NAME}-make.conf
2265 6f73c362 Renato Botelho
	fi
2266
2267 d94da298 Renato Botelho
	# Change version of pfSense meta ports for snapshots
2268
	if [ -z "${_IS_RELEASE}" ]; then
2269 b00400ef Renato Botelho
		local _meta_pkg_version="$(echo "${PRODUCT_VERSION}" | sed 's,DEVELOPMENT,ALPHA,')-${DATESTRING}"
2270
		sed -i '' -e "/^DISTVERSION/ s,^.*,DISTVERSION=	${_meta_pkg_version}," \
2271
			/usr/local/poudriere/ports/${POUDRIERE_PORTS_NAME}/security/${PRODUCT_NAME}/Makefile
2272 d94da298 Renato Botelho
	fi
2273
2274 6f73c362 Renato Botelho
	for jail_arch in ${_archs}; do
2275
		jail_name=$(poudriere_jail_name ${jail_arch})
2276
2277
		if ! poudriere jail -i -j "${jail_name}" >/dev/null 2>&1; then
2278
			echo ">>> Poudriere jail ${jail_name} not found, skipping..." | tee -a ${LOGFILE}
2279
			continue
2280
		fi
2281
2282
		if [ -f "${POUDRIERE_BULK}.${jail_arch}" ]; then
2283
			_ref_bulk="${POUDRIERE_BULK}.${jail_arch}"
2284
		else
2285
			_ref_bulk="${POUDRIERE_BULK}"
2286
		fi
2287
2288 83354dea Renato Botelho
		_bulk=${SCRATCHDIR}/poudriere_bulk.${POUDRIERE_BRANCH}
2289 6f73c362 Renato Botelho
		sed -e "s,%%PRODUCT_NAME%%,${PRODUCT_NAME},g" ${_ref_bulk} > ${_bulk}
2290
2291
		if ! poudriere bulk -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME}; then
2292
			echo ">>> ERROR: Something went wrong..."
2293
			print_error_pfS
2294
		fi
2295
2296
		echo ">>> Cleaning up old packages from repo..."
2297
		if ! poudriere pkgclean -f ${_bulk} -j ${jail_name} -p ${POUDRIERE_PORTS_NAME} -y; then
2298
			echo ">>> ERROR: Something went wrong..."
2299
			print_error_pfS
2300
		fi
2301
2302 97bc6c78 Phil Davis
		# ./ is intentional, it's an rsync trick to make it chdir to directory before sending it
2303 184b39e1 Renato Botelho
		pkg_repo_rsync "/usr/local/poudriere/data/packages/./${jail_name}-${POUDRIERE_PORTS_NAME}"
2304 6f73c362 Renato Botelho
	done
2305
}
2306 ab943fc9 Renato Botelho
2307
# This routine is called to write out to stdout
2308
# a string. The string is appended to $SNAPSHOTSLOGFILE
2309
# and we scp the log file to the builder host if
2310
# needed for the real time logging functions.
2311
snapshots_update_status() {
2312 919c8486 Renato Botelho
	if [ -z "$1" ]; then
2313
		return
2314
	fi
2315
	if [ -z "${SNAPSHOTS}" -a -z "${POUDRIERE_SNAPSHOTS}" ]; then
2316 ab943fc9 Renato Botelho
		return
2317
	fi
2318 0cc93b74 Renato Botelho
	echo "$*"
2319
	echo "`date` -|- $*" >> $SNAPSHOTSLOGFILE
2320 919c8486 Renato Botelho
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2321
		LU=$(cat $SNAPSHOTSLASTUPDATE 2>/dev/null)
2322
		CT=$(date "+%H%M%S")
2323 ab943fc9 Renato Botelho
		# Only update every minute
2324
		if [ "$LU" != "$CT" ]; then
2325 919c8486 Renato Botelho
			ssh ${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP} \
2326
				"mkdir -p ${SNAPSHOTS_RSYNCLOGS}"
2327
			scp -q $SNAPSHOTSLOGFILE \
2328
				${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log
2329 ab943fc9 Renato Botelho
			date "+%H%M%S" > $SNAPSHOTSLASTUPDATE
2330
		fi
2331
	fi
2332
}
2333
2334
# Copy the current log file to $filename.old on
2335
# the snapshot www server (real time logs)
2336
snapshots_rotate_logfile() {
2337 919c8486 Renato Botelho
	if [ -z "${DO_NOT_UPLOAD}" -a -n "${SNAPSHOTS_RSYNCIP}" ]; then
2338
		scp -q $SNAPSHOTSLOGFILE \
2339
			${SNAPSHOTS_RSYNCUSER}@${SNAPSHOTS_RSYNCIP}:${SNAPSHOTS_RSYNCLOGS}/build.log.old
2340 ab943fc9 Renato Botelho
	fi
2341
2342
	# Cleanup log file
2343 ce96c131 Renato Botelho
	rm -f $SNAPSHOTSLOGFILE;    touch $SNAPSHOTSLOGFILE
2344
	rm -f $SNAPSHOTSLASTUPDATE; touch $SNAPSHOTSLASTUPDATE
2345
2346 ab943fc9 Renato Botelho
}
2347
2348 97f7a154 Renato Botelho
create_sha256() {
2349
	local _file="${1}"
2350
2351
	if [ ! -f "${_file}" ]; then
2352
		return 1
2353
	fi
2354
2355
	( \
2356
		cd $(dirname ${_file}) && \
2357
		sha256 $(basename ${_file}) > $(basename ${_file}).sha256 \
2358
	)
2359
}
2360
2361 e28305f4 Renato Botelho
snapshots_create_latest_symlink() {
2362
	local _image="${1}"
2363
2364
	if [ -z "${_image}" ]; then
2365
		return
2366
	fi
2367
2368
	if [ -z "${TIMESTAMP_SUFFIX}" ]; then
2369
		return
2370
	fi
2371
2372 5a944dd5 Renato Botelho
	if [ ! -f "${_image}" ]; then
2373 a4a336b2 Renato Botelho
		return
2374 e28305f4 Renato Botelho
	fi
2375
2376 695ba439 Renato Botelho
	local _symlink=$(echo ${_image} | sed "s,${TIMESTAMP_SUFFIX},-latest,")
2377
	ln -sf $(basename ${_image}) ${_symlink}
2378 e28305f4 Renato Botelho
	ln -sf $(basename ${_image}).sha256 ${_symlink}.sha256
2379
}
2380
2381 ab943fc9 Renato Botelho
snapshots_copy_to_staging_nanobsd() {
2382
	for NANOTYPE in nanobsd nanobsd-vga; do
2383
		for FILESIZE in ${1}; do
2384 acd5c0cd Renato Botelho
			FILENAMEFULL="$(nanobsd_image_filename ${FILESIZE} ${NANOTYPE}).gz"
2385
			FILENAMEUPGRADE="$(nanobsd_image_filename ${FILESIZE} ${NANOTYPE} 1).gz"
2386 ab943fc9 Renato Botelho
			mkdir -p $STAGINGAREA/nanobsd
2387
			mkdir -p $STAGINGAREA/nanobsdupdates
2388
2389 9d811f63 Renato Botelho
			cp -l $IMAGES_FINAL_DIR/$FILENAMEFULL $STAGINGAREA/nanobsd/ 2>/dev/null
2390
			cp -l $IMAGES_FINAL_DIR/$FILENAMEUPGRADE $STAGINGAREA/nanobsdupdates 2>/dev/null
2391 ab943fc9 Renato Botelho
2392
			if [ -f $STAGINGAREA/nanobsd/$FILENAMEFULL ]; then
2393 97f7a154 Renato Botelho
				create_sha256 $STAGINGAREA/nanobsd/$FILENAMEFULL
2394 ab943fc9 Renato Botelho
			fi
2395
			if [ -f $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE ]; then
2396 97f7a154 Renato Botelho
				create_sha256 $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE
2397 ab943fc9 Renato Botelho
			fi
2398
2399
			# Copy NanoBSD auto update:
2400
			if [ -f $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE ]; then
2401 9d811f63 Renato Botelho
				cp -l $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE $STAGINGAREA/latest-${NANOTYPE}-$FILESIZE.img.gz 2>/dev/null
2402 97f7a154 Renato Botelho
				create_sha256 $STAGINGAREAA/latest-${NANOTYPE}-$FILESIZE.img.gz
2403 ab943fc9 Renato Botelho
				# NOTE: Updates need a file with output similar to date output
2404
				# Use the file generated at start of snapshots_dobuilds() to be consistent on times
2405
				cp $BUILTDATESTRINGFILE $STAGINGAREA/version-${NANOTYPE}-$FILESIZE
2406
			fi
2407
		done
2408
	done
2409
}
2410
2411
snapshots_copy_to_staging_iso_updates() {
2412 87925fd1 Renato Botelho
	local _img=""
2413 ab943fc9 Renato Botelho
2414 8c333b4d Renato Botelho
	for _img in ${ISOPATH} ${MEMSTICKPATH} ${MEMSTICKSERIALPATH} ${MEMSTICKADIPATH}; do
2415 87925fd1 Renato Botelho
		if [ ! -f "${_img}.gz" ]; then
2416
			continue
2417
		fi
2418 695ba439 Renato Botelho
		_img="${_img}.gz"
2419 97f7a154 Renato Botelho
		create_sha256 ${_img}
2420 9d811f63 Renato Botelho
		cp -l ${_img}* $STAGINGAREA/ 2>/dev/null
2421 87925fd1 Renato Botelho
		snapshots_create_latest_symlink ${STAGINGAREA}/$(basename ${_img})
2422
	done
2423
2424 812bbf0d Renato Botelho
	if [ -f "${UPDATES_TARBALL_FILENAME}" ]; then
2425 97f7a154 Renato Botelho
		create_sha256 ${UPDATES_TARBALL_FILENAME}
2426 812bbf0d Renato Botelho
		cp -l ${UPDATES_TARBALL_FILENAME}* $STAGINGAREA/ 2>/dev/null
2427
		snapshots_create_latest_symlink ${STAGINGAREA}/$(basename ${UPDATES_TARBALL_FILENAME})
2428
	fi
2429
2430 4119c74b Renato Botelho
	if [ -f "${OVAPATH}" ]; then
2431
		mkdir -p ${STAGINGAREA}/virtualization
2432 97f7a154 Renato Botelho
		create_sha256 ${OVAPATH}
2433 4119c74b Renato Botelho
		cp -l ${OVAPATH}* $STAGINGAREA/virtualization 2>/dev/null
2434
		snapshots_create_latest_symlink ${STAGINGAREA}/virtualization/$(basename ${OVAPATH})
2435
	fi
2436
2437 ab943fc9 Renato Botelho
	# NOTE: Updates need a file with output similar to date output
2438
	# Use the file generated at start of snapshots_dobuilds() to be consistent on times
2439
	if [ -z "${_IS_RELEASE}" ]; then
2440
		cp $BUILTDATESTRINGFILE $STAGINGAREA/version 2>/dev/null
2441
	fi
2442
}
2443
2444
snapshots_scp_files() {
2445 d4c6029e Renato Botelho
	if [ -z "${RSYNC_COPY_ARGUMENTS}" ]; then
2446 ab943fc9 Renato Botelho
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60 --bwlimit=${RSYNCKBYTELIMIT}" #--bwlimit=50
2447
	fi
2448 48396120 Renato Botelho
2449
	snapshots_update_status ">>> Copying core pkg repo to ${PKG_RSYNC_HOSTNAME}"
2450 97bc6c78 Phil Davis
	# Add ./ before last directory, it's an rsync trick to make it chdir to parent directory before sending
2451 784206e8 Renato Botelho
	pkg_repo_rsync $(echo "${CORE_PKG_PATH}" | sed -E 's,/$,,; s,/([^/]*)$,/./\1,')
2452 48396120 Renato Botelho
	snapshots_update_status ">>> Finished copying core pkg repo"
2453
2454 ab943fc9 Renato Botelho
	snapshots_update_status ">>> Copying files to ${RSYNCIP}"
2455
2456
	# Ensure directory(s) are available
2457 987712e8 jim-p
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/installer"
2458 ab943fc9 Renato Botelho
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/updates"
2459
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/nanobsd"
2460
	if [ -d $STAGINGAREA/virtualization ]; then
2461
		ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/virtualization"
2462
	fi
2463
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/.updaters"
2464
	# ensure permissions are correct for r+w
2465
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw ${RSYNCPATH}/."
2466 e5d0f71a Renato Botelho
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}${PRODUCT_NAME_SUFFIX}-*iso* \
2467 987712e8 jim-p
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/installer/
2468 e5d0f71a Renato Botelho
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}${PRODUCT_NAME_SUFFIX}-memstick* \
2469 987712e8 jim-p
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/installer/
2470 e5d0f71a Renato Botelho
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}${PRODUCT_NAME_SUFFIX}-*Update* \
2471 ab943fc9 Renato Botelho
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/updates/
2472
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/nanobsd/* \
2473
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/nanobsd/
2474
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/nanobsdupdates/* \
2475
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/updates/
2476
	if [ -d $STAGINGAREA/virtualization ]; then
2477
		rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/virtualization/* \
2478
			${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/virtualization/
2479
	fi
2480
2481
	# Rather than copy these twice, use ln to link to the latest one.
2482
2483
	ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest.tgz"
2484
	ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest.tgz.sha256"
2485
2486 5e4f6768 Renato Botelho
	LATESTFILENAME=$(basename ${UPDATES_TARBALL_FILENAME})
2487 ab943fc9 Renato Botelho
	ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${LATESTFILENAME} \
2488
		${RSYNCPATH}/.updaters/latest.tgz"
2489
	ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${LATESTFILENAME}.sha256 \
2490
		${RSYNCPATH}/.updaters/latest.tgz.sha256"
2491
2492 2957ba34 Renato Botelho
	for i in ${FLASH_SIZE}
2493 ab943fc9 Renato Botelho
	do
2494
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz"
2495
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz.sha256"
2496
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz"
2497
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz.sha256"
2498
2499 acd5c0cd Renato Botelho
		FILENAMEUPGRADE="$(nanobsd_image_filename ${i} nanobsd 1).gz"
2500 ab943fc9 Renato Botelho
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE} \
2501
			${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz"
2502
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE}.sha256 \
2503
			${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz.sha256"
2504
2505 acd5c0cd Renato Botelho
		FILENAMEUPGRADE="$(nanobsd_image_filename ${i} nanobsd-vga 1).gz"
2506 ab943fc9 Renato Botelho
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE} \
2507
			${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz"
2508
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE}.sha256 \
2509
			${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz.sha256"
2510
	done
2511
2512
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/version* \
2513
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/.updaters
2514
	snapshots_update_status ">>> Finished copying files."
2515
}