Search

Search papers and lessons

Layered File System

Level 2 · 10 min read

A layered file system shows several read-only layers stacked as one folder tree, with a thin writable layer on top, so many systems can share one base and still change files.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Linear algebra
  • Calculus
  • Electromagnetism
  • Algorithms
  • ReadReady to readNot yet

TL;DR

A layered file system presents several folder trees as one. The bottom layers are read-only, and a thin writable layer sits on top. Reads fall through the stack until a layer has the file, and a write copies the file up into the writable layer first. This lets many systems share one base image and still change files without touching the base.

Builds on: file system, copy-on-write · Leads to: container images

Where it's used

Container images. A container image is built as a stack of layers, each one a set of files that the previous layers did not have or that changed. Many containers started from the same image share the read-only layers on disk, and each running container adds its own writable layer on top. Deleting the container removes only that thin top layer. The image itself is not changed.

Live USB systems. A live operating system often boots from a read-only compressed image on the stick. A writable layer in memory or on a spare partition sits over it, so the system can save logs and settings while the stick itself stays unchanged. Removing the stick and plugging it in again gives back the original system.

Appliances and routers. Some embedded devices keep their firmware on read-only flash and put changes in a writable layer. A factory reset then means throwing away one layer, and a firmware update can replace the base while the device's own settings stay in the top layer. The exact arrangement varies from product to product.

How it works

Take a tiny stack of two layers. The bottom layer is the read-only base. It holds two files, a.txt containing hello and b.txt containing bye. The top layer is writable and starts empty. The user sees one folder with the two files.

  1. Read a.txt. The top layer does not have it, so the lookup falls through to the base. The user gets hello. Check: the base was read and nothing was written.
  2. Write to b.txt. The file lives only in the base, which must not change. The system copies b.txt up into the top layer first, and then the write lands on the copy. The top layer now holds one file, b.txt. Check: the base still says bye.
  3. Delete a.txt. The base still has a.txt, and the top layer cannot remove files from below. So the system writes a marker, called a whiteout, into the top layer that says "a.txt is gone." The user no longer sees a.txt. Check: the base still has a.txt; only the view changed.
  4. Create c.txt. New files go straight into the top layer, since nothing below has them. Check: the top layer now holds the copy of b.txt, the whiteout for a.txt and c.txt, and the base is untouched.

The lookup rule behind these steps can be drawn as a flowchart.

flowchart TD
  A[Look up a name] --> B{Top writable layer has it?}
  B -- yes, a whiteout --> X[Not found]
  B -- yes, a file --> F[Use the top copy]
  B -- no --> C{Next lower layer has it?}
  C -- yes --> G[Use that layer's copy]
  C -- no --> D{Any layers left?}
  D -- yes --> C
  D -- no --> X

The math

We model a stack of layers L1,L2,…,LkL_1, L_2, \ldots, L_k where L1L_1 is the top, writable layer and the others are read-only. Each layer is a set of names with contents. Let view(p)\mathrm{view}(p) be what a user sees at path pp.

Definition: Layered lookup

For a path pp, let i∗i^* be the smallest index such that layer LiL_i has an entry for pp, whether a file or a whiteout. If that entry is a whiteout, then view(p)\mathrm{view}(p) is absent. Otherwise view(p)\mathrm{view}(p) is the content of Li∗L_{i^*} at pp. If no layer has an entry, pp is absent.

Step 1 in symbols

Reading a.txta.txt in the walkthrough means i∗=2i^* = 2, because L1L_1 has no entry and L2L_2 does. The view is L2L_2's content, which is hello. Reads never change any layer.

Step 2 in symbols: copy-up

A write to a path pp with i∗>1i^* > 1 first copies the content from Li∗L_{i^*} into L1L_1, then applies the write to the copy in L1L_1. Only L1L_1 ever changes. In the simple model the copy is of the whole file, so the cost of the first write is about the size of the file, not the size of the change. The base layer stays as it was.

Step 3 in symbols: deletion

Deleting pp when i∗>1i^* > 1 writes a whiteout for pp into L1L_1. Afterwards the lookup in the definition finds the whiteout at index 11 and reports absent, while Li∗L_{i^*} still holds the file. Deletion therefore never shrinks a lower layer.

Step 4 in symbols: creation

Creating a new path pp with no entry in any layer writes the file into L1L_1 and touches nothing below it.

Worked examples

Example
1. Two layers, one edit

The base holds a.txt and b.txt. The top layer is empty. The user writes to b.txt. How many files does the top layer hold afterwards?

  1. The lookup for b.txt finds the first entry at layer 2, the base, so i∗=2i^* = 2.
  2. The write needs copy-up, so the top layer receives one copy of b.txt.
  3. No other file was touched, so the top layer now holds one file.

Interpretation: the top layer holds only what the user changed. Check: the base still holds two files.

Example
2. Three layers with shadowing

The base layer has x = v1 and y = v1. The runtime layer has x = v2. The app layer, on top, has z = v3, and it has also just deleted y. What does the user see for x, y and z?

  1. For x: the app layer has no entry, so go down. The runtime layer has x = v2, so i∗=2i^* = 2 and the view is v2. The base's v1 is hidden.
  2. For y: the app layer has a whiteout, so i∗=1i^* = 1 and the view is absent. The base still has v1, but the user does not see it.
  3. For z: the app layer has z = v3, so i∗=1i^* = 1 and the view is v3.

Interpretation: the layer order decides which version wins, and a whiteout hides everything below it. Checking y took one step because the whiteout stopped the search early.

Example
3. A container writes a log file

Suppose a container image has a 120 MB base layer, and a 15 MB application layer above it that contains /etc/app.conf. This is a hypothetical example, with sizes chosen only for arithmetic. A running container appends 2 MB of logs to /var/log/app.log, which lives in the base layer and is 5 MB in size. How much does the container's writable layer grow, and how much does the image change?

  1. The first write to /var/log/app.log triggers copy-up, and in the simple model the whole 5 MB file is copied into the container's writable layer.
  2. The append of 2 MB is applied to the copy, so the writable layer now holds 5 MB plus 2 MB, which is 7 MB.
  3. The image's 120 MB base and 15 MB application layer do not change, so the image on disk is unchanged.
  4. Delete the container: its 7 MB layer goes, and the next container starts from the same image.

Interpretation: a small change to a big file costs the whole file once in the writable layer. This is one reason write-heavy data is usually kept in a separate volume.

Try it: a container edits a 40 MB file that lives in a lower layer and changes only 1 KB of it. Under the simple model, how many megabytes does the writable layer hold after the edit? Answer: the whole file is copied up, so the layer holds 40 MB, even though only 1 KB of content changed.

Common mistakes and tradeoffs

It is tempting to think layers work like commits in a version history that you can reorder freely. In fact the lower layers are read-only and their order is fixed by how the stack was built. The order decides which version of a file wins, and reordering changes what the user sees.

It is tempting to think deleting a file frees its space in the image. In fact the lower layer keeps the file, and deletion only writes a whiteout above it. The image stays the same size, and the file reappears if the top layer is removed. Space comes back only when unused layers are pruned, a process similar to garbage collection.

It is tempting to think a small write costs a small copy. In fact the first write to a lower file copies the file up, so a small edit to a large file can use disk space in proportion to the whole file. This is the cost of copy-on-write applied to files.

The idea suits systems that start many copies of one base, need to ship a base unchanged, or want reset by discarding a layer. It suits them less well for write-heavy data, for very deep stacks where each lookup checks many layers, and for workloads that expect a clean in-place file system. Alternatives include mounting a separate volume for data, or flattening the stack into one image when the lookups get slow.

History

Early versions of the idea appeared in the 1990s in research systems that combined several directories into one view. Linux later gained union-style file systems, and in the 2010s OverlayFS became the common one. Container tools built on these layers because they gave a cheap way to share a base across many running programs. The idea caught on because it made starting many similar programs fast without copying the whole base each time.

Recap

  • A layered file system stacks read-only layers under one writable layer and shows them as a single tree.
  • Reads fall through the layers until one has the file; the top-most entry wins.
  • A write copies the file up into the writable layer first, so the lower layers never change.
  • A deletion writes a whiteout above the file instead of removing it from below.
  • Read next: container images, which use this stack to share one base across many containers.

Check yourself

QuestionIn one sentence, what does a layered file system show to the user, and where do writes go?Show answer

It shows several stacked folder trees as one merged tree, where the top-most entry for each path wins. Writes go to the thin writable top layer; a file from a lower layer is copied up before it is changed, so the read-only layers stay the same.

QuestionThe base has `c.txt` and the top layer is empty. The user deletes `c.txt`, then the base is shared with another container. Does the other container still see `c.txt`? Explain.Show answer

Yes. The deletion writes a whiteout only into the first container's top layer. The base layer, which is shared, still holds c.txt, so the other container's view is unchanged.

QuestionA stack has three layers. A path appears in the middle layer only, and the top layer has no entry for it. What does the lookup return, and how many layers does it check?Show answer

It returns the middle layer's file. The lookup checks the top layer (no entry), then the middle layer (entry found), and stops there, so it checks two layers.

QuestionA container image's base layer is 300 MB. A container edits one 20 MB file in the base in the simple copy-up model. How many megabytes does the container's writable layer grow by, and why?Show answer

It grows by 20 MB. The first write copies the whole file up into the writable layer, and the simple model does not copy only the changed bytes. The 300 MB base is not copied and does not change.

Sources