Course contentsShow
← Linux
Lesson 603 of 1,8196. Systemd Internals and Unit OrchestrationFree lesson

State Management via Generators

Understand how generators dynamically create unit files based on system hardware at boot.

In this lesson, you will learn how systemd generators allow the system to dynamically "write" unit files on the fly during the very early stages of the boot process.

The Problem: Static vs. Dynamic Reality

Usually, unit files are static text files sitting on your disk in /lib/systemd/system. However, modern hardware is unpredictable—you might have a specific set of block devices, network interfaces, or swap partitions that only exist based on the current hardware configuration. If you had to write a static unit file for every possible hardware combination, your system configuration would be impossibly massive and fragile.

How Generators Solve This

systemd generators are small, executable programs that run before the main dependency-resolution phase of boot. Think of them as a "pre-flight" script.

When the system boots, systemd executes all binaries found in /lib/systemd/system-generators/. These generators analyze the hardware environment, calculate what services or mount points are actually required, and dynamically generate unit files in a temporary in-memory directory (usually /run/systemd/generator/).

For example, a generator might detect a swap partition on your disk and instantly create a swap.target unit file:

# Conceptually, a generator logic:
if [ -b /dev/sdb1 ]; then
    cat <<EOF > /run/systemd/generator/dev-sdb1.swap
[Swap]
What=/dev/sdb1
EOF
fi

The Power of "Just-in-Time" Units

Because these files are created in /run—a temporary filesystem—they don't clutter your permanent configuration. Once the generator finishes, systemd treats these dynamically created files exactly as if they were written by a human. It reads them, builds the dependency tree, and starts the necessary services.

This mechanism is vital for systems like cloud instances or live USBs, where the hardware environment is unknown until the second the machine turns on. By using generators, systemd transforms from a static manager into a dynamic engine that adapts its service architecture to the physical reality of your machine at the exact moment it starts.

Key Takeaway: Generators are "boot-time blueprints" that inspect hardware and dynamically create unit files in memory, allowing systemd to tailor itself to the current environment before the main service startup begins.