Project

General

Profile

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

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

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

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

    
221
# Suck in local vars
222
. ${BUILDER_TOOLS}/builder_defaults.sh
223

    
224
# Suck in script helper functions
225
. ${BUILDER_TOOLS}/builder_common.sh
226

    
227
# Print var required with -V and exit
228
if [ -n "${var_to_print}"  ]; then
229
	eval "echo \$${var_to_print}"
230
	exit 0
231
fi
232

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

    
242
# This should be run first
243
launch
244

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

    
299
if [ "${BUILDACTION}" != "images" ]; then
300
	finish
301
	exit 0
302
fi
303

    
304
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
305
	_required=" \
306
		RSYNCIP \
307
		RSYNCUSER \
308
		RSYNCPATH \
309
		RSYNCLOGS \
310
		PKG_RSYNC_HOSTNAME \
311
		PKG_RSYNC_USERNAME \
312
		PKG_RSYNC_SSH_PORT \
313
		PKG_RSYNC_DESTDIR \
314
		PKG_REPO_SERVER_DEVEL \
315
		PKG_REPO_SERVER_RELEASE \
316
		PKG_REPO_SERVER_STAGING \
317
		PKG_REPO_BRANCH_DEVEL \
318
		PKG_REPO_BRANCH_RELEASE"
319

    
320
	for _var in ${_required}; do
321
		eval "_value=\${$_var}"
322
		if [ -z "${_value}" ]; then
323
			echo ">>> ERROR: ${_var} is not defined"
324
			exit 1
325
		fi
326
	done
327

    
328
	if [ ! -f /usr/local/bin/rsync ]; then
329
		echo "ERROR: rsync is not installed, aborting..."
330
		exit 1
331
	fi
332
fi
333

    
334
if [ $# -gt 1 ]; then
335
	echo "ERROR: Too many arguments given."
336
	echo
337
	usage
338
fi
339
if [ -z "${IMAGETYPE}" ]; then
340
	echo "ERROR: Need to specify image type to build."
341
	echo
342
	usage
343
fi
344

    
345
if [ "$IMAGETYPE" = "all" ]; then
346
	_IMAGESTOBUILD="iso fullupdate nanobsd nanobsd-vga memstick memstickserial"
347
	if [ "${TARGET}" = "amd64" ]; then
348
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
349
	fi
350
else
351
	_IMAGESTOBUILD="${IMAGETYPE}"
352
fi
353

    
354
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
355

    
356
if [ -n "${SNAPSHOTS}" ]; then
357
	snapshots_rotate_logfile
358

    
359
	snapshots_update_status ">>> Starting snapshot build operations"
360

    
361
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
362
		snapshots_update_status ">>> Updating builder packages... "
363
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
364
	fi
365
fi
366

    
367
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
368
	[ -n "${CORE_PKG_TMP}" -a -d "${CORE_PKG_TMP}" ] \
369
		&& rm -rf ${CORE_PKG_TMP}
370
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
371
		&& rm -rf ${CORE_PKG_PATH}
372

    
373
	# Cleanup environment before start
374
	clean_builder
375

    
376
	# Make sure source directories are present.
377
	update_freebsd_sources
378
	git_last_commit
379

    
380
	# Ensure binaries are present that builder system requires
381
	builder_setup
382

    
383
	# Output build flags
384
	print_flags
385

    
386
	# Check to see if pre-staging will be hosted on ram
387
	prestage_on_ram_setup
388

    
389
	# Build world, kernel and install
390
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
391
	make_world
392

    
393
	# Build kernels
394
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
395
	build_all_kernels
396

    
397
	# Prepare pre-final staging area
398
	clone_to_staging_area
399

    
400
	# Install packages needed for Product
401
	install_pkg_install_ports
402
fi
403

    
404
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
405

    
406
# XXX: Figure out why wait is failing and proper fix
407
# Global variable to keep track of process running in bg
408
export _bg_pids=""
409

    
410
for _IMGTOBUILD in $_IMAGESTOBUILD; do
411
	# Clean up items that should be cleaned each run
412
	staginareas_clean_each_run
413

    
414
	case "${_IMGTOBUILD}" in
415
		iso)
416
			create_iso_image
417
			;;
418
		memstick)
419
			create_memstick_image
420
			;;
421
		memstickserial)
422
			create_memstick_serial_image
423
			;;
424
		memstickadi)
425
			create_memstick_adi_image
426
			;;
427
		fullupdate)
428
			create_Full_update_tarball
429
			;;
430
		nanobsd|nanobsd-vga)
431
			if [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd" ]; then
432
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}_wrap"}
433
			elif [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
434
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSDVGA:-"${PRODUCT_NAME}_wrap_vga"}
435
			elif [ "${TARGET}" = "amd64" ]; then
436
				export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}"}
437
			fi
438
			# Create the NanoBSD disk image
439
			create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
440
			;;
441
		ova)
442
			old_custom_package_list="${custom_package_list}"
443
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
444
			install_pkg_install_ports
445
			create_ova_image
446
			export custom_package_list="${old_custom_package_list}"
447
			install_pkg_install_ports
448
			;;
449
	esac
450
done
451

    
452
core_pkg_create_repo
453

    
454
if [ -n "${_bg_pids}" ]; then
455
	if [ -n "${SNAPSHOTS}" ]; then
456
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
457
	else
458
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
459
	fi
460
	wait
461

    
462
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
463
	while [ -n "${_bg_pids}" ]; do
464
		_tmp_pids="${_bg_pids}"
465
		unset _bg_pids
466
		for p in ${_tmp_pids}; do
467
			[ -z "${p}" ] \
468
				&& continue
469

    
470
			kill -0 ${p} >/dev/null 2>&1 \
471
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
472
		done
473
		[ -n "${_bg_pids}" ] \
474
			&& sleep 1
475
	done
476
fi
477

    
478
if [ -n "${SNAPSHOTS}" ]; then
479
	snapshots_copy_to_staging_iso_updates
480
	snapshots_copy_to_staging_nanobsd "${FLASH_SIZE}"
481
	# SCP files to snapshot web hosting area
482
	if [ -z "${DO_NOT_UPLOAD}" ]; then
483
		snapshots_scp_files
484
	fi
485
	# Alert the world that we have some snapshots ready.
486
	snapshots_update_status ">>> Builder run is complete."
487
fi
488

    
489
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
490
ls -lah ${IMAGES_FINAL_DIR}
491

    
492
set -e
493
# Run final finish routines
494
finish
(7-7/8)