[Android General] Augustoandro:
Yes, I know XDA is filled with such guides.

But you can only port a TWRP if you implement parts of all the guides.

This guide is a summation of all those guides.

NOTE:

I would be using AIK-Linux in this tutorial, since I don't recommend Windows for all this, who knows what problems you will get into.

REQUIREMENTS:

1.Download these TWRP recoveries in accordance with your screen resolution.

A. If your screen resolution is 480x850, 480x854; download

https://twrp.me/lg/lgnexus4.html

B. If your screen resolution is 720x1280, 800x1280 or 720xsomething; download

https://twrp.me/asus/asusnexus720123g.html

C. If your screen resolution is 1080x1920, download

https://twrp.me/motorola/motorolanexus6.html

2. Download Android Image Kitchen-Linux from this thread:

https://xdaforums.com/showthread.php?t=2073775

LET'S BEGIN:

1. Unzip Android Image Kitchen zip file.

3. Now, navigate to the unzipped folder of AIK (Android Image Kitchen).

4. Just Right Click and select "Open Terminal here".

5. Copy and paste your stock recovery.img and paste it in AIK folder.

6. In the terminal, type the command

su

Enter your root password, then type

./unpackimg.sh

7. This will unpack your stock recovery.img. Do not close the terminal.

8. Cut the new folders, ramdisk and split_img, and paste them somewhere outside of AIK folder.

9. Remove the stock recovery.img in AIK folder.

10. Now, copy and paste the twrp_recovery.img according to your screen resolution in the AIK folder.

11. Now, revert back to the terminal and type

./unpackimg.sh

12. This will unpack your twrp_recovery.img

13. Delete all the files in the new split_img folder we got by unpackimg twrp_recovery.img

14. Copy and paste all the files in stock split_img folder to the new split_img folder.

15. Delete following files from new ramdisk folder.

A. default.prop

B. fstab.devicename_or_chipsetname

C. ueventd.rc

D. Any other file like ueventd.devicename.rc

16. Copy and paste these files from stock ramdisk folder to new ramdisk folder.

default.prop
fstab.devicename
ueventd.rc

17. Now go into stock ramdisk/etc folder and copy recovery.fstab

18. Paste the recovery.fstab file in new ramdisk/etc folder.

19. Edit the twrp.fstab file according to the newly pasted recovery.fstab

20. You will have to replace"/sdcard" with "/external_sd" and "/usb" with "/usb-otg".

21. If the partitions stated above are not available in your fstab files then add these lines. Order may change on the format in which the files are written.

/external_sd vfat /dev/block/mmcblk1p1 flags=removable;storage;display="SDCARD"
/usb-otg auto /dev/block/sda1 flags=removable;storage;display="USB-OTG"

22. Edit the following lines as shown in default.prop

ro.secure=0
ro.adb.secure=0
security.perf_harden=0
ro.debuggable=1
persist.sys.usb.config=adb,mtp

23. Now, come out of the ramdisk folder and open the terminal.

24. Type

./repackimg.sh

25. Your new TWRP img would be generated and named as image-new.img by default. You may change the name later.

When you have successfully built the TWRP, or if you want to request one instead of creating yourself, contact me on this thread:

https://xdaforums.com/android/general/unofficial-twrps-devices-source-code-t3833338

SPECIAL INSTRUCTIONS FOR MEDIATEK DEVICES:

1. The recovery.fstab you will get after unpacking your stock recovery.img would be flawed in most cases. I believe OEMs do this deliberately.

2. In order to know which partition is mounted at which location and at which device, you'll need DiskInfo (Install it from Play Store) and a root explorer.

3. Enable the following Diskinfo settings:

Show mount path
Show partition name
Show file system
Expert mode
Unmounted partitions
Device Mapper partitions
Temporary filesystems

4. Use the root explorer to find the path of your partitions. They would be located under /dev (mtk devices have bootimg and recovery here generally) and some other partitions like /system, /cache would be deeper inside.

5. You can trace their exact locations by navigating within the /dev directory for partition names like mmcblk0p5.

SPECIAL INSTRUCTIONS FOR DEVICES THAT DON'T HAVE RECOVERY.FSTAB WITHIN STOCK RECOVERY.IMG

1. In the ramdisk/etc folder, there might be a dummy recovery.fstab file with the symbol of link on it.

2. Right click on the file and select "Show Target". You'll get to know the path of original recovery.fstab file. It might be within the unpacked recovery or inside some other partition on your phone.

For example: Honor 7C devices have their recovery.fstab in /vendor/etc folder.

TROUBLESHOOTING INSTRUCTIONS:

1. If you get a dm-verity error or facing an encryption problem, go to this thread and flash the "dm-verity and forceencrypt disabler zip".

https://xdaforums.com/android/software/universal-dm-verity-forceencrypt-t3817389

2. If you encounter a bootloop while booting into your new TWRP, please consult post #20 of this thread.

3. At times, the path of partitions during recovery boot is not the same as those during normal boot. That's why you may get errors like

Updating partitions details...
Failed to mount '/cache' (Invalid argument)
Failed to mount '/system' (Invalid argument)
Failed to mount '/data' (Invalid argument)
Failed to mount '/metadata' (Invalid argument)
...done
Unable to mount storage
Failed to mount '/data' (Invalid argument)

To resolve this, in your TWRP terminal type the following command

find /dev/block -name by-name

Replace the paths of the partitions in your fstab files with the ones you get as output using the command above (only the partitions which failed to mount).

If there are some bugs or you need some assistance in porting TWRP, contact me by mentioning in your post, for example [MENTION=8355838]Augustoandro[/MENTION], we'll work on it to make it bug-free.

Special thanks to [MENTION=7285913]aIecxs[/MENTION] for his troubleshooting instructions of bootloop, "Failed to mount /partition_name (invalid argument)" error and also [MENTION=8099736]inguna[/MENTION] for guiding me on how to fix the "SDCARD not detected by the TWRP" problem and also fixing the MTP, ADB not working issue.

Another thanks to [MENTION=3666478]seanybiker[/MENTION] and [MENTION=5964608]avirus[/MENTION] for their troubleshooting instructions on "DM-Verity and forceencrypt" errors.

CREDITS:

[MENTION=6792590]HostZero[/MENTION] for his porting guide.

[MENTION=4544860]osm0sis[/MENTION] for his awesome Android Image Kitchen

[MENTION=6037748]Zackptg5[/MENTION] for his "DM-Verity and Forceencrypt disabler zip".