Project

General

Profile

Download (12.8 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. '512m 1g'. Default: 512m"
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 "		--enable-memorydisks - This will put stage_dir and iso_dir as MFS filesystems"
70
	echo "		--disable-memorydisks - Will just teardown these filesystems created by --enable-memorydisks"
71
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
72
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
73
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
74
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
75
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
76
	echo "		--do-not-upload|-u - Do not upload pkgs or snapshots"
77
	echo "		-V VARNAME - print value of variable VARNAME"
78
	exit 1
79
}
80

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

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

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

    
212
# Suck in local vars
213
. ${BUILDER_TOOLS}/builder_defaults.sh
214

    
215
# Suck in script helper functions
216
. ${BUILDER_TOOLS}/builder_common.sh
217

    
218
# Print var required with -V and exit
219
if [ -n "${var_to_print}"  ]; then
220
	eval "echo \$${var_to_print}"
221
	exit 0
222
fi
223

    
224
# Update snapshot status and exit
225
if [ "${BUILDACTION}" = "snapshot_status_message" ]; then
226
	export SNAPSHOTS=1
227
	snapshots_update_status "${snapshot_status_message}"
228
	exit 0
229
fi
230

    
231
# This should be run first
232
launch
233

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

    
288
if [ "${BUILDACTION}" != "images" ]; then
289
	finish
290
	exit 0
291
fi
292

    
293
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
294
	_required=" \
295
		RSYNCIP \
296
		RSYNCUSER \
297
		RSYNCPATH \
298
		RSYNCLOGS \
299
		PKG_RSYNC_HOSTNAME \
300
		PKG_RSYNC_USERNAME \
301
		PKG_RSYNC_SSH_PORT \
302
		PKG_RSYNC_DESTDIR \
303
		PKG_REPO_SERVER \
304
		PKG_REPO_CONF_BRANCH"
305

    
306
	for _var in ${_required}; do
307
		eval "_value=\${$_var}"
308
		if [ -z "${_value}" ]; then
309
			echo ">>> ERROR: ${_var} is not defined"
310
			exit 1
311
		fi
312
	done
313

    
314
	if [ ! -f /usr/local/bin/rsync ]; then
315
		echo "ERROR: rsync is not installed, aborting..."
316
		exit 1
317
	fi
318
fi
319

    
320
if [ $# -gt 1 ]; then
321
	echo "ERROR: Too many arguments given."
322
	echo
323
	usage
324
fi
325
if [ -z "${IMAGETYPE}" ]; then
326
	echo "ERROR: Need to specify image type to build."
327
	echo
328
	usage
329
fi
330

    
331
if [ "$IMAGETYPE" = "all" ]; then
332
	_IMAGESTOBUILD="iso fullupdate nanobsd nanobsd-vga memstick memstickserial"
333
	if [ "${TARGET}" = "amd64" ]; then
334
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
335
	fi
336
else
337
	_IMAGESTOBUILD="${IMAGETYPE}"
338
fi
339

    
340
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
341

    
342
if [ -n "${SNAPSHOTS}" ]; then
343
	snapshots_rotate_logfile
344

    
345
	snapshots_update_status ">>> Starting snapshot build operations"
346

    
347
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
348
		snapshots_update_status ">>> Updating builder packages... "
349
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
350
	fi
351
fi
352

    
353
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
354
	[ -n "${CORE_PKG_TMP}" -a -d "${CORE_PKG_TMP}" ] \
355
		&& rm -rf ${CORE_PKG_TMP}
356
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
357
		&& rm -rf ${CORE_PKG_PATH}
358

    
359
	# Cleanup environment before start
360
	clean_builder
361

    
362
	# Make sure source directories are present.
363
	update_freebsd_sources
364
	git_last_commit
365

    
366
	# Ensure binaries are present that builder system requires
367
	builder_setup
368

    
369
	# Output build flags
370
	print_flags
371

    
372
	# Check to see if pre-staging will be hosted on ram
373
	prestage_on_ram_setup
374

    
375
	# Build world, kernel and install
376
	echo ">>> Building world for ISO... $FREEBSD_BRANCH ..."
377
	make_world
378

    
379
	# Build kernels
380
	echo ">>> Building kernel configs: $BUILD_KERNELS for FreeBSD: $FREEBSD_BRANCH ..."
381
	build_all_kernels
382

    
383
	# Prepare pre-final staging area
384
	clone_to_staging_area
385

    
386
	# Install packages needed for Product
387
	install_pkg_install_ports
388
fi
389

    
390
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
391

    
392
# XXX: Figure out why wait is failing and proper fix
393
# Global variable to keep track of process running in bg
394
export _bg_pids=""
395

    
396
for _IMGTOBUILD in $_IMAGESTOBUILD; do
397
	# Clean up items that should be cleaned each run
398
	staginareas_clean_each_run
399

    
400
	if [ "${_IMGTOBUILD}" = "iso" ]; then
401
		create_iso_image
402
	elif [ "${_IMGTOBUILD}" = "memstick" ]; then
403
		create_memstick_image
404
	elif [ "${_IMGTOBUILD}" = "memstickserial" ]; then
405
		create_memstick_serial_image
406
	elif [ "${_IMGTOBUILD}" = "memstickadi" ]; then
407
		create_memstick_adi_image
408
	elif [ "${_IMGTOBUILD}" = "fullupdate" ]; then
409
		create_Full_update_tarball
410
	elif [ "${_IMGTOBUILD}" = "nanobsd" -o "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
411
		if [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd" ]; then
412
			export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}_wrap"}
413
		elif [ "${TARGET}" = "i386" -a "${_IMGTOBUILD}" = "nanobsd-vga" ]; then
414
			export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSDVGA:-"${PRODUCT_NAME}_wrap_vga"}
415
		elif [ "${TARGET}" = "amd64" ]; then
416
			export DEFAULT_KERNEL=${DEFAULT_KERNEL_NANOBSD:-"${PRODUCT_NAME}"}
417
		fi
418
		# Create the NanoBSD disk image
419
		create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
420
	elif [ "${_IMGTOBUILD}" = "ova" ]; then
421
		install_pkg_install_ports ${PRODUCT_NAME}-vmware
422
		create_ova_image
423
		install_pkg_install_ports
424
	fi
425
done
426

    
427
core_pkg_create_repo
428

    
429
if [ -n "${_bg_pids}" ]; then
430
	if [ -n "${SNAPSHOTS}" ]; then
431
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
432
	else
433
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
434
	fi
435
	wait
436

    
437
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
438
	while [ -n "${_bg_pids}" ]; do
439
		_tmp_pids="${_bg_pids}"
440
		unset _bg_pids
441
		for p in ${_tmp_pids}; do
442
			[ -z "${p}" ] \
443
				&& continue
444

    
445
			kill -0 ${p} >/dev/null 2>&1 \
446
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
447
		done
448
		[ -n "${_bg_pids}" ] \
449
			&& sleep 1
450
	done
451
fi
452

    
453
if [ -n "${SNAPSHOTS}" ]; then
454
	snapshots_copy_to_staging_iso_updates
455
	snapshots_copy_to_staging_nanobsd "${FLASH_SIZE}"
456
	# SCP files to snapshot web hosting area
457
	if [ -z "${DO_NOT_UPLOAD}" ]; then
458
		snapshots_scp_files
459
	fi
460
	# Alert the world that we have some snapshots ready.
461
	snapshots_update_status ">>> Builder run is complete."
462
fi
463

    
464
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
465
ls -lah ${IMAGES_FINAL_DIR}
466

    
467
set -e
468
# Run final finish routines
469
finish
(7-7/9)