[ASUS ZenFone 8] bnsmb:
After installing LineageOS 20.x on a phone, booting the phone from the TWRP image using the command

sudo fastboot boot <twrp_image_file>

does not work anymore : The phone starts to boot; prints the ASUS logo and hangs forever (at least on the ASUS Zenfone 8).

To work around this annoying problem, it is necessary to create a new TWRP boot image file using the boot image from the LineageOS and the ramdisk and kernel parameter from the TWRP image.

This can be done as follows:

Download the image file for the boot partition (boot.img) used for LineageOS 20.x for your phone

For the ASUS Zenfone 8 the file boot.img can be downloaded from this URL: https://download.lineageos.org/devices/sake/builds:

wget https://mirrorbits.lineageos.org/full/sake/20230616/boot.img

Unpack the boot image file using the Magisk binary magiskboot:

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ ls -ltr
total 98308
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 100663296 Jun 16 11:19 boot.img
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

# the parameter -h for magiskboot is necessary to create the file header
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ magiskboot unpack -h boot.img
Parsing boot image: [boot.img]
HEADER_VER [4]
KERNEL_SZ [39909888]
RAMDISK_SZ [1663229]
OS_VERSION [13.0.0]
OS_PATCH_LEVEL [2023-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [lz4_legacy]
VBMETA
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ ls -ltr
total 140364
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 100663296 Jun 16 11:19 boot.img
-rw-r--r--. 1 xtrnaw7 xtrnaw7 39909888 Jun 25 19:00 kernel
-rw-rw-rw-. 1 xtrnaw7 xtrnaw7 50 Jun 25 19:00 header
-rw-r--r--. 1 xtrnaw7 xtrnaw7 3147776 Jun 25 19:00 ramdisk.cpio
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

In another directory unpack the TWRP image for the phone using magiskboot:

# the parameter -h for magiskboot is necessary to create the file header
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $ magiskboot unpack -h ../../twrp-3.7.0_12-0-I006D-enhanced.img
Parsing boot image: [../../twrp-3.7.0_12-0-I006D-enhanced.img]
HEADER_VER [3]
KERNEL_SZ [18618540]
RAMDISK_SZ [38299974]
OS_VERSION [99.87.36]
OS_PATCH_LEVEL [2099-12]
PAGESIZE [4096]
CMDLINE [twrpfastboot=1]
KERNEL_FMT [gzip]
RAMDISK_FMT [gzip]
VBMETA
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $ ls -ltr
total 133992
-rw-rw-rw-. 1 xtrnaw7 xtrnaw7 66 Jun 25 19:03 header
-rw-r--r--. 1 xtrnaw7 xtrnaw7 41943552 Jun 25 19:03 kernel
-rw-r--r--. 1 xtrnaw7 xtrnaw7 95251796 Jun 25 19:03 ramdisk.cpio
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $

Now copy the file ramdisk.cpio file from the TWRP image to the directory with the unpacked boot image file from the LineageOS :

# backup the existing ramdisk file
#
cp /data/develop/android/twrp_for_lineageos/ramdisk.cpio /data/develop/android/twrp_for_lineageos/ramdisk.cpio.org

# copy the ramdisk file from the TWRP image
#
cp /data/develop/android/twrp_for_lineageos/twrp/ramdisk.cpio /data/develop/android/twrp_for_lineageos/ramdisk.cpio

Then copy the kernel parameter from the TWRP image to the file header from the LineageOS boot image file:

# print the kernel parameter from the TWRP image
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $ grep cmd /data/develop/android/twrp_for_lineageos/twrp/header
cmdline=twrpfastboot=1

# backup the file header from the LineageOS boot image file
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $ cp /data/develop/android/twrp_for_lineageos/header /data/develop/android/twrp_for_lineageos/header.org

# add the kernel parameter from the TWRP image to the file header from the LineageOS
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ sed -i -e "s/cmdline=/cmdline=twrpfastboot=1/g" /data/develop/android/twrp_for_lineageos/header
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

# check the result
#
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ] $ cat /data/develop/android/twrp_for_lineageos/header
cmdline=twrpfastboot=1
os_version=13.0.0
os_patch_level=2023-05
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos/twrp ]

