xref: /freebsd/release/scripts/make-oci-image.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#! /bin/sh
2
3# Build an Open Container Initiative (OCI) container image
4
5curdir=$1; shift
6rev=$1; shift
7branch=$1; shift
8arch=$1; shift
9image=$1; shift
10
11major=${rev%.*}
12minor=${rev#*.}
13
14abi=FreeBSD:${major}:${arch}
15
16echo "Building OCI freebsd${major}-${image} image for ${abi}"
17
18. ${curdir}/tools/oci-image-${image}.conf
19
20init_workdir() {
21	local abi=$1; shift
22	local workdir=$(mktemp -d -t oci-images)
23
24	mkdir ${workdir}/repos
25	cat > ${workdir}/repos/base.conf <<EOF
26FreeBSD-base: {
27  url: "file:///usr/obj/usr/src/repo/${abi}/latest"
28  signature_type: "none"
29  fingerprints: "none"
30}
31EOF
32	cp /etc/pkg/FreeBSD.conf ${workdir}/repos
33	echo ${workdir}
34}
35
36install_packages() {
37	local abi=$1; shift
38	local workdir=$1; shift
39	local rootdir=$1; shift
40	if [ ! -d ${rootdir}/usr/share/keys/pkg/trusted ]; then
41		mkdir -p ${rootdir}/usr/share/keys/pkg/trusted
42	fi
43	cp /usr/share/keys/pkg/trusted/* ${rootdir}/usr/share/keys/pkg/trusted
44	# We install the packages and then remove repository metadata (keeping the
45	# metadata for what was installed). This trims more than 40Mb from the
46	# resulting image.
47	env IGNORE_OSVERSION=yes ABI=${abi} pkg --rootdir ${rootdir} --repo-conf-dir ${workdir}/repos \
48		install -yq "$@" || exit $?
49	rm -rf ${rootdir}/var/db/pkg/repos
50}
51
52workdir=$(init_workdir ${abi})
53if [ -n "${OCI_BASE_IMAGE}" ]; then
54	base_image=freebsd${major}-${OCI_BASE_IMAGE}
55else
56	base_image=scratch
57fi
58
59c=$(buildah from --arch ${arch} ${base_image})
60m=$(buildah mount $c)
61oci_image_build
62buildah unmount $c
63buildah commit --rm $c freebsd${major}-${image}:latest
64