Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# build.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions are met:
11
#
12
# 1. Redistributions of source code must retain the above copyright notice,
13
#    this list of conditions and the following disclaimer.
14
#
15
# 2. Redistributions in binary form must reproduce the above copyright
16
#    notice, this list of conditions and the following disclaimer in
17
#    the documentation and/or other materials provided with the
18
#    distribution.
19
#
20
# 3. All advertising materials mentioning features or use of this software
21
#    must display the following acknowledgment:
22
#    "This product includes software developed by the pfSense Project
23
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
#
25
# 4. The names "pfSense" and "pfSense Project" must not be used to
26
#    endorse or promote products derived from this software without
27
#    prior written permission. For written permission, please contact
28
#    coreteam@pfsense.org.
29
#
30
# 5. Products derived from this software may not be called "pfSense"
31
#    nor may "pfSense" appear in their names without prior written
32
#    permission of the Electric Sheep Fencing, LLC.
33
#
34
# 6. Redistributions of any form whatsoever must retain the following
35
#    acknowledgment:
36
#
37
# "This product includes software developed by the pfSense Project
38
# for use in the pfSense software distribution (http://www.pfsense.org/).
39
#
40
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
# OF THE POSSIBILITY OF SUCH DAMAGE.
52

    
53
set +e
54
usage() {
55
	echo "Usage $0 [options] [ iso | nanobsd | ova | nanobsd-vga | memstick | memstickserial | memstickadi | fullupdate | all | none ]"
56
	echo "		all = iso nanobsd nanobsd-vga memstick memstickserial memstickadi fullupdate"
57
	echo "		none = upgrade only pkg repo"
58
	echo "	[ options ]: "
59
	echo "		--flash-size|-f size(s) - a list of flash sizes to build with nanobsd i.e. '2g 4g'. Default: 2g"
60
	echo "		--no-buildworld|-c - Will set NO_BUILDWORLD NO_BUILDKERNEL to not build kernel and world"
61
	echo "		--no-cleanobjdir|--no-cleanrepos|-d - Will not clean FreeBSD object built dir to allow restarting a build with NO_CLEAN"
62
	echo "		--resume-image-build|-r - Includes -c -d and also will just move directly to image creation using pre-staged data"
63
	echo "		--setup - Install required repo and ports builder require to work"
64
	echo "		--update-sources - Refetch FreeBSD sources"
65
	echo "		--rsync-repos - rsync pkg repos"
66
	echo "		--print-flags - Show current builder configuration"
67
	echo "		--rsync-snapshots - rsync snapshots images and pkg repos"
68
	echo "		--clean-builder - clean all builder used data/resources"
69
	echo "		--build-kernels - build all configured kernels"
70
	echo "		--build-kernel argument - build specified kernel. Example --build-kernel KERNEL_NAME"
71
	echo "		--install-extra-kernels argument - Put extra kernel(s) under /kernel image directory. Example --install-extra-kernels KERNEL_NAME_WRAP"
72
	echo "		--snapshots - Build snapshots"
73
	echo "		--poudriere-snapshots - Update poudriere packages and send them to PKG_RSYNC_HOSTNAME"
74
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
75
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
76
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
77
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
78
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
79
	echo "		--upload|-U - Upload pkgs and/or snapshots"
80
	echo "		--skip-final-rsync|-i - Skip rsync to final server"
81
	echo "		-V VARNAME - print value of variable VARNAME"
82
	exit 1
83
}
84

    
85
export BUILDER_ROOT=$(realpath $(dirname ${0}))
86
export BUILDER_TOOLS="${BUILDER_ROOT}/tools"
87

    
88
unset _SKIP_REBUILD_PRESTAGE
89
unset _USE_OLD_DATESTRING
90
unset pfPORTTOBUILD
91
unset IMAGETYPE
92
unset UPLOAD
93
unset SNAPSHOTS
94
unset POUDRIERE_SNAPSHOTS
95
unset ARCH_LIST
96
BUILDACTION="images"
97

    
98
# Maybe use options for nocleans etc?
99
while test "$1" != ""; do
100
	case "${1}" in
101
		--no-buildworld|-c)
102
			export NO_BUILDWORLD=YES
103
			export NO_BUILDKERNEL=YES
104
			;;
105
		--no-cleanobjdir|--no-cleanrepos|-d)
106
			export NO_CLEAN_FREEBSD_OBJ=YES
107
			export NO_CLEAN_FREEBSD_SRC=YES
108
			;;
109
		--flash-size|-f)
110
			shift
111
			if [ $# -eq 0 ]; then
112
				echo "--flash-size needs extra parameter."
113
				echo
114
				usage
115
			fi
116
			export FLASH_SIZE="${1}"
117
			;;
118
		--resume-image-build|-r)
119
			export NO_BUILDWORLD=YES
120
			export NO_BUILDKERNEL=YES
121
			export NO_CLEAN_FREEBSD_OBJ=YES
122
			export NO_CLEAN_FREEBSD_SRC=YES
123
			export DO_NOT_SIGN_PKG_REPO=YES
124
			_SKIP_REBUILD_PRESTAGE=YES
125
			_USE_OLD_DATESTRING=YES
126
			;;
127
		--setup)
128
			BUILDACTION="builder_setup"
129
			;;
130
		--rsync-repos)
131
			BUILDACTION="rsync_repos"
132
			export DO_NOT_SIGN_PKG_REPO=YES
133
			;;
134
		--rsync-snapshots)
135
			BUILDACTION="rsync_snapshots"
136
			export DO_NOT_SIGN_PKG_REPO=YES
137
			;;
138
		--build-kernels)
139
			BUILDACTION="buildkernels"
140
			;;
141
		--install-extra-kernels)
142
			shift
143
			if [ $# -eq 0 ]; then
144
				echo "--build-kernel needs extra parameter."
145
				echo
146
				usage
147
			fi
148
			export INSTALL_EXTRA_KERNELS="${1}"
149
			;;
150
		--snapshots)
151
			export SNAPSHOTS=1
152
			;;
153
		--poudriere-snapshots)
154
			export POUDRIERE_SNAPSHOTS=1
155
			;;
156
		--build-kernel)
157
			BUILDACTION="buildkernel"
158
			shift
159
			if [ $# -eq 0 ]; then
160
				echo "--build-kernel needs extra parameter."
161
				echo
162
				usage
163
			fi
164
			export BUILD_KERNELS="${1}"
165
			;;
166
		--update-sources)
167
			BUILDACTION="updatesources"
168
			;;
169
		--print-flags)
170
			BUILDACTION="printflags"
171
			_USE_OLD_DATESTRING=YES
172
			;;
173
		--clean-builder)
174
			BUILDACTION="cleanbuilder"
175
			;;
176
		--setup-poudriere)
177
			BUILDACTION="setup_poudriere"
178
			;;
179
		--create-unified-patch)
180
			BUILDACTION="create_unified_patch"
181
			;;
182
		--update-poudriere-jails)
183
			BUILDACTION="update_poudriere_jails"
184
			;;
185
		-a)
186
			shift
187
			if [ $# -eq 0 ]; then
188
				echo "-a needs extra parameter."
189
				echo
190
				usage
191
			fi
192
			export ARCH_LIST="${1}"
193
			;;
194
		--update-poudriere-ports)
195
			BUILDACTION="update_poudriere_ports"
196
			;;
197
		--update-pkg-repo)
198
			BUILDACTION="update_pkg_repo"
199
			;;
200
		--upload|-U)
201
			export UPLOAD=1
202
			;;
203
		--skip-final-rsync|-i)
204
			export SKIP_FINAL_RSYNC=1
205
			;;
206
		all|none|*iso*|*ova*|*memstick*|*memstickserial*|*memstickadi*|*nanobsd*|*nanobsd-vga*|*fullupdate*)
207
			BUILDACTION="images"
208
			IMAGETYPE="${1}"
209
			;;
210
		-V)
211
			_USE_OLD_DATESTRING=YES
212
			shift
213
			[ -n "${1}" ] \
214
				&& var_to_print="${1}"
215
			;;
216
		--snapshot-update-status)
217
			shift
218
			snapshot_status_message="${1}"
219
			BUILDACTION="snapshot_status_message"
220
			_USE_OLD_DATESTRING=YES
221
			;;
222
		*)
223
			_USE_OLD_DATESTRING=YES
224
			usage
225
	esac
226
	shift
227
done
228

    
229
# Suck in local vars
230
. ${BUILDER_TOOLS}/builder_defaults.sh
231

    
232
# Suck in script helper functions
233
. ${BUILDER_TOOLS}/builder_common.sh
234

    
235
# Print var required with -V and exit
236
if [ -n "${var_to_print}"  ]; then
237
	eval "echo \$${var_to_print}"
238
	exit 0
239
fi
240

    
241
# Update snapshot status and exit
242
if [ "${BUILDACTION}" = "snapshot_status_message" ]; then
243
	if [ -z "${POUDRIERE_SNAPSHOTS}" ]; then
244
		export SNAPSHOTS=1
245
	fi
246
	snapshots_update_status "${snapshot_status_message}"
247
	exit 0
248
fi
249

    
250
# This should be run first
251
launch
252

    
253
case $BUILDACTION in
254
	builder_setup)
255
		builder_setup
256
	;;
257
	buildkernels)
258
		update_freebsd_sources
259
		build_all_kernels
260
	;;
261
	buildkernel)
262
		update_freebsd_sources
263
		build_all_kernels
264
	;;
265
	cleanbuilder)
266
		clean_builder
267
	;;
268
	printflags)
269
		print_flags
270
	;;
271
	images)
272
		# It will be handled below
273
	;;
274
	updatesources)
275
		update_freebsd_sources
276
	;;
277
	setup_poudriere)
278
		poudriere_init
279
	;;
280
	create_unified_patch)
281
		poudriere_create_patch
282
	;;
283
	update_poudriere_jails)
284
		poudriere_update_jails
285
	;;
286
	update_poudriere_ports)
287
		poudriere_update_ports
288
	;;
289
	rsync_repos)
290
		export UPLOAD=1
291
		pkg_repo_rsync "${CORE_PKG_PATH}"
292
	;;
293
	rsync_snapshots)
294
		export UPLOAD=1
295
		snapshots_scp_files
296
	;;
297
	update_pkg_repo)
298
		if [ -n "${UPLOAD}" -a ! -f /usr/local/bin/rsync ]; then
299
			echo "ERROR: rsync is not installed, aborting..."
300
			exit 1
301
		fi
302
		poudriere_bulk
303
	;;
304
	*)
305
		usage
306
	;;
307
esac
308

    
309
if [ "${BUILDACTION}" != "images" ]; then
310
	finish
311
	exit 0
312
fi
313

    
314
if [ -n "${SNAPSHOTS}" -a -n "${UPLOAD}" ]; then
315
	_required=" \
316
		RSYNCIP \
317
		RSYNCUSER \
318
		RSYNCPATH \
319
		PKG_RSYNC_HOSTNAME \
320
		PKG_RSYNC_USERNAME \
321
		PKG_RSYNC_SSH_PORT \
322
		PKG_RSYNC_DESTDIR \
323
		PKG_REPO_SERVER_DEVEL \
324
		PKG_REPO_SERVER_RELEASE \
325
		PKG_REPO_SERVER_STAGING \
326
		PKG_REPO_BRANCH_DEVEL \
327
		PKG_REPO_BRANCH_RELEASE"
328

    
329
	for _var in ${_required}; do
330
		eval "_value=\${$_var}"
331
		if [ -z "${_value}" ]; then
332
			echo ">>> ERROR: ${_var} is not defined"
333
			exit 1
334
		fi
335
	done
336

    
337
	if [ ! -f /usr/local/bin/rsync ]; then
338
		echo "ERROR: rsync is not installed, aborting..."
339
		exit 1
340
	fi
341
fi
342

    
343
if [ $# -gt 1 ]; then
344
	echo "ERROR: Too many arguments given."
345
	echo
346
	usage
347
fi
348

    
349
if [ -n "${SNAPSHOTS}" -a -z "${IMAGETYPE}" ]; then
350
	IMAGETYPE="all"
351
fi
352

    
353
if [ -z "${IMAGETYPE}" ]; then
354
	echo "ERROR: Need to specify image type to build."
355
	echo
356
	usage
357
fi
358

    
359
if [ "$IMAGETYPE" = "none" ]; then
360
	_IMAGESTOBUILD=""
361
elif [ "$IMAGETYPE" = "all" ]; then
362
	_IMAGESTOBUILD="iso fullupdate nanobsd nanobsd-vga memstick memstickserial"
363
	if [ "${TARGET}" = "amd64" ]; then
364
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
365
	fi
366
else
367
	_IMAGESTOBUILD="${IMAGETYPE}"
368
fi
369

    
370
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
371

    
372
if [ -n "${SNAPSHOTS}" ]; then
373
	snapshots_update_status ">>> Starting snapshot build operations"
374

    
375
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
376
		snapshots_update_status ">>> Updating builder packages... "
377
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
378
	fi
379
fi
380

    
381
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
382
	[ -n "${CORE_PKG_TMP}" -a -d "${CORE_PKG_TMP}" ] \
383
		&& rm -rf ${CORE_PKG_TMP}
384
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
385
		&& rm -rf ${CORE_PKG_PATH}
386

    
387
	# Cleanup environment before start
388
	clean_builder
389

    
390
	# Make sure source directories are present.
391
	update_freebsd_sources
392
	git_last_commit
393

    
394
	# Ensure binaries are present that builder system requires
395
	builder_setup
396

    
397
	# Output build flags
398
	print_flags
399

    
400
	# Build world, kernel and install
401
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
402
	make_world
403

    
404
	# Build kernels
405
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
406
	build_all_kernels
407

    
408
	# Prepare pre-final staging area
409
	clone_to_staging_area
410

    
411
	# Install packages needed for Product
412
	install_pkg_install_ports
413

    
414
	# Create core repo
415
	core_pkg_create_repo
416
fi
417

    
418
# Send core repo to staging area
419
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
420

    
421
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
422

    
423
# XXX: Figure out why wait is failing and proper fix
424
# Global variable to keep track of process running in bg
425
export _bg_pids=""
426

    
427
for _IMGTOBUILD in $_IMAGESTOBUILD; do
428
	# Clean up items that should be cleaned each run
429
	staginareas_clean_each_run
430

    
431
	case "${_IMGTOBUILD}" in
432
		iso)
