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
			;;
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_DEVEL \
313
		PKG_REPO_SERVER_RELEASE \
314
		PKG_REPO_SERVER_STAGING \
315
		PKG_REPO_BRANCH_DEVEL \
316
		PKG_REPO_BRANCH_RELEASE"
317

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

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

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

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

    
352
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
353

    
354
if [ -n "${SNAPSHOTS}" ]; then
355
	snapshots_rotate_logfile
356

    
357
	snapshots_update_status ">>> Starting snapshot build operations"
358

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

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

    
371
	# Cleanup environment before start
372
	clean_builder
373

    
374
	# Make sure source directories are present.
375
	update_freebsd_sources
376
	git_last_commit
377

    
378
	# Ensure binaries are present that builder system requires
379
	builder_setup
380

    
381
	# Output build flags
382
	print_flags
383

    
384
	# Check to see if pre-staging will be hosted on ram
385
	prestage_on_ram_setup
386

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

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

    
395
	# Prepare pre-final staging area
396
	clone_to_staging_area
397

    
398
	# Install packages needed for Product
399
	install_pkg_install_ports
400
fi
401

    
402
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
403

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

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

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

    
450
core_pkg_create_repo
451

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

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

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

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

    
487
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
488
ls -lah ${IMAGES_FINAL_DIR}
489

    
490
set -e
491
# Run final finish routines
492
finish
(7-7/8)