Search This Blog

07 July 2011

Altering system.img, userdata.img and persist.img

If you have started building the Android source tree yourself, you have undoubtedly wondered how to modify the image files.  In this post, we'll look specifically at system.img.  This is based on the work found here.

Let's pretend our directories are:
~/work/android: directory where you did your git, envbuild, lunch/choosecombo from.
~/work/tmp: empty directory for us to play in
~/bin: directory on your path

If you have already done a build, you will find out/target/product/generic/system.img.  This is a YAFFS2 file.

Preparation

In my case, I did not have a way to mount yaffs2 filesystems.  I am sure I could have done some insmods and such; but instead, I chose to just compile this unyaffs.

~/work/tmp$ wget http://unyaffs.googlecode.com/files/unyaffs.c
~/work/tmp$ wget http://unyaffs.googlecode.com/files/unyaffs.h
~/work/tmp$ gcc -o unyaffs unyaffs.c
~/work/tmp$ cp unyaffs ~/bin
~/work/tmp$ rm *




Extracting system.img

~/work/tmp$ cp ~/work/android/out/target/product/generic/system.img .
~/work/tmp$ mkdir tmp2
~/work/tmp$ cd tmp2
~/work/tmp/tmp2$ unyaffs ../system.img

((modify files... for example, etc/init.goldfish.sh))


Rebuilding system.img

~/work/tmp$ rm system.img
~/work/tmp$ ~/work/android/out/host/linux-x86/bin/mkyaffs2image tmp2 system.img

Now you should be able to use the new system.img with your emulator =)


userdata.img and persist.img

These are handled in exactly the same way, just change the filenames.

Altering ramdisk.img

If you have started building the Android source tree yourself, you have undoubtedly wondered how to modify the image files.  In this post, we'll look specifically at ramdisk.img. This is based on the work found here.

Let's pretend our directories are:
~/work/android: directory where you did your git, envbuild, lunch/choosecombo from.
~/work/tmp: empty directory for us to play in

If you have already done a build, you will find out/target/product/generic/ramdisk.img.  This is a gzipped cpio file.
Extracting ramdisk.img

~/work/tmp$ cp ~/work/android/out/target/product/generic/ramdisk.img ./ramdisk.cpio.gz
~/work/tmp$ gzip -d ramdisk.cpio.gz
~/work/tmp$ mkdir tmp2
~/work/tmp$ cd tmp2
~/work/tmp/tmp2$ cpio -i -F ../ramdisk.cpio
((modify files... for example, init.rc or init.goldfish.rc))

Rebuilding ramdisk.img

~/work/tmp/tmp2$ cpio -i -t -F ../ramdisk.cpio | cpio -o -H newc -O ../ramdisk2.cpio
~/work/tmp/tmp2$ cd ..
~/work/tmp$ gzip -c ramdisk2.cpio > ramdisk.img


Now you should be able to use the new ramdisk.img with your emulator =)