Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# build.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
# All rights reserved.
8
#
9
# Licensed under the Apache License, Version 2.0 (the "License");
10
# you may not use this file except in compliance with the License.
11
# You may obtain a copy of the License at
12
#
13
# http://www.apache.org/licenses/LICENSE-2.0
14
#
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS,
17
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
# See the License for the specific language governing permissions and
19
# limitations under the License.
20

    
21
set +e
22
usage() {
23
	echo "Usage $0 [options] [ iso | nanobsd | ova | nanobsd-vga | memstick | memstickserial | memstickadi | all | none ]"
24
	echo "		all = iso nanobsd nanobsd-vga memstick memstickserial memstickadi"
25
	echo "		none = upgrade only pkg repo"
26
	echo "	[ options ]: "
27
	echo "		--flash-size|-f size(s) - a list of flash sizes to build with nanobsd i.e. '2g 4g'. Default: 2g"
28
	echo "		--no-buildworld|-c - Will set NO_BUILDWORLD NO_BUILDKERNEL to not build kernel and world"
29
	echo "		--no-cleanobjdir|--no-cleanrepos|-d - Will not clean FreeBSD object built dir to allow restarting a build with NO_CLEAN"
30
	echo "		--resume-image-build|-r - Includes -c -d and also will just move directly to image creation using pre-staged data"
31
	echo "		--setup - Install required repo and ports builder require to work"
32
	echo "		--update-sources - Refetch FreeBSD sources"
33
	echo "		--rsync-repos - rsync pkg repos"
34
	echo "		--clean-builder - clean all builder used data/resources"
35
	echo "		--build-kernels - build all configured kernels"
36
	echo "		--build-kernel argument - build specified kernel. Example --build-kernel KERNEL_NAME"
37
	echo "		--install-extra-kernels argument - Put extra kernel(s) under /kernel image directory. Example --install-extra-kernels KERNEL_NAME_WRAP"
38
	echo "		--snapshots - Build snapshots and upload them to RSYNCIP"
39
	echo "		--poudriere-snapshots - Update poudriere packages and send them to PKG_RSYNC_HOSTNAME"
40
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
41
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
42
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
43
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
44
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
45
	echo "		--do-not-upload|-u - Do not upload pkgs or snapshots"
46
	echo "		-V VARNAME - print value of variable VARNAME"
47
	exit 1
48
}
49

    
50
export BUILDER_ROOT=$(realpath $(dirname ${0}))
51
export BUILDER_TOOLS="${BUILDER_ROOT}/tools"
52

    
53
unset _SKIP_REBUILD_PRESTAGE
54
unset _USE_OLD_DATESTRING
55
unset pfPORTTOBUILD
56
unset IMAGETYPE
57
unset DO_NOT_UPLOAD
58
unset SNAPSHOTS
59
unset POUDRIERE_SNAPSHOTS
60
unset ARCH_LIST
61
BUILDACTION="images"
62

    
63
# Maybe use options for nocleans etc?
64
while test "$1" != ""; do
65
	case "${1}" in
66
		--no-buildworld|-c)
67
			export NO_BUILDWORLD=YES
68
			export NO_BUILDKERNEL=YES
69
			;;
70
		--no-cleanobjdir|--no-cleanrepos|-d)
71
			export NO_CLEAN_FREEBSD_OBJ=YES
72
			export NO_CLEAN_FREEBSD_SRC=YES
73
			;;
74
		--flash-size|-f)
75
			shift
76
			if [ $# -eq 0 ]; then
77
				echo "--flash-size needs extra parameter."
78
				echo
79
				usage
80
			fi
81
			export FLASH_SIZE="${1}"
82
			;;
83
		--resume-image-build|-r)
84
			export NO_BUILDWORLD=YES
85
			export NO_BUILDKERNEL=YES
86
			export NO_CLEAN_FREEBSD_OBJ=YES
87
			export NO_CLEAN_FREEBSD_SRC=YES
88
			_SKIP_REBUILD_PRESTAGE=YES
89
			_USE_OLD_DATESTRING=YES
90
			;;
91
		--setup)
92
			BUILDACTION="builder_setup"
93
			;;
94
		--rsync-repos)
95
			BUILDACTION="rsync_repos"
96
			;;
97
		--build-kernels)
98
			BUILDACTION="buildkernels"
99
			;;
100
		--install-extra-kernels)
101
			shift
102
			if [ $# -eq 0 ]; then
103
				echo "--build-kernel needs extra parameter."
104
				echo
105
				usage
106
			fi
107
			export INSTALL_EXTRA_KERNELS="${1}"
108
			;;
109
		--snapshots)
110
			export SNAPSHOTS=1
111
			;;
112
		--poudriere-snapshots)
113
			export POUDRIERE_SNAPSHOTS=1
114
			;;
115
		--build-kernel)
116
			BUILDACTION="buildkernel"
117
			shift
118
			if [ $# -eq 0 ]; then
119
				echo "--build-kernel needs extra parameter."
120
				echo
121
				usage
122
			fi
123
			export BUILD_KERNELS="${1}"
124
			;;
125
		--update-sources)
126
			BUILDACTION="updatesources"
127
			;;
128
		--clean-builder)
129
			BUILDACTION="cleanbuilder"
130
			;;
131
		--setup-poudriere)
132
			BUILDACTION="setup_poudriere"
133
			;;
134
		--create-unified-patch)
135
			BUILDACTION="create_unified_patch"
136
			;;
137
		--update-poudriere-jails)
138
			BUILDACTION="update_poudriere_jails"
139
			;;
140
		-a)
141
			shift
142
			if [ $# -eq 0 ]; then
143
				echo "-a needs extra parameter."
144
				echo
145
				usage
146
			fi
147
			export ARCH_LIST="${1}"
148
			;;
149
		--update-poudriere-ports)
150
			BUILDACTION="update_poudriere_ports"
151
			;;
152
		--update-pkg-repo)
153
			BUILDACTION="update_pkg_repo"
154
			;;
155
		--do-not-upload|-u)
156
			export DO_NOT_UPLOAD=1
157
			;;
158
		all|none|*iso*|*ova*|*memstick*|*memstickserial*|*memstickadi*|*nanobsd*|*nanobsd-vga*)
159
			BUILDACTION="images"
160
			IMAGETYPE="${1}"
161
			;;
162
		-V)
163
			_USE_OLD_DATESTRING=YES
164
			shift
165
			[ -n "${1}" ] \
166
				&& var_to_print="${1}"
167
			;;
168
		--snapshot-update-status)
169
			shift
170
			snapshot_status_message="${1}"
171
			BUILDACTION="snapshot_status_message"
172
			_USE_OLD_DATESTRING=YES
173
			;;
174
		*)
175
			_USE_OLD_DATESTRING=YES
176
			usage
177
	esac
178
	shift
179
done
180

    
181
# Suck in local vars
182
. ${BUILDER_TOOLS}/builder_defaults.sh
183

    
184
# Suck in script helper functions
185
. ${BUILDER_TOOLS}/builder_common.sh
186

    
187
# Print var required with -V and exit
188
if [ -n "${var_to_print}"  ]; then
189
	eval "echo \$${var_to_print}"
190
	exit 0
191
fi
192

    
193
# Update snapshot status and exit
194
if [ "${BUILDACTION}" = "snapshot_status_message" ]; then
195
	if [ -z "${POUDRIERE_SNAPSHOTS}" ]; then
196
		export SNAPSHOTS=1
197
	fi
198
	snapshots_update_status "${snapshot_status_message}"
199
	exit 0
200
fi
201

    
202
# This should be run first
203
launch
204

    
205
case $BUILDACTION in
206
	builder_setup)
207
		builder_setup
208
	;;
209
	buildkernels)
210
		update_freebsd_sources
211
		build_all_kernels
212
	;;
213
	buildkernel)
214
		update_freebsd_sources
215
		build_all_kernels
216
	;;
217
	cleanbuilder)
218
		clean_builder
219
	;;
220
	images)
221
		# It will be handled below
222
	;;
223
	updatesources)
224
		update_freebsd_sources
225
	;;
226
	setup_poudriere)
227
		poudriere_init
228
	;;
229
	create_unified_patch)
230
		poudriere_create_patch
231
	;;
232
	update_poudriere_jails)
233
		poudriere_update_jails
234
	;;
235
	update_poudriere_ports)
236
		poudriere_update_ports
237
	;;
238
	rsync_repos)
239
		unset SKIP_FINAL_RSYNC
240
		pkg_repo_rsync "${CORE_PKG_PATH}"
241
	;;
242
	update_pkg_repo)
243
		if [ -z "${DO_NOT_UPLOAD}" -a ! -f /usr/local/bin/rsync ]; then
244
			echo "ERROR: rsync is not installed, aborting..."
245
			exit 1
246
		fi
247
		poudriere_bulk
248
	;;
249
	*)
250
		usage
251
	;;
252
esac
253

    
254
if [ "${BUILDACTION}" != "images" ]; then
255
	finish
256
	exit 0
257
fi
258

    
259
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
260
	_required=" \
261
		RSYNCIP \
262
		RSYNCUSER \
263
		RSYNCPATH \
264
		PKG_RSYNC_HOSTNAME \
265
		PKG_RSYNC_USERNAME \
266
		PKG_RSYNC_SSH_PORT \
267
		PKG_RSYNC_DESTDIR \
268
		PKG_REPO_SERVER_DEVEL \
269
		PKG_REPO_SERVER_RELEASE \
270
		PKG_REPO_SERVER_STAGING \
271
		PKG_REPO_BRANCH_DEVEL \
272
		PKG_REPO_BRANCH_RELEASE"
273

    
274
	for _var in ${_required}; do
275
		eval "_value=\${$_var}"
276
		if [ -z "${_value}" ]; then
277
			echo ">>> ERROR: ${_var} is not defined"
278
			exit 1
279
		fi
280
	done
281

    
282
	if [ ! -f /usr/local/bin/rsync ]; then
283
		echo "ERROR: rsync is not installed, aborting..."
284
		exit 1
285
	fi
