Skip to content

NMS-20668: Optimize RPM/DEB packages and Docker images - #9223

Open
mershad-manesh wants to merge 8 commits into
release-37.xfrom
mem/NMS-20668
Open

mershad-manesh wants to merge 8 commits into
release-37.xfrom
mem/NMS-20668

Conversation

@mershad-manesh

Copy link
Copy Markdown
Contributor

Smaller images, faster upgrades and smaller packages by deduplicating jars

Images (core, Minion, Sentinel)

  • common/split-layers.sh splits the assembly into four layers, ordered from least to most likely to change: deps (third-party jars), onms (OpenNMS jars), app (everything else) and src. Each is added with COPY --link.
  • Identical jars, kar and war files are hardlinked within a layer. Nothing under etc/, data/ or deploy/ is linked, because those are volumes and contain editable config.
  • Every layer builds byte-identically each time. An unchanged deps layer keeps its digest across releases, so upgrades skip it.

Packages

  • Core DEB: system/*.jar copies are now symlinks into libopennms-java and libopennmsdeps-java, as the RPM already does.
  • Minion DEB and RPM: identical jars in lib/, system/ and repositories/ are hardlinked.

Estimated Savings (37.0.0-SNAPSHOT)

Before After
Core image, compressed pull 1184 MB 970 MB (−18%)
Minion image, compressed pull 579 MB 493 MB (−15%)
Upgrade pull when third-party jars are unchanged core 1016 MB, Minion 411 MB, Sentinel 295 MB 300 MB, 40 MB, 18 MB
opennms-common DEB 430 MB 220 MB
Minion DEB / RPM about 405 MB each about 320 MB each

Notes

  • Commit 91e6d46 will be reverted before merging this PR

All Contributors

External References

…duplicates

Every release shipped the whole assembly as one layer whose digest changes
on every build, so an upgrade pulled ~1.07 GB (Horizon), ~430 MB (Minion)
and ~316 MB (Sentinel) even though most files were byte-identical to the
previous release.

common/split-layers.sh now splits the unpacked assembly into four layers,
added with COPY --link from most to least stable:

  deps  third-party jars and the third-party part of system/ and
        repositories/ (directly on top of the base image)
  onms  OpenNMS-built jars and org/opennms in the Karaf repositories
  app   scripts, config, web assets, directories (real mtimes kept, Jetty
        derives Last-Modified/ETag from them)
  src   the bundled source archive

Files with identical content, mode and owner are hardlinked, removing the
lib/ <-> system/ (Horizon) and repositories/ <-> system/ (Minion) copies:
Horizon 2.18 -> 1.94 GB, Minion 1.01 GB -> 911 MB with a test assembly.
The deps and onms layers get a constant mtime so an unchanged layer keeps
the same digest across builds.

Verified with no-cache builds of all three images: the filesystem of the
old and new image is identical in path, type, mode, owner, size and
content; rebuilding from a re-touched assembly keeps the deps and onms
layer digests, and changing one OpenNMS file only changes the onms layer.

The script is passed to the builds as the "container-common" named build
context (docker buildx --build-context).
The tarball stage only unpacks, chmods, hashes, hardlinks and copies files
from an architecture-independent tarball, so it can run natively on the
build host instead of under QEMU when building the other architecture.

Measured with an emulated linux/amd64 build on an arm64 host, split step:
Horizon 50.3s -> 20.2s, Minion 24.9s -> 6.5s, Sentinel 12.0s -> 3.7s.
The image filesystems are identical to the previous build (path, type,
mode, owner, size, content), and the deps and onms layers now have the
same digest on amd64 and arm64, so registries store them once.

Minion no longer copies /opt from that stage: after the split it only
held the base image's /opt (prom-jmx-exporter), which the final image
already has with the same content, owner and mode, and which would now
come from the build host's architecture.
The core RPM already replaces system/ jars that also ship in lib/ with
symlinks; the DEBs and the Minion packages carried every copy.

Core DEB (debian/rules): each jar in opennms-common's system/ that is
byte-identical to one that libopennms-java or libopennmsdeps-java install
into /usr/share/java/opennms becomes a symlink to it. opennms-server
depends on all three packages at the same version.

Minion RPM and DEB: jars with the same SHA-256 under lib/, system/ and
the repositories/*/ Karaf repositories are hardlinked; they are all in
the one package, so a plain hardlink works for rpm and dpkg.

