Skip to content

fix: keep the overlay's Nix database in sync with the base image - #55

Open
raphaelcoeffic wants to merge 1 commit into
mainfrom
fix/overlay-base-sync
Open

raphaelcoeffic wants to merge 1 commit into
mainfrom
fix/overlay-base-sync

Conversation

@raphaelcoeffic

Copy link
Copy Markdown
Owner

Bug

Upgrading dive breaks the environment for anyone who installed a package before the upgrade:

  1. dive 0.2.6, pkg install tree
  2. upgrade to 0.2.7 (base image replaced on the next run): everything still works
  3. pkg remove tree → garbage collection deletes 742 MiB, the whole new base image: zsh, vim, curl, jq, sngrep, … are gone and interactive sessions fail (exit 127)

The same steps on a fresh 0.2.7 state, without upgrade, only free the package and the nixpkgs source.

Cause

The base image ships its Nix database (var/nix/db), but the first pkg install copies it up into the overlay's upper layer. After an upgrade, that copy hides the new database: it doesn't know the new base store paths (checked: 0 matches for the new base root, vs. 1 in the new image's database), so garbage collection deletes them. On disk, the base image is intact: the deletions are overlayfs whiteouts in the upper layer.

Fix

New base_sync module, run from SharedMountBuilder::make_mount when it mounts the overlay (holding the lock, no session running), if the base image hash differs from the one recorded in {rootful,rootless}/base.sha256:

  1. Before mounting: remove the whiteouts in upper/store hiding store paths of the base image — this repairs overlays already broken by the bug.
  2. After mounting: if the upper layer has its own database, register the base store paths with nix-store --load-db < /nix/.base.reginfo (shipped in the base image), in a forked child chrooted into a root where /nix is the overlay (reusing the image builder's bind_mount_all_dir). The child acts as the owner of /nix/var/nix/db seen through the overlay: root with an id-mapped overlay, the user for rootless containers.
  3. Record the base image hash: this runs once per base image. On failure, it logs a warning and is retried with the next session.

Without its own database (no package ever installed), the overlay uses the base image's database: only the hash is recorded.

Also: fork::tests::counts_threads asserted an exact process-wide thread count, which fails whenever other tests run in parallel — it failed consistently once more library tests were added. It now asserts a lower bound.

Testing

  • Unit tests for the sync marker and the whiteout restore (whiteouts are 0/0 character devices, which unprivileged users may create; also ran as root).
  • End to end, rootful alpine:3.22 container, scratch state directory:
    • Repair: state broken as above with the 0.2.7 release (742 MiB deleted, all base tools gone). First session with this build: restored 132 base image store paths, registering base image store paths → all base tools work, pkg install/pkg remove cycle afterwards only frees the package (252 MiB), base intact, interactive shell works. Upper layer files still owned by the user (no root-owned files: id mapping respected).
    • Prevention: 0.2.6 + pkg install tree, then this build upgrades the base image (-b with the base image artifact of main run #198): paths registered on the first session, tree and base tools work, pkg remove tree keeps the base image, no resync on the next session.
  • Not tested: rootless containers (no rootless runtime available here).

The base image ships its Nix database, but the first `pkg install`
copies it up into the overlay. After a base image upgrade, that copy
hides the new database: the new base store paths are unknown to Nix,
and the next garbage collection (`pkg remove`) deletes them. Upgrading
from 0.2.6 to 0.2.7 then removing a package deleted the whole base
image (742 MiB): zsh, vim, curl, ... gone, and the shell failing.

When the overlay is mounted (no session running) and the base image
changed since it was last synced:
- remove the whiteouts in the upper layer hiding base store paths,
  which repairs overlays already hit by the garbage collection,
- register the base store paths into the overlay's database, if it has
  its own, with `nix-store --load-db` in a chroot where /nix is the
  overlay, as the owner of the database seen through the overlay,
- record the base image hash, so this runs once per base image.

Also make the fork thread-count test tolerate other tests running in
parallel: it failed whenever more library tests ran at the same time.
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