By jonny789


2018-07-09 12:04:29 8 Comments

I have Android phone running Oreo 8.1.0. I have two partitions in sdcard. One partition is ext4 formatted. I mount it using:

mount -t ext4 -o rw /dev/block/mmcblk1p2 /data/sdext2

But after some time, the partition gets unmounted automatically and I have to mount it again.

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.

So, what is the best way to mount ext4 partition in Android persistently and what is the reason of such behavior?

1 comments

@Irfan Latif 2018-08-16 21:01:02

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 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.

@Sérgio 2018-11-12 14:10:49

"This is because Explorers (apps) are running in their own mount namespaces. Run this command as root to get an overview!" how we mount ext4 in their namespace with android 6.0.1 ?

@Sérgio 2018-11-12 18:47:02

yes apps , thanks looks great but I just can test it next weekend

@Sérgio 2018-11-18 22:20:54

I coun't solve this problem , nsenter after mount -t ext 4 [ "$(readlink /proc/self/ns/mnt)" = "$(readlink /proc/1/ns/mnt)" ] && echo 'In Global NS.' || echo 'Not in Global NS.' In Global NS.

@Sérgio 2018-11-19 18:16:29

@Sérgio 2018-11-20 20:28:09

thank you for your help , here is my new question android.stackexchange.com/questions/204123/… (this one is the correct link)

Related Questions

Sponsored Content

1 Answered Questions

Mount sdcard partition as /data

3 Answered Questions

[SOLVED] External Ext4 card mounted only for root (!) on Android 5.1 64 bits

  • 2015-09-19 23:19:20
  • Sopalajo de Arrierez
  • 8762 View
  • 3 Score
  • 3 Answer
  • Tags:   mount ext4

2 Answered Questions

1 Answered Questions

Link2sd, partition unmounted itself, can't mount back

1 Answered Questions

1 Answered Questions

2 Answered Questions

[SOLVED] How to restore damaged Internal SD card partition layout? Tried everything

0 Answered Questions

/data/sdext2 mount not found link2sd

2 Answered Questions

[SOLVED] EXT4 partition not recognized by system

Sponsored Content