Forcing the Android emulator to store changes to /system

Update: The -nand option was apparently removed from the most recent versions of QEMU, so the following may not work anymore.

Update: See the accepted answer for the current way of doing things.


Yury's answer is the common one, but if you want to be able to script things (which is what I want in the end), you need to be able to find the emulator image in the /tmp directory.

I discovered that you can override QEMU's behavior. This is a bit hackish, but it works, so I ended up doing this :

  1. Copy system.img from the platform directory to your AVD directory.
  2. Convert the amount of space you need to hex. For instance, if you need 500M, this is 0x1f400000.
  3. Run the emulator in read-write mode :

    emulator -avd Galaxy_Nexus -qemu -nand system,size=0x1f400000,file=/home/fx/.android/avd/Galaxy_Nexus/system.img
    

    (If you put the emulator in verbose mode, you'll notice that it will, by default, use initfile= instead of just file=)

  4. Make your changes, they are immediately saved to the local system.img

  5. Now you can just start the emulator with emulator -avd Galaxy_Nexus, and it'll use your changes

Update: If scripting, QEMU does not sync changes immediately to the image, so if you're rebooting immediately after changing something, chances are you will lose data. I'm looking for a way around this...

Next update: Use adb -e emu kill to stop the emulator. reboot will just do bad things.


At the moment (with Build Tools v26, if Google doesn't change things as often as they do) if you use the -writable-system directive while booting up the emulator from the command line, it will allow persistence to the /system partition through reboots. That is you will be able to write to the /system partition and if you reboot, the changes will still be maintained.

emulator -avd <AVD_NAME> -writable-system

This will also persist your changes to a qcow2 image file usually in .android/avd/<AVD_NAME>.avd/system.img.qcow2

You can even copy this system.img.qcow2 file, wipe the data off the AVD using the -wipe-data directive, place this file back into the directory, reboot and the system changes you made initially will still be persisted. (Caveat: at least for now, coz Google keeps changing things)