Project

General

Profile

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

    
52
set +e
53
usage() {
54
	echo "Usage $0 [options] [ iso | nanobsd | ova | nanobsd-vga | memstick | memstickserial | memstickadi | fullupdate | all | none ]"
55
	echo "		all = iso nanobsd nanobsd-vga memstick memstickserial memstickadi fullupdate"
56
	echo "		none = upgrade only pkg repo"
57
	echo "	[ options ]: "
58
	echo "		--flash-size|-f size(s) - a list of flash sizes to build with nanobsd i.e. '2g 4g'. Default: 2g"
59
	echo "		--no-buildworld|-c - Will set NO_BUILDWORLD NO_BUILDKERNEL to not build kernel and world"
60
	echo "		--no-cleanobjdir|--no-cleanrepos|-d - Will not clean FreeBSD object built dir to allow restarting a build with NO_CLEAN"
61
	echo "		--resume-image-build|-r - Includes -c -d and also will just move directly to image creation using pre-staged data"
62
	echo "		--setup - Install required repo and ports builder require to work"
63
	echo "		--update-sources - Refetch FreeBSD sources"
64
	echo "		--rsync-repos - rsync pkg repos"
65
	echo "		--print-flags - Show current builder configuration"
66
	echo "		--clean-builder - clean all builder used data/resources"
67
	echo "		--build-kernels - build all configured kernels"
68
	echo "		--build-kernel argument - build specified kernel. Example --build-kernel KERNEL_NAME"
69
	echo "		--install-extra-kernels argument - Put extra kernel(s) under /kernel image directory. Example --install-extra-kernels KERNEL_NAME_WRAP"
70
	echo "		--snapshots - Build snapshots and upload them to RSYNCIP"
71
	echo "		--poudriere-snapshots - Update poudriere packages and send them to PKG_RSYNC_HOSTNAME"
72
	echo "		--enable-memorydisks - This will put stage_dir and iso_dir as MFS filesystems"
73
	echo "		--disable-memorydisks - Will just teardown these filesystems created by --enable-memorydisks"
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 "		--do-not-upload|-u - Do not upload pkgs or snapshots"
80
	echo "		-V VARNAME - print value of variable VARNAME"
81
	exit 1
82
}
83

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

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

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

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

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

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

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

    
246
# This should be run first
247
launch
248

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

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

    
312
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
313
	_required=" \
314
		RSYNCIP \
315
		RSYNCUSER \
316
		RSYNCPATH \
317
		RSYNCLOGS \
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
	fi
365
else
366
	_IMAGESTOBUILD="${IMAGETYPE}"
367
fi
368

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

    
371
if [ -n "${SNAPSHOTS}" ]; then
372
	snapshots_rotate_logfile
373

    
374
	snapshots_update_status ">>> Starting snapshot build operations"
375

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

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

    
388
	# Cleanup environment before start
389
	clean_builder
390

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

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

    
398
	# Output build flags
399
	print_flags
400

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

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

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

    
412
	# Prepare pre-final staging area
413
	clone_to_staging_area
414

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

    
419
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
420

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

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

    
429
	case "${_IMGTOBUILD}" in
430
		iso)
431
			create_iso_image
432
			;;
433
		memstick)
434
			create_memstick_image
435
			;;
436
		memstickserial)
437
			create_memstick_serial_image
438
			;;
439
		memstickadi)
440
			create_memstick_adi_image
441
			;;
442
		fullupdate)
443
			create_Full_update_tarball
444
			;;
445
		nanobsd|nanobsd-vga)
446
			if [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd" ]; then
447
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}_wrap"}
448
			elif [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
449
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSDVGA:-"${PRODUCT_NAME}_wrap_vga"}
450
			elif [ "${TARGET}" = "amd64" ]; then
451
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}"}
452
			fi
453
			# Create the NanoBSD disk image
454
			create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
455
			;;
456
		ova)
457
			old_custom_package_list="${custom_package_list}"
458
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
459
			install_pkg_install_ports
460
			create_ova_image
461
			export custom_package_list="${old_custom_package_list}"
462
			install_pkg_install_ports
463
			;;
464
	esac
465
done
466

    
467
core_pkg_create_repo
468

    
469
if [ -n "${_bg_pids}" ]; then
470
	if [ -n "${SNAPSHOTS}" ]; then
471
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
472
	else
473
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
474
	fi
475
	wait
476

    
477
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
478
	while [ -n "${_bg_pids}" ]; do
479
		_tmp_pids="${_bg_pids}"
480
		unset _bg_pids
481
		for p in ${_tmp_pids}; do
482
			[ -z "${p}" ] \
483
				&& continue
484

    
485
			kill -0 ${p} >/dev/null 2>&1 \
486
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
487
		done
488
		[ -n "${_bg_pids}" ] \
489
			&& sleep 1
490
	done
491
fi
492

    
493
if [ -n "${SNAPSHOTS}" ]; then
494
	if [ "${IMAGETYPE}" = "none" -a -z "${DO_NOT_UPLOAD}" ]; then
495
		pkg_repo_rsync "${CORE_PKG_PATH}"
496
	elif [ "${IMAGETYPE}" != "none" ]; then
497
		snapshots_copy_to_staging_iso_updates
498
		snapshots_copy_to_staging_nanobsd "${FLASH_SIZE}"
499
		# SCP files to snapshot web hosting area
500
		if [ -z "${DO_NOT_UPLOAD}" ]; then
501
			snapshots_scp_files
502
		fi
503
	fi
504
	# Alert the world that we have some snapshots ready.
505
	snapshots_update_status ">>> Builder run is complete."
506
fi
507

    
508
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
509
ls -lah ${IMAGES_FINAL_DIR}
510

    
511
set -e
512
# Run final finish routines
513
finish
(7-7/8)