Module 4 · Lesson 134 of 156

A read-only root filesystem with overlayfs

Configure a read-only root, give services writable directories, and verify how a volatile OverlayFS mount over /etc behaves across reboot and image updates.

  • Beginner to advanced
  • 8 min read
  • 38 min lab
What you will need

Disposable Ubuntu 24.04 VM with an ext4 root filesystem, systemd, OverlayFS support, sudo access, a VM console and a snapshot. No board required.

Your place in the module
By the end you can 4 objectives
  • Identify runtime writes that need separate mounts when the root is read-only
  • Configure read-only root options, tmpfs mounts and a volatile bind mount
  • Mount and verify an OverlayFS view of /etc with a read-only lower directory
  • Explain how upper-layer changes affect configuration after an image update
Video coming soonA read-only root filesystem with overlayfs
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from D-Bus and why daemons use it
  • The system bus serves machine-wide software, while the session bus normally serves one user's applications and services.
  • Daemons use named D-Bus interfaces for typed requests, replies and event notifications.
  • Bus names are scoped to a bus, so a successful connection can still be on the wrong bus for a service.
01

Keep the system image read-only

4 min read

A robust field system is read-only where possible, which changes how /var and /etc work. This lesson answers what breaks on a read-only root, tmpfs and bind mounts, overlayfs lower/upper/work, and combining the two for field updates.

A logger that opens a file under /var/log needs to write even when the code it runs and the libraries it loads stay fixed. If that path is on the read-only root, Linux rejects the write with EROFS. File permissions and root privileges cannot make that write succeed through the read-only mount. A separate writable filesystem mounted at /var can accept the same write while / remains read-only. This follows from how Linux resolves paths across mount points, as described in mount(8).

On an embedded device, a read-only system image prevents normal runtime writes from altering installed binaries and baseline configuration. It reduces writes to flash and keeps application writes off the root filesystem during normal operation, including when power is lost. Data on separate writable filesystems still needs its own persistence and power-loss policy. Start by identifying which directories services write to and which files must survive reboot.

  • /var holds logs, caches, queues and application state. A logger may fail to open a log file, and a service may fail to start if it cannot create a database under /var/lib.
  • /tmp holds temporary files. Programs that create them need a writable directory with the usual sticky permissions, 1777, so users cannot freely delete each other's files.
  • /run holds boot-local state such as PID files and Unix sockets. Ubuntu with systemd provides /run as tmpfs early in boot. Services need it available before they create their runtime files.
Mounts on a read-only root/read-only system image/runtmpfs for boot state/tmptmpfs for temporary files/vartmpfs in the lab/usrfiles on read-only root/read-only system image/runtmpfs for boot state/tmptmpfs for temporary files/vartmpfs in the lab/usrfiles on read-only root
Writable mounts take over selected paths below a read-only root

For a block-backed root on Ubuntu, edit the existing / entry in /etc/fstab while the image is writable. Keep its device identifier, filesystem type and other required settings. Replace rw with ro if present, or add ro to the options field. The kernel command-line ro requests a read-only initial root mount. During boot, systemd-remount-fs.service applies the configured mount options to /, so an initially read-only mount alone does not establish the final policy. Inspect the active mount with findmnt -no TARGET,FSTYPE,OPTIONS /.

For a controlled image whose entire /var may be volatile, the following are example /etc/fstab entries. The size values are example limits for a VM. Check existing mount definitions before adding entries for the same paths.

bash
# Source  Target  Type   Options                    Dump  Pass
tmpfs     /var    tmpfs  rw,mode=0755,size=128M     0     0
tmpfs     /tmp    tmpfs  rw,mode=1777,size=64M      0     0

Mounting tmpfs at /var covers the image's existing /var tree. The new filesystem starts empty. On a production image, create the required directory layout and ownership before dependent services start. Seed any initial files those services need from the image. A stock Ubuntu installation keeps package metadata under /var/lib/dpkg, so covering its entire /var can disrupt package management and other services. Scope the change to selected directories when only logs or caches should be volatile. Tmpfs consumes memory as it fills, and writes can fail when it reaches its limit.

A lets you make just one location volatile. For example, ship /var/cache/myapp as an existing mount point in the image. Once /run is available, create /run/myapp-cache with the application owner's permissions, then run mount --bind /run/myapp-cache /var/cache/myapp before starting the application. The bind mount uses the storage of its source. Here it is volatile because the source lives on /run tmpfs. Put state that must survive reboot on a dedicated writable filesystem.

02

Give /etc a writable view

3 min read

A fresh tmpfs mounted directly on /etc would hide configuration that programs need to read. OverlayFS keeps those defaults visible and provides somewhere to store changes. The lower directory supplies the image's /etc. The upper directory stores new and changed entries. The work directory is scratch space for OverlayFS operations. Start with an empty work directory on the same filesystem as the upper directory, and keep the two as separate directories.

