Knowledge.ToString()

Tutorial: How to Make a Change Permanent in “system” Folder for Android Emulator (yaffs2/ext4)?

In my android emulator, couple of fonts were missing and wanted to add these fonts and retain the change. I had a difficulty retaining the change in the emulator. Finally with lots of trial and error, I found a working instruction set.

Following ideas did not work for me

Audience

A programmer with a basic understanding of Android Studio, SDK Manager, AVD Manager, Android Device Monitor, Android Emulator, pushing/pulling a file to/from emulator using Android Device Monitor/adb command

Development Environment

  • Windows 7 (/Linux/Mac)
  • Android Studio 1.2.2
  • Android SDK with emulator
  • Virtual Device/Emulator (1 GB SD card setup is needed for this solution)

Path

  • Android Studio – “C:\Program Files\Android\Android Studio”
  • SDK Manager – Android Studio > Tools > Android > SDK Manager
  • AVD Manager – Android Studio > Tools > Android > AVD Manager
  • Android Device Monitor – Android Studio > Tools > Android > Android Device Monitor
  • AVD Location – C:\Users\YOUR_USERNAME\.android\avd\AVD_NAME
  • Emulator Base Image Location (path may change based on SDK API and processor type for emulator) – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\system-images\android-22\default\armeabi-v7a\
  • Virtual Device Temporary File – C:\Users\YOUR_USERNAME\AppData\Local\Temp\AndroidEmulator\RANDOM_FILE_NAME.tmp
  • Emulator.exe – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\tools
  • adb.exe – C:\Users\YOUR_USERNAME\AppData\Local\Android\sdk\platform-tools

How Android Emulator Works

When we create virtual device using AVD Manager, it copies certain files EXCEPT “system.img” from Emulator Base Image Location to AVD Location. When the emulator is started from command prompt (Windows) or using AVD manager, it will get the “system.img” from Emulator Base Image Location and rest of the files like “cache.img”, “sdcard.img”, “userdata.img” and “userdata-qemu.img”.

If the “system.img” file exists at AVD Location, that file will be used rather than “system.img” file available at Emulator Base Image Location.

Emulator combines all “*.img” file and creates a Virtual Device Temporary File and work off of it. When you close the emulator, this file will be deleted.

Any changes to “cache.img”, “sdcard.img”, “userdata.img” and “userdata-qemu.img” will be stored but any changes to “system.img” will not be stored when the emulator is closed.

Goal

Once we make the change in “system.img” (i.e. /system folder) within emulator, extract the “system.img” out of emulator, and place it under AVD Location. This way, all our changes will be stored.

Android Emulator File System

Android Emulator comes in two different file system flavor.

  • yaffs2
  • ext4

Each “*.img” belongs to one of these file systems and both are not compatible with each other. Before we begin anything, we need to find out the file system that the Virtual Device uses.

How to Find Virtual Device File System

Run Virtual Device using command prompt (Windows) only and not using AVD Manager. Replace AVD_NAME with your virtual device name in the following command

emulator -avd AVD_NAME

Do not close command prompt. Once the virtual device is up and ready, issue following command

adb shell mount

In the output, locate the text similar to “/dev/block/mtdblock0 /system” and the next word (yaffs2/ext4) notifies the file system.

Now once you know the file system, you need to download following additional tools in your computer

Close/Exit virtual device. (Don’t keep it running and go to next step otherwise next steps will not work)

For File System YAFFS2

Download “mkfs.yaffs2.arm” (or mkfs.yaffs2.x86 if you are using x86 processor for your Virtual Device) from https://code.google.com/p/android-group-korea/downloads/list in your computer

Rename “mkfs.yaffs2.arm” to “mkfs.yaffs2”

For File System EXT4

Download “make_ext4fs” from http://web.djodjo.org/article/download/android/tools/arm/alltools in your computer

Download “EXT4 Unpacker” from http://sourceforge.net/projects/androidicsjbext/ (This tools may work ICS onwards only but I have not tested for older Android version. It is working for Lollipop)

Make Changes

In order to run Virtual Device, open a command prompt (Windows) and issue following command. Please note that you have to use command prompt (Windows) only and cannot use AVD Manager to run it. You must explicitly provide the -partition-size parameter

emulator.exe -avd AVD_NAME -partition-size 300 -no-snapshot-load

Now you are ready to make changes in /system folder which is nothing but “system.img” mounted as /system folder. You can view whole Android file structure using Android Device Monitor > File Explorer tab. As of now, whole android device is readonly. Using this File Explorer, you can pull the file to your computer but you cannot push the file from your computer to virtual device. (You will get an error in the console log of Android Device Monitor if you try it). So we are making the “/system” folder as readwrite. In the same command prompt (Windows), issue following command

adb remount

Run following commands to give certain folders full permission

adb shell chmod 777 /system
adb shell chmod 777 /system/*
adb shell chmod 777 /sdcard
adb shell chmod 777 /sdcard/*

At this moment, we are ready to make any change. We may copy the fonts/apks/binaries and do whatever we want with our Virtual Device. Once you are done, proceed to the next steps

If file system is YAFFS2

Push the file “mkfs.yaffs2” from your computer to “/system/bin” using Android Device Manager > File Explorer.

On the command prompt (Windows), issue following command to generate “system-new.img” out of modified “/system” folder. This command will create “system-new.img” file

adb shell mkfs.yaffs2 /system /sdcard/system-new.img

Let the command complete. It may take several minutes.

Pull the file “/sdcard/system-new.img” from Virtual Device using Android Device Manager > File Explorer to your computer at AVD Location

Rename “system-new.img” to “system.img” at AVD Location

If file system is EXT4

Push the file “make_ext4fs” from your computer to “/system/bin” using Android Device Manager > File Explorer

On the command prompt (Windows), issue following command to generate “system-new.img” out of modified “/system” folder. This command will create “system-new.img” file

adb shell make_ext4fs -s -l 512M -a system sdcard/system-new.img system/

Let the command complete. It may take several minutes.

Pull the file “/sdcard/system-new.img” from Virtual Device using Android Device Manager > File Explorer to your computer at AVD Location

Open the file “system-new.img” in the program “EXT4 Unpacker” that you downloaded.

Click on the button “Save as EXT4” which will ask your for location and file name. Provide AVD Location and give the file name “system.img”

Close “EXT4 Unpacker”

Delete file “system-new.img” from AVD Location

Close/Exit/Turn off Virtual Device

You are all set and your changes are now permanent. It is always a good idea to take a backup of “system.img” in case you want to delete the Virtual Device which will save you from hassle of going through these steps.

Share

Comments

6 responses to “Tutorial: How to Make a Change Permanent in “system” Folder for Android Emulator (yaffs2/ext4)?”

  1. Sulhan Avatar
    Sulhan

    This is what I got

    C:\Users\Sulhan>adb remount
    remount succeeded

    C:\Users\Sulhan>adb shell chmod 777 /system
    – exec ‘/system/bin/sh’ failed: Read-only file system (30) –

    How to fix?

    1. Vishal Monpara Avatar
      Vishal Monpara

      Hello Sulhan,

      Quick search showed the link https://android.stackexchange.com/questions/162902/adb-push-not-working-on-system-read-only Try the solution and check if it works.

      Regards,
      Vishal Monpara

  2. PizzaG Avatar
    PizzaG

    I am Trying to do this on Remix OS player and having a heck of a time. Was hoping someone would be able to help me out. Upon boot of Android 6.0.1 Emulator the system is reporting it’s RW enabled, but as soon as I try to write anything to it, it fails and reverts to RO until I reboot. The funny thing is that The root file system remains RW and I can do what I’d like with it. I found files in there related to ro,barrier=1, system ro and I have edited and rechecked the files multiple times before rebooting and the changes stick. Upon reboot the files are back to what they were. I’m stuck on your guide at this particular line: emulator.exe -avd AVD_NAME -partition-size 300 -no-snapshot-load I cannot for the life of me get it to go through. What makes it harder is that Remix OS Player is set up a bit different and file locations are weird. I have a Remix_OS.avd folder located inside the avd folder and a Remix_OS.ini file that I changed to reflect the REMIX_OS.avd folder. Any help would be greatly appreciated, thanks

    1. Vishal Avatar
      Vishal

      Unfortunately I am not aware of solution.

  3. Peng Avatar
    Peng

    thanks for the share.

    I guess that you pushed mkfs.yaffs2 down to the /system/bin/. And change it to be executable.

    1. Vishal Avatar
      Vishal

      Hi Peng,

      You are right about pushing the file to /system/bin/. I have updated my post accordingly for YAFFS2 and EXT4.

Leave a Reply

Your email address will not be published. Required fields are marked *