release.sh revision 326014
1230557Sjimharris#!/bin/sh
2230557Sjimharris#-
3230557Sjimharris# Copyright (c) 2013-2017 The FreeBSD Foundation
4230557Sjimharris# Copyright (c) 2013 Glen Barber
5230557Sjimharris# Copyright (c) 2011 Nathan Whitehorn
6230557Sjimharris# All rights reserved.
7230557Sjimharris#
8230557Sjimharris# Portions of this software were developed by Glen Barber
9230557Sjimharris# under sponsorship from the FreeBSD Foundation.
10230557Sjimharris#
11230557Sjimharris# Redistribution and use in source and binary forms, with or without
12230557Sjimharris# modification, are permitted provided that the following conditions
13230557Sjimharris# are met:
14230557Sjimharris# 1. Redistributions of source code must retain the above copyright
15230557Sjimharris#    notice, this list of conditions and the following disclaimer.
16230557Sjimharris# 2. Redistributions in binary form must reproduce the above copyright
17230557Sjimharris#    notice, this list of conditions and the following disclaimer in the
18230557Sjimharris#    documentation and/or other materials provided with the distribution.
19230557Sjimharris#
20230557Sjimharris# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21230557Sjimharris# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22230557Sjimharris# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23230557Sjimharris# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24230557Sjimharris# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25230557Sjimharris# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26230557Sjimharris# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27230557Sjimharris# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28230557Sjimharris# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29230557Sjimharris# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30230557Sjimharris# SUCH DAMAGE.
31230557Sjimharris#
32230557Sjimharris# release.sh: check out source trees, and build release components with
33230557Sjimharris#  totally clean, fresh trees.
34230557Sjimharris# Based on release/generate-release.sh written by Nathan Whitehorn
35230557Sjimharris#
36230557Sjimharris# $FreeBSD: stable/10/release/release.sh 326014 2017-11-20 15:46:23Z gjb $
37230557Sjimharris#
38230557Sjimharris
39230557Sjimharrisexport PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
40230557Sjimharris
41230557SjimharrisVERSION=2
42230557Sjimharris
43230557Sjimharris# Prototypes that can be redefined per-chroot or per-target.
44230557Sjimharrisload_chroot_env() { }
45230557Sjimharrisload_target_env() { }
46230557Sjimharrisbuildenv_setup() { }
47230557Sjimharris
48230557Sjimharrisusage() {
49230557Sjimharris	echo "Usage: $0 [-c release.conf]"
50230557Sjimharris	exit 1
51230557Sjimharris}
52230557Sjimharris
53230557Sjimharris# env_setup(): Set up the default build environment variables, such as the
54230557Sjimharris# CHROOTDIR, VCSCMD, SVNROOT, etc.  This is called before the release.conf
55230557Sjimharris# file is sourced, if '-c <release.conf>' is specified.
56230557Sjimharrisenv_setup() {
57230557Sjimharris	# The directory within which the release will be built.
58230557Sjimharris	CHROOTDIR="/scratch"
59230557Sjimharris	RELENGDIR="$(dirname $(realpath ${0}))"
60230557Sjimharris
61230557Sjimharris	# The default version control system command to obtain the sources.
62230557Sjimharris	for _dir in /usr/bin /usr/local/bin; do
63230557Sjimharris		for _svn in svn svnlite; do
64230557Sjimharris			[ -x "${_dir}/${_svn}" ] && VCSCMD="${_dir}/${_svn}"
65230557Sjimharris			[ ! -z "${VCSCMD}" ] && break 2
66230557Sjimharris		done
67230557Sjimharris	done
68230557Sjimharris	VCSCMD="${VCSCMD} checkout"
69230557Sjimharris
70230557Sjimharris	# The default svn checkout server, and svn branches for src/, doc/,
71230557Sjimharris	# and ports/.
72230557Sjimharris	SVNROOT="svn://svn.FreeBSD.org/"
73230557Sjimharris	SRCBRANCH="base/head@rHEAD"
74230557Sjimharris	DOCBRANCH="doc/head@rHEAD"
75230557Sjimharris	PORTBRANCH="ports/head@rHEAD"
76230557Sjimharris
77230557Sjimharris	# Set for embedded device builds.
78230557Sjimharris	EMBEDDEDBUILD=
79230557Sjimharris
80230557Sjimharris	# Sometimes one needs to checkout src with --force svn option.
81230557Sjimharris	# If custom kernel configs copied to src tree before checkout, e.g.
82230557Sjimharris	SRC_FORCE_CHECKOUT=
83230557Sjimharris
84230557Sjimharris	# The default make.conf and src.conf to use.  Set to /dev/null
85230557Sjimharris	# by default to avoid polluting the chroot(8) environment with
86230557Sjimharris	# non-default settings.
87230557Sjimharris	MAKE_CONF="/dev/null"
88230557Sjimharris	SRC_CONF="/dev/null"
89230557Sjimharris
90230557Sjimharris	# The number of make(1) jobs, defaults to the number of CPUs available
91230557Sjimharris	# for buildworld, and half of number of CPUs available for buildkernel.
92230557Sjimharris	WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
93230557Sjimharris	KERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
94230557Sjimharris
95230557Sjimharris	MAKE_FLAGS="-s"
96230557Sjimharris
97230557Sjimharris	# The name of the kernel to build, defaults to GENERIC.
98230557Sjimharris	KERNEL="GENERIC"
99230557Sjimharris
100230557Sjimharris	# Set to non-empty value to disable checkout of doc/ and/or ports/.
101230557Sjimharris	# Disabling ports/ checkout also forces NODOC to be set.
102230557Sjimharris	NODOC=
103230557Sjimharris	NOPORTS=
104230557Sjimharris
105230557Sjimharris	# Set to non-empty value to build dvd1.iso as part of the release.
106230557Sjimharris	WITH_DVD=
107230557Sjimharris	WITH_COMPRESSED_IMAGES=
108230557Sjimharris
109230557Sjimharris	# Set to non-empty value to build virtual machine images as part of
110230557Sjimharris	# the release.
111230557Sjimharris	WITH_VMIMAGES=
112230557Sjimharris	WITH_COMPRESSED_VMIMAGES=
113230557Sjimharris	XZ_THREADS=0
114
115	# Set to non-empty value to build virtual machine images for various
116	# cloud providers as part of the release.
117	WITH_CLOUDWARE=
118
119	return 0
120} # env_setup()
121
122# env_check(): Perform sanity tests on the build environment, such as ensuring
123# files/directories exist, as well as adding backwards-compatibility hacks if
124# necessary.  This is called unconditionally, and overrides the defaults set
125# in env_setup() if '-c <release.conf>' is specified.
126env_check() {
127	chroot_build_release_cmd="chroot_build_release"
128	# Fix for backwards-compatibility with release.conf that does not have
129	# the trailing '/'.
130	case ${SVNROOT} in
131		*svn*)
132			SVNROOT="${SVNROOT}/"
133			;;
134		*)
135			;;
136	esac
137
138	# Prefix the branches with the SVNROOT for the full checkout URL.
139	SRCBRANCH="${SVNROOT}${SRCBRANCH}"
140	DOCBRANCH="${SVNROOT}${DOCBRANCH}"
141	PORTBRANCH="${SVNROOT}${PORTBRANCH}"
142
143	if [ -n "${EMBEDDEDBUILD}" ]; then
144		WITH_DVD=
145		WITH_COMPRESSED_IMAGES=
146		NODOC=yes
147		case ${EMBEDDED_TARGET}:${EMBEDDED_TARGET_ARCH} in
148			arm:arm*|arm64:aarch64)
149				chroot_build_release_cmd="chroot_arm_build_release"
150				;;
151			*)
152				;;
153		esac
154	fi
155
156	# If PORTS is set and NODOC is unset, force NODOC=yes because the ports
157	# tree is required to build the documentation set.
158	if [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
159		echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
160		echo "            and NOPORTS is set."
161		NODOC=yes
162	fi
163
164	# If NOPORTS and/or NODOC are unset, they must not pass to make as
165	# variables.  The release makefile verifies definedness of the
166	# NOPORTS/NODOC variables instead of their values.
167	DOCPORTS=
168	if [ -n "${NOPORTS}" ]; then
169		DOCPORTS="NOPORTS=yes "
170	fi
171	if [ -n "${NODOC}" ]; then
172		DOCPORTS="${DOCPORTS}NODOC=yes"
173	fi
174
175	# The aggregated build-time flags based upon variables defined within
176	# this file, unless overridden by release.conf.  In most cases, these
177	# will not need to be changed.
178	CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
179	if [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
180		ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
181	else
182		ARCH_FLAGS=
183	fi
184	# Force src checkout if configured
185	FORCE_SRC_KEY=
186	if [ -n "${SRC_FORCE_CHECKOUT}" ]; then
187		FORCE_SRC_KEY="--force"
188	fi
189
190	if [ -z "${CHROOTDIR}" ]; then
191		echo "Please set CHROOTDIR."
192		exit 1
193	fi
194
195	if [ $(id -u) -ne 0 ]; then
196		echo "Needs to be run as root."
197		exit 1
198	fi
199
200	CHROOT_MAKEENV="${CHROOT_MAKEENV} \
201		MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
202	CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
203	CHROOT_IMAKEFLAGS="${CONF_FILES}"
204	CHROOT_DMAKEFLAGS="${CONF_FILES}"
205	RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} \
206		${CONF_FILES}"
207	RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} \
208		KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
209	RELEASE_RMAKEFLAGS="${ARCH_FLAGS} \
210		KERNCONF=\"${KERNEL}\" ${CONF_FILES} ${DOCPORTS} \
211		WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
212		WITH_CLOUDWARE=${WITH_CLOUDWARE} XZ_THREADS=${XZ_THREADS}"
213
214	return 0
215} # env_check()
216
217# chroot_setup(): Prepare the build chroot environment for the release build.
218chroot_setup() {
219	load_chroot_env
220	mkdir -p ${CHROOTDIR}/usr
221
222	if [ -z "${SRC_UPDATE_SKIP}" ]; then
223		${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
224	fi
225	if [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
226		${VCSCMD} ${DOCBRANCH} ${CHROOTDIR}/usr/doc
227	fi
228	if [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
229		${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
230	fi
231
232	if [ -z "${CHROOTBUILD_SKIP}" ]; then
233		cd ${CHROOTDIR}/usr/src
234		env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
235		env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
236			DESTDIR=${CHROOTDIR}
237		env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
238			DESTDIR=${CHROOTDIR}
239	fi
240
241	return 0
242} # chroot_setup()
243
244# extra_chroot_setup(): Prepare anything additional within the build
245# necessary for the release build.
246extra_chroot_setup() {
247	mkdir -p ${CHROOTDIR}/dev
248	mount -t devfs devfs ${CHROOTDIR}/dev
249	[ -e /etc/resolv.conf -a ! -e ${CHROOTDIR}/etc/resolv.conf ] && \
250		cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
251	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
252	# is created.  This is needed by ports-mgmt/pkg.
253	eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
254
255	# If MAKE_CONF and/or SRC_CONF are set and not character devices
256	# (/dev/null), copy them to the chroot.
257	if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
258		mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
259		cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
260	fi
261	if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
262		mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
263		cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
264	fi
265
266	if [ -d ${CHROOTDIR}/usr/ports ]; then
267		# Trick the ports 'run-autotools-fixup' target to do the right
268		# thing.
269		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
270		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
271		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
272		UNAME_r=${REVISION}-${BRANCH}
273		if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
274			PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
275			PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
276			PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
277			chroot ${CHROOTDIR} make -C /usr/ports/textproc/docproj \
278				${PBUILD_FLAGS} OPTIONS_UNSET="FOP IGOR" \
279				FORCE_PKG_REGISTER=1 \
280				install clean distclean
281		fi
282	fi
283
284	if [ ! -z "${EMBEDDEDPORTS}" ]; then
285		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
286		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
287		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
288		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
289		PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
290		PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
291		for _PORT in ${EMBEDDEDPORTS}; do
292			eval chroot ${CHROOTDIR} make -C /usr/ports/${_PORT} \
293				FORCE_PKG_REGISTER=1 ${PBUILD_FLAGS} install clean distclean
294		done
295	fi
296
297	buildenv_setup
298
299	return 0
300} # extra_chroot_setup()
301
302# chroot_build_target(): Build the userland and kernel for the build target.
303chroot_build_target() {
304	load_target_env
305	if [ ! -z "${EMBEDDEDBUILD}" ]; then
306		RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
307			TARGET=${EMBEDDED_TARGET} \
308			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
309		RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
310			TARGET=${EMBEDDED_TARGET} \
311			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
312	fi
313	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
314	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
315
316	return 0
317} # chroot_build_target
318
319# chroot_build_release(): Invoke the 'make release' target.
320chroot_build_release() {
321	load_target_env
322	if [ ! -z "${WITH_VMIMAGES}" ]; then
323		if [ -z "${VMFORMATS}" ]; then
324			VMFORMATS="$(eval chroot ${CHROOTDIR} \
325				make -C /usr/src/release -V VMFORMATS)"
326		fi
327		if [ -z "${VMSIZE}" ]; then
328			VMSIZE="$(eval chroot ${CHROOTDIR} \
329				make -C /usr/src/release -V VMSIZE)"
330		fi
331		RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
332			VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
333	fi
334	eval chroot ${CHROOTDIR} make -C /usr/src/release \
335		${RELEASE_RMAKEFLAGS} release
336	eval chroot ${CHROOTDIR} make -C /usr/src/release \
337		${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
338		WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
339		WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
340
341	return 0
342} # chroot_build_release()
343
344# chroot_arm_build_release(): Create arm SD card image.
345chroot_arm_build_release() {
346	load_target_env
347	eval chroot ${CHROOTDIR} make -C /usr/src/release obj
348	case ${EMBEDDED_TARGET} in
349		arm|arm64)
350			if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
351				. "${RELENGDIR}/tools/arm.subr"
352			fi
353			;;
354		*)
355			;;
356	esac
357	[ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
358	WORLDDIR="$(eval chroot ${CHROOTDIR} make -C /usr/src/release -V WORLDDIR)"
359	OBJDIR="$(eval chroot ${CHROOTDIR} make -C /usr/src/release -V .OBJDIR)"
360	DESTDIR="${OBJDIR}/${KERNEL}"
361	IMGBASE="${CHROOTDIR}/${OBJDIR}/${KERNEL}.img"
362	OSRELEASE="$(eval chroot ${CHROOTDIR} make -C /usr/src/release \
363		TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
364		-V OSRELEASE)"
365	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
366	chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
367	export mddev=$(chroot ${CHROOTDIR} \
368		mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
369	arm_create_disk
370	arm_install_base
371	arm_install_uboot
372	mdconfig -d -u ${mddev}
373	chroot ${CHROOTDIR} rmdir ${DESTDIR}
374	mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${KERNEL}.img
375	chroot ${CHROOTDIR} mkdir -p /R
376	chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${KERNEL}.img \
377		/R/${OSRELEASE}-${KERNEL}.img
378	chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${KERNEL}.img
379	cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
380		> CHECKSUM.SHA512
381	cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
382		> CHECKSUM.SHA256
383
384	return 0
385} # chroot_arm_build_release()
386
387# main(): Start here.
388main() {
389	set -e # Everything must succeed
390	env_setup
391	while getopts c: opt; do
392		case ${opt} in
393			c)
394				RELEASECONF="$(realpath ${OPTARG})"
395				;;
396			\?)
397				usage
398				;;
399		esac
400	done
401	shift $(($OPTIND - 1))
402	if [ ! -z "${RELEASECONF}" ]; then
403		if [ -e "${RELEASECONF}" ]; then
404			. ${RELEASECONF}
405		else
406			echo "Nonexistent configuration file: ${RELEASECONF}"
407			echo "Using default build environment."
408		fi
409	fi
410	env_check
411	trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
412	chroot_setup
413	extra_chroot_setup
414	chroot_build_target
415	${chroot_build_release_cmd}
416
417	return 0
418} # main()
419
420main "${@}"
421