Now repack the boot image file from the LineageOS 20.x to create the new TWRP image:

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ magiskboot repack ./boot.img ./twrp_3.7.0_12-0-I006D_for_lineageOS20.img
Parsing boot image: [./boot.img]
HEADER_VER [4]
KERNEL_SZ [39909888]
RAMDISK_SZ [1663229]
OS_VERSION [13.0.0]
OS_PATCH_LEVEL [2023-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [lz4_legacy]
VBMETA
Repack to boot image: [./twrp_3.7.0_12-0-I006D_for_lineageOS20.img]
HEADER_VER [4]
KERNEL_SZ [39909888]
RAMDISK_SZ [44166258]
OS_VERSION [13.0.0]
OS_PATCH_LEVEL [2023-05]
PAGESIZE [4096]
CMDLINE [twrpfastboot=1]
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $ ls -ltr twrp_3.7.0_12-0-I006D_for_lineageOS20.img
-rw-r--r--. 1 xtrnaw7 xtrnaw7 100663296 Jun 25 19:11 twrp_3.7.0_12-0-I006D_for_lineageOS20.img
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrp_for_lineageos ] $

The TWRP image file twrp_3.7.0_12-0-I006D_for_lineageOS20.img can now be used boot the phone via fastboot if the LineageOS 20.x is installed.

I successfuly tested this on an ASUS Zenfone 8 with the Lineage OS 20.x from the file lineage-20.0-20230616-nightly-sake-signed.zip and the TWRP image file twrp-3.7.0_12-0-I006D-enhanced.img.

Update 26.04.2024

I also tested this approach successfully with the in-official LineageOS 21.x image lineage-21.0-20240220-UNOFFICIAL-KSU-GPU_UV-sake.zip and the TWRP image file twrp-3.7.0_12-0-I006D.img.

Update 27.04.2024

Note that the TWRP image for LineageOS created using this method is version and build (!) dependent : A TWRP image must be created for each LineageOS image (even with the same LineageOS version).

And the TWRP images for LineageOS cannot be used if another ROM is installed on the phone.

Therefor this is really only a very ugly workaround!

Update 25.06.2024

I also tested this approach successfully with the StatixOS and LMODroid

Issues

The TWRP GUI does not resume after the screensaver from TWRP was active (-> the screen remains black; the access to TWRP via adb shell still works)

Workaround

increase the screen timeout in the TWRP GUI

(The TWRP settings are stored in the config file /data/media/0/TWRP/.twrps; this file is a binary file that can not be edited)

I did not find any other issues until now .. but I only did a few tests. I guess there are most probably more issues.

But the main function I use TWRP for in this configuration is access via adb - and that works.

(The recovery from LineageOS also supports access via adb but adb is disabled after booting into the recovery and must be manually enabled in the GUI of the recovery)

Notes

Booting the phone from the recovery image for LineageOS 19.x does also not work anymore after installing LineageOS 20.

The file boot.img from the Download page for LineageOS 20.x (https://download.lineageos.org/devices/sake/builds for the ASUS Zenfone 8) is also the Recovery image for that OS.

Using the file boot.img to boot the phone via sudo fastboot boot boot.img does not work

Unpacking and repacking the boot image files in the example above was done on a PC running the Linux OS and using the x86 version of magiskboot from the Magisk apk file.

To extract the magiskboot binary for 64 Bit Linux from the apk file use these commands:

unzip -p Magisk-v26.1.apk lib/x86_64/libmagiskboot.so >magiskboot
chmod 755 magiskboot

The x86 binaries from the Magisk apk file are static linked binaries and should work on every Linux OS running on x86.

The commands listed above can also be done in an adb session on the phone with installed Magisk or in a booted TWRP image if no machine running Linux is available.