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 docs:
This means that any installation of Magisk v26+ using
custom recovery will be incomplete; a subsequent
re-installation through the Magisk app after booting up is
required.
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 help:
Detailed usage
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
Usage
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]
All parameter are optional. The parameter can be
used in any order.
Use the parameter "help" or "-H" to print the
detailed usage help; use the parameter "-h" to print only
the short usage help
The parameter "boot_slot" can be a, b, active,
inactive, next, current; default is the current boot slot
of the phone
The value for the parameter "wait" is the number of
seconds to wait before starting the script to install
Magisk on the phone
This seems to be necessary to avoid errors while
repacking the boot image. The default wait time is 10
seconds.
Use the parameter "dd" to request repatching via dd
in an adb session ; use the parameter "fastboot" to
request repatching via "fastboot"
Default is to use "dd" to flash the patched boot
image.
The parameter "twrp_image" can be used to define
another TWRP image to use. The parameter is optional - the
default for "twrp_image" is hardcoded in the script
(variable TWRP_IMAGE)
The default TWRP image of the script is the TWRP for
the ASUS Zenfone 8.
Use the parameter "cleanup" to delete the directory
/data/MagiskInst at script end
If the parameter "delete_adb_dir" is used the script
will delete all files and directories created in the
directory /data after Magisk
was successfully installed into the boot partition
If the parameter "adb_only" is used the script will
only install the directories and binaries for Magisk in
the directory /data/adb.
The script will unpack the necesseray files for
adding Magisk to the boot partition from the installed
Magisk apk file to
the temporary directory /data/MagiskInst if the
files in /data/adb/magisk are missing.
Use the parameter "use_apk" to force the script to
use the files from the Magisk app even if the files in
/data/adb/magisk exist.
If the parameter "copy_apk" is used the script will
copy the Magisk apk file to the phone.
The script now can also install Magisk into a boot
partition without an installed Magisk app. To use this
feature copy the Magisk apk file
(Magisk*apk) to the directory /data or
/sdcard/Download on the phone before starting the script
or use the parameter "copy_apk".
The phone to patch must be attached via USB.
The phone can be either in fastboot mode, in normal
mode with enabled adb support, or already booted from the
TWRP image
To change some of the values used by the script you
can set environment variables before starting the script:
Set the environment variable REBOOT to "yes"
before starting the script to automatically reboot the
phone after patching the new image
Set the environment variable REBOOT to "no"
before starting the script to disable the automatic reboot
after patching the new image.
Set the environment variable SERIAL_NUMBER to
the serial number of the phone to patch if there is more
then one phone connected via USB
Set the environment variable ADB_OPTIONS to
the options to be used with the adb command
Set the environment variable FASTBOOT_OPTIONS
to the options to be used with the fastboot command
Set the environment variable
DOWNLOAD_DIR_ON_PHONE to the data directory to use on the
phone (def.: /sdcard/Download)
Set the envionment variable MAGISK_APK_FILE
to the Magisk apk file to use
The default TWRP image to use is
"/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
[ OmniRom 13 Dev - xtrnaw7@t15g
/data/develop/android/scripts_on_linux ] $
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
if that does not work disconnect and reconnect the USB cable
Example output of the script
install_magisk_via_twrp.sh
[xtrnaw7@t15g /data/backup/Android/scripts_on_linux]$ ./install_magisk_via_twrp.sh
./install_magisk_via_twrp.sh version - 2.0.0.0 - add Magisk to the boot partition of a phone running Android using TWRP
Using the TWRP image hardcoded in the script: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Checking the script prerequisites ...
Will wait 10 second(s) before starting the script on the phone
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will patch the boot partition on the attached phone with the serial number "M6AIB760D0939LX"
Using the options "-d -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking for a connected phone booted into fastboot mode ...
No attached phone in fastboot mode found
Checking for an attached phone with working access via adb (USB) ...
Linux localhost 5.4.147-Omni-qgki-perf-gea6f815f5af9-dirty #42 SMP PREEMPT Tue Sep 27 18:03:41 CEST 2022 aarch64
... found a phone connected via USB with working adb access
The phone is booted in normal mode
The installed OS is based on Android 12 (Wed Aug 10 06:59:46 CEST 2022); the description for the distribution is "omni_zenfone8-user 12 SQ3A.220605.009.A1 eng.xtrnaw.20220810.065947 release-keys"
The installed version of Magisk is com.topjohnwu.magisk versionCode 25200
Booting the phone into the fastboot mode now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.489s]
Booting OKAY [ 10.359s]
Finished. Total time: 12.888s
Waiting up to 60 seconds for the boot of the phone ....
Retrieving the current boot slot from the phone ...
The current boot slot is "_b"
The boot slot to patch is "_b"
The boot partition to patch is "boot_b"
Checking if the Magisk patch script "boot_patch.sh" exists on the phone ....
-rwxr-xr-x 1 root root 5987 2022-10-27 07:33 /data/adb/magisk/boot_patch.sh
Checking if the download directory "/sdcard/Download" exists on the phone ....
/sdcard/Download
Creating the boot image file "/sdcard/Download/boot_b.732627.img" from the partition "/dev/block/by-name/boot_b" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 0.308243 s, 311 M/s
Checking the result ...
-rw-rw---- 1 root media_rw 100663296 2022-10-28 09:51 /sdcard/Download/boot_b.732627.img
The check sums are:
The check sum of the boot partition "/dev/block/by-name/boot_b" on the phone is "4108888341"
The check sum of th boot image file on the phone is "/sdcard/Download/boot_b.732627.img" is "4108888341"
Patching the boot image file "/sdcard/Download/boot_b.732627.img" ...
Waiting now 10 seconds ...
- Unpacking boot image
Parsing boot image: [/sdcard/Download/boot_b.732627.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34080597]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
- Checking ramdisk status
Loading cpio: [ramdisk.cpio]
- Stock boot image detected
- Patching ramdisk
Loading cpio: [ramdisk.cpio]
Add entry [init] (0750)
Create directory [overlay.d] (0750)
Create directory [overlay.d/sbin] (0750)
Add entry [overlay.d/sbin/magisk64.xz] (0644)
Patch with flag KEEPVERITY=[false] KEEPFORCEENCRYPT=[false]
Found fstab file [system/etc/fstab.postinstall]
Remove pattern [,avb_keys=/product/etc/security/avb/system_other.avbpubkey]
Loading cpio: [ramdisk.cpio.orig]
Backup mismatch entry: [init] -> [.backup/init]
Record new entry: [overlay.d] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/magisk64.xz] -> [.backup/.rmlist]
Backup mismatch entry: [system/etc/fstab.postinstall] -> [.backup/system/etc/fstab.postinstall]
Create directory [.backup] (0000)
Add entry [.backup/.magisk] (0000)
Dump cpio: [ramdisk.cpio]
- Repacking boot image
Parsing boot image: [/sdcard/Download/boot_b.732627.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34080597]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
Repack to boot image: [new-boot.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34331854]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
Checking the result ...
-rw-r--r-- 1 root root 100663296 2022-10-28 09:51 /data/adb/magisk/new-boot.img
The patched boot image is "/sdcard/Download/patched_boot_b.732627.img"
Waiting now 5 seconds ...
Patching the partition "/dev/block/by-name/boot_b" from the patched boot image file "/sdcard/Download/patched_boot_b.732627.img" via dd ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 4.721236 s, 20 M/s
Checking the result ....
The check sums for the images and devices on the phone are:
4108888341 100663296 /sdcard/Download/boot_b.732627.img
3369959638 100663296 /sdcard/Download/patched_boot_b.732627.img
3369959638 100663296 /dev/block/by-name/boot_b
OK, patching the boot partition "/dev/block/by-name/boot_b" was successfull
*** Press return to reboot the phone now
Rebooting the phone now ...
[xtrnaw7@t15g /data/backup/Android/scripts_on_linux]$
Update 03.07.2022 /bs
The script now uses the directory
/tmp for temporary
files 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 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
Update 30.12.2022 /bs
Updated the post to match the new script
version 2.3.0.0
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 docs:
This means that any installation of Magisk v26+ using
custom recovery will be incomplete; a subsequent
re-installation through the Magisk app after booting up is
required.
Therefor currently the installation of Magisk v26.x
via script without user intervention does not yet work