Built from the same CI assemblies with the CI build image and compared
with packages built from the previous packaging:
  opennms-common  455.2 MB -> 234.6 MB (462 jars become symlinks)
  opennms-minion  DEB 425.3 MB -> 335.3 MB, RPM 429.8 MB -> 338.9 MB
                  (204 jars become hardlinks, /opt/minion 459M -> 362M)
Every path is present in both builds and every link resolves to the
same content; install, upgrade from the previous packaging, dpkg -V /
rpm -V, and a Minion start show the same bundles and features.
split-layers.sh hardlinked any files with identical content, including
config under etc/. Docker keeps hardlinks when it populates a VOLUME from
the image, so an in-place edit of one such file (e.g.
snmp-adhoc-graph.properties) also changed its twin
(response-adhoc-graph.properties). Only *.jar, *.kar and *.war are linked
now, and never under etc/, data/ or deploy/.

Also:
- restore directory mtimes changed by "ln -f", and give each layer root
  the source directory's mtime, so all four layers are reproducible
- fail on file types other than files, directories and symlinks instead
  of dropping them
- avoid a SIGPIPE race under pipefail in the file name check
- rebuild the image when split-layers.sh changes
debian/rules recipes run under /bin/sh. With dash, "read -d ''" fails and
the loop silently created no symlinks.

@marshallmassengill marshallmassengill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Revert commit 91e6d46 before you merge this PR. This commit contains temporary CI changes.
  • Update opennms-container/README.md. The docker build commands in this file do not work with the new Dockerfiles.
  • Add these two parameters to the README commands: --build-context container-common=../common and --build-arg VERSION=.
  • I installed the core DEB packages from the CI build of ef79967 on Debian 12 with JDK 25. OpenNMS started correctly.
  • 462 JAR files in system/ are symbolic links. No link is broken. Karaf loaded 253 bundles from these links.
  • I installed the Minion DEB on Ubuntu 26.04 and the Minion RPM on Rocky Linux 9. The Minion started correctly on each system.
  • In each Minion installation, 378 JAR files are hardlinked. karaf.log contains no errors.
  • Some Minion bundles stayed in the GracePeriod or Starting state. The test had no core server. This is the probable cause.
  • Optional: put the Minion JAR deduplication commands in one shared script. The same commands are in minion.spec and in the Minion debian/rules file.
  • Optional: make these commands safe for paths that contain spaces. Such paths do not occur now.
  • Not related to this PR: the opennms-server post-installation script fails when it does not find JDK 25. The fix-permissions command stops because java.conf does not exist.

dino2gnt
dino2gnt previously approved these changes Dec 2, 2026

@dino2gnt dino2gnt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. No notes.

@indigo423

indigo423 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

I've just spent some time in the container part in depth. What I don't like is the layer split you have introduced building the image. That comes with a very high prize tag and creates a lot of fragility into the container build. Here is some of my reasoning:

The only thing a release upgrade fetches is the assembly layer. So the question is only whether cutting that one layer into four reduces what gets fetched. Measured, it does not:

  • Today deps changes on every build because of the rebuilt Karaf jar (see example below).
  • After that is fixed, one bumped dependency still re-pulls all 561 MB, and that happens in roughly 10 of 12 releases.
  • The remaining winners are people who pull snapshots or hotfixes back to back. That is mostly developers and CI, not users upgrading monthly.

Some other points why I would not do the split:

  • Name-based sorting. A file that lands in the wrong layer breaks nothing visible. The Karaf jar is already one such miss, and nobody noticed until two published images were compared.
  • Fixed 2000-01-01 mtimes. A rebuilt jar can keep its name and its mtime across releases. Whether any Karaf or Jetty cache is fooled by that is unverified, but it is a class of bug that is very hard to trace back to the image build.
  • Plain docker build no longer works. It needs the named build context.
  • No tests. Every rule in the script can be broken without a failing build.
  • Cache coupling. The package install step now reruns when a jar changes.

What really pays off is the hardlink deduplication and that would be something we should keep.

What I measured

  • The whole size gain comes from the hardlinks: Horizon 1235.7 MB to 1025.1 MB compressed (minus 17 %), Minion 677.0 MB to 566.1 MB (minus 16 %). The old and new image filesystems are identical in path, mode, owner, size and SHA-256.
  • The split saves nothing on a release upgrade today. org.apache.karaf.features.core-4.4.9.jar is rebuilt under its upstream name, lands in deps, and its zip timestamps change with every build. Between the published 36.0.5 and 36.0.6 images this jar and pax-url-aether-2.6.17.jar keep name and size and change hash. So deps gets a new digest on every build.
  • Fixing that jar does not change much. deps is one 561 MB blob, and one bumped third-party jar re-pulls all of it. At least 10 of the last 12 release pairs changed a third-party version in the root pom.xml.
  • app and src get a new digest on every build, even from identical input.

What I suggest

  1. Move the RPM/DEB deduplication into its own PR.
  2. Keep the hardlink step for the images with the single COPY as before, real mtimes, and a check at the end of the script that fails when a linked file sits under etc/, data/ or deploy/.
  3. Drop the four-layer split. If we come back to it, the bar should be a measured pull saving between two published releases.
  4. Drop commit 91e6d46dc2d before merge.

Two ideas I have not tested: the bundled source archive is a 72 MB layer in 36.0.6 and may not be needed in the runtime image, and creating the hardlinks in the assembly tarball itself could give images and packages the same gain without a build-time script.

Example: the patched Karaf jars

Two jars in deps show why sorting by file name is fragile.

Both patches are needed. The problem is how they meet the split:

  • Both jars are repacked in every Maven build, and neither module sets project.build.outputTimestamp. The class content stays the same, but the zip entry timestamps change. In the Karaf jar 113 of 1652 entries carry the build date.
  • Neither file name contains opennms or the project version. split-layers.sh:136 therefore treats both as third-party and puts them in deps.
  • One jar with a new hash gives the whole deps layer a new digest. So every build re-pulls all of deps, even when no dependency changed.

With a single assembly layer this never mattered. With the split it cancels the gain, and nothing in the build reports it. I only saw it by comparing the published 36.0.5 and 36.0.6 images. Any future patch module of this kind would do the same.

@indigo423

indigo423 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Just some context how I have looked at this problem without AI and why I think this should conceptually solved differently and your approach with the split bash script is on the wrong layer. The script tries to recover structure during image build time. The whole Maven build already knew all that and throws it away when it gives you that one tarball. Maven knows which jar is a third-party and which is OpenNMS code. Your bash script has to guess that back from file names. The Karaf jar is an example what a wrong guess looks like. If we fix it with with Maven and smaller assemblies that would fix that problem at the source.

I see it as two separate things. The main benefit of smaller assemblies is modularity and a smaller footprint, and that deserves its own issue and design.

@mershad-manesh

Copy link
Copy Markdown
Contributor Author

Just some context how I have looked at this problem without AI and why I think this should conceptually solved differently and your approach with the split bash script is on the wrong layer. The script tries to recover structure during image build time. The whole Maven build already knew all that and throws it away when it gives you that one tarball. Maven knows which jar is a third-party and which is OpenNMS code. Your bash script has to guess that back from file names. The Karaf jar is an example what a wrong guess looks like. If we fix it with with Maven and smaller assemblies that would fix that problem at the source.

I see it as two separate things. The main benefit of smaller assemblies is modularity and a smaller footprint, and that deserves its own issue and design.

Thanks @indigo423, agreed.

  • Bundled source: it's there for AGPL compliance, so I'm leaving it for now.
  • Hardlinks in the assembly tarball won't work. plexus-archiver (including the latest 4.16.0) can only write symlinks, and the core DEB splits lib/ and system/ across packages.
  • The layer split was also meant to allow parallel downloads, but every layer changes on each build, so it isn't worth the complexity.

Changes in this PR:

  • Drop the layer split, the container-common build context and the fixed mtimes. Back to a single COPY, so plain docker build works again (this covers @marshallmassengill's README point).
  • Keep the image hardlink step, and fail the build if a linked file is under etc/, data/ or deploy/.
  • Keep the RPM/DEB dedup.
  • Revert 91e6d46 before merge.

The four-layer split did not reduce what an upgrade pulls: about ten jars
are rebuilt in-tree under their upstream names and land in deps, so every
layer changes on every build. All of the size saving comes from the
hardlinks.

common/hardlink-duplicates.sh replaces split-layers.sh. It only hardlinks
identical *.jar/*.kar/*.war files and fails the build if a linked file ends
up under etc/, data/ or deploy/. It runs as the last step of the tarball
stage, since a later chmod would break the links, and the images go back to
a single COPY with real mtimes.

The script is copied into each build context by "make unpack-tarball", so
the named build context is gone and a plain docker build works again. The
Minion staging stage now uses /opt/usr-share/minion like core and Sentinel,
so only the Minion directory is copied from the build-platform stage.

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.

4 participants