Skip to content
systemcoding.dev
Embedded Linux

Share DL_DIR and SSTATE_DIR Across Multiple Yocto Builds

Point DL_DIR and SSTATE_DIR at directories outside any build tree and every Yocto build on the machine reuses the same downloads and cached task output.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, NXP and Debian logos.

The second Yocto build directory you create will start from nothing, download every source tarball again and recompile the compiler. That is a waste, because by default each build directory keeps its downloads and its cache of finished work inside itself. Two variables fix it: DL_DIR for downloaded sources and SSTATE_DIR for the shared state cache, the store of already built task output. Point several builds at the same two directories and the second build reuses most of what the first did. This post shows how to set them, how to pick locations, how shared access behaves when two builds run at once, and what to avoid, on Scarthgap 5.0 LTS. It is the local half of a story whose team half is the SSTATE_MIRRORS post.

What the two directories hold

DL_DIR is where the fetcher stores source archives and git mirrors. When a recipe's SRC_URI names a tarball, the file lands in DL_DIR along with a .done marker. The next build, in any directory, finds the file and skips the download. The default is ${TOPDIR}/downloads, which is inside the build directory.

SSTATE_DIR is where the build stores the output of tasks as compressed archives, keyed by the task's signature hash. If a later build needs a task with the same hash, it unpacks the archive instead of running the task. This is what turns a multi-hour first build into a short rebuild. The default is ${TOPDIR}/sstate-cache.

Both defaults are per build directory, so by default nothing is shared. They are separate variables because they have different lifetimes and different safety properties. Downloads are inputs you should be able to rebuild anywhere offline. The cache is purely an optimisation: delete it and the next build recompiles everything, correctly.

Set the two variables

Choose directories outside every build tree and outside the poky checkout, ideally on a disk with room. For a single-user machine, your home directory is enough:

Text
DL_DIR = "/home/builder/yocto-shared/downloads"
SSTATE_DIR = "/home/builder/yocto-shared/sstate-cache"

Put those lines in conf/local.conf of every build directory, or better in a file that all of them read. BitBake reads an optional conf/site.conf after local.conf, and it also checks for a site.conf anywhere on BBPATH. A build-directory-independent approach used in many setups is to place the lines in a shared site.conf in one location and include it from each local.conf:

Text
require /home/builder/yocto-shared/site.inc

Where site.inc holds the two assignments. require fails loudly if the file is missing, which is what you want, whereas include ignores a missing file silently and gives you an unshared build without a warning.

Do not set them with ?= unless you want them overridable, since the sample local.conf may already contain a commented default. Also do not point them at ${TOPDIR}-relative paths, since that defeats the sharing.

Check that the setting took effect

Before the next build, confirm the final values with bitbake -e, which prints the variables as BitBake sees them:

Shell
bitbake -e core-image-minimal | grep -E '^(DL_DIR|SSTATE_DIR)='
Text
DL_DIR="/home/builder/yocto-shared/downloads"
SSTATE_DIR="/home/builder/yocto-shared/sstate-cache"

If you see paths below your build directory instead, a later assignment overrode yours or you edited a different build's local.conf. Fix that before spending a day on a build that is not sharing anything.

What happens when two builds run at once

Sharing between builds that run at the same time is supported. The downloader takes a lock file per download (a file named like the archive with .lock added), so two builds fetching the same tarball do not corrupt each other: one fetches, the other waits and then finds the .done marker. The state cache is written with atomic file operations. Each archive is written to a temporary name and then renamed into place, so a reader never sees a half-written archive.

Two things still deserve care.

  1. Disk space is shared. If you use BB_DISKMON_DIRS (the default local.conf does), the monitor watches ${DL_DIR} and ${SSTATE_DIR} and stops the build before the disk is full. Make sure those two paths are covered.

  2. Permissions. If several users share the directories, they need group write access and a umask that keeps new files group-writable. Without it, the second user's build fails with a permission error when it tries to add a file to a directory owned by the first. Set the directories group-owned by a shared group with the setgid bit (chmod g+ws), and set umask 0002 in the shell that runs BitBake.

Shell
sudo mkdir -p /srv/yocto/downloads /srv/yocto/sstate-cache
sudo chgrp -R yocto /srv/yocto
sudo chmod -R g+ws /srv/yocto

That creates a shared location owned by the group yocto. The group name is an example. Create it with groupadd and add the users who will build.

Making the build use the cache

The cache is used automatically once SSTATE_DIR points at it. You can see it working in the build output: when a task is restored, BitBake prints a line that includes Sstate summary:

Text
Sstate summary: Wanted 1672 Local 1668 Mirrors 0 Missed 4 Current 0 (99% match, 0% complete)

The numbers belong to the build that printed them. Wanted is how many tasks were eligible for restore, Local is how many were found in the local SSTATE_DIR, and Missed is how many had to run. A second build of the same image, from the same layer revisions and settings, shows a match close to 100 percent. If the match is low, something differs in the task signatures, and the bitbake-diffsigs post later in this series shows how to find what.

What invalidates sharing

Sharing works only when the signatures match. A signature is a hash of a task's inputs: the recipe, the variables it uses, the sources and the signatures of its dependencies. These changes break the match between builds:

  • A different Poky release or different layer revisions.

  • A different MACHINE, DISTRO, TUNE_FEATURES or relevant flags. Different machines build native tools (compilers that run on the host) with the same hashes, so those are shared, while target packages differ.

  • A different host distribution for tasks that depend on the host (the NATIVELSBSTRING value in the banner is part of the native hash for some tasks).

Two builds on the same layer revisions with different MACHINE values will still share the native tools and the toolchain components that do not depend on the machine. That is where much of the saving comes from.

Housekeeping

The shared directories grow. DL_DIR has no automatic cleanup: sources of old versions stay. The cache has a helper, scripts/sstate-cache-management.py, in the Poky tree, that can remove entries by age or by which are unused by your current layers. Read its --help for the options of your release before using it; deleting by age is safe because the cache is a pure optimisation, so the worst outcome is a slower next build.

For DL_DIR, you can regenerate everything from the network, but you should not rely on that for a product build you must reproduce in five years, because upstream sites vanish. Keep the downloads directory in backup storage, or mirror it, for any release you ship.

You may also set BB_GENERATE_MIRROR_TARBALLS = "1" so that git sources are stored as tarballs in DL_DIR, which makes the directory usable as a source mirror for other machines, as the offline post in this series explains.

When not to share

Do not share one SSTATE_DIR between builds you do not trust equally. The cache restores binaries, so a poisoned or corrupted entry gets into every build that reads it. On a single developer machine that risk is negligible. On a team server, the cache should be written by a controlled build job and be read-only for everyone else, which is the subject of the mirror post. And do not put either directory on a network filesystem with weak locking, because the lock files that protect concurrent downloads rely on working file locks.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.