286
fi
287

    
288
if [ $# -gt 1 ]; then
289
	echo "ERROR: Too many arguments given."
290
	echo
291
	usage
292
fi
293

    
294
if [ -n "${SNAPSHOTS}" -a -z "${IMAGETYPE}" ]; then
295
	IMAGETYPE="all"
296
fi
297

    
298
if [ -z "${IMAGETYPE}" ]; then
299
	echo "ERROR: Need to specify image type to build."
300
	echo
301
	usage
302
fi
303

    
304
if [ "$IMAGETYPE" = "none" ]; then
305
	_IMAGESTOBUILD=""
306
elif [ "$IMAGETYPE" = "all" ]; then
307
	_IMAGESTOBUILD="iso nanobsd nanobsd-vga memstick memstickserial"
308
	if [ "${TARGET}" = "amd64" ]; then
309
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
310
		if [ -n "${_IS_RELEASE}"  ]; then
311
			_IMAGESTOBUILD="${_IMAGESTOBUILD} ova"
312
		fi
313
	fi
314
else
315
	_IMAGESTOBUILD="${IMAGETYPE}"
316
fi
317

    
318
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
319

    
320
if [ -n "${SNAPSHOTS}" ]; then
321
	snapshots_update_status ">>> Starting snapshot build operations"
322

    
323
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
324
		snapshots_update_status ">>> Updating builder packages... "
325
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
326
	fi
327
fi
328

    
329
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
330
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
331
		&& rm -rf ${CORE_PKG_PATH}
332

    
333
	# Cleanup environment before start
334
	clean_builder
335

    
336
	# Make sure source directories are present.
337
	update_freebsd_sources
338
	git_last_commit
339

    
340
	# Ensure binaries are present that builder system requires
341
	builder_setup
342

    
343
	# Build world, kernel and install
344
	make_world
345

    
346
	# Build kernels
347
	build_all_kernels
348

    
349
	# Install kernel on installer
350
	installkernel ${INSTALLER_CHROOT_DIR}
351

    
352
	# Prepare pre-final staging area
353
	clone_to_staging_area
354

    
355
	# Install packages needed for Product
356
	install_pkg_install_ports
357
fi
358

    
359
# Create core repo
360
core_pkg_create_repo
361

    
362
# Send core repo to staging area
363
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
364

    
365
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
366

    
367
# XXX: Figure out why wait is failing and proper fix
368
# Global variable to keep track of process running in bg
369
export _bg_pids=""
370

    
371
for _IMGTOBUILD in $_IMAGESTOBUILD; do
372
	# Clean up items that should be cleaned each run
373
	staginareas_clean_each_run
374

    
375
	case "${_IMGTOBUILD}" in
376
		iso)
377
			create_iso_image
378
			;;
379
		memstick)
380
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
381
				for _variant in ${MEMSTICK_VARIANTS}; do
382
					create_memstick_image ${_variant}
383
				done
384
			else
385
				create_memstick_image
386
			fi
387
			;;
388
		memstickserial)
389
			create_memstick_serial_image
390
			;;
391
		memstickadi)
392
			create_memstick_adi_image
393
			;;
394
		nanobsd|nanobsd-vga)
395
			create_nanobsd_diskimage ${_IMGTOBUILD} "${FLASH_SIZE}"
396
			;;
397
		ova)
398
			old_custom_package_list="${custom_package_list}"
399
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
400
			install_pkg_install_ports
401
			create_ova_image
402
			export custom_package_list="${old_custom_package_list}"
403
			install_pkg_install_ports
404
			;;
405
	esac
406
done
407

    
408
if [ -n "${_bg_pids}" ]; then
409
	if [ -n "${SNAPSHOTS}" ]; then
410
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
411
	else
412
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
413
	fi
414
	wait
415

    
416
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
417
	while [ -n "${_bg_pids}" ]; do
418
		_tmp_pids="${_bg_pids}"
419
		unset _bg_pids
420
		for p in ${_tmp_pids}; do
421
			[ -z "${p}" ] \
422
				&& continue
423

    
424
			kill -0 ${p} >/dev/null 2>&1 \
425
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
426
		done
427
		[ -n "${_bg_pids}" ] \
428
			&& sleep 1
429
	done
430
fi
431

    
432
if [ -n "${SNAPSHOTS}" ]; then
433
	if [ "${IMAGETYPE}" = "none" -a -z "${DO_NOT_UPLOAD}" ]; then
434
		pkg_repo_rsync "${CORE_PKG_PATH}"
435
	elif [ "${IMAGETYPE}" != "none" ]; then
436
		snapshots_create_sha256
437
		# SCP files to snapshot web hosting area
438
		if [ -z "${DO_NOT_UPLOAD}" ]; then
439
			snapshots_scp_files
440
		fi
441
	fi
442
	# Alert the world that we have some snapshots ready.
443
	snapshots_update_status ">>> Builder run is complete."
444
fi
445

    
446
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
447
(cd ${IMAGES_FINAL_DIR} && find ${IMAGES_FINAL_DIR} -type f)
448

    
449
set -e
450
# Run final finish routines
451
finish
(8-8/8)