Copy-on-Write at Runtime
In one sentence: The writable layer stays nearly empty because the overlay driver copies a file up from the read-only layers only at the moment of first write — and "deletes" shared files by masking them with whiteouts.
You already know
- From Lesson 9: reads resolve top-down; the upper shadows the lowers.
- From Lesson 12: lower layers are immutable — nothing can subtract from them.
Pay only on first write
Opening a lower-layer file for writing triggers copy-up: the driver copies the whole file into the upperdir, then lets the write proceed on the copy. Every later read or write hits the upper copy (Lesson 9's shadowing). The costs are real and worth knowing: the first write to a large file pays a full-file copy — appending one log line to a 5 GB file in a lower layer copies 5 GB. Deletion inverts the trick: since lowers can't lose files, the driver plants a whiteout marker in the upper that masks the name; the merged view shows nothing, the lower still holds the bytes.
flowchart TB
OP["file operation"] --> Q1{"write or delete?"}
Q1 -- write --> Q2{"file already
in upperdir?"}
Q2 -- yes --> W["write in place (cheap)"]
Q2 -- "no (lower only)" --> CU["copy-up whole file,
then write the copy"]
Q1 -- delete --> Q3{"file lives in
upper or lower?"}
Q3 -- upper --> RM["remove from upper"]
Q3 -- lower --> WO["plant whiteout marker
(lower bytes remain)"]
style CU fill:#fff8ec,stroke:#f2ddb0
sequenceDiagram
participant P as Process
participant O as Overlay driver
participant U as upperdir
participant L as lower layers
P->>O: append to /var/log/app.log (first write)
O->>L: file found in lower (200 MB)
O->>U: copy-up all 200 MB
O->>U: apply the append
O-->>P: done (slow — paid the copy)
P->>O: append again
O->>U: file present in upper
O-->>P: done (fast — native write)
Anchor these
- Copy-up happens once, at first write, and copies the whole file.
- Deletion = whiteout mask; lower bytes are never touched.
- Heavy-write paths (databases, logs) don't belong on overlay — that's Part 5's opening argument.
See what actually stuck.
Take the practice scenarios now.