The OverlayFS mount over /etcMerged /etcpath used by programsUpper directorychanged files on tmpfsWork directoryscratch space on same tmpfsLower directoryoriginal read-only /etcMerged /etcpath used by programsUpper directorychanged files on tmpfsWork directoryscratch space on same tmpfsLower directoryoriginal read-only /etc
The merged /etc reads image defaults and records changes on tmpfs

Programs use the merged mount at /etc. When the same filename exists in the upper and lower directories, the upper entry takes precedence. When corresponding entries are directories, their names are normally merged. If a program opens a lower-only regular file for writing, OverlayFS can first copy it to the upper directory and then modify the copy. This operation is called . Files that are only read can stay in the lower directory.

The lab uses a VM console and systemd's emergency target so you can cover /var without starting the usual services. Take a snapshot first. Check that you can authenticate at the emergency console. On a disposable Ubuntu VM with a locked root account, set a lab password with sudo passwd root before entering emergency mode. Edit the root entry and add the /var tmpfs entry beforehand. Reboot with systemd.unit=emergency.target appended to the kernel command line for that boot. At the root shell, remount / with mount -o remount,ro / if needed and verify it with findmnt. Confirm that /run is already tmpfs, then mount /var with mount /var if it is still on the root filesystem. Keep the VM in emergency mode for the test.

As root, use the following commands once per boot. The bind mount gives you a separate path to the original /etc before the overlay covers it. Because the source is already on the read-only root, the lower directory is read-only through that path as well.

bash
mkdir -p -m 0700 /run/etc-overlay
mkdir /run/etc-overlay/{lower,upper,work}
mount --bind /etc /run/etc-overlay/lower
mount -t overlay overlay \
  -o lowerdir=/run/etc-overlay/lower,upperdir=/run/etc-overlay/upper,workdir=/run/etc-overlay/work \
  /etc
findmnt -no TARGET,FSTYPE,OPTIONS /etc

Verify behavior as well as mount options. Creating /m4-root-probe should fail with a read-only filesystem error. Creating /var/m4-probe and /etc/m4-probe should succeed. Check that /run/etc-overlay/upper/m4-probe exists and /run/etc-overlay/lower/m4-probe is absent. To see copy-up, create a separate marker file in the image's /etc before rebooting into emergency mode, then change it through the merged mount. Compare the upper and lower copies. On a second emergency boot, repeat the mount setup and check that the probes are gone and the image marker has its original contents. Restore the snapshot when finished.

Going deeperExplore the mechanism

Deleting a lower-only name through the merged mount creates a whiteout in the upper layer. The original remains in the lower directory and the merged view hides it. An upper directory marked opaque also hides the corresponding lower directory's entries. These records matter when restoring or migrating a persistent upper layer.

OverlayFS requires a compatible upper filesystem with support for the extended attributes it uses and valid directory entry types. NFS is unsuitable as an upper filesystem. With default behavior, renaming a lower or merged directory can return EXDEV. Avoid editing the backing directories directly while their overlay is mounted. Container runtimes commonly use OverlayFS for the same mechanism: read-only image layers supply the baseline files and each writable container gets its own upper layer.

03

Keep image updates separate from runtime changes

2 min read

On a deployed system, set up the writable mounts before the services that depend on them. For an overlay covering all of /etc, timing matters because systemd and other programs can read configuration early. Set up the merged view in the initramfs before handing control to the real root's init, and make sure the overlay and its backing mounts are available after the handoff. A service running after boot can test the mount as in the lab, but services that have already read /etc may retain earlier settings.

Configuration after an image updateBootreplacementimageRead newdefaultsMount freshoverlayApplystoredsettingsBootreplacementimageRead newdefaultsMount freshoverlayApplystoredsettings
A replacement image supplies defaults while persistent device settings are stored separately

An image-based field updater writes a replacement root filesystem outside the running root mount. After rebooting into that image, the new /etc supplies the lower directory. With a tmpfs upper, configuration edits from the previous boot are gone, so the new image's defaults are visible. Store settings that must survive, such as a device's network provisioning, on separate persistent storage. Have the application read them there or apply the required settings to the volatile view during startup.

A persistent upper can retain edits across boots. If the updater reuses it unchanged, an old upper copy of a configuration file can take precedence over the revised file in the new image. An old deletion can also keep a name hidden after an update. If the product needs a persistent upper, decide which changes belong to the device and validate or migrate them against the new image. Before shipping an update, check the effective configuration through the merged mount as well as the defaults in the replacement image.

Consider a device with a new factory default and an existing site-specific network setting. Decide which file should come from the new lower directory and which setting should be loaded from persistent storage. Then check which value the application actually reads after reboot. The next lesson, Why updates matter and the A/B scheme, explains where a replacement image can be installed while the device is still running its current image.

Key takeaways

  • A read-only root can contain mount points for writable filesystems
  • Tmpfs contents disappear when the filesystem is unmounted or the system reboots
  • OverlayFS presents a merged view and directs changes to the upper layer
  • Persistent upper entries can hide configuration shipped in a new image
Reflect
  1. Explain to a colleague, in two sentences: identify runtime writes that need separate mounts when the root is read-only
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close