xref: /freebsd/release/release.sh (revision ec4deee4e4f2aef1b97d9424f25d04e91fd7dc10)
1#!/bin/sh
2#-
3# Copyright (c) 2013-2018 The FreeBSD Foundation
4# Copyright (c) 2013 Glen Barber
5# Copyright (c) 2011 Nathan Whitehorn
6# All rights reserved.
7#
8# Portions of this software were developed by Glen Barber
9# under sponsorship from the FreeBSD Foundation.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32# release.sh: check out source trees, and build release components with
33#  totally clean, fresh trees.
34# Based on release/generate-release.sh written by Nathan Whitehorn
35#
36# $FreeBSD$
37#
38
39export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
40
41VERSION=2
42
43# Prototypes that can be redefined per-chroot or per-target.
44load_chroot_env() { }
45load_target_env() { }
46buildenv_setup() { }
47
48usage() {
49	echo "Usage: $0 [-c release.conf]"
50	exit 1
51}
52
53# env_setup(): Set up the default build environment variables, such as the
54# CHROOTDIR, VCSCMD, SVNROOT, etc.  This is called before the release.conf
55# file is sourced, if '-c <release.conf>' is specified.
56env_setup() {
57	# The directory within which the release will be built.
58	CHROOTDIR="/scratch"
59	RELENGDIR="$(dirname $(realpath ${0}))"
60
61	# The default version control system command to obtain the sources.
62	for _dir in /usr/bin /usr/local/bin; do
63		for _svn in svn svnlite; do
64			[ -x "${_dir}/${_svn}" ] && VCSCMD="${_dir}/${_svn}"
65			[ ! -z "${VCSCMD}" ] && break 2
66		done
67	done
68	VCSCMD="${VCSCMD} checkout"
69
70	# The default svn checkout server, and svn branches for src/, doc/,
71	# and ports/.
72	SVNROOT="svn://svn.FreeBSD.org/"
73	SRCBRANCH="base/head@rHEAD"
74	DOCBRANCH="doc/head@rHEAD"
75	PORTBRANCH="ports/head@rHEAD"
76
77	# Set for embedded device builds.
78	EMBEDDEDBUILD=
79
80	# Sometimes one needs to checkout src with --force svn option.
81	# If custom kernel configs copied to src tree before checkout, e.g.
82	SRC_FORCE_CHECKOUT=
83
84	# The default make.conf and src.conf to use.  Set to /dev/null
85	# by default to avoid polluting the chroot(8) environment with
86	# non-default settings.
87	MAKE_CONF="/dev/null"
88	SRC_CONF="/dev/null"
89
90	# The number of make(1) jobs, defaults to the number of CPUs available
91	# for buildworld, and half of number of CPUs available for buildkernel.
92	WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
93	KERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
94
95	MAKE_FLAGS="-s"
96
97	# The name of the kernel to build, defaults to GENERIC.
98	KERNEL="GENERIC"
99
100	# Set to non-empty value to disable checkout of doc/ and/or ports/.
101	# Disabling ports/ checkout also forces NODOC to be set.
102	NODOC=
103	NOPORTS=
104
105	# Set to non-empty value to disable distributing source tree.
106	NOSRC=
107
108	# Set to non-empty value to build dvd1.iso as part of the release.
109	WITH_DVD=
110	WITH_COMPRESSED_IMAGES=
111
112	# Set to non-empty value to build virtual machine images as part of
113	# the release.
114	WITH_VMIMAGES=
115	WITH_COMPRESSED_VMIMAGES=
116	XZ_THREADS=0
117
118	# Set to non-empty value to build virtual machine images for various
119	# cloud providers as part of the release.
120	WITH_CLOUDWARE=
121
122	return 0
123} # env_setup()
124
125# env_check(): Perform sanity tests on the build environment, such as ensuring
126# files/directories exist, as well as adding backwards-compatibility hacks if
127# necessary.  This is called unconditionally, and overrides the defaults set
128# in env_setup() if '-c <release.conf>' is specified.
129env_check() {
130	chroot_build_release_cmd="chroot_build_release"
131	# Fix for backwards-compatibility with release.conf that does not have
132	# the trailing '/'.
133	case ${SVNROOT} in
134		*svn*)
135			SVNROOT="${SVNROOT}/"
136			;;
137		*)
138			;;
139	esac
140
141	# Prefix the branches with the SVNROOT for the full checkout URL.
142	SRCBRANCH="${SVNROOT}${SRCBRANCH}"
143	DOCBRANCH="${SVNROOT}${DOCBRANCH}"
144	PORTBRANCH="${SVNROOT}${PORTBRANCH}"
145
146	if [ -n "${EMBEDDEDBUILD}" ]; then
147		WITH_DVD=
148		WITH_COMPRESSED_IMAGES=
149		NODOC=yes
150		case ${EMBEDDED_TARGET}:${EMBEDDED_TARGET_ARCH} in
151			arm:arm*|arm64:aarch64)
152				chroot_build_release_cmd="chroot_arm_build_release"
153				;;
154			*)
155				;;
156		esac
157	fi
158
159	# If PORTS is set and NODOC is unset, force NODOC=yes because the ports
160	# tree is required to build the documentation set.
161	if [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
162		echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
163		echo "            and NOPORTS is set."
164		NODOC=yes
165	fi
166
167	# If NOSRC, NOPORTS and/or NODOC are unset, they must not pass to make
168	# as variables.  The release makefile verifies definedness of the
169	# NOPORTS/NODOC variables instead of their values.
170	SRCDOCPORTS=
171	if [ -n "${NOPORTS}" ]; then
172		SRCDOCPORTS="NOPORTS=yes"
173	fi
174	if [ -n "${NODOC}" ]; then
175		SRCDOCPORTS="${SRCDOCPORTS}${SRCDOCPORTS:+ }NODOC=yes"
176	fi
177	if [ -n "${NOSRC}" ]; then
178		SRCDOCPORTS="${SRCDOCPORTS}${SRCDOCPORTS:+ }NOSRC=yes"
179	fi
180
181	# The aggregated build-time flags based upon variables defined within
182	# this file, unless overridden by release.conf.  In most cases, these
183	# will not need to be changed.
184	CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
185	if [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
186		ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
187	else
188		ARCH_FLAGS=
189	fi
190	# Force src checkout if configured
191	FORCE_SRC_KEY=
192	if [ -n "${SRC_FORCE_CHECKOUT}" ]; then
193		FORCE_SRC_KEY="--force"
194	fi
195
196	if [ -z "${CHROOTDIR}" ]; then
197		echo "Please set CHROOTDIR."
198		exit 1
199	fi
200
201	if [ $(id -u) -ne 0 ]; then
202		echo "Needs to be run as root."
203		exit 1
204	fi
205
206	# Unset CHROOTBUILD_SKIP if the chroot(8) does not appear to exist.
207	if [ ! -z "${CHROOTBUILD_SKIP}" -a ! -e ${CHROOTDIR}/bin/sh ]; then
208		CHROOTBUILD_SKIP=
209	fi
210
211	CHROOT_MAKEENV="${CHROOT_MAKEENV} \
212		MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
213	CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
214	CHROOT_IMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
215	CHROOT_DMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
216	RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} \
217		${CONF_FILES}"
218	RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} \
219		KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
220	RELEASE_RMAKEFLAGS="${ARCH_FLAGS} \
221		KERNCONF=\"${KERNEL}\" ${CONF_FILES} ${SRCDOCPORTS} \
222		WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
223		WITH_CLOUDWARE=${WITH_CLOUDWARE} XZ_THREADS=${XZ_THREADS}"
224
225	return 0
226} # env_check()
227
228# chroot_setup(): Prepare the build chroot environment for the release build.
229chroot_setup() {
230	load_chroot_env
231	mkdir -p ${CHROOTDIR}/usr
232
233	if [ -z "${SRC_UPDATE_SKIP}" ]; then
234		${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
235	fi
236	if [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
237		${VCSCMD} ${DOCBRANCH} ${CHROOTDIR}/usr/doc
238	fi
239	if [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
240		${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
241	fi
242
243	if [ -z "${CHROOTBUILD_SKIP}" ]; then
244		cd ${CHROOTDIR}/usr/src
245		env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
246		env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
247			DESTDIR=${CHROOTDIR}
248		env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
249			DESTDIR=${CHROOTDIR}
250	fi
251
252	return 0
253} # chroot_setup()
254
255# extra_chroot_setup(): Prepare anything additional within the build
256# necessary for the release build.
257extra_chroot_setup() {
258	mkdir -p ${CHROOTDIR}/dev
259	mount -t devfs devfs ${CHROOTDIR}/dev
260	[ -e /etc/resolv.conf -a ! -e ${CHROOTDIR}/etc/resolv.conf ] && \
261		cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
262	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
263	# is created.  This is needed by ports-mgmt/pkg.
264	eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
265
266	# If MAKE_CONF and/or SRC_CONF are set and not character devices
267	# (/dev/null), copy them to the chroot.
268	if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
269		mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
270		cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
271	fi
272	if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
273		mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
274		cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
275	fi
276
277	if [ -d ${CHROOTDIR}/usr/ports ]; then
278		# Trick the ports 'run-autotools-fixup' target to do the right
279		# thing.
280		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
281		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
282		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
283		UNAME_r=${REVISION}-${BRANCH}
284		if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
285			PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
286			PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
287			PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
288			PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
289			PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
290			chroot ${CHROOTDIR} env ${PBUILD_FLAGS} \
291				OPTIONS_UNSET="AVAHI FOP IGOR" make -C \
292				/usr/ports/textproc/docproj \
293				FORCE_PKG_REGISTER=1 \
294				install clean distclean
295		fi
296	fi
297
298	if [ ! -z "${EMBEDDEDPORTS}" ]; then
299		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
300		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
301		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
302		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
303		PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
304		PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
305		PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
306		PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
307		for _PORT in ${EMBEDDEDPORTS}; do
308			eval chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
309				/usr/ports/${_PORT} \
310				FORCE_PKG_REGISTER=1 deinstall install clean distclean
311		done
312	fi
313
314	buildenv_setup
315
316	return 0
317} # extra_chroot_setup()
318
319# chroot_build_target(): Build the userland and kernel for the build target.
320chroot_build_target() {
321	load_target_env
322	if [ ! -z "${EMBEDDEDBUILD}" ]; then
323		RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
324			TARGET=${EMBEDDED_TARGET} \
325			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
326		RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
327			TARGET=${EMBEDDED_TARGET} \
328			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
329	fi
330	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
331	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
332
333	return 0
334} # chroot_build_target
335
336# chroot_build_release(): Invoke the 'make release' target.
337chroot_build_release() {
338	load_target_env
339	if [ ! -z "${WITH_VMIMAGES}" ]; then
340		if [ -z "${VMFORMATS}" ]; then
341			VMFORMATS="$(eval chroot ${CHROOTDIR} \
342				make -C /usr/src/release -V VMFORMATS)"
343		fi
344		if [ -z "${VMSIZE}" ]; then
345			VMSIZE="$(eval chroot ${CHROOTDIR} \
346				make -C /usr/src/release -V VMSIZE)"
347		fi
348		RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
349			VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
350	fi
351	eval chroot ${CHROOTDIR} make -C /usr/src/release \
352		${RELEASE_RMAKEFLAGS} release
353	eval chroot ${CHROOTDIR} make -C /usr/src/release \
354		${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
355		WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
356		WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
357
358	return 0
359} # chroot_build_release()
360
361efi_boot_name()
362{
363	case $1 in
364		arm)
365			echo "bootarm.efi"
366			;;
367		arm64)
368			echo "bootaa64.efi"
369			;;
370		amd64)
371			echo "bootx64.efi"
372			;;
373	esac
374}
375
376# chroot_arm_build_release(): Create arm SD card image.
377chroot_arm_build_release() {
378	load_target_env
379	case ${EMBEDDED_TARGET} in
380		arm|arm64)
381			if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
382				. "${RELENGDIR}/tools/arm.subr"
383			fi
384			;;
385		*)
386			;;
387	esac
388	[ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
389	export MAKE_FLAGS="${MAKE_FLAGS} TARGET=${EMBEDDED_TARGET}"
390	export MAKE_FLAGS="${MAKE_FLAGS} TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
391	export MAKE_FLAGS="${MAKE_FLAGS} ${CONF_FILES}"
392	eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release obj
393	export WORLDDIR="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release -V WORLDDIR)"
394	export OBJDIR="$(eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release -V .OBJDIR)"
395	export DESTDIR="${OBJDIR}/${KERNEL}"
396	export IMGBASE="${CHROOTDIR}/${OBJDIR}/${BOARDNAME}.img"
397	export OSRELEASE="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release \
398		TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
399		-V OSRELEASE)"
400	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
401	chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
402	export mddev=$(chroot ${CHROOTDIR} \
403		mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
404	arm_create_disk
405	arm_install_base
406	arm_install_boot
407	arm_install_uboot
408	mdconfig -d -u ${mddev}
409	chroot ${CHROOTDIR} rmdir ${DESTDIR}
410	mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img
411	chroot ${CHROOTDIR} mkdir -p /R
412	chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img \
413		/R/${OSRELEASE}-${BOARDNAME}.img
414	chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${BOARDNAME}.img
415	cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
416		> CHECKSUM.SHA512
417	cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
418		> CHECKSUM.SHA256
419
420	return 0
421} # chroot_arm_build_release()
422
423# main(): Start here.
424main() {
425	set -e # Everything must succeed
426	env_setup
427	while getopts c: opt; do
428		case ${opt} in
429			c)
430				RELEASECONF="$(realpath ${OPTARG})"
431				;;
432			\?)
433				usage
434				;;
435		esac
436	done
437	shift $(($OPTIND - 1))
438	if [ ! -z "${RELEASECONF}" ]; then
439		if [ -e "${RELEASECONF}" ]; then
440			. ${RELEASECONF}
441		else
442			echo "Nonexistent configuration file: ${RELEASECONF}"
443			echo "Using default build environment."
444		fi
445	fi
446	env_check
447	trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
448	chroot_setup
449	extra_chroot_setup
450	chroot_build_target
451	${chroot_build_release_cmd}
452
453	return 0
454} # main()
455
456main "${@}"
457