Project

General

Profile

Download (13.1 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
			;;
213
		*)
214
			usage
215
	esac
216
	shift
217
done
218

    
219
# Suck in local vars
220
. ${BUILDER_TOOLS}/builder_defaults.sh
221

    
222
# Suck in script helper functions
223
. ${BUILDER_TOOLS}/builder_common.sh
224

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

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

    
240
# This should be run first
241
launch
242

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

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

    
302
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
303
	_required=" \
304
		RSYNCIP \
305
		RSYNCUSER \
306
		RSYNCPATH \
307
		RSYNCLOGS \
308
		PKG_RSYNC_HOSTNAME \
309
		PKG_RSYNC_USERNAME \
310
		PKG_RSYNC_SSH_PORT \
311
		PKG_RSYNC_DESTDIR \
312
		PKG_REPO_SERVER \
313
		PKG_REPO_CONF_BRANCH"
314

    
315
	for _var in ${_required}; do
316
		eval "_value=\${$_var}"
317
		if [ -z "${_value}" ]; then
318
			echo ">>> ERROR: ${_var} is not defined"
319
			exit 1
320
		fi
321
	done
322

    
323
	if [ ! -f /usr/local/bin/rsync ]; then
324
		echo "ERROR: rsync is not installed, aborting..."
325
		exit 1
326
	fi
327
fi
328

    
329
if [ $# -gt 1 ]; then
330
	echo "ERROR: Too many arguments given."
331
	echo
332
	usage
333
fi
334
if [ -z "${IMAGETYPE}" ]; then
335
	echo "ERROR: Need to specify image type to build."
336
	echo
337
	usage
338
fi
339

    
340
if [ "$IMAGETYPE" = "all" ]; then
341
	_IMAGESTOBUILD="iso fullupdate nanobsd nanobsd-vga memstick memstickserial"
342
	if [ "${TARGET}" = "amd64" ]; then
343
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
344
	fi
345
else
346
	_IMAGESTOBUILD="${IMAGETYPE}"
347
fi
348

    
349
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
350

    
351
if [ -n "${SNAPSHOTS}" ]; then
352
	snapshots_rotate_logfile
353

    
354
	snapshots_update_status ">>> Starting snapshot build operations"
355

    
356
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
357
		snapshots_update_status ">>> Updating builder packages... "
358
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
359
	fi
360
fi
361

    
362
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
363
	[ -n "${CORE_PKG_TMP}" -a -d "${CORE_PKG_TMP}" ] \
364
		&& rm -rf ${CORE_PKG_TMP}
365
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
366
		&& rm -rf ${CORE_PKG_PATH}
367

    
368
	# Cleanup environment before start
369
	clean_builder
370

    
371
	# Make sure source directories are present.
372
	update_freebsd_sources
373
	git_last_commit
374

    
375
	# Ensure binaries are present that builder system requires
376
	builder_setup
377

    
378
	# Output build flags
379
	print_flags
380

    
381
	# Check to see if pre-staging will be hosted on ram
382
	prestage_on_ram_setup
383

    
384
	# Build world, kernel and install
385
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
386
	make_world
387

    
388
	# Build kernels
389
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
390
	build_all_kernels
391

    
392
	# Prepare pre-final staging area
393
	clone_to_staging_area
394

    
395
	# Install packages needed for Product
396
	install_pkg_install_ports
397
fi
398

    
399
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
400

    
401
# XXX: Figure out why wait is failing and proper fix
402
# Global variable to keep track of process running in bg
403
export _bg_pids=""
404

    
405
for _IMGTOBUILD in $_IMAGESTOBUILD; do
406
	# Clean up items that should be cleaned each run
407
	staginareas_clean_each_run
408

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

    
447
core_pkg_create_repo
448

    
449
if [ -n "${_bg_pids}" ]; then
450
	if [ -n "${SNAPSHOTS}" ]; then
451
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
452
	else
453
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
454
	fi
455
	wait
456

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

    
465
			kill -0 ${p} >/dev/null 2>&1 \
466
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
467
		done
468
		[ -n "${_bg_pids}" ] \
469
			&& sleep 1
470
	done
471
fi
472

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

    
484
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
485
ls -lah ${IMAGES_FINAL_DIR}
486

    
487
set -e
488
# Run final finish routines
489
finish
(7-7/8)