3

I download Android Emulator M1 Initial Preview from https://github.com/google/android-emulator-m1-preview/releases

Inside the .app file I found api30-gphone-arm64-v8a/system.img which isn't a yaffs2 nor sparse file:

./unyaffs2 /tmp/android-wip/system.img /tmp/android-wip/system-img/
unyaffs2-0.2.8: image extracting tool for YAFFS2
image size (3232759808) is NOT a mutiple of 2048 + 64

operation incomplete files contents may be broken

simg2img system.img.ext4 system.img
Invalid sparse file format at header magic
Failed to read sparse file

So what is the format of such file?

Updates: Based on the direction from @Robert, I can see that the file is an fdisk image:

$ fdisk -lu system.img
Disk system.img: 3,1 GiB, 3232759808 bytes, 6313984 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 41F73643-DBCB-41A1-A27E-9FDB53578618

Device Start End Sectors Size Type system.img1 2048 4095 2048 1M Linux filesystem system.img2 4096 6311935 6307840 3G Linux filesystem

Now, it's unclear what format of the partition inside, because mount it as a regular linux partition fails:

$ sudo mount -o loop,offset=1048576 system.img /mnt/android-1
mount: /mnt/android-1: wrong fs type, bad option, bad superblock on /dev/loop17, missing codepage or helper program, or other error.

gparted also doesn't understand the partition:

enter image description here

  • 2
    The file is not a partition but a whole disk. You can open it with fdisk and see that it contains multiple partitions. – Robert Apr 01 '21 at 11:12
  • @Robert Thanks. Do you have further information on what kind of format for each partition? It doesn't seem to be ext4 partitions? – Phương Nguyễn Apr 02 '21 at 09:34

1 Answers1

4

wrong offset, partition start at sector 4096. you can use kpartx instead

newer android running dynamic partitions. the super partition can be unpacked with Dynamic Partition Tools

furthermore partitions are flagged as read-only, therefore mount -r flag is required

sudo -i

git clone https://github.com/AndroidDumps/Firmware_extractor.git PATH=$PATH:$(realpath Firmware_extractor/tools/Linux/bin)

mkdir -p /mnt/android-1/system_root ../extracted kpartx -av system.img lpunpack -p system /dev/mapper/loop2p2 ../extracted

mount -t ext4 -o loop,ro,noexec,noload,noatime ../extracted/system.img /mnt/android-1/system_root thunar /mnt/android-1/system_root

(or whatever kpartx tell you 2nd partition is mapped)


in case you need to make ext4-dedup/logical partition writeable, increase partition first, then duplicate shared blocks

cd ../extracted
dd if=/dev/zero bs=1G seek=1 count=0 of=system.img
resize2fs system.img 1G
e2fsck -E unshare_blocks system.img

you should be able to mount -w afterwards


re-packing requires new vbmeta signature with avbtool.py stored in first partition system.img1 start at sector 2048 (which goes too far here)

superrepack third-party cross-platform tool for easy unpack/repack system.img2 is recommended to automatize the whole re-packing process

you can however do it manually with lpmake (requires all partitions to be unpacked into ../extracted see above step lpunpack)
source

resize2fs -M system.img
e2fsck -yf system.img
lpmake --metadata-size 65536 --super-name super --metadata-slots 1 --device super:3229614080 --group main:2369171456 --partition system:readonly:787050496:main --image system=system.img --partition vendor:readonly:83619840:main --image vendor=vendor.img --partition product:readonly:1366814720:main --image product=product.img --partition system_ext:readonly:131686400:main --image system=system_ext.img --sparse --output super.img
alecxs
  • 4,034
  • 3
  • 16
  • 34
  • It failed with this error:
    mount -t ext4 -o loop,rw,noatime /dev/mapper/loop19p2 /mnt/android-1/system
    mount: /mnt/android-1/system: wrong fs type, bad option, bad superblock on /dev/loop20, missing codepage or helper program, or other error.
    
    – Phương Nguyễn Apr 04 '21 at 07:29
  • Thanks for the enlightening answer. I was able to mount the image and changing the content inside now – Phương Nguyễn Apr 05 '21 at 01:22
  • 1
    you won't be able to run modified partition unless avb/dm-verity is disabled. maybe it's already sufficient to set the flag 2 in system.img1 with hex editor which will instruct to skip hashtree verification (never tested myself, but that's what magisk does) https://android.stackexchange.com/q/221756 – alecxs Apr 05 '21 at 02:20
  • superrepack come with a warning from the author just yesterday: I have bad news for you! This tool is dangerous! Do not use please! What I have found now while checked modified partition with e2fsck, found a ton of "Multiply-claimed blocks already reassigned or cloned.", when I do e2fsck repair it got repaired but bad news is some files is deleted from partition – Phương Nguyễn Apr 05 '21 at 02:34
  • just wait a few days it will solved soon :) you can meanwhile go the official way as descriped by Brepro1, it's well tested – alecxs Apr 05 '21 at 02:38
  • I was able to produce the new super image with lpmake:
    lpmake --metadata-size 65536 --super-name super --metadata-slots 1 --device super:3229614080 --group main:2366435328 --partition system:readonly:784314368:main --image... --sparse --output super.simg
    

    How can I pack it back as a super partition together with a vbmeta partition like the original one?

    – Phương Nguyễn Apr 06 '21 at 01:53
  • (The original one is an fdisk file with 2 partition vbmeta of 1M and super of 3G) – Phương Nguyễn Apr 06 '21 at 01:57
  • 1
    cat super.img > /dev/mapper/loop2p2 – alecxs Apr 06 '21 at 07:07
  • The emulator won't boot with the new system.img generated by the method above. I go ahead and hex patched the byte to disable verification but I guess I don't know enough to make the correct patch: https://imgur.com/8remGKm – Phương Nguyễn May 05 '21 at 08:57
  • superrepack doesn't work for me neither. I'm getting clone_file: Could not allocate block in ext2 filesystem returned from clone_file_block while running superrepack on the emulator - I guess because I'm running it on emulator, not real device? – Phương Nguyễn May 05 '21 at 08:58
  • there is also dm-verity one must disable in fstab – alecxs May 05 '21 at 09:10