Project

General

Profile

Download (14.3 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 "		--clean-builder - clean all builder used data/resources"
68
	echo "		--build-kernels - build all configured kernels"
69
	echo "		--build-kernel argument - build specified kernel. Example --build-kernel KERNEL_NAME"
70
	echo "		--install-extra-kernels argument - Put extra kernel(s) under /kernel image directory. Example --install-extra-kernels KERNEL_NAME_WRAP"
71
	echo "		--snapshots - Build snapshots and upload them to RSYNCIP"
72
	echo "		--poudriere-snapshots - Update poudriere packages and send them to PKG_RSYNC_HOSTNAME"
73
	echo "		--enable-memorydisks - This will put stage_dir and iso_dir as MFS filesystems"
74
	echo "		--disable-memorydisks - Will just teardown these filesystems created by --enable-memorydisks"
75
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
76
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
77
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
78
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
79
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
80
	echo "		--do-not-upload|-u - Do not upload pkgs or snapshots"
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 DO_NOT_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
			_SKIP_REBUILD_PRESTAGE=YES
124
			_USE_OLD_DATESTRING=YES
125
			;;
126
		--setup)
127
			BUILDACTION="builder_setup"
128
			;;
129
		--rsync-repos)
130
			BUILDACTION="rsync_repos"
131
			;;
132
		--build-kernels)
133
			BUILDACTION="buildkernels"
134
			;;
135
		--install-extra-kernels)
136
			shift
137
			if [ $# -eq 0 ]; then
138
				echo "--build-kernel needs extra parameter."
139
				echo
140
				usage
141
			fi
142
			export INSTALL_EXTRA_KERNELS="${1}"
143
			;;
144
		--snapshots)
145
			export SNAPSHOTS=1
146
			;;
147
		--poudriere-snapshots)
148
			export POUDRIERE_SNAPSHOTS=1
149
			;;
150
		--build-kernel)
151
			BUILDACTION="buildkernel"
152
			shift
153
			if [ $# -eq 0 ]; then
154
				echo "--build-kernel needs extra parameter."
155
				echo
156
				usage
157
			fi
158
			export BUILD_KERNELS="${1}"
159
			;;
160
		--update-sources)
161
			BUILDACTION="updatesources"
162
			;;
163
		--print-flags)
164
			BUILDACTION="printflags"
165
			_USE_OLD_DATESTRING=YES
166
			;;
167
		--clean-builder)
168
			BUILDACTION="cleanbuilder"
169
			;;
170
		--enable-memorydisks)
171
			BUILDACTION="enablememorydisk"
172
			;;
173
		--disable-memorydisks)
174
			BUILDACTION="disablememorydisk"
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
		--do-not-upload|-u)
201
			export DO_NOT_UPLOAD=1
202
			;;
203
		all|none|*iso*|*ova*|*memstick*|*memstickserial*|*memstickadi*|*nanobsd*|*nanobsd-vga*|*fullupdate*)
204
			BUILDACTION="images"
205
			IMAGETYPE="${1}"
206
			;;
207
		-V)
208
			_USE_OLD_DATESTRING=YES
209
			shift
210
			[ -n "${1}" ] \
211
				&& var_to_print="${1}"
212
			;;
213
		--snapshot-update-status)
214
			shift
215
			snapshot_status_message="${1}"
216
			BUILDACTION="snapshot_status_message"
217
			_USE_OLD_DATESTRING=YES
218
			;;
219
		*)
220
			_USE_OLD_DATESTRING=YES
221
			usage
222
	esac
223
	shift
224
done
225

    
226
# Suck in local vars
227
. ${BUILDER_TOOLS}/builder_defaults.sh
228

    
229
# Suck in script helper functions
230
. ${BUILDER_TOOLS}/builder_common.sh
231

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

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

    
247
# This should be run first
248
launch
249

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

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

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

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

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

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

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

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

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

    
372
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
373

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

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

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

    
389
	# Cleanup environment before start
390
	clean_builder
391

    
392
	# Make sure source directories are present.
393
	update_freebsd_sources
394
	git_last_commit
395

    
396
	# Ensure binaries are present that builder system requires
397
	builder_setup
398

    
399
	# Output build flags
400
	print_flags
401

    
402
	# Check to see if pre-staging will be hosted on ram
403
	prestage_on_ram_setup
404

    
405
	# Build world, kernel and install
406
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
407
	make_world
408

    
409
	# Build kernels
410
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
411
	build_all_kernels
412

    
413
	# Prepare pre-final staging area
414
	clone_to_staging_area
415

    
416
	# Install packages needed for Product
417
	install_pkg_install_ports
418
fi
419

    
420
# Create core repo
421
core_pkg_create_repo
422

    
423
# Send core repo to staging area
424
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
425

    
426
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
427

    
428
# XXX: Figure out why wait is failing and proper fix
429
# Global variable to keep track of process running in bg
430
export _bg_pids=""
431

    
432
for _IMGTOBUILD in $_IMAGESTOBUILD; do
433
	# Clean up items that should be cleaned each run
434
	staginareas_clean_each_run
435

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

    
492
if [ -n "${_bg_pids}" ]; then
493
	if [ -n "${SNAPSHOTS}" ]; then
494
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
495
	else
496
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
497
	fi
498
	wait
499

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

    
508
			kill -0 ${p} >/dev/null 2>&1 \
509
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
510
		done
511
		[ -n "${_bg_pids}" ] \
512
			&& sleep 1
513
	done
514
fi
515

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

    
531
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
532
ls -lah ${IMAGES_FINAL_DIR}
533

    
534
set -e
535
# Run final finish routines
536
finish
(7-7/8)