Introduction: The Immutable Android System Challenge
Modern Android systems, particularly those leveraging ‘system-as-root’ and Project Treble, have adopted a highly immutable architecture. The core `/system` partition is typically mounted read-only, ensuring system integrity, enhancing security, and facilitating seamless over-the-air (OTA) updates. While these advancements bring significant benefits to device stability and user security, they pose a formidable challenge for power users and developers who wish to apply persistent system-level modifications, install apps directly to system partitions, or deeply customize their device’s behavior. Any changes made to `/system` are transient; they are lost upon reboot or overwritten by the next OTA update.
This is where OverlayFS steps in. OverlayFS is a union filesystem service that allows you to combine multiple directory trees into a single, unified view. Crucially, it enables a read-write layer to sit atop a read-only layer, making it an elegant solution for achieving persistent modifications on immutable file systems like Android’s `/system` partition.
Understanding OverlayFS Fundamentals
OverlayFS operates on a principle known as ‘copy-on-write’. When you attempt to modify a file that exists in the read-only layer, OverlayFS transparently copies that file to a writable layer before applying the changes. This ensures the original read-only base remains untouched while providing the illusion of a fully writable filesystem.
Key Components: Lower, Upper, Work, and Merged Directories
- Lower Directory (
lowerdir): This is the original, typically read-only, filesystem you want to modify. In our Android context, this will be your `/system` partition. OverlayFS reads existing files from here. - Upper Directory (
upperdir): This is a writable directory where all modifications (new files, modified files, deletions) are stored. On Android, this will reside on a writable partition like `/data`. - Work Directory (
workdir): This is a temporary, empty directory within the same filesystem as theupperdir. OverlayFS uses it for internal operations and to prepare file moves and renames. It must be empty when the OverlayFS is mounted. - Merged Directory: This is the unified view presented to the user. It combines the contents of the
lowerdirandupperdir. When you navigate or interact with files in the merged directory, OverlayFS decides whether to retrieve them from thelowerdir(if untouched) or theupperdir(if modified or new).
When a file is modified, its new version is stored in the upperdir. If a file is deleted from the merged view, OverlayFS creates a special ‘whiteout’ file in the upperdir, signaling that the file from the lowerdir should be hidden. New files are simply created directly in the upperdir.
Prerequisites for OverlayFS Implementation on Android
Before diving into the setup, ensure you have the following prerequisites in place:
- Rooted Android Device: Essential for accessing and modifying core system files and running privileged commands.
- Custom Recovery (e.g., TWRP): Useful for initial backups, flashing necessary tools, and sometimes for gaining initial shell access outside the booted OS.
- ADB Access: Android Debug Bridge is crucial for sending shell commands to your device from your computer.
busyboxortoyboxwith OverlayFS support: Modern Android devices typically include a `toybox` implementation that supports `mount -t overlay`. If not, you may need to install `busybox` (or a `busybox` Magisk module) to ensure the `mount` command has the necessary capabilities.- Basic Linux Shell Knowledge: Familiarity with commands like `ls`, `mkdir`, `mount`, `cp`, `chmod`, `chown`.
- Backup Your Device!: While OverlayFS is relatively safe as it doesn’t modify the `lowerdir`, errors in setup can lead to boot loops. Always back up critical data.
Step-by-Step OverlayFS Setup for Persistent Tweaks
This guide assumes you are performing these actions via an adb shell with root privileges (su).
1. Identify Target Partitions and Prepare Directories
First, identify your read-only `/system` partition. On many devices, it’s mounted directly at `/system`, but the underlying block device might vary.
adb shellsu# Check current mounts and identify /systemmount | grep /system
You’ll typically see something like `/dev/block/dm-0 on /system type ext4 (ro,seclabel,relatime,block_validity,delalloc,barrier,user_xattr)` indicating a read-only mount. Note the actual mount point (`/system`).
Next, create the upperdir and workdir on a writable partition. The `/data` partition is the ideal choice for this, as it is always writable and typically has ample space.
mkdir -p /data/overlay/system_upper/mkdir -p /data/overlay/system_work/
2. Unmount and Remount the Original System (if necessary)
For OverlayFS to correctly take over, the original `/system` mount point needs to be free or mounted read-only. If your `/system` is already read-only, you likely don’t need to unmount it. If it was temporarily remounted read-write, ensure it’s unmounted first.
umount /system # Only if currently mounted read-write. If already RO, this might fail or isn't needed.
3. Mount the OverlayFS Layer
This is the core command that brings OverlayFS to life. We will mount the overlay *over* the existing `/system` mount point.
mount -t overlay overlay -o lowerdir=/system,upperdir=/data/overlay/system_upper,workdir=/data/overlay/system_work /system
-t overlay: Specifies the filesystem type as OverlayFS.overlay: The device argument for OverlayFS (it’s a pseudo-filesystem, so this is just a name).lowerdir=/system: The base read-only layer.upperdir=/data/overlay/system_upper: The writable layer where changes are stored.workdir=/data/overlay/system_work: The temporary working directory./system: The mount point where the merged view will be accessible.
4. Verify the OverlayFS Mount
After executing the mount command, verify that OverlayFS is active and correctly mounted:
mount | grep overlay
You should see an entry similar to: `overlay on /system type overlay (rw,relatime,lowerdir=/system,upperdir=/data/overlay/system_upper,workdir=/data/overlay/system_work)`
To further test, try creating a dummy file in `/system` and then check if it appears in your `upperdir`:
touch /system/test_overlay_file.txtls /data/overlay/system_upper/test_overlay_file.txt
If the file is visible in both `/system` and `/data/overlay/system_upper`, your OverlayFS is working correctly!
Practical Applications: Persistent App Installs and System Modifications
Installing a System App Persistently
Let’s say you want to install an app directly into the `/system/priv-app` directory, making it a privileged system app that persists across reboots.
# Create the app directory in the merged /systemadb shell# Make sure you are rootsu# Create directory for your appmkdir -p /system/priv-app/MyCustomApp# Push the APK file (from your computer)adb push MyCustomApp.apk /system/priv-app/MyCustomApp/MyCustomApp.apk# Set correct permissions and ownershipchown root:root /system/priv-app/MyCustomApp/MyCustomApp.apkchmod 644 /system/priv-app/MyCustomApp/MyCustomApp.apk
After rebooting, the app will still be present and function as a system app because its files are stored in `/data/overlay/system_upper/priv-app/MyCustomApp/`.
Modifying System Configuration Files
You can also persistently modify existing system files, like `build.prop`.
# Make a backup (will be in upperdir)cp /system/build.prop /system/build.prop.bak# Append a custom propertyecho
Android Mobile Specs & Compare Directory
Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!
Compare Devices Specs →