[Advanced Help Request] Lineage 18.1 GSI + TWRP + Magisk + Gapps

I would like to request a step by step guide to install Lineage 18.1 GSI + TWRP + Magisk + Gapps please! (This exclamation indicates begging, not demanding)

Expected/Requested format:

  1. Use SP Flash Tool & Teracube_2e_06_20201116 to get to a stock setup
  2. Download file A
  3. Download file B
  4. Command 1
  5. Command 2
  6. Command 3
  7. Etc.
Background: (click to expand)

I currently work as an “I.T. Assistant” and consider myself to have a wide marsh of I.T. related knowledge with the deepest part being dinking around with Android. That being said I just cannot seem to get this setup to work. I have primarily been trying to merge these 2 guides as base guides:

https://community.myteracube.com/t/how-to-install-lineageos-17-1-gsi-on-the-teracube-2e/2691/3

https://community.myteracube.com/t/advanced-users-only-twrp-recovery-root-for-teracube-2e/1729/20

I have probably spent 20-40 hours trying to figure out how to get this to work and I finally “gave up” and am asking for help. I have had tons of ideas along the lines of “maybe if I try this!” Toward the end I noticed my new google searches were resulting in pages of purple (clicked) links. My new ideas were failing to bring me new information.

I am not going to flood this post with everything I’ve tried because the post would be far too large…and honestly I can’t remember half of the things I tried and I didn’t document my attempts. That being said I will link the file names I’ve been working the most with.

Files used/tried: (click to expand)
  • lineage-18.1-20210914-UNOFFICIAL-treble_arm64_bvS.img
  • lineage-18.1-20210914-UNOFFICIAL-treble_arm64_avS.img (not sure why bvs is the file that has to be used since we’re not AB…this file resulted in boot loops)
  • MindTheGapps-11.0.0-arm64-20210920_084011.zip
  • open_gapps-arm64-11.0-nano-20211007.zip
  • vbmeta.img
  • twrp-3.5.2_10-2-Teracube-2e.img
  • twrp-3.5.0_10-2-Teracube-2e.img
  • Magisk-v23.0.zip
  • Windows 10_MTK+VCOM+USB+Preloader+Drivers.zip
  • MediaTek_Preloader_USB_VCOM_Drivers_Setup_Signed.zip
  • MTK_Android_USB_Driver.zip
  • platform-tools_r31.0.3-windows (verified adb and fastboot are 31.0.3 via adb --help and fastboot --help)
  • SP_Flash_Tool_exe_Windows_v5.2048.00.000
  • Teracube_2e_06_20201116

I believe the main issue I am having is either a TWRP issue or zip file issues. I am not sure if TWRP is responsible for “knowing” how to work with our dynamic non-AB partitions or if the zip files are supposed to have the information to handle that? I don’t think I’ve been able to install anything through TWRP without some type of error. I also noticed when TWRP is installed I cannot get to fastbootd and TWRP fastboot mode does not work at all. The command “fastboot devices” lists nothing from within TWRP fastboot mode. If I click switch to TWRP adb mode and try “adb devices” I do have a device show up.

I’m at a loss for what to do but will be very responsive in testing any guides anyone posts and quoting the exact error I am given at various points.

Thanks in advanced to any random internet hero who takes pity on my soul and helps!

This is possible, but there are a lot of pitfalls. Before getting started, please make sure you have SP Flash handy and the stock image ready to re-flash if something goes sideways. Super handy tool that I couldn’t recommend more, and a crutch as I explored this request. Also, forgive me for the intentionally broken links–as a new user here, I guess I can only do two links. :frowning:

Prerequisites
While these instructions should generally help with any device–these are specific to the Teracube 2e 1st Batch (SN: 2020, Board: yk673v6_lwg62_64). Attempting these instructions verbatim on another board might require you to reflash stock images and start over. Also, these are written from a Linux perspective. I’m sure there are ways to get the following on Windows, but I’m only focused on Linux.

Tools and downloads:

  • SP Flash: https_//spflashtoolofficial.com/
  • Advanced SP Flash Error Resolution: https_//www.codetd.com/en/article/11797301
  • Android tools (adb, fastboot, simg2img, img2simg, lpunpack, lpmake)
  • Android partition tools (parted and gdisk) – note: I had to compile from source to get these
  • LineageOS zip: I specifically used https_//github.com/2e-dev/releases/releases/download/1.3/lineage-18.1-2e-signed-img.zip for this process. I also renamed it to shorten the name.
  • Magisk: https_//github.com/topjohnwu/Magisk/releases
  • Open GApps: https_//opengapps.org/
  • TWRP: Download the attachment from https_//community.myteracube.com/t/advanced-users-only-twrp-recovery-root-for-teracube-2e-1st-batch-sn-2020/1729. Convenience link: https_//dtox94jqmvr57.cloudfront.net/TWRP/twrp-3.5.0_10-2-Teracube-2e.img

