[ASUS ZenFone 8] bnsmb:
After installing an operating system update on a phone with the Android operating system and A/B slots, Magisk can be installed in the boot partition of the inactive slot with the new operating system version using the "Install to inactive slot" installation option in the Magisk App.

Unfortunately this option is not available for TWRP.

Therefor I wrote a litlle script to install TWRP into the boot partition of the inactive slot with the new operating system version after an OS upgrade.

The method works on my ASUS Zenfone 8 and should in principle also work on other phones with the same architecture (Qualcomm CPU, A/B slots, and boot partitions with ram disks)

Looking at the source code for TWRP (https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp) it can be seen that the installation of TWRP is in principle only the installation of a new ramdisk:

Spoiler content hidden.

Therefor to install TWRP via script after an OS version update two steps are neccessary:

Step one

First we must create the ramdisk with TWRP; this step must only be done once for each TWRP version.

Boot the phone from the TWRP image (NOT from a TWRP installed in the recovery!), e.g:

adb rebboot bootloader
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img

Then open an adb session to the running TWRP on the phone and execute:

# create the RAM disk
#
cd / && sha256sum --status -c ramdisk-files.sha256sum
if [ $? -eq 0 ] ; then
cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/ramdisk_with_twrp.cpio && /system/bin/gzip -f /tmp/ramdisk_with_twrp.cpio
fi

# copy the RAM disk and magiskboot to a directory on the internal storage that supports the execution permission bit (not /sdcard/<something>)
#
mkdir -p /data/develop/twrp
cp /tmp/ramdisk_with_twrp.cpio.gz /data/develop/twrp/ramdisk_with_twrp.cpio
cp /bin/magiskboot /data/develop/twrp/

Notes:

It is recommended to store the files ramdisk_with_twrp.cpio and magiskboot also on the PC.

The executable magiskboot is only required if Magisk is not installed on the phone.

Now copy the script install_twrp_ramdisk.sh.sh to the phone; this script can be copied to any filesystem with write access on the phone.

That's it .

Step two

The next time an OS update should be done using these steps:

1. install the new OS version either manually or via OTA but do NOT reboot afterwards.

2. execute the script install_twrp_ramdisk.sh.sh to install TWRP into the boot partition for the inactive slot either in an shell on the phone or in an adb shell, e.g. (sh is only necessary if the script is on a filesystem that does not support the execution permission)

sh /sdcard/Download/install_twrp_ramdisk.sh

Note that the script needs root access to read and write the boot partition

3. optional: Install Magisk into the new boot partition using the install option "Install to inactive slot" in the Magisk App

4. reboot the phone to activate the new OS version

Test Environment

ASUS Zenfone 8

OmniROM 13

Magisk v25.2

TWRP 3.7.0

Notes

Set the environment variable WORK_DIR before executing the script to use another directory with the image

Set the environment variable TWRP_RAMDISK_NAME before executing the script to use another filename for the ram disk image.

see How to add additional files to an TWRP image for how to add additional files to the TWRP image

see How to install TWRP via script for how to install TWRP with a script running on the PC

Usage for the script install_twrp_ramdisk.sh:

Spoiler content hidden.

Update 04.06.2023:

see https://github.com/Magisk-Modules-Repo/twrp-keep for a Magisk module with a script to reinstall TWRP into the inactive boot partition after an OS Udate

Update 11.06.2023:

I successfully tested the Magisk Module twrp-keep on an ASUS Zenfone 8 with OmniROM 13, TRWP 3.7.0_12, and Magisk v26.2.

ToDo:

Create an App with GUI to execute the script