Android Run script in ADB shell - android

I am trying to fallow this tutorial in order to install SSL certificate on Android emulator.
I need to start the emulator from command line, so I run
emulator -avd myDevice -http-proxy myIp:8888
After device is started I want to copy my certificate file from PC to the device, so I run those commands
mount -o remount,rw /system
cp /sdcard/5ed36f99.0 /system/etc/security/cacerts/
cd /system/etc/security/cacerts/
chmod 644 5ed36f99.0
I bundle them all together using this suggestion
The final command looks like this:
adb shell su -c 'mount -o remount,rw /system; cp /sdcard/5ed36f99.0 /system/etc/security/cacerts/; cd /system/etc/security/cacerts/; chmod 644 5ed36f99.0'
But I am getting an error:
su: invalid uid/gid '-c'
If I do it from the shell it works, but then when I restart the emulator it restore the system to previous state without saving my changes.
How can I solve those two problems?

Related

Edit marshmallow hosts file

I'm trying to edit the hosts file on the Marshmallow emulator, but I can't do it. I use ADB tool:
I start emulator using emulator.exe
adb mount
adb pull /system/etc/hosts c:\temp
Then I edit hosts file
ADB push c:\temp\hosts /system/etc (this step doesn't work, because I get error - "read-only file system").
Also, I tried to do adb shell and then SU commands and then mount -o rw,remount /system. But every time when I try to edit hosts file, I get the error that file system is read only.
Any ideas?
Try this for the remount:
adb shell
setenforce 0
mount -o rw,remount /system
setenforce 1
These steps work for me (Windows 8.1 and Android Studio emulator (qemu))
Open a Command Prompt
cd %localappdata%\Android\sdk\tools
Run the emulator from the command line with -writable-system flag, e.g.
emulator -avd (your AVD name) -writable-system
While the emulator is booting (wait until the Android splash appears):
cd %localappdata%\Android\sdk\platform-tools
adb root (do not proceed until this command has been successful)
adb remount
adb push c:\temp\hosts /system/etc/hosts

Attempting to gain r/w access to android /system

Working on a root script for the Nexus 4 with the latest stock rom .img for google (occam) and I have the following code snippet:
./adb wait-for-device
echo "remounting system"
./adb shell "mount -o remount,rw /system"
./adb push su /system/bin/
echo "pushing super user"
./adb push Superuser.apk /system/app/
echo "pushing busybox"
./adb push busybox /system/xbin/
./adb shell "chmod 06755 /system/bin/su"
./adb shell "chmod 0644 /system/app/Superuser.apk"
./adb shell "chmod 04755 /system/xbin/busybox"
./adb shell "cd /system/xbin"
./adb shell "busybox --install /system/xbin/"
I keep getting the error
mount: Operation not permitted
failed to copy 'su' to '/system/bin//su': Read-only file system
pushing super user
failed to copy 'Superuser.apk' to '/system/app//Superuser.apk': Read-only file system
pushing busybox
failed to copy 'busybox' to '/system/xbin//busybox': Read-only file system
Unable to chmod /system/bin/su: No such file or directory
Unable to chmod /system/app/Superuser.apk: No such file or directory
Unable to chmod /system/xbin/busybox: No such file or directory
/system/bin/sh: busybox: not found
I've tried using multiple methods of obtaining r/w access, but nothing seems to be working. I have to automate this process due to the fact that other people will use the script so it needs to be automation friendly, but I just can't figure this out.
I've also tried the
#su
#mount
#mount | grep system
followed by inputting the partition with the system mount and changing it to r/w access, but that also hasn't worked.
Really frustrated at this point. Can anyone help?
It gives the error because you aren't root. The system partition is mounted read-only. You can try to push the binary to /data/local/tmp. Then you can make su executable and eventual run it. But it doesn't mean you can have root. To become root you need to push an exploit like psneuter to /data/local/tmp and run it. It crashes the shell and reopen a new one with root rights. Then you can remount the system-partition read-write and install su.
Try the commands below
adb shell "su -c mount -o remount,rw /system"
adb shell "su -c chmod 06755 /system/bin/su"
and so on.

Rooting ICS emulator; pushing su fails with "Out of memory"

I'm trying to root my ICS AVD, and have tried this:
adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
adb push su /system/xbin/su
adb shell chmod 06755 /system
adb shell chmod 06755 /system/xbin/su
It fails with the following error:
failed to copy 'su' to '/system/xbin/su': Out of memory
How can I fix this?
This blog explains the problem:
To avoid the "Out of memory" error when trying to copy the su-executable to /system/bin, you need to start the emulator manually with a large –partion-size argument:
$ emulator -avd MYNAME -partition-size 300
Then:
$ adb remount
$ adb push su /system/bin/su
$ adb shell chmod 06755 /system/bin/su
Note that MYNAME above is the name of the emulator. Also note that if you are using snapshots, you will need to start the emulator without it due to the change in hardware configuration. Append -no-snapshot-load to do so.
With this you'll have 512MB of size on /system
ICS is the name I gave to my android emulator:
android-sdk-linux/tools$ emulator -avd ICS -partition-size 512

Executing shell commands programmatically

I want to execute this shell commands by program. How can I do it?
cd C:\android-sdk\platform-tools
adb shell
su
mount -t rfs -o remount,rw /dev/block/stl9 /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
We can execute shell comands by using Runtime class.
Runtime.getRuntime().exec("ls");
The above piece of code will create a native process for given command ls, will return same process as a Process object.
For more details about it Check here
You Should write the exact syntax you used here in a .bat file, and then just execute it.
It seems you are on a Microsoft station so considering using batch would give you this :
1st method : Stay on your station and send usefull commands
cd C:\android-sdk\platform-tools
adb shell "su -c 'mount -o rw,remount /system'"
adb shell "su -c 'cp /sdcard/MyApp.apk /system/app/MyApp.apk'"
adb shell "su -c 'mount -o ro,remount /system'"
The only thing is you will launch and close 3 shells but its not really and issue.
2nd method : Stay on your station send a sh script on sdcard and execute it
cd C:\android-sdk\platform-tools
adb push myscript.sh /sdcard/
adb shell "su -c 'sh /sdcard/myscript.sh'"
with "myscript.sh" containing :
#!/system/bin/sh
mount -o rw,remount /system
cp /sdcard/MyApp.apk /system/app/MyApp.apk
mount -o ro,remount /system
Remember that Android shell scripts created on Microsoft station have CRLF line ending !
You need to get LF only ending your lines on UNIX like systems !

Android emulator error: 'Not enough memory in emulator'

I'm trying to install rsyncdroid app in my emulator using the following steps:
$ ./adb push rsync /sdcard/rsync
$ ./adb shell mount -o remount,rw /system
$ ./adb shell "cat /sdcard/rsync > /system/bin/rsync"
$ ./adb shell chmod 755 /system/bin/rsync
$ ./adb shell mount -o remount,ro /system
I'm getting stuck on step 2, which results in the error:
cannot create /system/bin/rsync : not enough memory in emulator
The System partition of the standard avd's have not much free space. You can start the emulator on the command line, adding the following parameter:
-partition-size <size>
alternatively you can also declare the partition size in the command line options of your android run/debug configuration in eclipse.
Have you created new AVD ?? Use SDKManager to create new AVD. and assign 1000 MB to SDCard

Categories

Resources