Skip to content

build: generalize SNAP_BUILD_VARIANT into a variant payload mechanism (RFC) - #491

Open
chris-bainbridge wants to merge 4 commits into
canonical:mainfrom
chris-bainbridge:upstream/variant-machinery
Open

chris-bainbridge wants to merge 4 commits into
canonical:mainfrom
chris-bainbridge:upstream/variant-machinery

Conversation

@chris-bainbridge

Copy link
Copy Markdown
Contributor

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_VARIANT is derived from the Launchpad recipe name (or a
local build-env file), suffixed onto the version (<date>+<variant>)
and recorded as SNAP_BUILD_NAME, and parts and hooks branch on it —
that is how the cloud-init variant wires itself in today, and the
commented-out fips branch 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 to
the rootfs by the install target after the base static/ files are
copied and before the hooks run:

knob path effect
extra files variants/<v>/static/ copied verbatim on top of the base static/ files (same-path variant file wins)
fstab variants/<v>/fstab.d/*.conf appended to /etc/fstab, in glob order
tmpfiles.d variants/<v>/writable.conf.d/*.conf appended to core-writable.conf, in glob order

plus two supporting pieces:

  • tools/build-variant.sh <variant> — builds and packs a variant as
    core26-<variant>_<version>_<arch>.snap (the snap name cannot change
    inside 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);
  • a per-variant architecture declaration, 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 every
knob (one extra file, one writable mount with its C line and factory
template, one d line nested under that mount, an architecture
declaration), so the machinery is proven with real content in CI
without carrying any product payload. variants/README.md documents
all 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-init builds produce payload-identical
output to stock:

  • hook-tests/995-variant-payload.test (runs inside every snapcraft
    build) 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 fstab and core-writable.conf are primed
    byte-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.
  • the 993-factory-files test gains the nested-d-under-mount rule
    the 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-desktop branch desktop/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 are
shaped to carry, and its variants/README.md documents the desktop
payload 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?

  • (a) In this repository — variants/desktop/ moves into
    core-base; core-base builds and publishes core26-desktop the same
    way it builds core26 / core26-cloud-init (the recipe-name
    mechanism 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.
  • (b) Machinery only, payload in the sibling repository — what this
    PR ships: core-base owns the mechanism, canonical/core-base-desktop
    owns the desktop payload and its build/publish. The two repos
    coordinate on the machinery interface only.
  • (c) No variant at all: one universal core26 for Ubuntu Core and
    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.sh repacks the result as
core26-<variant> because the project name must stay static during the
build. 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 right
shape, 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-tests run inside every build; the new 995-variant-payload
    test enforces the applied-exactly-once / nothing-leaks invariant, and
    993-factory-files gates the fstab/tmpfiles/factory synchronization
    for variant content.
  • The example variant was built end to end with
    tools/build-variant.sh example: full snapcraft build with all
    in-build hook tests green, producing
    core26-example_20261123+example_amd64.snap with the name and
    +example version suffix verified from the packed metadata.
  • A full-clean regular build of this series: all in-build hook tests
    green including the 995 regular-build branch, and verified directly
    from the packed snap: etc/fstab and usr/lib/tmpfiles.d/ core-writable.conf byte-identical to the static/ sources, no
    variant payload file present, and no VARIANT_ID in os-release.

The machinery commits were extracted from the
canonical/core-base-desktop desktop/26 branch, where they have been
in 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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant