[GUIDE] How to port A system image to AB (system-as-root)

Would a root app to convert A images to A/B be useful?

  • Yes

    Votes: 64 95.5%
  • No

    Votes: 3 4.5%

  • Total voters
    67

kdrag0n

Senior Member
Feb 19, 2016
572
1,492
133
kdrag0n.dev
For those with an A/B device, it can be frustrating to wait for GSI maintainers to build A/B variants. Don't worry, it's not too complicated to port A-only system images to A/B.

The difference that matters is that on A-only images, everything in / such as init.rc is in the ramdisk, which is in the boot partition. On A/B devices, the ramdisk is used for recovery instead, so all those files go on the system partition. This partition is actually mounted at / instead of /system. It functions as both the ramdisk and the system partition. This can actually lead to faster boots.
A/B devices also have a different Chrome OS derived updater that supports seamless OTAs to the other slot, but we won't worry about that.

First of all, you will need Linux. This is because Windows just can't handle the extra data we need to preserve, such as POSIX permissions and SELinux contexts.

You are expected to have experience with command-line tools. This is NOT for newbies. You may need to compile a tool.

Steps:
  1. Download an A/B GSI with the same Android version. We will use this image for the root files. (I was able to use an 8.1 base for a 9.0 system with some patches, but YMMV)
  2. Use the simg2img tool to convert the sparse A/B image to a raw one: simg2img system_arm64_ab.img base_ab_raw.img
  3. Use the same tool to convert the A-only image to raw: simg2img system_arm64_a.img system_a_raw.img
  4. Enter a root shell. This is to avoid any problems with permissions. sudo -s
  5. Create the directories to mount the images on: mkdir /mnt/ab; mkdir /mnt/a
  6. Mount the A/B image with flags rw,noatime: mount -o rw,noatime base_ab_raw.img /mnt/ab
  7. Mount the A-only with flags rw,noatime: mount -o rw,noatime system_a_raw.img /mnt/a
  8. Create a system folder in the A-only image: mkdir /mnt/a/system
  9. Set the SELinux context for the system folder: setfattr -n security.selinux -v u:eek:bject_r:system_file:s0 /mnt/a/system
  10. Set permissions for the system folder just in case: chmod 755 /mnt/a/system
  11. Delete the system folder in the A/B image: rm -fr /mnt/ab/system
  12. Copy all the root files from A/B to the A-only image. These exact arguments are important to preserve everything. cp -Raf /mnt/ab/* /mnt/a/
  13. Unmount the A/B image: umount /mnt/ab
  14. Unmount the newly transformed A/B image: umount /mnt/a
  15. Convert the new A/B image to sparse format with img2simg: img2simg system_a_raw.img system_arm64_ab_new.img
  16. Flash and enjoy!

If you encounter any errors, the resulting image will most likely not work.

If you have a device with a small system partition that cannot fit the original A-only image you are trying to convert, it will be necessary to create a new, smaller image for the purpose. To create that new image, follow these steps after the ones above:
  1. Create the new raw sparse image: fallocate -l 2G system_ab_raw_small.img
  2. Create an ext4 filesystem on the new image: mkfs.ext4 system_ab_raw_small.img
  3. Mount the new image: mount -o rw,noatime system_ab_raw_small.img /mnt/ab
  4. Mount the old converted A/B image: mount -o ro system_a_raw.img /mnt/a
  5. Copy all the files: cp -Raf /mnt/a/* /mnt/ab/
  6. Unmount the new image: umount /mnt/ab
  7. Unmount the old image: umount /mnt/a
  8. Convert the new image to sparse: img2simg system_ab_raw_small.img system_arm64_ab_new_small.img
  9. Flash and enjoy!

If there's enough demand, I might make a tool to automate this. Let me know what you think.
 
Last edited:

yuzui

New member
Jul 31, 2016
2
0
0
For those with an A/B device, it can be frustrating to wait for GSI maintainers to build A/B variants. Don't worry, it's not too complicated to port A-only system images to A/B.

The difference that matters is that on A-only images, everything in / such as init.rc is in the ramdisk, which is in the boot partition. On A/B devices, the ramdisk is used for recovery instead, so all those files go on the system partition. This partition is actually mounted at / instead of /system. It functions as both the ramdisk and the system partition. This can actually lead to faster boots.
A/B devices also have a different Chrome OS derived updater that supports seamless OTAs to the other slot, but we won't worry about that.

First of all, you will need Linux. This is because Windows just can't handle the extra data we need to preserve, such as POSIX permissions and SELinux contexts.

You are expected to have experience with command-line tools. This is NOT for newbies. You will need to compile a few tools.

Steps:
Download an A/B GSI with the same Android version. We will use this image for the root files. (I was able to use an 8.1 base for a 9.0 system with some patches, but YMMV)
Use the same tool to convert the A-only image to raw: simg2img system_arm64_a.img system_a_raw.img
Create the directories to mount the images on: mkdir /mnt/ab; mkdir /mnt/a
Mount the A-only with flags rw,noatime: mount -o rw,noatime system_a_raw.img /mnt/a
Set the SELinux context for the system folder: setfattr -n security.selinux -v u:eek:bject_r:system_file:s0 /mnt/a/system
Delete the system folder in the A/B image: rm -fr /mnt/ab/system
Unmount the A/B image: umount /mnt/ab
Convert the new A/B image to sparse format with img2simg: img2simg system_a_raw.img system_arm64_ab_new.img
Create the new raw sparse image: fallocate -l 2G system_ab_raw_small.img
Mount the new image: mount -o rw,noatime system_ab_raw_small.img /mnt/ab
Copy all the files: cp -Raf /mnt/a/* /mnt/ab/
Unmount the old image: umount /mnt/a
Flash and enjoy!


If there's enough demand, I might make a tool to automate this. Let me know what you think.
Ab partition of the device can not install Android p, ask God to make a direct brush into the system. Img
 

kdrag0n

Senior Member
Feb 19, 2016
572
1,492
133
kdrag0n.dev
i have sucessfully converted A TO A/B...but when i flash it ..it doesnt boot just stuck at logo..my device in mi A1...can you please tell me that i need to port this file???
What ROM is it? I can try to port it.

Most likely your problem is either SELinux being on Enforcing (change it to Permissive in TWRP -> Advanced -> Tissot Manager) or wrong permissions in the image caused by missing a step.
 

kdrag0n

Senior Member
Feb 19, 2016
572
1,492
133
kdrag0n.dev

Skorm92

Senior Member
Mar 16, 2013
52
52
0
Sprang-Capelle
I'm using a macbook, and I really like to try to convert the MIUI rom (https://forum.xda-developers.com/pr...-only-roms/8-1-miui10-project-treble-t3840198) to an A/B rom for my Oneplus6 (Treble, of course). basically I have 2 questions;
- You mention that you NEED linux, is it linux, or is it actually UNIX? (a.k.a. also do-able with MacOS)
- Given the ROM I want to port is using a backup restore method, is one still able to port it to an AB rom?

Even if my goal is not achievable, thank you for creating a guide on how to port. Sharing dev knowledge is the foundation of what drives people to create custom roms!
 

kdrag0n

Senior Member
Feb 19, 2016
572
1,492
133
kdrag0n.dev
I'm using a macbook, and I really like to try to convert the MIUI rom (https://forum.xda-developers.com/pr...only-roms/8-1-miui10-project-treble-t3840198) to an A/B rom for my Oneplus6 (Treble, of course). basically I have 2 questions;
- You mention that you NEED linux, is it linux, or is it actually UNIX? (a.k.a. also do-able with MacOS)
- Given the ROM I want to port is using a backup restore method, is one still able to port it to an AB rom?

Even if my goal is not achievable, thank you for creating a guide on how to port. Sharing dev knowledge is the foundation of what drives people to create custom roms!
You really NEED Linux. macOS doesn't support ext4, and any third-party driver won't be able to preserve the SELinux xattrs properly.

In that case it's much harder. I'd suggest you try the port on-device in TWRP. Push the reference A/B image to the device and mount it in /mnt, then perform the operations in /system instead of the ROM image mountpoint. You also won't need to create the image at the end. This won't need Linux on your computer because it's all done on the phone.

I used this method to port an A-only OnePlus 6 OxygenOS port to my Mi A1. It booted but not very far because of issues with the port.
 

Skorm92

Senior Member
Mar 16, 2013
52
52
0
Sprang-Capelle
You really NEED Linux. macOS doesn't support ext4, and any third-party driver won't be able to preserve the SELinux xattrs properly.

In that case it's much harder. I'd suggest you try the port on-device in TWRP. Push the reference A/B image to the device and mount it in /mnt, then perform the operations in /system instead of the ROM image mountpoint. You also won't need to create the image at the end. This won't need Linux on your computer because it's all done on the phone.

I used this method to port an A-only OnePlus 6 OxygenOS port to my Mi A1. It booted but not very far because of issues with the port.
Clear, thanks for the response.
I will try your suggestion, if it boots but fails at certain point, I will find a way to debug it.
The problem primarily on my OP6 is whenever I boot into a treble rom that has issue's, My OP6 is triggerhappy on sending a qualcom kernel panic crash.
 

kdrag0n

Senior Member
Feb 19, 2016
572
1,492
133
kdrag0n.dev
Clear, thanks for the response.
I will try your suggestion, if it boots but fails at certain point, I will find a way to debug it.
The problem primarily on my OP6 is whenever I boot into a treble rom that has issue's, My OP6 is triggerhappy on sending a qualcom kernel panic crash.
A kernel patch should be able to prevent that, check the custom kernels available.
 

ereneren564

Senior Member
Oct 31, 2010
182
128
0
Amsterdam
You really NEED Linux. macOS doesn't support ext4, and any third-party driver won't be able to preserve the SELinux xattrs properly.

In that case it's much harder. I'd suggest you try the port on-device in TWRP. Push the reference A/B image to the device and mount it in /mnt, then perform the operations in /system instead of the ROM image mountpoint. You also won't need to create the image at the end. This won't need Linux on your computer because it's all done on the phone.

I used this method to port an A-only OnePlus 6 OxygenOS port to my Mi A1. It booted but not very far because of issues with the port.
You can always give it a shot by creating an ubuntu or alpine container on Docker.
 

ereneren564

Senior Member
Oct 31, 2010
182
128
0
Amsterdam
You can't natively have a Docker container on macOS. If you did it'd be inside a Linux VirtualBox VM anyway. I don't think you'd be able to passthrough USB to the container for ADB.
As you can copy files to container, you can also copy files from container to host. So your argument is invalid.

docker cp <containerId>:/file/path/within/container /host/path/target
 
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone