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.

Since the linking behaviors

Since the linking behaviors may be changed, the order of linking zlib now matters, the command of compilation of make_ext4fs should be this:

gcc -Wl,--as-needed 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 -lz -o make_ext4fs

Worked on Ubuntu 14.04

Re: Since the linking behaviors

Thanks for the feedback! Much appreciated. Smile

ext4 util compilation with Android source 4.4.x

Someone could rewrite the article how to compile ext4 utils (simg2img - make_ext4) using the latest source file (Androdi 4.4.r2.01)

Re: ext4 util compilation with Android source 4.4.x

I'll look into this.

How it can be made in Odin

Can I convert it to tar file to use in Odin

Re: How it can be made in Odin

Eg. search for the terms "how create samsung tar odin site:forum.xda-developers.com" on Google. It will give you plenty of tutorials on how to put a custom system.img into a *.tar.md5 so you can flash it with Odin.

To obtain such a mistake I pack

I enter the following command:
mkuserimg.sh -s /mnt/my_system new.img ext4 /tmp/ 1024M

Get an error
file_context_open:error file context handle(no such file or directory)
no such file or directory
...
exit 4

Re: To obtain such a mistake I pack

Are you sure this is the entire output of the command you were trying to execute?
Was there anything else besides what is hidden behind the three dots?
What output do the three dots represent?

collect2: error:

I got the next error when compile simg2img

/usr/bin/ld: cannot find -lz
colect2: error: ld returned 1 exit status.

may somebody help me plz

PS. using ubuntu-15.04 and/or 14.04 under windows10/virtualbox


Tks

Bob

system.img does not start

Hello,

Is anyone familiar how to change system image in wear ?
I am using the checkout for android_4.1.1 as in this example. I hope it should be OK with wear preview 2 too.

I use the non-preview release in "Preview image for LGE Watch Urbane 2nd Edition", and installed it on wear.
Then I tried to use simg2img&make_ext4fs inorder to extract and pack back to img.
I made no change in system files. Just extracted and packed again. No error been recieved.
But on flashing the new system.img, it fails to start correctly (nothing is seen in screen after boot).


2304 sudo ./simg2img system.img system.raw
2308 sudo mount -t ext4 -o loop system.raw xxx/
2309 sudo ./make_ext4fs -s -l 512M system_new2.img xxx/



Did anyone have success in programming new system image into waer ?
Why didn't it work ?
Is there another simpler way I should try ?

Thanks,
ranchu

Syndicate content