Repository navigation
build: generalize SNAP_BUILD_VARIANT into a variant payload mechanism (RFC) - #491
Open
chris-bainbridge wants to merge 4 commits into
Open
chris-bainbridge wants to merge 4 commits into
chris-bainbridge wants to merge 4 commits into
Conversation
The build already supports selecting a variant of the base snap:
SNAP_BUILD_VARIANT is derived from the Launchpad recipe name (or a
local build-env file), suffixed onto the version and recorded as
SNAP_BUILD_NAME, and parts and hooks branch on it (the cloud-init
variant wires itself this way). What a variant could not do so far is
carry content.
Give a variant a payload directory, variants/<variant>/, applied to
the rootfs by the install target (tools/apply-variant-payload.sh)
after the base static/ files are copied and before the hooks run:
* static/ extra files, copied on top of the base static
files (same-path variant file wins)
* fstab.d/*.conf appended to /etc/fstab, in glob order
* writable.conf.d/*.conf appended to core-writable.conf, in glob order
The 993-factory-files test learns to accept d/f entries nested under
an existing fstab mount, the way a payload pins individual entries
inside a mounted tree without declaring a mount of its own.
Multiarch paths in a payload use the @multiarch@ placeholder,
substituted with the build environment's triplet at apply time.
Content that cannot live in the tree (per-arch ELFs resolved from
archive debs or built from source) is staged by the variant's parts
into $CRAFT_STAGE/<variant>-payload/ and copied in on that variant's
builds only.
The machinery is inert without a variant: a regular or cloud-init
build applies no payload, and the new hook-tests/995-variant-payload
test enforces both directions - the selected variant's fragments are
applied exactly once, and on a regular build the base fstab and
core-writable.conf are primed byte-identical to the static sources
with no variant payload file present in the prime.
The example variant is a tiny demonstration payload exercising every
knob (one extra file, one writable mount with its C line and factory
template, one nested d line) so the machinery is exercised end to end
with content, in CI, without carrying any product payload.
Add tools/build-variant.sh <variant>: select the variant the same way the Launchpad recipes do (write build-env with SNAP_BUILD_VARIANT, remove it afterwards), run snapcraft pack, then pack the result a second time as core26-<variant>. The snap name cannot be changed inside the build: snapcraft.yaml's project name is static (changing it would swap snapcraft's project-keyed LXD build instance and lose the incremental build state). The packed snap is therefore packed again from the build instance's prime directory with meta/snap.yaml's name patched to core26-<variant>, as real root inside the LXD container so file ownership and the /dev device nodes survive the round trip. The version gets its "+<variant>" suffix re-applied there if the build lost it to the cached-state quirk (on incremental builds craft-parts re-applies the project_vars snapshots recorded in the other parts' cached step states, which clobbers the freshly set version), and the final snap's name and version suffix are verified from its packed metadata. The build instance holding the matching prime is located by comparing each candidate's /root/prime/meta/snap.yaml version against the version just packed. Instances the script starts temporarily are stopped again on every exit path, already-running candidates are left running, and a prime-version collision between candidates is an error rather than a silent pick.
A variant payload is architecture specific: its chroot hooks and chisel slice resolution execute the build architecture's binaries and there is no cross-build. Let a variant declare the architectures it supports in variants/<variant>/architectures (whitespace-separated Debian architecture names) and fail loudly on any other build architecture, instead of silently producing a snap whose payload assumes the wrong one. The check runs in apply-variant-payload.sh, so it holds in every build path including Launchpad, and earlier in tools/build-variant.sh, so a local build fails before the long build starts. A variant without the file is unrestricted and builds on every architecture the project builds; the core26 project itself declares no platforms and stays that way - the restriction belongs to the variant, not to the shared snapcraft project. The example variant declares amd64 and arm64 (the CI matrix builds it natively on both); the synthetic payload in the 995 test exercises the rejection path and asserts the rejected application leaves the rootfs untouched.
Add a dispatch-only workflow that builds a variant of the core26 base snap with tools/build-variant.sh and uploads it as an artifact, defaulting to the example variant: the machinery (selection, payload application, architecture declaration, hook tests, naming) gets exercised end to end on real hosted amd64 and arm64 builds. Dispatch-only on purpose: the regular and cloud-init variants are already built and tested by the spread tests on every push and pull request, and the hook tests (including 995-variant-payload) run inside every snapcraft build. Whether the example variant should join the spread matrix, and whether variant snaps should be published anywhere from this repository, is part of the variant-hosting discussion this workflow informs. No publishing: the workflow uploads artifacts only.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
build: generalize SNAP_BUILD_VARIANT into a variant payload mechanism (RFC)
This is an RFC as much as a PR. The machinery here is complete and
verified, but one design question needs a maintainer decision before
this can be more than an proposal: where the desktop variant's payload
should live long-term (see "Questions for maintainers" below). We are
opening this primarily to put the mechanism and that question in front
of the core-base maintainers, not because we expect an immediate merge.
What this adds
The build already supports selecting a variant of the base snap:
SNAP_BUILD_VARIANTis derived from the Launchpad recipe name (or alocal
build-envfile), suffixed onto the version (<date>+<variant>)and recorded as
SNAP_BUILD_NAME, and parts and hooks branch on it —that is how the
cloud-initvariant wires itself in today, and thecommented-out
fipsbranch shows the mechanism was expected to grow.What a variant could not do so far is carry content. This series
gives a variant a payload directory,
variants/<variant>/, applied tothe rootfs by the
installtarget after the basestatic/files arecopied and before the hooks run:
variants/<v>/static/static/files (same-path variant file wins)variants/<v>/fstab.d/*.conf/etc/fstab, in glob ordervariants/<v>/writable.conf.d/*.confcore-writable.conf, in glob orderplus two supporting pieces:
tools/build-variant.sh <variant>— builds and packs a variant ascore26-<variant>_<version>_<arch>.snap(the snap name cannot changeinside the build without losing the project-keyed LXD build instance,
so the result is repacked from the build instance's prime with the
name patched);
variants/<v>/architectures:a variant payload is architecture-specific (its chroot hooks and
chisel slice resolution execute the build architecture's binaries,
there is no cross-build), and a build on an undeclared architecture
fails loudly in every build path — including Launchpad — instead of
producing a wrong-architecture snap.
variants/example/is a tiny demonstration payload exercising everyknob (one extra file, one writable mount with its
Cline and factorytemplate, one
dline nested under that mount, an architecturedeclaration), so the machinery is proven with real content in CI
without carrying any product payload.
variants/README.mddocumentsall of it.
The invariant: variant-less builds are untouched
The machinery is inert without a variant, and the series is structured
so that regular and
cloud-initbuilds produce payload-identicaloutput to stock:
hook-tests/995-variant-payload.test(runs inside every snapcraftbuild) enforces both directions: on a variant build, the selected
variant's fstab/tmpfiles fragments are applied exactly once and the
pre-payload fstab is byte-identical to the base static one; on a
regular build, the base
fstabandcore-writable.confare primedbyte-identical to the static sources and no variant payload file —
every file and symlink every in-tree variant ships, reconciled
against the chisel manifest — may appear in the prime.
993-factory-filestest gains the nested-d-under-mount rulethe fragment knobs rely on, and continues to gate fstab /
core-writable.conf / factory synchronization for variant content
exactly as it does for the base.
Full-clean regular builds on the branch this machinery was developed on
were verified payload-identical to a stock core26 build this way.
What this deliberately does not include
No product payload. This series carries the machinery and the example
variant only. The motivating consumer — the Ubuntu Core Desktop thin
base — lives in
canonical/core-base-desktopbranchdesktop/26,which is built from this machinery: its
variants/desktop/payload(gdm host integration, the writable-mount set for
colord/AccountsService/gdm3/wayland-sessions/polkit/fprint, PAM/NSS
modules staged from archive debs with recorded provenance, an
in-tree-built
xdg-fuse-broker) is exactly what the knobs above areshaped to carry, and its
variants/README.mddocuments the desktoppayload in detail. None of that content is proposed for this
repository here.
For context on why the desktop base is a variant of core26 at all: the
previous core26-desktop base was a forked live-build rootfs (241.5 MB
packed / 15,223 files vs stock core26's 63.6 MB / 4,808). Re-parenting
it onto the stock core-base machinery and re-homing everything that
can leave the base (daemons to provider snaps, binaries to content
snaps, configuration to the image build) shrank it to ~85 MB packed and
cut 118.7 MB from the whole desktop image — and both the GNOME and KDE
desktop images built on it have been verified end to end (boot, first
boot wizard and cloud-init-free user seeding, login, lock/unlock, store
installs, audio, disks, power, logout/reboot). What remains in the base
is the residue that has no other home today: host-side GDM integration,
a FUSE broker that must mount from the host mount namespace (snap-confine's
slave mounts make a snap daemon impossible for it until snapd grows a
host-namespace helper), the PAM/account plumbing the greeter and
first-boot flows need, and the writable-mount set.
Questions for maintainers
1. Where should the desktop variant's payload live long-term?
variants/desktop/moves intocore-base; core-base builds and publishes
core26-desktopthe sameway it builds
core26/core26-cloud-init(the recipe-namemechanism already supports it). One repo, one CI, one release
process; the cost is that desktop payload iteration (which is much
higher-frequency than base churn) lands in core-base review.
PR ships: core-base owns the mechanism,
canonical/core-base-desktopowns the desktop payload and its build/publish. The two repos
coordinate on the machinery interface only.
Ubuntu Core Desktop. The residue listed above is what makes this
expensive today: shipping it in every core26 costs ~21 MB packed on
every cloud/server/device image, and shrinking it further needs
snapd-side work (host-namespace mount helper) rather than base-side
work. The variant mechanism is how one codebase serves both products
without paying that cost on every image.
We are genuinely undecided between (a) and (b) and would like the
maintainers' preference; (c) is included because it is the obvious
"why not just one base?" alternative and we can provide the full
re-homing inventory behind the residue claim if useful.
2. Snap naming.
tools/build-variant.shrepacks the result ascore26-<variant>because the project name must stay static during thebuild. Is that acceptable as the long-term mechanism for
store-published variant snaps, or should variant identity be handled
differently (build-plan support for per-variant names, or a fixed name
with variant identity only in
VARIANT_ID/SNAP_BUILD_NAME)?3. Per-variant architecture declaration. Is
variants/<v>/architectures(checked in every build path) the rightshape, versus per-variant
platforms:in snapcraft.yaml (impossible —the project is shared) or recipe-side-only restriction?
4. CI appetite. The added workflow is dispatch-only and publishes
nothing: it builds the example variant on hosted amd64 and arm64
runners and uploads artifacts. Should the example variant join the
spread-test matrix (one more full build per push/PR on the self-hosted
runners)? Should variant snaps ever be published from this repository,
or is that out of scope for core-base?
Verification
hook-testsrun inside every build; the new995-variant-payloadtest enforces the applied-exactly-once / nothing-leaks invariant, and
993-factory-filesgates the fstab/tmpfiles/factory synchronizationfor variant content.
tools/build-variant.sh example: full snapcraft build with allin-build hook tests green, producing
core26-example_20261123+example_amd64.snapwith the name and+exampleversion suffix verified from the packed metadata.green including the
995regular-build branch, and verified directlyfrom the packed snap:
etc/fstabandusr/lib/tmpfiles.d/ core-writable.confbyte-identical to thestatic/sources, novariant payload file present, and no
VARIANT_IDin os-release.The machinery commits were extracted from the
canonical/core-base-desktopdesktop/26branch, where they have beenin production use (the desktop variant snap they build is consumed by
the Ubuntu Core Desktop image CI), rebased onto current main and made
desktop-agnostic — the payload-leak test and the architecture
declaration were reworked to be variant-generic; the desktop-specific
assertions remain on the desktop branch.
cc @bboozzoo @Meulengracht