ballerburg9005 / android-exfat-ntfs-ext4-sdcard-usb-otg-mount-hack

App + bash script mounts anything inside /sdcard/_sdXX/ even works for old apps
GNU General Public License v3.0
3 stars 0 forks source link

need -t, Invalid argument comes if using -t, files not visible in file manager #2

Open BoostUpStation opened 1 year ago

BoostUpStation commented 1 year ago

:/data/data/com.termux/files/home # mount -o rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,umask=0000,context=u:object_r:fuse:s0 /dev/block/mmcblk0p1 /sdcard/_SDCARD mount: /dev/block/mmcblk0p1: need -t

:/data/data/com.termux/files/home # mount -o rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,umask=0000,context=u:object_r:fuse:s0 -t ext4 /dev/block/mmcblk0p1 /sdcard/_SDCARD mount: '/dev/block/mmcblk0p1'->'/sdcard/_SDCARD': Invalid argument

and if using only rw with -o options:- :/data/data/com.termux/files/home # mount -o rw -t ext4 /dev/block/mmcblk0p1 /sdcard/_SDCARD < :/data/data/com.termux/files/home # ls /sdcard/_SDCARD
1736254.mp4 QRCode.jpg NewData-20231107_083302.zip lost+found :/data/data/com.termux/files/home #

It displays data in termux with root, but not in file manager, folder is empty in file manager. and even empty if accessing that folder using root in file manager.

Using the release app is not working as well.

And if using the fat32 card, it gives mount point in /storage/SOME_SHORT_UUID and that uuid changes everytime we plug in and out the sdcard. so mounting at that point is not working as well, already tried that.

Hoping for a solution, thank you.

BoostUpStation commented 1 year ago

above i did with ext4 sdcard and same problem with exfat formatted sdcard as well.

ballerburg9005 commented 1 year ago

Well as mentioned in the section "trouble", you need all the options from the mount command for the sdcard to become read-writable to non-root users, specifically you need "context=u:object_r:fuse:s0" and I think also the uid and gid options. Please check if the actual uid and gid of your android user is 1023. I believe at least that it should match that, not sure.

On top of that you need the 3 bind mounts for the sdcard's content to show up in apps:

# _$mdir is e.g. _SDCARD, where you mounted the card
                mount -o bind /sdcard/_$mdir /mnt/runtime/write/emulated/0/_$mdir
                mount -o bind /sdcard/_$mdir /mnt/runtime/read/emulated/0/_$mdir
                mount -o bind /sdcard/_$mdir /mnt/runtime/default/emulated/0/_$mdir

Like mentioned those might differ with some vendors. Also if your phone is really really old, the whole way Android handles SD cards has changed like 10 years back twice or thrice, from Android 4-6 or so ... all I just said might be somewhat untrue or different in that case. The easiest way is to mount an SD card FAT32 formatted, without the script as normal, and then check the "mount" output and reproduce that.

I have used my script with several phones without initial issues, but it seems from your mount command that your phone has a lot of different stuff going on, and you have to rewrite your mountsd script accordingly.

There also might be an additional issue, since the ntfs and extFAT binaries supplied are compiled with fuse support (user space mounts), but you are trying to mount ext4 with the system mount command that's not working via fuse ... I think though, all you need to do then is to create those 3 bind mounts in the correct path and have the permissions on your ext4 set to the android user correctly. This is assuming that your phone uses the normal selinux/fuse methods to mount stuff. Ultimately it is much safer in your case to check what it does via FAT32 card first, and then try to do the same with extFAT or NTFS.

Also please use mount.ntfs, and not mount -t ntfs, as the former calls the custom binary but the latter calls the stock mount command.

Why does the APK not work, is the API version too high? You can compile it yourself with Android studio. I will check if I can compile it for lower API version.

BoostUpStation commented 1 year ago

I am using android 12. Have a look at the screenshot I am sharing. Now I'll try with first fat32 and then exfat. Let me know what else I have to do with fat32 before moving to exfat. So that we have everything we want for exfat.Screenshot_20231109-222754~2.png

ballerburg9005 commented 1 year ago

The Vold in Android 12 does support exFAT if I remember correctly, but you still need to supply the mount.exfat binary if the vendor did not. You might be able to do all this via Magisk module, so you don't need to do this hack, just load the module, insert SD card and it mounts.

Check out this for example: https://xdaforums.com/t/mod-magisk-android-12-vold-exfat-otg-support-for-pixel-6-pro.4401527/

It only works for exFAT though, there never has been an option for anything else like ext4 or NTFS.

I am not sure if this is possible on early Android 12 versions, but its worth a shot.

ballerburg9005 commented 1 year ago

If you still want to do this hack like before, then all you need to do is to:

  1. copy the output of "mount" to a text file
  2. insert a FAT32 formatted SD card and again copy the output of "mount"
  3. diff the two files

It should then show you the exact options how the SD card was mounted and which mount points were created. As mentioned before, this should consist of a particular command to mount the block device via fuse context, and 3 write/read/default bind mounts. But maybe there is more you have to bind-mount.

BoostUpStation commented 1 year ago

The Vold in Android 12 does support exFAT if I remember correctly, but you still need to supply the mount.exfat binary if the vendor did not. You might be able to do all this via Magisk module, so you don't need to do this hack, just load the module, insert SD card and it mounts.

Check out this for example: https://xdaforums.com/t/mod-magisk-android-12-vold-exfat-otg-support-for-pixel-6-pro.4401527/

It only works for exFAT though, there never has been an option for anything else like ext4 or NTFS.

I am not sure if this is possible on early Android 12 versions, but its worth a shot.

this showing same error of "Tap to Fix" with both otg and inside. for otg we can use paragon app from playstore though. but inside is better. which paragon doesn't provide, if they do, would have been better.

BoostUpStation commented 1 year ago

So here is the difference of fat32, please help me with doing same with exfat. Thank you.

EDIT:- (proper formatting)


Output of blkid (exfat):-
/dev/block/mmcblk0p1: LABEL="Inch" UUID="C85C-9F1E" TYPE="exfat" 

Output of blkid (fat32):-
/dev/block/mmcblk0p1: UUID="381B-1718" TYPE="vfat" 

Output of Mount in fat32:-
/dev/block/vold/public:179,1 on /mnt/media_rw/381B-1718 type vfat (rw,dirsync,nosuid,nodev,noexec,noatime,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)
/dev/fuse on /storage/381B-1718 type fuse (rw,lazytime,nosuid,nodev,noexec,noatime,user_id=0,group_id=0,allow_other)
/dev/fuse on /mnt/user/0/381B-1718 type fuse (rw,lazytime,nosuid,nodev,noexec,noatime,user_id=0,group_id=0,allow_other)
/dev/fuse on /mnt/installer/0/381B-1718 type fuse (rw,lazytime,nosuid,nodev,noexec,noatime,user_id=0,group_id=0,allow_other)
/dev/fuse on /mnt/androidwritable/0/381B-1718 type fuse (rw,lazytime,nosuid,nodev,noexec,noatime,user_id=0,group_id=0,allow_other)
/dev/block/vold/public:179,1 on /mnt/pass_through/0/381B-1718 type vfat (rw,dirsync,nosuid,nodev,noexec,noatime,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)

Output of df in fat32:-
Filesystem                   1K-blocks     Used Available Use% Mounted on
/dev/block/vold/public:179,1 500571744     1088 500570656   1% /mnt/media_rw/381B-1718
/dev/fuse                    500571744     1088 500570656   1% /storage/381B-1718
ballerburg9005 commented 12 months ago

It should be as simple as this ...

  1. mount the card and change permissions:
mount -o rw -t ext4 /dev/block/mmcblk0p1 /sdcard/_SDCARD
chmod 777 /sdcard/_SDCARD -R

Replicate the bind mounts for files to show up in apps:

mount -o bind /sdcard/_SDCARD /mnt/installer/0/emulated/0/_SDCARD
mount -o bind /sdcard/_SDCARD /mnt/androidwritable/0/emulated/0/_SDCARD
mount -o bind /sdcard/_SDCARD /mnt/user/0/emulated/0/_SDCARD
mount -o bind /sdcard/_SDCARD /mnt/pass_through/0/emulated/0/_SDCARD

Not sure if it will do the trick.

If you want to mount ntfs or exFAT you still have to use the supplied binaries like so:

mount.ntfs -o rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,umask=0000,context=u:object_r:fuse:s0 /dev/block/mmcblk0p1 /sdcard/_SDCARD
BoostUpStation commented 11 months ago

I have an update. the ext4 sdcard got mounted, but with mixplorer only able to read and delete, can't paste(only 1 file at a time). With Total Commander and default android file managers works fine.

And it only gets mounted from linux laptop via adb shell and su. What to do to make it work via termux? or any other commandline app for android?

ballerburg9005 commented 11 months ago

The APK of this repository is an App, which executes su mountsd when you launch it. Thus you only have to start the app when you want the card to be mounted.

I don't know why the other apps are not working for you, but there should be no such issues when you mounted the card and have the bind mounts as explained. It should be identical to writing to the internal storage.

It could be that you are missing some bind mounts?

BoostUpStation commented 11 months ago

Bind mounts are all there. Used all which were there when mounting fat32. And none from the termux commands and your release apk works. Only mounting via laptop is working :(

ballerburg9005 commented 11 months ago

I can only guess that the PATH variable is set differently in your shell. Check it with echo $PATH, and use the binaries with absolute path as solution or set the variable to what is working for you before you do your stuff.

Compile the APK yourself with Android Studio (though it should install and open as I compiled it). Then change the mountsd script to do what works for you. Also make sure the APK source does call mountsd with absolute path also.

BoostUpStation commented 11 months ago

PATH in adb shell using linux:- /product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin PATH in android termux :- /sbin:/sbin/su:/su/bin:/su/xbin:/system/bin:/system/xbin

The APK is opening in android, but not mounting the sdcard. Did you set mmcblk1p1 in the script inside apk or mmcblk0p1(this is mine) ? could that be an issue why apk is not mounting the sdcard.?

ballerburg9005 commented 11 months ago

The script checks usb sticks and sdcards and it only works with ntfs or exFAT. Also it doesn't do the bind mounts correctly, as those are weird in your case. You have to change the script accordingly, to add your bind mounts and a line for ext4 mount command that works for you.

ballerburg9005 commented 11 months ago

If the whole app however doesn't launch, then you have to recompile the APK with Android studio.

BoostUpStation commented 11 months ago

I changed the mountsd script same as which works from linux terminal via adb and su. (adding -t ext4 and bind mounts as well). Even tried adding the same PATH variables as in linux adb su prompt. But still its not mounting the sdcard to _SDCARD folder inside internal storage. What could be the reason?

And by the way, its automatically doing the bind mounts when mounting via linux adb su. just have to run mount command with -t ext4 in linux adb su. Any way we can do the same using termux?