Programmatically mounting USB storage on android 4.2.1 - android

I've search and searched here, and no topics come close to answering the question.
Mounting and unmounting a USB stick from within a rooted APK. I've been successful in doing it from the command line via adb as follows:
prompt>> mount -t vfat -o rw /dev/block/sda1 /sdcard/usb
After this command, I can "cd /sdcard/usb" and can see the contents of the USB stick.
If I try this in code using the Process class, I can't see anything there from the command line in adb, a file explorer on the device, etc:
proc = Runtime.getRuntime().exec(new String[]{"/system/xbin/su", "-c", "mount -t vfat -o rw /dev/block/sdb1 /sdcard/usb"});
proc.waitFor();
This is a sandbox problem. It's driving me nuts. Here's what I think is going on, and I have no idea how to solve it: When the Process class invokes su, it does so in a completely new userspace -- it's own sandbox. The mount succeeds (I can see that from some debugging), then the process dies and returns to the app, which is in a different sandbox. Because of that, not only can I not see the mount, it unmounted with the su process going away.
I need to be able to mount a USB stick from my application, read/write to a file, then unmount it before it is removed (otherwise risk data corruption).
I've looked and looked for an android or java interface to the Linux mount(2) and umount(2) commands and have come up empty. There must be some way to do this!!

you must read that carefully. mounting and unmounting should be done using that
http://developer.android.com/guide/topics/connectivity/usb/host.html

Related

Which Android Emulator image do contain 'su'?

How to know which Emulator image will contain su?
I can see that often it only contains su or google play
Run adb root and you get a root shell ... otherwise su needs to be manually installed.
However, when already having a root shell available, installing it isn't much of a problem.
All images are rooted, but SDK apps requesting escalation of privileges do rely upon su.
This question here generally duplicates: How to get root access on Android emulator?
adb shell scripting must:
start the emulator
run adb root
run adb shell
remount system partition
adb push the su binary
chmod to set permissions
exit
Or to answer the question:
start the emulator
and check if the file exists, eg. with adb shell stat /usr/bin/su
Where stat gives this response, when it's not installed:
stat: '/usr/bin/su': No such file or directory
One could even loop all Android images installed in $ANDROID_SDK_HOME...
there's no "one click" solution, but adb can be fully automated with Bash or Batch. And one wouldn't even have to run the emulator, but can mount QCOW2 as a nbd network block device.

Phone is rooted but can't pull files from /data/data folder

My phone Samsung Galaxy S5 mini is rooted. I'm trying to pull files from /data/data/myapp.package/ to folder on my PC.
adb pull /data/data/myapp.package E:\myapp\myapp.package
it gives me this error
adb: error: failed to copy '/data/data/myapp.package' to 'E:\myapp\myapp.package': Permission denied
I found many questions like mine but no answer solved my problem. Some suggested to execute this command adb root before pulling files. Some suggested to install adbd insecure app to enable root access. In fact after installing that app, phone disappeared from adb terminal. Both solution didn't work for me.
BTW, I can copy files using cp command from adb shell but I have to copy files to sdcard and then pull from sdcard. I'm looking for solution which allows me to copy files directly from /data/data/myapp.package to my PC
Any solution?
For your adb to be able to access /data/data directly (for adb pull), your adbd should be running as root - which can generally be done by adb root command.
adb root would not work on commercial devices like Samsung Galaxy S5 mini as commercial devices have ro.secure=1, i.e., the adbd can't be restarted as root due to a check of property called ro.secure. adbd insecure app circumvents this and restarts adbd in root mode to enable adb pull, etc. to work.
In short, if adbd insecure app doesn't work for you, it's not possible to do adb pull from /data/data in your existing ROM. It might be possible if you change the ROM / do some boot.img tweaks, but I would probably suggest trying latest version / different versions of adbd insecure app before going for ROM changes.
Read more on rooting here.
First you need to hit these two command from command line
adb root
adb remount
then
adb pull /data/data/myapp.package E:\myapp\myapp.package
This is my example pulling DB file from the root directory
adb -e shell "run-as com.example.project cp /data/data/com.example.project/databases/project.db /sdcard"
The key is run-as
Here's a one-liner that lets you pull a file without installing anything else and without having to copy it to a public location on the device to then pull it to your computer:
adb exec-out su -c cat /data/data/myapp.package/my_file.apk > my_file.apk
What this does:
adb exec-out runs a command and outputs the raw binary output
su -c runs the provided command as root
cat <file> prints out the file contents
> <file> redirects the output from adb (i.e. the raw file contents) to a local file.

Fuse on android

I'm trying to use FUSE to mount a virtual FS on a rooted android.
I've compiled a simple program that mounts a readonly virtual FS containing a single file (hello) containing the contents Hello world.
After compiling the program, I ran (as root)
mkdir /mnt/test
chmod 777 /mnt/test
a.out -d -oallow_other mnt/test
When I run ls /mnt/test as a root user, everything works as expected.
When I run ls /mnt/test as any other user, or from any other application (explorer / adb shell), I receive the contents of the underlying filesystem (in this case, the root filesystem) instead of the output from the FUSE fs.
After digging into it a little, I found out that the FUSE mounts don't show up when running cat /proc/mounts as a user, but it does show up as root. Does android (or maybe even linux ?) have special facility for hiding mountpoints to specific users ?
Solution:
I decided to run my program as daemon in initrc, and it's work well
on post-fs-data
start hello
service hello /sbin/hello
seclabel u:r:su:s0

Android - Mount a Samba share in code

I'm looking for a way to mount Samaba shares programatically.
I am aware of this question which, if it works, would allow browsing a Samba share within my app, but I want the shares to be available to other apps once mounted.
I know ES File Explorer can do this, so it must be possible to implement in code, but I'm trying to figure out how it's done. The only method I can think of is opening a Process with Runtime.getRuntime().exec(...) but then there's the issue of what commands to send. I've been playing about in a terminal emulator app and the standard mount command doesn't seem to be working. I've tried the following:
mount -t smbfs //[ipaddress] /mnt/sdcard/net/Share1
and
mount -t cifs //[ipaddress] /mnt/sdcard/net/Share1
but in both cases I'm getting the rather uninformative error message "mount: no such device"
Where could I be going wrong?
Probably smbfs/cifs are currently not supported by your kernel. As Delyan said, ensure your kernel can mount these filesystem :
$ cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev cgroup
nodev tmpfs
nodev debugfs
nodev sockfs
nodev usbfs
....
If they are not listed, you should try to do a modprobing (sometimes the module you want just have to be activated), get root access then :
# modprobe <modulename(without.ko)>
e.g. :
# modprobe cifs
If it doesn't work you will have to change or recompile your kernel (including appropriate modules).
I ran into the exact same problem. Cifs manager was working, but the command from the terminal was not. For me anyway, it turned out that I just had to modify the command slightly and it worked. Try the following command:
mount -o username=guest,password=guest -t cifs //[ipaddress]/[share] /sdcard/cifs/nas
Make sure that the local folder /sdcard/cifs/nas (or your desired equivalent) exists before running the command or you might get a "file or directory doesn't exist" error.

android : unable to run shell script(.sh) from my application

i am not able to run shell scripts from my application.only "ls -l" and echo commands respond.rest dont seem to do anything.not even "touch" command works.i tried creating a file in sdcard using touch command in my script..only the echo command worked.my shell script is in sdcard too
The sdcard is mounted with noexec. You will have to copy the scripts to the app's local data drive to execute.
You should be able to see the mounted permissions of all file-systems by logging into the phone (adb shell or whatever else) and typing mount
The rest of the commands like "cd" etc doesnot work in Android as it encounters lot of permission issues.
So the best solution is to trigger "script manager" or "terminal emulator" apps already available through your app.

Categories

Resources