Project

General

Profile

Download (14.5 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 "		--enable-memorydisks - This will put stage_dir and iso_dir as MFS filesystems"
75
	echo "		--disable-memorydisks - Will just teardown these filesystems created by --enable-memorydisks"
76
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
77
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
78
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
79
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
80
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
81
	echo "		--upload|-U - Upload pkgs and/or snapshots"
82
	echo "		-V VARNAME - print value of variable VARNAME"
83
	exit 1
84
}
85

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

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

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

    
233
# Suck in local vars
234
. ${BUILDER_TOOLS}/builder_defaults.sh
235

    
236
# Suck in script helper functions
237
. ${BUILDER_TOOLS}/builder_common.sh
238

    
239
# Print var required with -V and exit
240
if [ -n "${var_to_print}"  ]; then
241
	eval "echo \$${var_to_print}"
242
	exit 0
243
fi
244

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

    
254
# This should be run first
255
launch
256

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

    
319
if [ "${BUILDACTION}" != "images" ]; then
320
	finish
321
	exit 0
322
fi
323

    
324
if [ -n "${SNAPSHOTS}" -a -n "${UPLOAD}" ]; then
325
	_required=" \
326
		RSYNCIP \
327
		RSYNCUSER \
328
		RSYNCPATH \
329
		PKG_RSYNC_HOSTNAME \
330
		PKG_RSYNC_USERNAME \
331
		PKG_RSYNC_SSH_PORT \
332
		PKG_RSYNC_DESTDIR \
333
		PKG_REPO_SERVER_DEVEL \
334
		PKG_REPO_SERVER_RELEASE \
335
		PKG_REPO_SERVER_STAGING \
336
		PKG_REPO_BRANCH_DEVEL \
337
		PKG_REPO_BRANCH_RELEASE"
338

    
339
	for _var in ${_required}; do
340
		eval "_value=\${$_var}"
341
		if [ -z "${_value}" ]; then
342
			echo ">>> ERROR: ${_var} is not defined"
343
			exit 1
344
		fi
345
	done
346

    
347
	if [ ! -f /usr/local/bin/rsync ]; then
348
		echo "ERROR: rsync is not installed, aborting..."
349
		exit 1
350
	fi
351
fi
352

    
353
if [ $# -gt 1 ]; then
354
	echo "ERROR: Too many arguments given."
355
	echo
356
	usage
357
fi
358

    
359
if [ -n "${SNAPSHOTS}" -a -z "${IMAGETYPE}" ]; then
360
	IMAGETYPE="all"
361
fi
362

    
363
if [ -z "${IMAGETYPE}" ]; then
364
	echo "ERROR: Need to specify image type to build."
365
	echo
366
	usage
367
fi
368

    
369
if [ "$IMAGETYPE" = "none" ]; then
370
	_IMAGESTOBUILD=""
371
elif [ "$IMAGETYPE" = "all" ]; then
372
	_IMAGESTOBUILD="iso fullupdate nanobsd nanobsd-vga memstick memstickserial"
373
	if [ "${TARGET}" = "amd64" ]; then
374
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
375
		if [ -n "${_IS_RELEASE}"  ]; then
376
			_IMAGESTOBUILD="${_IMAGESTOBUILD} ova"
377
		fi
378
	fi
379
else
380
	_IMAGESTOBUILD="${IMAGETYPE}"
381
fi
382

    
383
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
384

    
385
if [ -n "${SNAPSHOTS}" ]; then
386
	snapshots_update_status ">>> Starting snapshot build operations"
387

    
388
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
389
		snapshots_update_status ">>> Updating builder packages... "
390
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
391
	fi
392
fi
393

    
394
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
395
	[ -n "${CORE_PKG_TMP}" -a -d "${CORE_PKG_TMP}" ] \
396
		&& rm -rf ${CORE_PKG_TMP}
397
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
398
		&& rm -rf ${CORE_PKG_PATH}
399

    
400
	# Cleanup environment before start
401
	clean_builder
402

    
403
	# Make sure source directories are present.
404
	update_freebsd_sources
405
	git_last_commit
406

    
407
	# Ensure binaries are present that builder system requires
408
	builder_setup
409

    
410
	# Output build flags
411
	print_flags
412

    
413
	# Check to see if pre-staging will be hosted on ram
414
	prestage_on_ram_setup
415

    
416
	# Build world, kernel and install
417
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
418
	make_world
419

    
420
	# Build kernels
421
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
422
	build_all_kernels
423

    
424
	# Prepare pre-final staging area
425
	clone_to_staging_area
426

    
427
	# Install packages needed for Product
428
	install_pkg_install_ports
429

    
430
	# Create core repo
431
	core_pkg_create_repo
432
fi
433

    
434
# Send core repo to staging area
435
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
436

    
437
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
438

    
439
# XXX: Figure out why wait is failing and proper fix
440
# Global variable to keep track of process running in bg
441
export _bg_pids=""
442

    
443
for _IMGTOBUILD in $_IMAGESTOBUILD; do
444
	# Clean up items that should be cleaned each run
445
	staginareas_clean_each_run
446

    
447
	case "${_IMGTOBUILD}" in
448
		iso)
449
			if [ -n "${ISO_VARIANTS}" ]; then
450
				for _variant in ${ISO_VARIANTS}; do
451
					create_iso_image ${_variant}
452
				done
453
			else
454
				create_iso_image
455
			fi
456
			;;
457
		memstick)
458
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
459
				for _variant in ${MEMSTICK_VARIANTS}; do
460
					create_memstick_image ${_variant}
461
				done
462
			else
463
				create_memstick_image
464
			fi
465
			;;
466
		memstickserial)
467
			create_memstick_serial_image
468
			;;
469
		memstickadi)
470
			create_memstick_adi_image
471
			;;
472
		fullupdate)
473
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
474
				for _variant in ${MEMSTICK_VARIANTS}; do
475
					create_Full_update_tarball ${_variant}
476
				done
477
			else
478
				create_Full_update_tarball
479
			fi
480
			;;
481
		nanobsd|nanobsd-vga)
482
			if [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd" ]; then
483
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}_wrap"}
484
			elif [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
485
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSDVGA:-"${PRODUCT_NAME}_wrap_vga"}
486
			elif [ "${TARGET}" = "amd64" ]; then
487
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}"}
488
			fi
489
			# Create the NanoBSD disk image
490
			create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
491
			;;
492
		ova)
493
			old_custom_package_list="${custom_package_list}"
494
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
495
			install_pkg_install_ports
496
			create_ova_image
497
			export custom_package_list="${old_custom_package_list}"
498
			install_pkg_install_ports
499
			;;
500
	esac
501
done
502

    
503
if [ -n "${_bg_pids}" ]; then
504
	if [ -n "${SNAPSHOTS}" ]; then
505
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
506
	else
507
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
508
	fi
509
	wait
510

    
511
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
512
	while [ -n "${_bg_pids}" ]; do
513
		_tmp_pids="${_bg_pids}"
514
		unset _bg_pids
515
		for p in ${_tmp_pids}; do
516
			[ -z "${p}" ] \
517
				&& continue
518

    
519
			kill -0 ${p} >/dev/null 2>&1 \
520
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
521
		done
522
		[ -n "${_bg_pids}" ] \
523
			&& sleep 1
524
	done
525
fi
526

    
527
if [ -n "${SNAPSHOTS}" ]; then
528
	if [ "${IMAGETYPE}" = "none" -a -n "${UPLOAD}" ]; then
529
		pkg_repo_rsync "${CORE_PKG_PATH}"
530
	elif [ "${IMAGETYPE}" != "none" ]; then
531
		snapshots_copy_to_staging_iso_updates
532
		snapshots_copy_to_staging_nanobsd "${FLASH_SIZE}"
533
		# SCP files to snapshot web hosting area
534
		if [ -n "${UPLOAD}" ]; then
535
			snapshots_scp_files
536
		fi
537
	fi
538
	# Alert the world that we have some snapshots ready.
539
	snapshots_update_status ">>> Builder run is complete."
540
fi
541

    
542
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
543
ls -lah ${IMAGES_FINAL_DIR}
544

    
545
set -e
546
# Run final finish routines
547
finish
(7-7/8)