Universal System Mount on Android
I've written an app that modifies file system contents. One of the requirements to do this is mounting the system as read-write. From here and other sources, I've found the widely accepted command is this:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
I'm hoping someone can better explain how this works. I understood it to mean that we're working with a yaffs2 file system and going to mount the directory /dev/block/mtdblock3 as /system. When I issue the mount command on my phone though, /system is listed as ext3, not yaffs2 (if I substitute ext3 for yaffs2 the command works equally well). Also, /dev/block/mtdblock3 is not an existing direc开发者_JAVA技巧tory.
This may be more of a Linux question than Android, but I'm sure someone knows all about this. Most importantly, how universal is this command? I'm planning on releasing this to a huge variety of devices, do I need to accomodate other file systems? Or will this "just work"?
I use this in one of my apps to make it as universal as possible. Of course it needs a rooted phone in order to work.
FileInputStream inputStream;
BufferedReader r;
String line, device = "", fs = "";
try {
inputStream = new FileInputStream("/proc/mounts");
} catch (FileNotFoundException e) {
Log.e("Could not read mounts");
return;
}
r = new BufferedReader(new InputStreamReader(inputStream));
//find device mounted as system and file system type
try {
while ((line = r.readLine()) != null) {
if(line.indexOf("/system") >= 0) {
String mount[] = line.split(" ");
device = mount[0];
fs = mount[2];
break;
}
}
inputStream.close();
} catch (IOException e) {
Log.e("Could not read mounts");
return;
}
//mount system as rw
CommandCapture command = new CommandCapture(0, "mount -o rw,remount -t " + fs + " " + device + " /system");
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (Exception e) {
Log.e("Couldn't mount system as rw!");
return;
}
It uses RootTools for convenience on shell commands and assumes the user already granted root access to your app.
The location of the device corresponding to the /system
partition is device-specific, as is the filesystem in use; there is no single standard command. Moreover, there is no guarantee that the filesystem will be mutable at all; it's perfectly possible for the root fs to be on cramfs, or on some type of flash that's not easily randomly overwritten. For some phones it may be enough to check /proc/mounts
for the actual device and fs-type corresponding to /system
; for others you may not be able to remount it read-write.
Try this:
grep " /system " /proc/mounts | awk '{system("mount -o rw,remount -t "$3" "$1" "$2)}'
Remember your android device must be rooted and busybox must be installed.
Chat again
精彩评论