Important Note Regarding SP Flash
Since some optional instructions below involve changing partitions, if you want to start over–you must select “Format All + Download” when running SP Flash. If you haven’t changed any partitions, you can run in “Download” mode only.

Preparation Work

Enable Developer Mode

Go to Settings > About > tap 7 times on “Build number”

Enable OEM Unlocking

Go to Settings > System > Advanced > Developer options > Enable OEM Unlocking

Enable ADB debugging

Go to Settings > System > Advanced > Developer options > USB debugging

Background
System partitions are no longer just a simple block device (and for good reason). To increase the system partition you must work with the super partition. As you’ll see below, you can either increase the system.img partition completely within the super partition, and be done–or, if you need more than 2GB extra, you’ll need to resize the super partition and resize the system partition within the new super partition space.

Getting Started
There are two options to proceed. Both require expanding the system partition. The first option is by far the easiest, but really only works well if you can start from scratch and your packages only need 2GB or less. The second option is very involved, but allows you to expand the system partition just about to any size you need (taking from the userdata partition).

Resizing the Underlying Partition (Not Easy!)

Only run through this part if you really need more than an extra 2GB in your system partition. For example, if you want a 6GB system partition with 4GB+ free.

  • Open a shell and run
  1. adb reboot bootloader
  2. fastboot flashing unlock
  3. fastboot flashing unlock #(Note: press Volume Up to accept)
  4. fastboot -w
  5. fastboot update lineage.zip
  • At this point, it’ll take a bit, and your device should reboot twice (IIRC). You should see LineageOS installed. Follow the instructions above to re-enable ADB debugging and run these commands in your shell:
  1. adb reboot bootloader
  2. fastboot flash recovery twrp.img
  3. fastboot reboot recovery
  4. adb pull /dev/block/by-name/super super.img
  5. lpunpack super.img .
  6. fallocate -x -l 5G system.img
  7. resize2fs system.img 5G
  8. e2fsck -yf system.img
  9. img2simg product.img product.sparse.img
  10. img2simg vendor.img vendor.sparse.img
  11. img2simg system.img system.sparse.img
  12. img2simg system_ext.img system_ext.sparse.img
  13. Run something like this (read note first!)
lpmake \
	--super-name super \
	--device super:6442450944 \
	--metadata-size=65536 \
	--metadata-slots=2 \
	--group=main:4294967296 \
	--partition=product:readonly:339254784:main --image=product=./product.sparse.img \
	--partition=vendor:readonly:386854400:main --image=vendor=./vendor.sparse.img \
	--partition=system:readonly:5368709120:main --image=system=./system.sparse.img \
	--partition=system_ext:readonly:183549440:main --image=system_ext=./system_ext.sparse.img \
	--sparse \
	--output=super.img

