28

I know this is a recurrent question when working with the Android emulator, but is there a way to force the emulator to accept persistent changes to /system?

The emulator is based on QEMU, so it should be possible, in theory, to force the system image to behave the same way userdata (for instance) does, but I'm not familiar with how QEMU handles things. Any pointers?

4
  • Actually, even the local system image (in my AVD directory) is not persisted in the most recent versions of the emulator... Unless I did something wrong?
    – F.X.
    Mar 14, 2013 at 18:27
  • See Yury's answer about that, then maybe mine for an alternative way (found this just yesterday). You can use adb remount to make changes to /system, but they are not persisted if you restart the emulator, which is what I need.
    – F.X.
    Mar 15, 2013 at 7:28
  • 1
    Thanks! The blog where I originally found a solution was shut down recently. I definitely needed a new reference. ;) Mar 15, 2013 at 7:59
  • see just this post stackoverflow.com/a/58935109/4797289 Nov 19, 2019 at 13:22

10 Answers 10

17

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)

5
  • 1
    Thanks for keeping this up to date :)
    – F.X.
    Apr 12, 2017 at 18:29
  • 1
    the files remain changed, but the root is not active. Do you have a solution for permanent root with latest build-tools? Aug 30, 2017 at 9:34
  • -wipe-data invalid command, also how to resize system partition? -partition-size also doesn't work, says invalid command (removed?)
    – user25
    Jun 10, 2018 at 9:37
  • This works, but note that it writes to a separated qcow2 file instead of to the .img directly: android.stackexchange.com/questions/110927/… Jan 31, 2019 at 15:08
  • 5
    important note: you need to keep using -writable-system to see your previous persistent changes. omitting -writable-system will use the default system image, without your changes. Aug 19, 2019 at 7:27
15

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.

6
  • 1
    Hey man, this was a really amazing tip! Didn't knew at all, that it's possible to start the emulator in a read/write mode. Until now I've tried around with yaff2 to dynamically create images, but your answer made things much more easier. I'm using your mechanism now in the latest version of my Scala Libs for android installation script. Thanks a lot.
    – sven
    Mar 22, 2013 at 23:05
  • Thanks! It's too bad the Android team didn't add an option for that, instead of having to do some crazy stuff with obscure QEMU options... (By the way, I set the accepted answer to mine since we're using it in the latest sbt-android plugin, it works really well for setting up the emulator in one easy step!)
    – F.X.
    Mar 23, 2013 at 16:14
  • 1
    qemu-system-x86_64: -nand: invalid option — that means they removed support for doing that? Jul 21, 2016 at 4:24
  • where did you find those commands? system,size=0x1f400000 I tried to change system partition size using -partition-size command from developer.android.com/studio/run/… but it doesn't work anymore (and they don't update information on their site)...
    – user25
    Jun 10, 2018 at 9:25
  • and this value in bytes? 0x1f400000, 524288000 == 500mb, right?
    – user25
    Jun 10, 2018 at 9:29
8

It's actually a very good question. I had the same troubles. Here are my findings. Actually, I use Ubuntu and I'll describe the steps for Ubuntu. If you use Windows, you should just change the paths.

  1. Create new AVD, for instance example.avd
  2. Copy system.img from android-sdk-linux/platforms/android-10/images to ~/.android/avd/example.avd
  3. Make system.img as writable and readable (either in the properties or simply using terminal)
  4. Run your AVD using command emulator -avd example
  5. Remount your system as rw using adb shell mount -o rw,remount -t yaffs2 /dev/block/mtd0 /system (to discover the partition use command cat /proc/mtd)
  6. Make your changes...
  7. Now during the run of emulator find tmp emulator in /tmp/android-<your_computer_name> with strange name like: emulator-PQ7LgE and copy it in ~/.android/avd/example.avd
  8. Delete system.img and rename copied tmp emulator into system.img
  9. Close emulator
  10. Delete cache.img, userdata.img and userdata-qemu.img from ~/.android/avd/example.avd
  11. Run your emulator once again

Good luck!

2
  • Thanks for the detailed answer, now I don't know which one to choose from ;)
    – F.X.
    Mar 15, 2013 at 8:05
  • -1 'Make system.img as writable and readable (either in the properties or simply using terminal)' more details are needed ... Feb 19, 2014 at 21:01
2

The solution #2 is amazing.

Here are some hints if you are using MS Win as host AVD directories are located as below, but the "short" path names should be used as file= path parameter. The quoted path variant doesn't work for some reason.

Win XP: C:\Documents and Settings\ (username) \ .android\avd\ ... Short C:\DOCUME~1\ (username) \ANDROI~1\avd\ ...

Win 7 C:\Users\ (username) \ .android\avd\ ...

You can create an own bat file, say "startrw.bat" as per following example:

@echo off
C:\<ADTFOLDER>\sdk\tools\emulator -avd <AVDNAME> -qemu -nand system,size=0x1f400000,file=C:\DOCUME~1\<USERNAME>\ANDROI~1\avd\<AVDNAME>.AVD\system.img
cd C:\<ADTFOLDER>\sdk\platform-tools
echo .
echo Wait ... 
echo .
echo When emulator is FULLY loaded press any key to connect adb shell to it
echo To make /system writeable type in adb shell:
echo .
echo -----------------------------
echo mount -o rw,remount /system 
echo -----------------------------
echo .
echo You can use the Eclipse ADT DDMS File Browser to browse or push-pull files now.
echo .
echo Closing this window closes the emulator ! 
echo .
echo Wait emulator to load Android. When done
pause
C:\<ADTFOLDER>\sdk\platform-tools\adb shell

This way you can load in one click. Once finished modifying, just close the current command window to kill the emulator.

It takes long time for most of things to load like Emulator, the ADT editor, Hooking DDMS file browser (you need to click on the emulator line left side to see the files tree on right window) and so on.

0
2

I had trouble finding the system.img file and AVD directory that F.X. posted about. On Mac you can find them here:

  • system.img: ~/Library/Android/sdk/system-images/android-19/default/
  • AVD Directory: ~/.android/avd/Nexus.avd/

I don't have enough reputation to add it as a comment, so I had to post it like this. Sorry.

1

use this command in terminal

cd /Users/NAME_USER/Library/Android/sdk/emulator # Move to emulator folder
sudo ./emulator -writable-system -avd NAME_AVD -partition-size 280 # Run avd by writable system 
sudo adb root # Change to root
sudo adb remount # remount again
sudo adb shell 
su
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system 

change NAME_USER and NAME_AVD

1
  • Does not work. adbd cannot run as root in production builds.
    – e-info128
    Aug 18, 2021 at 23:58
0

F.X.'s answer does not work for me with Android API 19 and 21 on Windows as it stands. With the given parameters

emulator -avd <AVD> -qemu -nand system,size=0x<SIZE IN HEX>,file=<PATH/TO/system.img>

the system fails to mount the image. By looking at the kernel output I found out that it uses suspicious page size (2048) and two nonzero parameter values: extra size and "erase". These parameters are not documented anywhere, but you can see them in qemu's source code. What ended up working for me was

emulator -avd <AVD> -qemu -nand system,size=0x<SIZE IN HEX>,file=<PATH/TO/system.img>,pagesize=512,extrasize=0

With those parameters specified, "erase" gets set to 0 as well.

I was also surprised to find out that with these parameters the system is unable to boot to the graphical environment, but adb works.

2
  • where did you find those commands (like system,size) where is it documented?
    – user25
    Jun 10, 2018 at 9:27
  • value should be in bytes or megabytes?
    – user25
    Jun 10, 2018 at 9:28
0

The best way to do this is to use the following command:

<path-to-emulator-executable> -avd <AVD> -partition-size 512
1
  • you may need to do ./emulator or emulator-x86. Basically replace emulator with the path to your emulator executable file.
    – user6754053
    Jun 11, 2018 at 12:55
0

As said here at https://justus.berlin/2015/02/make-persistent-changes-to-system-in-android-emulator/ Copying system.img into avd folder will work.

-1

New year new solutions.

Just copy the system.img to your avd folder and rename the system.img to system-qemu.img

For me, copying C:\Android\sdk\system-images\android-22\android-tv\armeabi-v7a\system.img to C:\Users\Neil Agarwal\.android\avd\Android_TV_1080p_API_22.avd\ and renaming it to system-qemu.img did the trick.

Please restart your avd after this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.