SHORT ANSWER
Go to Magisk Settings and set Mount Namespace Mode
to Global
once for all. In SuperSU app, disable Mount Namespace Separation
.
OR
For one time solution, use this command instead:
~$ su -mm -c 'mount <device> <mountpoint>'
LONG ANSWER
but after some time, the partition gets unmounted automatically and I have to mount it again.
ANDROID NAMESPACES
This behavior is due to Mount Namespace implemented by Android since Android-6 for the purpose of apps sandboxing / isolation. In order to control app's access to system resources and filesystems - particularly SDCards - every app (Dalvik or ART VM which processes Java bytcode to executable .dex binary) is started by zygote (an Android init process) in a new mount namespace where it can independently unmount any filesystem (except rootfs) or remount, not affecting other namespaces. Every process the app starts, lives in the same (or further) isolated namespace.
Usually mnt
(mount) and net
(network) namespaces are enabled on Android kernel by default. Other namespaces pid
, user
and uts
can be enabled by rebuilding kernel.
GLOBAL NAMESPACE
Very first process started by kernel at boot: init along with all kernel processes (kworkers etc.) and other init daemon processes (such as ueventd, logd, servicemanager, vndbinder, mediaserver, netd etc.) live in Global / Root Namespace. When we install mods (such as Magisk, Xposed etc.), they also start as a process in root namespace, usually at initial stage of boot process.
Android filesystems (real or pseudo; /system, /data, /proc etc.) are also initially mounted in global namespace. An app's namespace has all mounts (including rootfs) set to slave
, so that any new mount inside them in root namespace, propagates to the app's namespace but not vice versa. See mount propagation.
NAMESPACE TOOLS
Linux command lsns
can be used to view all namespaces. Mount namespace can be created by unshare -m
. To enter a namespace, nsenter
is an easily available SETNS wrapper. In most cases these commands don't work without root privileges.
SUPER USER AND MOUNT NS
When we execute su
command in a Terminal emulator app, a new process (shell) is started with elevated capabilities. This process lives in the same mount namespace as that of Terminal app. So the mount
command is also executed in the same namespace and thus filesystem is only visible within that namespace. Once we exit
that shell, mountpoint won't show filesystem contents. If there is no running process in a namespace, it's automatically cleaned.
Please note that an app is completely killed when its Dalvik process is killed by Force Stop or by Android's memory management.
If the Terminal app wasn't completely killed, su
may enter the same namespace, only when Inherited Namespace
option is enabled in Magisk. Isolated Namespace
will always create a new mount namespace.
Now coming to your question:
Even when it is mounted, the contents are not visible in file explorer apps like MiXplorer and ES Explorer, and are only visible in terminal.
This is because Explorers (apps) are running in their own mount namespaces. Run this command as root to get an overview:
~# ps f -p2 --ppid 2 --deselect -o mntns,pid,cmd --sort=mntns
You may use Termux for a full version of ps
.
When I set this command to execute on boot by placing inside init.d directory in /etc, then it works normally and ext4 partition does not unmount itself automatically.
This is because that init.d script is executed by init
in global namespace.
So, what is the best way to mount ext4 partition in Android persistently and what is the reason of such behavior?
To escape the whole enigma, always mount your frequently accessed filesystems in Global Mount Namespace (though it's vulnerable to security breaches) unless necessary otherwise. A simple check if we are in global namespace:
~# [ "$(readlink /proc/self/ns/mnt)" = "$(readlink /proc/1/ns/mnt)" ] && echo 'In Global NS.' || echo 'Not in Global NS.'
NOTE: This test works only in initial PID NS.
Another possibility is to create namespace with unshare -m --propagation shared
. Now any new mounts in this namespace will propagate to all namespaces. But this isn't applicable to already created apps' namespaces. mount --make-rshared /
doesn't work (at least for me) if namespace was originally created with slave
or private
propagation.
See this thread post for more details.