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