Very important note about lpmake:
You could technically copy and paste these numbers and likely be fine; however, to understand them better you should run: lpdump super.img. For each partition listed you’ll see a display of “0 … #####” (for example, on my product partition it was “0 … 662607”). Take this number and multiply by the sector size (hardcoded to 512: https_//android.googlesource.com/platform/system/core/+/master/fs_mgr/liblp/include/liblp/metadata_format.h#105) or look for “#define LP_SECTOR_SIZE 512”). For the system partition, I’m targeting 5GB, so I multiply 1,073,741,824 x 5 (see https_//www.aqua-calc.com/page/powers-of-two for where this number comes from for 1GB).
Also, before continue, be sure to unmount any partitions inside TWRP: Mount > Uncheck all
14. adb push parted /
15. adb push gdisk /
16. adb shell
17. chmod 0700 parted
18. chmod 0700 gdisk
The goal is now to resize the super partition to hold the new larger system partition, stealing space from userdata. Unfortunately, you must also “move” some partition data that sits between super and userdata.
19. gdisk /dev/block/mmcblk0 #Note: if your phone has multiple storage devices, the underlying block could be mmc0blk0 or mmc1blk0 and so on. I only have one, so it’s just mmcblk0.
20. p #Print the partition table, and look for “super” and “userdata” (I would expect it to be 33 and 36 for this specific device and board). Also take note of the partition names in between (for example, vbmeta and cache). Also, take note of the filesystem types (I’d expect 0700, but just be careful to take note just in case)
21. The following is to make a backup of the current partition data (at a raw level)
22. adb pull /dev/block/mmcblk0p34 vbmeta.img
23. adb pull /dev/block/mmcblk0p35 cache.img
Note, we don’t care about userdata.img, since we’re going to reformat that anyway.
24. adb shell
25. gdisk /dev/block/mmcblk0
Note, we are now going to resize everything and delete partitions and change where they start. Run the following commands within gdisk
26. d
27. 33
28. d
29. 34
30. d
31. 35
32. d
33. 36
Now that these have been deleted, we will rebuild them with new boundaries. Feel free to adjust according to your needs. Here, I’m making the “super” partition 6G on disk
34. n
35. 33
36. <Enter> # accept default
37. +6G
38. 0700 # Must match the original code from “p” command, in my case, it is 0700
39. n
40. 34
41. <Enter>
42. +8M
43. 0700
44. n
45. 35
46. <Enter>
47. +432M
48. 0700
49. n
50. 36
51. <Enter>
52. <Enter> # Will fill the user partition to the end of all available space
53. 0700
54. w # write changes
55. y # confirm write
gdisk should have exited, but if not, type “q” to quit
56. parted /dev/block/mmcblk0
57. print
58. name 33 super # Restore this partition name, and all the following
59. name 34 vbmeta
60. name 35 cache
61. name 36 userdata
62. quit
63. exit # Leave adb shell
Now reflash the original partition data back to the partitions we modified (excluding userdata, since we don’t care about this one)
64. adb push vbmeta.img /dev/block/mmcblk0p34
65. adb push cache.img /dev/block/mmcblk0p35
66. adb reboot bootloader
67. fastboot flash super super.img #(I had trouble flashing the raw image, so I used the sparse one via fastboot)

Resizing System Partition (the Easy Way)
  • Extract system.img from lineage ZIP file
  • Run the following commands:
  1. simg2img system.img system.raw.img #(Note: this would be a good time to mount system.raw.img and make other changes if desired; otherwise, we’ll proceed with just increasing the filesystem itself and nothing else)
  2. fallocate -x -l 3G system.raw.img
  3. resize2fs system.raw.img 3G
  4. e2fsck -yf system.raw.img
  5. img2simg system.raw.img system.img
  • Now repack the lineage ZIP, replacing the existing system.img with the one you just modified.
  • Go back to your shell
  1. adb reboot bootloader
  2. fastboot flashing unlock #(Note: press Volume Up to accept)
  3. fastboot -w
  4. fastboot update lineage.zip
  • At this point, it’ll take a bit, and your device should reboot twice (IIRC). You should see LineageOS installed. Follow the instructions above to re-enable ADB debugging and run these commands in your shell:
  1. adb reboot bootloader
  2. fastboot flash recovery twrp.img
  3. fastboot reboot recovery
Configure GApps as Desired (optional)
  1. Create a new gapps-config.txt file, with your various changes and whatever you’re interested in or not. See https_//github.com/opengapps/opengapps/wiki/Advanced-Features-and-Options for more info.
  2. adb push gapps-config.txt /data
Install GApps

Within TWRP: Advanced > ADB Sideload > Check all > Swipe to Start Sideload
adb sideload open_gapps.zip
Note: It seems some GApps packages will cause an issue where rebooting to system will take forever, then fail and reboot back into recovery. Installing a different package version can help resolve this type of issue.

Rooting

Within TWRP: Advanced > ADB Sideload > Check all > Swipe to Start Sideload
adb sideload Magisk-v24.3.apk
Note: after installing Magisk, I had to launch the app and finish the installation. After that, root access was completely enabled.

Final Remarks
If you want to shrink the system partition image to be conservative after you’re all done, you can pull the current system image, shrink it, and rebuild the super image and reflash. It’s a bit much for my taste, but if you want to go through it, you can shrink system.img via:

resize2fs -M system.img

You’ll need to make a new system.sparse.img, recalculate the special number in “–partition=system:readonly:########:main --image=system=./system.sparse.img” to match the file size of system.img (just make sure it’s a multiple of 512), and rebuild super.sparse.img as shown previously and reflash.

1 Like