3

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 '19 at 11:19
  • It is showing as 00 00 00 00.. Please check the Update 2. – Vatish Sharma Aug 16 '19 at 11:40
  • 1
    Looks like the image is already unpacked. – Robert Aug 16 '19 at 11:59
  • Oh really :o So, how to add a file in this .img file? – Vatish Sharma Aug 16 '19 at 12:09
  • 1
    use busybox cp -acv instead of cp if you want to preserve permissions. selinux enabled (x86_64) binary of busybox (works on pc) sudo mount -t ext4 -o loop,rw,noatime system.img system_mount works only when directory system_root exist in same location as system.img – alecxs Aug 16 '19 at 18:24
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

|improve this answer|||||
  • 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 '19 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 '19 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 '19 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 '19 at 13:27
  • @IrfanLatif here – Ankit Srivastava Sep 7 '19 at 16:27
0

These commands don't work always: sometimes the system_new.img size becomes greater than system.img.ext4 and hence it doesn't flash on the device. As per my knowledge, the new image size should be equal to or less than the size of the original system image. ---- If anyone will be able to determine the new commands then please share it.

To Unpack-Modify-Pach the system.img, I have followed the following procedure:

a) Unpacking

  • Run file system.img and make sure that system.img is Android Sparse Image.
  • Rename system.img to system.img.ext4. // Not required if you will use other name for raw image in below steps.
  • With simg2img system.img.ext4 system.img, you will get a raw image file named system.img
  • With mkdir system, create directory to mount system.img
  • With sudo mount -t ext4 -o loop system.img system/ you will get all files of system.img in system folder

b) Modifying

  • With ls -l system/init.rc note permissions: 750
  • With sudo chmod 777 system/init.rc give write permissions
  • With sudo echo "#MODIFICATION " >> system/init.rc done some modification in init.rc
  • With sudo chmod 750 init.rc reset init.rc to the noted permissions

c) Calculate system sector size

  • With tune2fs -l system.img | grep "Block size\|Block count" you will get block size and count
  • With echo $((1553064 * 4096)) multiply both results. I got 6361350144

d) Packing

  • With sudo make_ext4fs -s -l 6361350144 -a system system_new.img system/ you will get system_new.img “Android Sparse Image” that has all changes
|improve this answer|||||
  • Getting 'can't set android permissions - built without android support' error when executing the packing command. Any idea? – Sakkeer Hussain Feb 4 at 10:57
  • Never get this error. Probably using another(or previous version of) make_ext4fs tool can resolve your problem. – Vatish Sharma Feb 5 at 6:33
  • 1
    Thanks for your response, Could you please tell me what is the 'sys/' mentioned in the tail of the packing command means? – Sakkeer Hussain Feb 7 at 11:00
  • It should be system/ means the folder in which raw system.img extracted. Fixed it in the answer as well. – Vatish Sharma Feb 10 at 3:46
  • These commands don't work always: sometimes the system_new.img size becomes greater than system.img.ext4 and hence it doesn't flash on the device. As per my knowledge, the new image size should be equal to or less than the size of the original system image. ---- If anyone will be able to determine the new commands then please share it with me too. – Vatish Sharma Feb 10 at 3:49

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.