433
			if [ -n "${ISO_VARIANTS}" ]; then
434
				for _variant in ${ISO_VARIANTS}; do
435
					create_iso_image ${_variant}
436
				done
437
			else
438
				create_iso_image
439
			fi
440
			;;
441
		memstick)
442
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
443
				for _variant in ${MEMSTICK_VARIANTS}; do
444
					create_memstick_image ${_variant}
445
				done
446
			else
447
				create_memstick_image
448
			fi
449
			;;
450
		memstickserial)
451
			create_memstick_serial_image
452
			;;
453
		memstickadi)
454
			create_memstick_adi_image
455
			;;
456
		fullupdate)
457
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
458
				for _variant in ${MEMSTICK_VARIANTS}; do
459
					create_Full_update_tarball ${_variant}
460
				done
461
			else
462
				create_Full_update_tarball
463
			fi
464
			;;
465
		nanobsd|nanobsd-vga)
466
			if [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd" ]; then
467
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}_wrap"}
468
			elif [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
469
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSDVGA:-"${PRODUCT_NAME}_wrap_vga"}
470
			elif [ "${TARGET}" = "amd64" ]; then
471
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}"}
472
			fi
473
			# Create the NanoBSD disk image
474
			create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
475
			;;
476
		ova)
477
			old_custom_package_list="${custom_package_list}"
478
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
479
			install_pkg_install_ports
480
			create_ova_image
481
			export custom_package_list="${old_custom_package_list}"
482
			install_pkg_install_ports
483
			;;
484
	esac
485
done
486

    
487
if [ -n "${_bg_pids}" ]; then
488
	if [ -n "${SNAPSHOTS}" ]; then
489
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
490
	else
491
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
492
	fi
493
	wait
494

    
495
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
496
	while [ -n "${_bg_pids}" ]; do
497
		_tmp_pids="${_bg_pids}"
498
		unset _bg_pids
499
		for p in ${_tmp_pids}; do
500
			[ -z "${p}" ] \
501
				&& continue
502

    
503
			kill -0 ${p} >/dev/null 2>&1 \
504
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
505
		done
506
		[ -n "${_bg_pids}" ] \
507
			&& sleep 1
508
	done
509
fi
510

    
511
if [ -n "${SNAPSHOTS}" ]; then
512
	if [ "${IMAGETYPE}" = "none" -a -n "${UPLOAD}" ]; then
513
		pkg_repo_rsync "${CORE_PKG_PATH}"
514
	elif [ "${IMAGETYPE}" != "none" ]; then
515
		snapshots_copy_to_staging_iso_updates
516
		snapshots_copy_to_staging_nanobsd "${FLASH_SIZE}"
517
		# SCP files to snapshot web hosting area
518
		if [ -n "${UPLOAD}" ]; then
519
			snapshots_scp_files
520
		fi
521
	fi
522
	# Alert the world that we have some snapshots ready.
523
	snapshots_update_status ">>> Builder run is complete."
524
fi
525

    
526
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
527
ls -lah ${IMAGES_FINAL_DIR}
528

    
529
set -e
530
# Run final finish routines
531
finish
(7-7/8)