1

I have OnePlus 6T device which has A/B partition system and has a ROM of user type i.e [ro.build.type]: [user]. This device is rooted with Magisk. I have a requirement(Want to place customized sepolicy file under system_root directory) to modify system.img.

I have tried different tools like:

  1. simg2img :
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/img-tools$ ./simg2img system.img sys.raw
Invalid sparse file format at header magi
Failed to read sparse file
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/img-tools$ 
  1. imgtools
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ sudo ./imgtool system.img extract
[sudo] password for OMEN: 
system.img is not a recognized image. Sorry
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ 
  1. and more tools even on windows..

but none of them is capable of parsing my system.img.

I have copied system.img directly from OnePlus6T ROM setup which installs Android 9 on this device without any issue.

Any help on:

  1. How to fix the system.img so it will be extracted and repacked fine?
  2. Any command that can unpack/repack system.img?
  3. Any working tool to accomplish this task?

Update 1: I have run file system.img and I found that its ext2 image and the tools support ext4.

system.img: Linux rev 1.0 ext2 filesystem data, UUID=d09c08e9-628d-590e-a610-3a14de2a8db0 (extents) (large files) (huge files)

Update 2: Tried to find the magic number and have following result:

OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ xxd system.img | head
00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea/imgtool$ 

Update 3 Fing the image already unpacked. So to add the required file, I have mount the image as sudo mount -o loop system.img system_mount and then tried to copy the contents to another folder with cp system_mount/* system/ so that I can add the required file and make new image out of it but I got following errors:

root@OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea# mkdir system && cp system_mount/* system/
cp: omitting directory 'system_mount/acct'
cp: cannot stat 'system_mount/bin': No such file or directory
cp: cannot stat 'system_mount/bt_firmware': No such file or directory
cp: cannot stat 'system_mount/bugreports': No such file or directory
cp: cannot stat 'system_mount/cache': No such file or directory
cp: cannot stat 'system_mount/charger': No such file or directory
cp: cannot stat 'system_mount/charger_log': No such file or directory
cp: omitting directory 'system_mount/config'
cp: omitting directory 'system_mount/d'
cp: omitting directory 'system_mount/data'
cp: omitting directory 'system_mount/dev'
cp: cannot stat 'system_mount/dsp': No such file or directory
cp: cannot stat 'system_mount/etc': No such file or directory
cp: cannot stat 'system_mount/firmware': No such file or directory
cp: omitting directory 'system_mount/lost+found'
cp: omitting directory 'system_mount/mnt'
cp: omitting directory 'system_mount/odm'
cp: omitting directory 'system_mount/oem'
cp: omitting directory 'system_mount/op1'
cp: omitting directory 'system_mount/op2'
cp: cannot stat 'system_mount/persist': No such file or directory
cp: omitting directory 'system_mount/postinstall'
cp: omitting directory 'system_mount/proc'
cp: cannot stat 'system_mount/product': No such file or directory
cp: omitting directory 'system_mount/res'
cp: omitting directory 'system_mount/sbin'
cp: cannot stat 'system_mount/sdcard': No such file or directory
cp: omitting directory 'system_mount/storage'
cp: omitting directory 'system_mount/sys'
cp: omitting directory 'system_mount/system'
cp: omitting directory 'system_mount/vendor'
root@OMEN-by-HP-Laptop-15-dc0xxx:~/WorkArea# 

  • 1
    Does the system.img you have start with hex 3AFF 26ED? If not check if the file header magic bytes is something known. – Robert Aug 16 at 11:19
  • It is showing as 00 00 00 00.. Please check the Update 2. – Vatish Sharma Aug 16 at 11:40
  • 1
    Looks like the image is already unpacked. – Robert Aug 16 at 11:59
  • Oh really :o So, how to add a file in this .img file? – Vatish Sharma Aug 16 at 12:09
  • Sorry, I thought that it is obvious to mount an ext2/4 file if you want to modify it. – Robert Aug 16 at 12:21
0

Use this tool on linux, I was able to extract the image of my emulator using ext4 on vm running ubuntu.

https://github.com/qmfrederik/extfstools

Still figuring out a way to pack it back. Will update here once I am able to do it

  • Why not simply mount? Repacking is simple. Create sparse file with truncate. Do mkfs.ext4 and mount. Add files and umount. No special tools needed. But how would you preserve permissions, ownership, timestamps, xattr of files this way? – Irfan Latif Sep 7 at 11:08
  • @IrfanLatif I don't know how to mount on VM. and b) I am noob at ubuntu. Installed vm last night. c) Until last night permissions etc were alien to me. I just need to run a small app on auto on a server on emulator so I don't think i need to worry about permissions but i do want to provide root access to some apps . Do you mind having a chat about it and helping me out ? – Ankit Srivastava Sep 7 at 11:40
  • Chat on what? Ask a question specifically if not answered already. For Linux/Android basics do some research and study. I'm available to help if not available otherwise. – Irfan Latif Sep 7 at 12:18
  • @Chat about if you would be interested in doing it for us and we will pay you. I am not sure if I can ask a question related to that here – Ankit Srivastava Sep 7 at 13:27
  • @IrfanLatif here – Ankit Srivastava Sep 7 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.