How to pack and unpack system.img and userdata.img from an Android factory image


  1. Download the https://android.googlesource.com/platform/system/extras repository:
    git clone https://android.googlesource.com/platform/system/extras
  2. Check out a revision of your choice:
    cd extras
    git checkout android-4.1.1_r1
  3. Compile simg2img:
    cd ext4_utils
    gcc -o simg2img -lz sparse_crc32.c simg2img.c
  4. Unpack your Android image files:
    cd ../../
    ./extras/ext4_utils/simg2img system.img system.raw.img
    ./extras/ext4_utils/simg2img userdata.img userdata.raw.img
  5. Do whatever you want with the images (eg. you can use Paragon's ExtFS on a Mac or just simply mount the images in linux via the loop device).
    (Update, 2012.02.16: Paragon's ExtFS -or at least v8- does not work well. Sad It doesn't show all files that are in the ext4 image.)
    Eg.
    mkdir /mnt/my_system /mnt/my_userdata
    mount -t ext4 -o loop system.raw.img /mnt/my_system
    mount -t ext4 -o loop userdata.raw.img /mnt/my_userdata
  6. Compile make_ext4fs:
    cd extras/ext4_utils
    gcc -o make_ext4fs -lz make_ext4fs_main.c make_ext4fs.c ext4fixup.c ext4_utils.c allocate.c backed_block.c output_file.c contents.c extent.c indirect.c uuid.c sha1.c sparse_crc32.c wipe.c
  7. Repack the images:
    cd ../../
    PATH="$PATH:$(pwd)/extras/ext4_utils/make_ext4fs" ./extras/ext4_utils/mkuserimg.sh -s /mnt/my_system_dir my_system.img ext4 /tmp 512M
P.S.: if compiling stuff is not your thing, you can just download simg2img and make_ext4fs from here.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Something Missing in Step 6

Are you missing extension (.c) in last part on step 6?
...make_ext4fs
probably should be
...make_ext4fs.c

Re: Something Missing in Step 6

Not at all. I just switched the order of the gcc parameters by mistake.
I wrote:
gcc -o allocate.c backed_block.c contents.c ext4_utils.c extent.c indirect.c make_ext4fs.c make_ext4fs_main.c sha1.c sparse_crc32.c uuid.c wipe.c make_ext4fs -lz
whereas the correct order is:
gcc -o make_ext4fs -lz allocate.c backed_block.c contents.c ext4_utils.c extent.c indirect.c make_ext4fs.c make_ext4fs_main.c sha1.c sparse_crc32.c uuid.c wipe.c

Thanks for bringing this to my attention. Already fixed it.

Compiling simg2img and make_ext4fs from Android v4.2.1 sources

The process is a bit different now.
Let's assume that the $AOSP_ROOT variable points to our AOSP root (eg. export AOSP_ROOT="$HOME/Android/AOSP").

  1. Download the https://android.googlesource.com/platform/system/core repository:
    cd "$AOSP_ROOT"
    [ ! -d "platform/system" ] && mkdir -p platform/system
    cd platform/system
    git clone https://android.googlesource.com/platform/system/core
  2. Check out a revision of your choice:
    cd core
    git checkout android-4.2.1_r1
  3. Compile simg2img:
    cd libsparse
    gcc -o simg2img -Iinclude -lz simg2img.c sparse_crc32.c backed_block.c output_file.c sparse.c sparse_err.c sparse_read.c
The unpacking is quite the same:
  • Unpack your Android image files (of course you should adjust the commands for the correct path to the IMG files):
    cd ../../
    ./core/libsparse/simg2img system.img system.raw.img
    ./core/libsparse/simg2img userdata.img userdata.raw.img
  • Do whatever you want with the images (eg. you can use Paragon's ExtFS on a Mac or just simply mount the images in linux via the loop device). Eg.
    mkdir /mnt/my_system /mnt/my_userdata
    mount -t ext4 -o loop system.raw.img /mnt/my_system
    mount -t ext4 -o loop userdata.raw.img /mnt/my_userdata
And the compilation of make_ext4fs did get more complicated too:
  1. Download the https://android.googlesource.com/platform/system/extras repository:
    cd "$AOSP_ROOT/platform"
    [ ! -d "system" ] && mkdir system
    cd system
    git clone https://android.googlesource.com/platform/system/extras
  2. Check out a revision of your choice:
    cd extras
    git checkout android-4.2.1_r1
  3. Compile make_ext4fs:
    cd ..
    gcc -o make_ext4fs -Icore/libsparse/include -lz extras/ext4_utils/make_ext4fs_main.c extras/ext4_utils/make_ext4fs.c extras/ext4_utils/ext4fixup.c extras/ext4_utils/ext4_utils.c extras/ext4_utils/allocate.c extras/ext4_utils/contents.c extras/ext4_utils/extent.c extras/ext4_utils/indirect.c extras/ext4_utils/uuid.c extras/ext4_utils/sha1.c extras/ext4_utils/wipe.c core/libsparse/backed_block.c core/libsparse/output_file.c core/libsparse/sparse.c core/libsparse/sparse_crc32.c core/libsparse/sparse_err.c core/libsparse/sparse_read.c
And the repacking is all the same too:
PATH="$PATH:$(pwd)" ./extras/ext4_utils/mkuserimg.sh -s /mnt/my_system_dir my_system.img ext4 /tmp 512M

error("can't set android permissions - built without android sup

Hi am getting below error :

error("can't set android permissions - built without android support");

Re: error

Hi! A bit more info would help. Smile What is your environment where you try to build/compile? Exactly which step produces the error?

had the same error (when

had the same error (when running make_ext4fs)

gcc calls need this too:
-Icore/include -DANDROID

Re: had the same error

Thanks again. Smile I actually never tried to run/use make_ext4fs (I only compiled it). I only used simg2img to unpack the Android images so I could get a closer look.

After putting " -lz" at the

After putting " -lz" at the very end of the gcc invocation line compiling works.
Otherwise: undefined reference to `gzwrite'

Ubuntu 12.04.1

Re: After putting " -lz" at the

Thanks for the feedback. I did the compilation on Mac OS X (10.6.8) and for the version of gcc I had the -lz option worked where I put it. It seems that gcc is not that much standardized after all.

Compiling make_ext4fs from Android v4.3 sources

The compilation of simg2img is pretty much the same as for 4.2.*. But the compilation of make_ext4fs got a lot more complicated. First of all, it seems that this cannot be compiled (at least out-of-the-box) on a Mac anymore because of SELinux dependencies.

If you try to compile it on linux, the following might be sufficient:
gcc -o make_ext4fs -Isystem/core/libsparse/include -Isystem/core/include -Iexternal/libselinux/include -lz external/libselinux/src/android.c external/libselinux/src/booleans.c external/libselinux/src/callbacks.c external/libselinux/src/check_context.c external/libselinux/src/freecon.c external/libselinux/src/getenforce.c external/libselinux/src/init.c external/libselinux/src/label.c external/libselinux/src/label_android_property.c external/libselinux/src/label_file.c system/extras/ext4_utils/make_ext4fs_main.c system/extras/ext4_utils/make_ext4fs.c system/extras/ext4_utils/ext4fixup.c system/extras/ext4_utils/ext4_utils.c system/extras/ext4_utils/allocate.c system/extras/ext4_utils/contents.c system/extras/ext4_utils/extent.c system/extras/ext4_utils/indirect.c system/extras/ext4_utils/uuid.c system/extras/ext4_utils/sha1.c system/extras/ext4_utils/wipe.c system/extras/ext4_utils/crc16.c system/core/libsparse/backed_block.c system/core/libsparse/output_file.c system/core/libsparse/sparse.c system/core/libsparse/sparse_crc32.c system/core/libsparse/sparse_err.c system/core/libsparse/sparse_read.c

I didn't have the chance to test it (tried to compile on a Mac, but gave up after hitting the SELinux problem), but it seems to be the complete gcc line for make_ext4fs.

Syndicate content