Mastering Linux OverlayFS: Deep Dive into Lowerdir, Upperdir, and Merged Layers

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Problem: The Cost of Full Filesystem Duplication

A few years ago, I had to provision ephemeral testing environments for our staging team. We were dealing with a 32GB monolith repo packed with machine learning runtimes, Node modules, and pre-compiled shared libraries. Every time three engineers wanted to validate branch builds at the same time, our provisioning script churned through three full copies via cp -a or rsync.

That burned nearly 100GB of NVMe storage on our test runner. Worse, copying 32GB took roughly 3 to 4 minutes per runner, thrashing our I/O queue while developers sat waiting for CI jobs to start.

Tearing down dirty test environments was just as painful because rogue scripts often leaked temporary files across shared paths. We needed a true copy-on-write (CoW) mechanism: one that keeps the baseline directory strictly immutable and isolates all writes into a disposable scratch space. That is precisely what the Linux kernel’s OverlayFS driver delivers.

Core Concepts: Deconstructing the OverlayFS Architecture

OverlayFS is an in-kernel union mount driver merged upstream in Linux 3.18. It does not format block devices or track partition tables. Instead, it overlays existing host directories, merging them into a unified virtual mount that running processes interact with as if it were a single normal directory.

1. The Lower Directory (lowerdir)

The lowerdir is your immutable foundation. It is strictly read-only. The kernel never modifies files here under any circumstances. You can stack up to 500 lower directories together by separating absolute paths with colons (:). This lets you compose stacked runtime environments—for instance, mounting an application layer over a shared Debian base rootfs.

2. The Upper Directory (upperdir)

The upperdir is your read-write delta layer. Whenever a process creates, modifies, or appends to a file in the merged mount, OverlayFS directs that operation here. Unmount the overlay and inspect upperdir directly: you will see only the modified files and newly written data. The base layer remains bit-for-bit identical to where you started.

3. The Work Directory (workdir)

OverlayFS requires a dedicated scratch space called workdir. It must live on the same mounted filesystem as upperdir. The kernel uses this empty directory to prepare atomic file transactions—like copy-up routines and inode unlinks—before swapping them into the active merged tree. This staging step protects your filesystem from state corruption if a node loses power mid-write.

4. The Merged Mount Point

The merged directory is the unified presentation layer. When a process reads a path, the kernel checks the upper layer first. If the file exists there, OverlayFS serves it directly. If not, lookup requests traverse down through the lower stack. Modifying an existing lower file triggers an automatic copy-up: the kernel duplicates the entire file into upperdir in milliseconds, then applies your write to that new local copy.

How File Deletions Work: Whiteout Files

What happens when you run rm on a file that lives in an immutable lower directory? The kernel cannot delete it from the read-only layer. Instead, OverlayFS creates a character device node with major/minor numbers 0,0 inside upperdir using the exact same filename. This special marker is called a whiteout file. When the virtual filesystem (VFS) scans the merged tree, it sees the whiteout device and hides the corresponding lower file from directory listings.

Hands-On Practice: Building and Inspecting an OverlayFS Stack

To see the mechanics in action, open a Linux shell with root privileges or sudo access.

Step 1: Set Up Directory Structures and Mock Data

Create five separate directories to represent two base layers, our read-write delta, the kernel staging workspace, and the mount target:

mkdir -p /tmp/overlay-lab/{base_os,app_code,upper,work,merged}

# Populate our mock layers
echo "Ubuntu Base 22.04 LTS" > /tmp/overlay-lab/base_os/os-release.txt
echo "DATABASE_URL=postgres://localhost:5432/prod" > /tmp/overlay-lab/app_code/config.env
echo "print('Server running on port 8080')" > /tmp/overlay-lab/app_code/server.py

Step 2: Mount Multi-Tier Lower Directories

When stacking multiple lower directories, OverlayFS evaluates them from left (top priority) to right (bottom priority). In this setup, app_code sits directly on top of base_os:

sudo mount -t overlay overlay \
  -o lowerdir=/tmp/overlay-lab/app_code:/tmp/overlay-lab/base_os,upperdir=/tmp/overlay-lab/upper,workdir=/tmp/overlay-lab/work \
  /tmp/overlay-lab/merged

Run a directory listing against the merged target to confirm that files from both lower directories show up in one place:

ls -la /tmp/overlay-lab/merged
# Output:
# config.env
# os-release.txt
# server.py

Step 3: Trigger a Copy-on-Write and Inspect the Delta

Next, alter an existing configuration file and create a new logfile inside the unified directory:

# Modify an existing lower file
echo "DATABASE_URL=postgres://staging.internal:5432/testdb" > /tmp/overlay-lab/merged/config.env

# Create a brand-new file
echo "[INFO] Worker started" > /tmp/overlay-lab/merged/worker.log

Check the original file in app_code versus the new file in upper:

# Verify the base layer is unchanged
cat /tmp/overlay-lab/app_code/config.env
# Output: DATABASE_URL=postgres://localhost:5432/prod

# Inspect the upper layer directly
ls -l /tmp/overlay-lab/upper
# Output shows config.env and worker.log

cat /tmp/overlay-lab/upper/config.env
# Output: DATABASE_URL=postgres://staging.internal:5432/testdb

The base directory stayed completely untouched. The upper folder holds only our 1KB delta.

Step 4: Inspect Whiteout Nodes on Deletion

Now delete server.py from the merged mount and check how the filesystem tracks the removal:

rm /tmp/overlay-lab/merged/server.py

# Confirm the file disappeared from the merged view
ls -l /tmp/overlay-lab/merged/server.py
# Output: ls: cannot access '.../server.py': No such file or directory

# Check what was written to upper
ls -la /tmp/overlay-lab/upper/server.py

Notice the output format from ls -la: the permissions line begins with a c, and the file size shows 0, 0:

crw-r--r-- 1 root root 0, 0 Nov 29 10:45 /tmp/overlay-lab/upper/server.py

That character device node is our whiteout marker. It masks the lower file without ever writing to app_code.

Step 5: Clean Up

Always release the mount before deleting underlying directories, or the kernel will throw busy inode warnings:

sudo umount /tmp/overlay-lab/merged
rm -rf /tmp/overlay-lab

Practical Takeaways

OverlayFS powers core container runtimes like Docker and containerd under the overlay2 storage driver. Instead of burning minutes copying full filesystems, mounting an overlay takes under 15 milliseconds. Remember two golden rules when deploying union mounts in production: never modify a lowerdir behind the kernel’s back while an overlay is active, and ensure your workdir shares a physical partition with upperdir to guarantee atomic copy-up operations.

Share: