website/docs/guide/metamodule.md
Metamodules are a revolutionary feature in KernelSU that transfers critical module system capabilities from the core to pluggable modules. This architectural shift maintains KernelSU's stability and security while unleashing greater innovation potential for the module ecosystem.
A metamodule is a special type of KernelSU module that provides core infrastructure functionality for the module system. Unlike regular modules that modify system files, metamodules control how regular modules are installed and mounted.
Metamodules are a plugin-based extension mechanism that allows complete customization of KernelSU's module management infrastructure. By delegating mounting and installation logic to metamodules, KernelSU avoids being a fragile detection point while enabling diverse implementation strategies.
Key characteristics:
Traditional root solutions bake mounting logic into their core, making them easier to detect and harder to evolve. KernelSU's metamodule architecture solves these problems through separation of concerns.
Strategic advantages:
Mounting flexibility:
meta-overlayfs)Beyond mounting:
::: warning IMPORTANT
Without a metamodule installed, modules will NOT be mounted. Fresh KernelSU installations require installing a metamodule (such as meta-overlayfs) for modules to function.
:::
Install a metamodule the same way as regular modules:
meta-overlayfs.zip)The meta-overlayfs metamodule is the official reference implementation that provides traditional overlayfs-based module mounting with ext4 image support.
You can check which metamodule is currently active in the KernelSU Manager app's Module page. The active metamodule will be displayed in your module list with its special designation.
::: danger WARNING Uninstalling a metamodule will affect ALL modules. After removal, modules will no longer be mounted until you install another metamodule. :::
To uninstall:
After uninstalling, you should install another metamodule if you want modules to continue working.
Only one metamodule can be installed at a time. If you try to install a second metamodule, KernelSU will prevent the installation to avoid conflicts.
To switch metamodules:
If you're developing regular KernelSU modules, you don't need to worry much about metamodules. Your modules will work as long as users have a compatible metamodule installed (like meta-overlayfs).
What you need to know:
system directory in your module will only be mounted if the user has a metamodule installed that provides mounting functionality::: tip If you're familiar with Magisk module development, your modules will work the same way in KernelSU when metamodule is installed, as it provides Magisk-compatible mounting. :::
Creating a metamodule allows you to customize how KernelSU handles module installation, mounting, and uninstallation.
A metamodule is identified by a special property in its module.prop:
id=meta-example
name=My Custom Metamodule
version=1.0
versionCode=1
author=Your Name
description=Custom module mounting implementation
metamodule=1
Key requirements:
metamodule=1 (or metamodule=true) property marks this as a metamodule. Without this property, the module will be treated as a regular module.meta- (e.g., meta-overlayfs, meta-magicmount, meta-custom). This helps users easily identify metamodules and prevents naming conflicts with regular modules.A metamodule structure:
meta-example/
├── module.prop (must include metamodule=1)
│
│ *** Metamodule-specific hooks ***
├── metamount.sh (optional: custom mount handler)
├── metainstall.sh (optional: installation hook for regular modules)
├── metauninstall.sh (optional: cleanup hook for regular modules)
│
│ *** Standard module files (all optional) ***
├── customize.sh (installation customization)
├── post-fs-data.sh (post-fs-data stage script)
├── service.sh (late_start service script)
├── boot-completed.sh (boot completed script)
├── uninstall.sh (metamodule's own uninstallation script)
└── [any additional files]
Metamodules can use all standard module features (lifecycle scripts, etc.) in addition to their special metamodule hooks.
Metamodules can provide up to three special hook scripts:
Purpose: Controls how modules are mounted during boot.
When executed: Execution Order below.
Environment variables:
MODDIR: The metamodule's directory path (e.g., /data/adb/modules/meta-example)Responsibilities:
skip_mount flags::: danger CRITICAL REQUIREMENT
When performing mount operations, you MUST set the source/device name to "KSU". This identifies mounts as belonging to KernelSU.
Example (correct):
mount -t overlay -o lowerdir=/lower,upperdir=/upper,workdir=/work KSU /target
For modern mount APIs, set the source string:
fsconfig_set_string(fs, "source", "KSU")?;
This is essential for KernelSU to identify and manage its mounts properly. :::
Example script:
#!/system/bin/sh
MODDIR="${0%/*}"
# Example: Simple bind mount implementation
for module in /data/adb/modules/*; do
if [ -f "$module/disable" ] || [ -f "$module/skip_mount" ]; then
continue
fi
if [ -d "$module/system" ]; then
# Mount with source=KSU (REQUIRED!)
mount -o bind,dev=KSU "$module/system" /system
fi
done
Purpose: Customize how regular modules are installed.
When executed: During module installation, after files are extracted but before installation completes. This script is sourced (not executed) by the built-in installer, similar to how customize.sh works.
Environment variables and functions:
This script inherits all variables and functions from the built-in install.sh:
MODPATH, TMPDIR, ZIPFILE, ARCH, API, IS64BIT, KSU, KSU_VER, KSU_VER_CODE, BOOTMODE, etc.ui_print <msg> - Print message to consoleabort <msg> - Print error and terminate installationset_perm <target> <owner> <group> <permission> [context] - Set file permissionsset_perm_recursive <directory> <owner> <group> <dirpermission> <filepermission> [context] - Set permissions recursivelyinstall_module - Call the built-in module installation processUse cases:
install_module when ready)Note: This script is NOT called when installing the metamodule itself.
Purpose: Clean up resources when regular modules are uninstalled.
When executed: During module uninstallation, before the module directory is removed.
Environment variables:
MODULE_ID: The ID of the module being uninstalledUse cases:
Example script:
#!/system/bin/sh
# Called when uninstalling regular modules
MODULE_ID="$1"
IMG_MNT="/data/adb/metamodule/mnt"
# Remove module files from image
if [ -d "$IMG_MNT/$MODULE_ID" ]; then
rm -rf "$IMG_MNT/$MODULE_ID"
fi
Understanding the boot execution order is crucial for metamodule development:
post-fs-data stage:
1. Common post-fs-data.d scripts execute
2. Prune modules, restorecon, load sepolicy.rule
3. Metamodule's post-fs-data.sh executes (if exists)
4. Regular modules' post-fs-data.sh execute
5. Load system.prop
6. Metamodule's metamount.sh executes
└─> Mounts all modules systemlessly
7. post-mount.d stage runs
- Common post-mount.d scripts
- Metamodule's post-mount.sh (if exists)
- Regular modules' post-mount.sh
service stage:
1. Common service.d scripts execute
2. Metamodule's service.sh executes (if exists)
3. Regular modules' service.sh execute
boot-completed stage:
1. Common boot-completed.d scripts execute
2. Metamodule's boot-completed.sh executes (if exists)
3. Regular modules' boot-completed.sh execute
Key points:
metamount.sh runs AFTER all post-fs-data scripts (both metamodule and regular modules)post-fs-data.sh, service.sh, boot-completed.sh) always run before regular module scripts.d directories run before metamodule scriptspost-mount stage runs after mounting is completeWhen a metamodule is installed, KernelSU creates a symlink:
/data/adb/metamodule -> /data/adb/modules/<metamodule_id>
This provides a stable path for accessing the active metamodule, regardless of its ID.
Benefits:
The meta-overlayfs metamodule is the official reference implementation. It demonstrates best practices for metamodule development.
meta-overlayfs uses a dual-directory architecture:
Metadata directory: /data/adb/modules/
module.prop, disable, skip_mount markersContent directory: /data/adb/metamodule/mnt/
modules.img)Here's how meta-overlayfs implements the mount handler:
#!/system/bin/sh
MODDIR="${0%/*}"
IMG_FILE="$MODDIR/modules.img"
MNT_DIR="$MODDIR/mnt"
# Mount ext4 image if not already mounted
if ! mountpoint -q "$MNT_DIR"; then
mkdir -p "$MNT_DIR"
mount -t ext4 -o loop,rw,noatime "$IMG_FILE" "$MNT_DIR"
fi
# Set environment variables for dual-directory support
export MODULE_METADATA_DIR="/data/adb/modules"
export MODULE_CONTENT_DIR="$MNT_DIR"
# Execute the mount binary
# (The actual mounting logic is in a Rust binary)
"$MODDIR/meta-overlayfs"
Overlayfs mounting:
/data/adb/modules/.rw/Source identification:
// From meta-overlayfs/src/mount.rs
fsconfig_set_string(fs, "source", "KSU")?; // REQUIRED!
This sets dev=KSU for all overlay mounts, enabling proper identification.
When developing metamodules:
skip_mount and disableecho or logging for debuggingBefore releasing:
For users: Only if you want to use modules that require mounting. If you only use modules that run scripts without modifying system files, you don't need a metamodule.
For module developers: No, you develop modules normally. Users need a metamodule only if your module requires mounting.
For advanced users: Only if you want to customize mounting behavior or create alternative mounting implementations.
No. Only one metamodule can be installed at a time. This prevents conflicts and ensures predictable behavior.
Modules will no longer be mounted. Your device will boot normally, but module modifications won't apply until you install another metamodule.
No. It provides standard overlayfs mounting compatible with most modules. You can create your own metamodule if you need different behavior.