Course contentsShow
← Linux
Lesson 1483 of 1,81913. Containerization Primitives and NamespacesFree lesson

Copy-on-Write Mechanisms

Trace the movement of file blocks when modified in a read-only layered structure.

In this lesson, you will learn how the kernel manages file modifications within layered filesystems like OverlayFS to ensure efficiency without duplicating data.

The Problem of Shared Layers

When you run a container, you are typically using a "layered" filesystem where the base image is mounted as a read-only lower directory, and your container gets a writable upper directory. If every container had to copy every single file from the base image to perform a small change, disk usage and boot times would explode. Copy-on-Write (CoW) solves this by delaying the duplication of data until the exact moment a write request is made.

The Mechanism: Laziness as an Optimization

CoW treats the layered stack as a single merged view. When a process inside a container attempts to modify a file that resides in the read-only lower layer, the kernel intercepts the write syscall. It performs a three-step dance:

  1. Check existence: The kernel determines that the target file exists in the lower layer but is not yet in the upper layer.
  2. Copying (The "Copy" in CoW): The kernel performs an "up-copy." It copies the data block (or the entire file, depending on the implementation) from the read-only layer into the writable upper layer.
  3. Redirection: Once the file is copied to the upper layer, the kernel updates the merged view so that subsequent reads and writes for that file are directed exclusively to the copy in the upper layer.

Visualization of the Workflow

Imagine a base image with config.txt.

  1. Process requests write to config.txt:
    # Inside the container
    echo "new settings" > /etc/config.txt
    
  2. The Kernel Action:
    • OverlayFS sees /etc/config.txt is read-only.
    • It creates a copy of config.txt in the /upper directory.
    • The process writes "new settings" into that new copy.
    • The original file in the lower layer remains untouched, effectively "hidden" or "obscured" by the new version in the upper layer.

This mechanism ensures that the base image remains pristine and immutable, shared by all containers, while each container maintains its own private, minimal delta of changes.

Key Takeaway: Copy-on-Write minimizes overhead by treating read-only layers as immutable references, only creating full, writable copies of file blocks at the precise moment a write operation is initiated.