Skip to content

Commit 6789ee2

Browse files
committed
Provide, and use, a buildah script to construct zbm-builder images
Using buildah directly provides flexibility that can not be achieved with a Dockerfile. It also prevents the layer problem that bloats image sizes, avoiding the need to squash the image. Closes #230.
1 parent fdb22b3 commit 6789ee2

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

releng/docker/README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,28 @@ the commands should work just as well by substituting `docker` for `podman`.
1515

1616
# Creating a ZFSBootMenu Builder Image
1717

18-
The provided `Dockerfile` automates creation of the ZFSBootMenu builder image.
19-
From this directory, simply run
18+
The script `image-build.sh` uses `buildah` to construct a ZBM builder image.
19+
This is the preferred way to construct the image and may, in the future,
20+
provide features not available with a `podman build` workflow. The script
21+
requires a single argument, the tag to use when naming the image.
22+
23+
An optional second argument is a Git commit-like reference (a hash or tag) that
24+
will be recorded as `/etc/zbm-commit-hash` in the image. The contents of this
25+
file are used to checkout a specific state of the ZFSBootMenu repository. If
26+
the tag is unspecified on the command line, the build script will attempt to
27+
capture a reference to the current HEAD commit if the image is built in an
28+
active git repository. If a commit-like name is not provided and cannot be
29+
discovered, no default will be recorded and containers will attempt to build
30+
from the current `master`.
31+
32+
The `image-build.sh` script expects to be run from the root of the ZFSBootMenu
33+
tree by default. From there, the path `releng/docker/zbm-build.sh` defines the
34+
entrypoint for build containers. To run the `image-build.sh` script from
35+
another directory, simply set the `ZBM_BUILDER` environment variable to the
36+
location of the `zbm-build.sh` script to use.
37+
38+
For those without access to `buildah`, the `Dockerfile` will also create of a
39+
ZFSBootMenu builder image. From this directory, simply run
2040

2141
```sh
2242
podman build --squash -t zbm .
@@ -34,10 +54,10 @@ the latest release version packaged for Void; manually editing the `Dockerfile`
3454
to add new dependencies may be necessary until a new release is packaged.
3555

3656
The builder image does **not** contain a ZFSBootMenu installation or a copy of
37-
the upstream git repository. Instead, the image contains a build script,
38-
installed as `/zbm-build.sh`, that runs by default. The script ensures that a
39-
ZFSBootMenu repository is available in a running container and invokes
40-
`generate-zbm` to build images.
57+
the upstream git repository. Instead, the entrypoint `/zbm-build.sh` will fetch
58+
a ZFSBootMenu archive when the container is instantiated (or allow a local copy
59+
to be bind-mounted) and, as noted above, attempt to check out a specific commit
60+
based on the contents of `/etc/zbm-commit-hash`.
4161

4262
# Running a ZFSBootMenu Builder Container
4363

releng/docker/image-build.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/sh
2+
# vim: softtabstop=2 shiftwidth=2 expandtab
3+
4+
set -o errexit
5+
6+
# A tag for the image is required
7+
tag="${1}"
8+
if [ -z "${tag}" ]; then
9+
echo "USAGE: $0 <tag> [zbm-commit-like]"
10+
exit 1
11+
fi
12+
13+
# If a commit hash is unspecified, try to pull HEAD from git
14+
zbm_commit_hash="$2"
15+
if [ -z "${zbm_commit_hash}" ]; then
16+
if ! zbm_commit_hash="$(git rev-parse HEAD 2>/dev/null)"; then
17+
unset zbm_commit_hash
18+
fi
19+
fi
20+
21+
if [ -z "${ZBM_BUILDER}" ]; then
22+
ZBM_BUILDER="./releng/docker/zbm-build.sh"
23+
fi
24+
25+
if [ ! -r "${ZBM_BUILDER}" ]; then
26+
echo "ERROR: cannot find build script at ${ZBM_BUILDER}"
27+
echo "Run from ZFSBootMenu root or override \$ZBM_BUILDER"
28+
exit 1
29+
fi
30+
31+
maintainer="ZFSBootMenu Team, https://zfsbootmenu.org"
32+
container="$(buildah from voidlinux/voidlinux:latest)"
33+
34+
buildah config --label author="${maintainer}" "${container}"
35+
36+
# Make sure image is up to date
37+
buildah run "${container}" xbps-install -Syu xbps
38+
buildah run "${container}" xbps-install -yu
39+
40+
# Prefer an LTS version over whatever Void thinks is current
41+
buildah run "${container}" sh -c "cat > /etc/xbps.d/10-nolinux.conf" <<-EOF
42+
ignorepkg=linux
43+
ignorepkg=linux-headers
44+
EOF
45+
46+
# Install ZFSBootMenu dependencies and components necessary to build images
47+
buildah run "${container}" \
48+
sh -c 'xbps-query -Rp run_depends zfsbootmenu | xargs xbps-install -y'
49+
buildah run "${container}" xbps-install -y \
50+
linux5.10 linux5.10-headers gummiboot-efistub curl yq-go bash kbd terminus-font
51+
52+
# Remove headers and development toolchain, but keep binutils for objcopy
53+
buildah run "${container}" sh -c 'echo "ignorepkg=dkms" > /etc/xbps.d/10-nodkms.conf'
54+
buildah run "${container}" xbps-pkgdb -m manual binutils
55+
buildah run "${container}" xbps-remove -Roy linux5.10-headers dkms
56+
buildah run "${container}" sh -c 'rm -f /var/cache/xbps/*'
57+
58+
# Record a commit hash if one is available
59+
if [ -n "${zbm_commit_hash}" ]; then
60+
echo "${zbm_commit_hash}" | \
61+
buildah run "${container}" sh -c 'cat > /etc/zbm-commit-hash'
62+
fi
63+
64+
buildah copy "${container}" "${ZBM_BUILDER}" /zbm-build.sh
65+
buildah run "${container}" chmod 755 /zbm-build.sh
66+
67+
buildah config \
68+
--workingdir / \
69+
--entrypoint '[ "/zbm-build.sh" ]' \
70+
--cmd '[ ]' \
71+
"${container}"
72+
73+
buildah commit --rm "${container}" "${tag}"

releng/make-binary.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ esac
2626

2727
buildtag="${2:-localhost/zbm-builder:$(date '+%Y%m%d')}"
2828
if ! podman inspect "${buildtag}" >/dev/null 2>&1; then
29-
if ! bldctx="$( realpath -e releng/docker )"; then
30-
error "missing releng/docker, cannot create image ${buildtag}"
31-
fi
32-
33-
build_args=( "--squash" )
29+
build_args=( "${buildtag}" )
3430

35-
if ZBM_COMMIT_HASH="$(git rev-parse HEAD)" && [ -n "${ZBM_COMMIT_HASH}" ]; then
36-
build_args+=( "--build-arg=ZBM_COMMIT_HASH=${ZBM_COMMIT_HASH}" )
31+
if [ -n "${ZBM_COMMIT_HASH}" ]; then
32+
build_args+=( "${ZBM_COMMIT_HASH}" )
3733
fi
3834

39-
if ! podman build -t "${buildtag}" "${build_args[@]}" "${bldctx}"; then
35+
if ! ./releng/docker/image-build.sh "${build_args[@]}"; then
4036
error "failed to create builder image"
4137
fi
4238
fi

0 commit comments

Comments
 (0)