Why partition gets unmounted automatically after some time?


Question

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?


Answer

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.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes