[ASUS ZenFone 8] bnsmb:
HI

Update 13.04.2023 /bs

To install Magisk v26.0 or newer the script version 25.01.2023 v2.5.1.0 is necessary.

Unfortunately there is a new feature in Magisk v26.x that prevents the full installation of Magisk from within a booted recovery; from the Magisk docs:

Therefor currently the installation of Magisk v26.x via script without user intervention does not yet work

I wrote a little shell script for Linux (bash) to add Magisk to the boot partition of a phone without user intervention called install_magisk_via_twrp.sh.

This is useful for example to re-install Magisk into the boot partition after an OS upgrade .

The script uses only the commands fastboot and adb, a recovery image (e.g. TWRP), and the patch script from Magisk to re-install Magisk into the boot partition.

Notes

Note that the script version 1.1.0.0 or newer also works if the phone is secured via PIN or something similar.

Since version 2.0.0.0 it is not necessary anymore to install Magisk via Magisk App into a boot image file before starting the script.

Since version 2.0.0.0 the script can install Magisk into the partition even if the Magisk app is not installed yet.

SInce version 2.1.0.0 the script will also copy the Magisk apk to the phone file if requested via parameter.

Version 2.5.1.0 of the script install_magisk_via_twrp.sh or newer of the script is necessary to install Magisk v26.1 or newer

The usage for the script is:

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./install_magisk_via_twrp.sh -h
install_magisk_via_twrp.sh version - v2.3.0.0 - add Magisk to the boot partition of a phone running Android using TWRP

install_magisk_via_twrp.sh [-h|help|-H] [boot_slot] [wait=n] [dd|fastboot] [twrp_image] [cleanup] [delete_adb_dir] [adb_only] [use_apk] [copy_apk]

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $

Use the parameter -H to print the detailed usage:

Spoiler content hidden.

In the default the script will ask the user for input before rebooting the phone from the patched boot partition.

To disable this request call the script like this

REBOOT=yes install_magisk_via_twrp.sh

Additional infos

Prerequisites

the Magisk App; either installed or as apk file

a computer running Linux with working adb and fastboot binaries available via PATH variable

a phone with unlocked boot loader

a working recovery image (e.g. TWRP) for the attached phone that automatically mounts /data and has enabled adb support (For the ASUS Zenfone 8 the official TWRP image from https://twrp.me/asus/zenfone8.html version 3.7.0 or newer can be used)

Test Environment

Tested on an ASUS Zenfone 8 and with

OmniROM 12 (Android 12) and Magisk v24.3

OmniROM 12 (Android 12) and Magisk v25.0

OmniROM 12 (Android 12) and Magisk v25.2

OmniROM 13 (Android 13) and Magisk v25.2

AospExtended 9.0 w/o GAPPS (Android 12) and Magisk v24.3

AospExtended 9.0 w/o GAPPS (Android 12) and Magisk v25.0

ASUS Original Android 11 and Magisk v24.3

ASUS Original Android 12 and Magisk v24.3

ASUS Original Android 13 Beta 1 and Magisk v25.0

Details

The script uses the scripts and binaries from the directory /data/adb/magisk. If these executables do not exist the script searches for the installed Magisk apk file /data/app/*/com.topjohnwu.magisk*/base.apk and extracts the scripts and binaries from that file. If the Magisk apk is not installed the script searches for the Magisk APK file Magisk*.apk in the directories /sdcard/Download and /data and extracts the scripts and executables from that file if found.

If the parameter copy_apk is used the script copies the Magisk apk file to the directory /sdcard/Download on the phone or, if that directory does not exist, to the directory /data on the phone

The patched boot images will be created in the directory /sdcard/Downloads on the phone (variable DOWNLOAD_DIR_ON_PHONE)

The script uses the original script called boot_patch.sh from Magisk to patch the boot image.

You can execute the script as often as you like.

The script will not delete the created files in the directory /sdcard/Download on the phone so you might do a cleanup of that directory manual

If the directory /sdcard/Download is not available the script will use the directory /tmp for temporary files (Note: /tmp is mounted on a ramdisk)

If the check sum of the boot image before the patching is equal to the check sum after the successfull patching then Magisk was already installed in the boot partition

The script will boot the phone up to 4 times.

Trouble Shooting

- Error message like this

/data/adb/magisk/boot_patch.sh[211]: can't create /proc/self/fd/: Is a directory

can be ignored (these are only error messages from the function to print some additional messages)

Note: Since version 2.0.0.0 these error messages should not occur anymore.

- Patching the boot partition via "dd" fails sometimes for unknown reason so you should only use it if you know how to fix a damaged boot partition!

- Downloading the patched boot image via "adb pull" (neccessary for patching the boot partition using fastboot) sometimes fails for unknown reason:

So if you get an error message like this:

[ 77%] /sdcard/Download/patched_boot_a.391985.img
ERROR: Error downloading the file "/sdcard/Download/patched_boot_a.391985.img" from the phone!

just restart the script again (or download the file and flash the boot partition manually)

- If the adb connection dies and there are error messages like this

adb: insufficient permissions for device

restart the adb server using

adb kill-server

if that does not work disconnect and reconnect the USB cable

Spoiler content hidden.

Update 03.07.2022 /bs

The script now uses the directory /tmp for temporary file if the directory /sdcard/Download is not available. Therefor it will also work if the phone is secured by a PIN or something similar.

Update 05.07.2022/bs

The script is also available on my web site:

http://bnsmb.de/files/public/Android/install_magisk_via_twrp.sh

Update 30.12.2022/bs

Updated the usage help for the script to match the version 2..3.0 of the script

Update 28.10.2022 /bs

Updated the post to match the new script version 2.0.0.0

Update 17.12.2022 /bs

Updated the post to match the new script version 2.1.0.0

see here for How to install TWRP via script

Update 13.04.2023 /bs

To install Magisk v26.0 or newer the script version 25.01.2023 v2.5.1.0 is necessary.

Unfortunately there is a new feature in Magisk v26.x that prevents the full installation of Magisk from within a booted recovery; from the Magisk docs:

Therefor currently the installation of Magisk v26.x via script without user intervention does not yet work