My HowTos for Android

Last Update: 23.09.2023/bs

Contents

 
  1. My HowTos for Android
    1. Contents
    2. Introduction
    3. History
    4. HowTos
      1. How to compile the OmniROM for the ASUS Zenfone 8
      2. How to use the Hardware Test App from ASUS on a Zenfone running a CustomROM
      3. How to run a script at every boot using Magisk
      4. How to install Magisk into the boot partition using a script
      5. How to change files in the directory /system with Magisk
      6. How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)
      7. How to install packages (apk files) for Android via script
      8. How to replace the Fake Store from OmniROM with MicroG with a patched Playstore
      9. How to change the home directory for the user root on an Android phone
      10. Some hints for using Magisk on Android phones
      11. How to change files in the boot image using Magisk
      12. How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8
      13. How to create a swap device in the OmniROM 12 using Magisk
      14. How to switch the active slot
      15. How to run a script at shutdown
      16. How to trigger an action when a property is changed
      17. How to install TWRP via script
      18. How to add additional files to an TWRP image
      19. How to process Android boot image files on a PC running the Linux OS
      20. How to make files in /system writable
      21. How to boot a phone from a TWRP recovery image via script
      22. How to restore the boot partition to remove Magisk
      23. How to compile the OmniROM 13 for the ASUS Zenfone 8
      24. How to change files in Android XML Binary format
      25. How to compile TWRP for the ASUS Zenfone 8
      26. Some hints for using TWRP
      27. How to enable root access using Magisk in a script
      28. Where does the Magisk App store the settings?
      29. How to install an OS image using the TWRP binary twrp
      30. How to "fix" the error "Cannot load Android system. Your data may be corrupt."
      31. How to enable access via adb on a new installed OS
      32. Some hints about the Android safe mode
      33. How to install and configure the Android OS via Script
      34. How to change any file or directory using Magisk
        1. How to use an overlay filesystem to make filesystems writable
      35. How to configure the WiFI in Android via script
      36. How to configure the WiFI in Android via CLI command
      37. How to install Magisk v26.0 or newer via script
      38. How to recover data from a phone a with non-working touch screen
      39. How to re-install the Android OS from ASUS on the ASUS Zenfone 8
      40. How to Install TWRP into the boot partition again after the installation of an OS update
      41. Hints for adding files to the boot partition
      42. How to install a more recent version of the Playstore in the OmniROM
      43. How to upgrade the OS with another Android "distribution"
      44. How to temporary change a file on a read-only filesystem in the Android OS
      45. How to change the kernel boot parameter for the Android kernel
      46. How to use TWRP if LineageOS 20.x is installed
      47. ASUS Zenfone 8 FIrmware Download
    5. Links
      1. Files mentioned in the HowTos
      2. Other Android related files
      3. General Android LInks
      4. TWRP related links
      5. Magisk related links
      6. Links for the OmniROM
      7. Links for ROM Development
      8. Links for useful Tools
      9. Links for the ASUS Zenfone 8
      10. Distributions for the ASUS Zenfone 8
      11. Misc Links
 

Introduction


On this page I mirror my HowTos regarding Android which I published elsewhere (for example in the XDA Forum)

The list of my HowTos in the XDA Forum can be found using this search: https://forum.xda-developers.com/f/asus-zenfone-8.12291/?prefix_id=37&starter_id=8498037

There is also a section with links to other useful URLs for using or developing Android on this page.
 


Back to my home page


History


ChangeLog


23.09.2023

updated the list of links for the ASUS Zenfone 8 

11.09.2023

added the section How to configure the WiFI in Android via CLI command 



26.08.2023/bs


Added a link to an XDA Forum entry with a list of firmware image availabe on non-ASUS websites


17.08.2023 /bs

added the section ASUS Zenfone 8 FIrmware Download 


11.07.2023 /bs

added more details about how to configure the SELinux context for the new file in the section How to change any file or directory using Magisk


08.07.2023 /bs

small error corrections in some of the howtos


30.06.2023 /bs

updarted How to change the kernel boot parameter for the Android kernel 


29.06.2023 /bs

published How to change the kernel boot parameter for the Android kernel 


26.06.2023 /bs

published the section How to use TWRP if LineageOS 20.x is installed  
updated the section Some hints for using TWRP (see here)


25.06.2023 /bs

added the section How to change the kernel boot parameter for the Android kernel 
added the section How to use TWRP if LineageOS 20.x is installed



24.06.2023 /bs

added infos about using an overlayfs to make read-only filesystems writable to the section How to change any file or directory using Magisk  (see here)


12.06.2023 /bs

finished and published the section How to temporary change a file on a read-only filesystem in the Android OS 


11.06.2023/bs

updated the section How to install TWRP into the boot partition again after the installation of an OS update 
added the section How to upgrade the OS with another Android "distribution" 
added the section How to temporary change a file on a read-only filesystem in the Android OS 



08.06.2023 /bs

updated the trouble shooting instruction in the section How to install a more recent version of the Playstore in OmniROM  


05.06.2023 /bs

added a link to a Magisk module with another script to do the same to the section How to install TWRP into the boot partition again after the installation of an OS update 
added the section How to install a more recent version of the Playstore in OmniROM  



26.05.2023 /bs

added a small hint about the variable MAGISKTMP to the section Hints for adding files to the boot partition 


24.05.2023 /bs

corrected some not working links
added the section Hints for adding files to the boot partition 


19.05.2023 /bs

updated the section Some hints for using TWRP (see here)


16.05.2023 /bs

added the section How to install TWRP into the boot partition again after the installation of an OS update  


12.05.2023 /bs

added the section How to re-install the Android OS from ASUS on the ASUS Zenfone 8  
updated the section Some hints for using TWRP (see here)


07.05.2023 /bs

updated the section Some hints for using Magisk on Android phones - see also here.


06.05.2023 /bs

updated the section How to install Magisk into the boot partition using a script
added the version of the scripts in the list of available scripts 
fixed some typos

30.04.2023 /bs

updated the section Some hints for using TWRP (see here)


29.04.2023/bs

corrected the URL because for the XDA post about How to install Magisk v26.0 or newer via script was moved by the forum admins


23.04.2023 /bs

updated the section Some hints for using TWRP (see here)
added the section How to recover data from a phone with non-working touch screen 


18.04.2023 /bs

added the section How to install Magisk v26.0 or newer via script 


13.04.2023 /bs

updated the section How to install Magisk into the boot partition using a script with hints about installing Magisk v26.x via script


15.03.2023 /bs

Added the section Other Android related files  


02.02.2023 /bs

add the section How to configure the WiFi in Android via script 


27.01.2023 /bs

updated the section How to compile the OmniROM 13 for the ASUS Zenfone 8
updated and released the section How to install and configure the Android OS via script 


18.01.2023 /bs

added the section How to install and configure the Android OS 
added the section How to change any file or directory using Magisk 


11.01.2023 /bs

updated the section How to enable access via adb on a new installed OS 


01.01.2023 /bs

Added the section Some hints about the Android safe mode 


30.12.2022 /bs

updated the section Some hints for using Magisk on Android phones - see also here.
updated the section How to change files in the boot image using Magisk - see also here.


29.12.2022 /s

added the section How to enable access via adb on a new installed OS 


27.12.2022 /bs

updated the section Some hints for using TWRP (see also here)
added the section How to fix the error Cannot load Android system. Your data may be corrupt. 


19.12.2022 /bs

added the links to some distributions for the ASUS Zenfone 8 


18.12.2022 /bs

added the section How to enable adb via script
updated the section How to boot a phone from a TWRP recovery image via script
updated the section How to install Magisk into the boot partition using a script to match version 2.1.0.0 of the script
added the section How to install an OS image using the TWRP binary twrp  


15.12.2022 /bs

updated the Magisk module addbin 


07.12.2022 /bs

updated and published the section How to boot a phone from a TWRP recovery image via script


06.12.2022 /bs

added the section How to enable root access using Magisk in a script 
update the info about the touch screen bug the section TWRP related links 
added the section Where does the Magisk App store the settings? 


02.12.2022 /bs

updated the section How to boot a phone from a TWRP recovery image via script  (the script was rewritten using functions now)
updated the section Some hints for using Magisk on Android phones - see also here.
added the section Some hints for using TWRP 


27.11.2022/ bs

added the section How to compile TWRP for the ASUS Zenfone 8


26.11.2022 / bs

updated the section How to boot a phone from a TWRP recovery image via script


25.11.2022 /bs

updated the section Some hints for using Magisk on Android phones - see also here.


23.11.2022 /bs

updated the section How to change files in Android XML Binary format 


21.11.2022 /bs

added the section How to change files in Android XML Binary format 


14.11.2022 /bs

added the section How to compile the OmniROM 13 for the ASUS Zenfone 8 


08.11.2022/bs

updated the section Some hints for using Magisk on Android phones - see also here.


07.11.2022/bs

added the section How to restore the boot partition to remove Magisk
updated the section Some hints for using Magisk on Android phones - see also here.
all links to the other posts in the posts are now links to the section in this page


06.11.2022 /bs

added the section How to boot a phone from a TWRP recovery image via script
updated the section How to switch the active slot 
updated the section Some hints for using Magisk on Android phones - see also here.


04.11.2022/bs

added the section How to make files in /system writable 
added the section HowTo Overview  
updated the section Some hints for using Magisk on Android phones - see also here.


03.11.2022 /bs

updated the section How to compile the OmniROM for the ASUS Zenfone 8 


02.11.2022 /bs

added the section How to process boot image files on a PC running the Linux OS 


29.10.2022 /bs

added the section How to add additional files to an TWRP image 
corrected invalid URLs in the section with the URLs


28.10.2022 /bs


added the section How to install TWRP via script 
added the section TWRP related links 
updated the section Some hints for using Magisk on Android phones - see also here.
updated the section How to install Magisk into the boot partition using a script to match version 2.0.0 of the script


04.10.2022 /bs

added the section How to trigger an action when a property is changed  


02.10.2022 /bs

added the section How to run a script at shutdown 


30.09.2022 /bs

added infos about how to change files in the root filesystem using Magisk to the section How to change files in the boot image using Magisk 
updated the section Some hints for using Magisk on Android phones - see also here.


29.09.2022 /bs

updated the section How to compile the OmniROM for the ASUS Zenfone 8 
added the section How to switch the active slot 


20.09.2022 /bs

added the section How to change files in the boot image using Magisk 
added the section How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8 
added the section How to create a swap device in the OmniROM 12 using Magisk  
added the section Misc Links   


07.08.2022 /bs

updated the section Some hints for using Magisk on Android phones - see also here.
updated the section with links


05.08.2022/bs

new section with link to various tools 


04.08.2022 /bs

added test instructions to instructions to use the ASUS Hardware Testtool   


23.07.2022 /bs

added the section Some hints for using Magisk on Android phones 


19.07.2022 /bs

updated the section with links

 
16.07.2022 /bs

added the section How to change the home directory for the user root on an Android phone
fixed some non-working links


05.07.2022 /bs

added the section How to install packages (apk files) for Android via script 
added the section How to replace the Fake Store from OmniROM with MicroG with a patched Playstore 


04.07.2022 /bs

updated the script to install Magisk 
added the section with Links


28.06.2022 /bs

added the section How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone) 


26.06.2022 /bs

minor fixes in the instructions to use the ASUS Hardware Testtool 


25.06.2022 /bs

initial release


 

HowTos

 



Howto Overview

List of URLs for all also elsewhere published HowTos sorted by type of the HowTo


Android related articles


How to install packages (apk files) for Android via script

https://forum.xda-developers.com/t/how-to-install-packages-apk-files-for-android-via-script.4464987/


How to trigger an action when a property is changed

https://forum.xda-developers.com/t/how-to-trigger-an-action-when-a-property-is-changed.4501489/


How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)


https://forum.xda-developers.com/t/how-to-backup-the-data-from-the-phone-using-rsync-and-ssh-including-some-hints-for-using-sshd-on-an-android-phone.4462327/


How to change the home directory for the user root on an Android phone

https://forum.xda-developers.com/t/how-to-change-the-home-directory-for-the-user-root-on-an-android-phone.4469305/


How to process Android boot image files on a PC running the Linux OS

https://forum.xda-developers.com/t/how-to-process-android-boot-image-files-on-a-pc-running-the-linux-os.4513845/


How to change files in Android XML Binary format

https://forum.xda-developers.com/t/how-to-change-files-in-android-xml-binary-format.4521513/


How to fix the error "Cannot load Android system. Your data may be corrupt".

https://forum.xda-developers.com/t/how-to-fix-the-error-cannot-load-android-system-your-data-may-be-corrupt.4534237/


How to enable access via adb on a new installed OS

https://forum.xda-developers.com/t/how-to-enable-access-via-adb-on-a-new-installed-os.4535165/


Some hints about the Android safe mode

https://forum.xda-developers.com/t/some-hints-about-the-android-safe-mode.4536269/


How to install and configure the Android OS via script

https://forum.xda-developers.com/t/how-to-install-and-configure-the-android-os-via-script.4546375/


How to configure the WiFI in Android via Script

https://forum.xda-developers.com/t/how-to-configure-the-wifi-in-android-via-script.4548489/


How to configure the WiFI in Android via cli command

https://forum.xda-developers.com/t/how-to-configure-the-wifi-in-android-via-cli-command.4626435/


How to upgrade the OS with another Android "distribution"

https://forum.xda-developers.com/t/how-to-upgrade-the-os-with-another-android-distribution.4595049/


How to temporary change a file on a read-only filesystem in the Android OS 

https://forum.xda-developers.com/t/how-to-temporary-change-a-file-on-a-read-only-filesystem-in-the-android-os.4595429/


How to change the kernel boot parameter for the Android kernel 

https://forum.xda-developers.com/t/how-to-change-the-kernel-boot-parameter-for-the-android-kernel.4600821/




Magisk related articles


Some hints for using Magisk on Android phones

https://forum.xda-developers.com/t/some-hints-for-using-magisk-on-android-phones.4471857/


Where does the Magisk App store the settings?

https://forum.xda-developers.com/t/where-does-the-magisk-app-store-the-settings.4527129/


How to install Magisk into the boot partition using a script

https://forum.xda-developers.com/t/how-to-install-magisk-into-the-boot-partition-using-a-script.4456621/


How to change files in the directory /system with Magisk

https://forum.xda-developers.com/t/how-to-change-files-in-the-directory-system-with-magisk.4454489/


How to change any file or directory using Magisk

https://forum.xda-developers.com/t/how-to-change-any-file-or-directory-using-magisk.4543103/



How to run a script at every boot using Magisk

https://forum.xda-developers.com/t/how-to-run-at-script-a-every-boot-using-magisk.4454493/


How to change files in the boot image using Magisk

https://forum.xda-developers.com/t/how-to-change-files-in-the-boot-image-using-magisk.4495645/


Hints for adding files to the boot partition 

https://forum.xda-developers.com/t/how-to-change-files-in-the-boot-image-using-magisk.4495645/#post-88571069


How to run a script at shutdown

https://forum.xda-developers.com/t/how-to-run-a-script-at-shutdown.4500719/


How to restore the boot partition to remove Magisk


https://forum.xda-developers.com/t/how-to-restore-the-boot-partition-to-remove-magisk.4515759/


How to enable root access using Magisk in a script

https://forum.xda-developers.com/t/how-to-enable-root-access-using-magisk-in-a-script.4527035/


How to install Magisk v26.0 or newer via script

https://forum.xda-developers.com/t/how-to-install-magisk-into-the-boot-partition-using-a-script.4456621/post-88429939




TWRP related articles


Some hints for using TWRP

https://forum.xda-developers.com/t/some-hints-using-twrp.4525833/


How to install TWRP via script

https://forum.xda-developers.com/t/how-to-install-twrp-via-script.4511355/


How to add additional files to an TWRP image

https://forum.xda-developers.com/t/how-to-add-additional-files-to-an-twrp-image.4511827/


How to compile TWRP for the ASUS Zenfone 8

https://forum.xda-developers.com/t/how-to-compile-twrp-for-the-asus-zenfone-8.4523817/


How to boot a phone from a TWRP recovery image via script

https://forum.xda-developers.com/t/how-to-boot-a-phone-from-a-twrp-recovery-image-via-script.4527547/


How to install an OS image using the TWRP binary twrp

https://forum.xda-developers.com/t/how-to-install-an-os-image-using-the-twrp-binary-twrp.4531345/


How to recover data from a phone with non-working touch screen

https://forum.xda-developers.com/t/how-to-recover-data-from-a-phone-with-a-non-working-touch-screen.4578795/


How to install TWRP into the boot partition again after the installation of an OS update

https://forum.xda-developers.com/t/installing-twrp-into-the-boot-partition-again-after-the-installation-of-an-os-update.4586725/


How to use TWRP if LineageOS 20.x is installed

https://forum.xda-developers.com/t/how-to-use-twrp-if-lineageos-20-x-is-installed.4599721/




OmniROM related articles


How to compile the OmniROM for the ASUS Zenfone 8

https://forum.xda-developers.com/t/how-to-compile-the-omnirom-for-the-asus-zenfone-8.4461525/


How to compile the OmniROM 13 for the ASUS Zenfone 8

https://forum.xda-developers.com/t/how-to-compile-the-omnirom-13-for-the-android-zenfone-8.4518733/


How to replace the Fake Store from OmniROM with MicroG with a patched Playstore

https://forum.xda-developers.com/t/magisk-module-to-replace-the-fakestore-in-the-omnirom-with-a-patched-playstore-from-nanodroid.4456421/


How to install a more recent version of the Playstore in OmniROM

https://forum.xda-developers.com/t/how-to-install-a-more-recent-version-of-the-playstore-in-the-omnirom.4593203/


How to create a swap device in the OmniROM 12 using Magisk

https://forum.xda-developers.com/t/how-to-create-or-change-a-swap-device-in-the-omnirom-using-magisk.4495415/




ASUS Zenfone 8 related articles


How to use the Hardware Test App from ASUS on a Zenfone running a CustomROM

https://forum.xda-developers.com/t/how-to-use-the-hardware-test-app-from-asus-on-a-zenfone-running-a-customrom.4461533/


How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8

https://forum.xda-developers.com/t/how-to-disable-or-change-the-swap-device-in-the-android-12-from-asus-for-the-zenfone-8.4495085/


How to re-install the Android OS from ASUS on the ASUS Zenfone 8

https://forum.xda-developers.com/t/asus-zenfone-8-how-to-rollback-from-lineageos-to-original-rom.4583833/#post-88525935


Download firmware for the ASUS Zenfone 8

https://forum.xda-developers.com/t/zenfone-8-need-android-12-firmware-to-downgrade-from-android-13.4617265/#post-88887299





General articles


How to switch the active slot

https://forum.xda-developers.com/t/how-to-manually-switch-the-active-slot.4499789/







The HowTos are listed below in the chronological order in which I created them.
 

How to compile the OmniROM for the ASUS Zenfone 8


URL:  https://forum.xda-developers.com/t/how-to-compile-the-omnirom-for-the-asus-zenfone-8.4461525/

How to compile the OmniROM for the ASUS Zenfone 8


Update 14.11.2022 /bs

This HowTo is about how to compile the OmniROM 12 for the ASUS Zenfone 8 --  see How to compile the OmniROM 13 for the ASUS Zenfone 8 for how to compile the OmniROM 13 for the ASUS Zenfone 8.

Correction

There was a spelling error in the name of the command to list all available targets : the command name is breakfast


Compiling the CustomROM OmniROM from within Linux is a very simple and straight forward process -- the developer for the OmniROM did a really amazing good job setting up the build environment for the OmniROM.

But some information are missing in the instructions (at least I did not found them ...)

Therefor I document here what I did to compile the OmniROM for the ASUS Zenfone 8 on my Linux notebook:

First install the neccessary tool called "repo" to maintain the repositories for the OmniROM ; see here https://gerrit.googlesource.com/git-repo/

Next create a new directory on your PC; switch to that directory, and initialize the necessarry repositories like described in the section "Getting Started" here: https://github.com/omnirom/android :


Note [29.07.2022]:

The repositories for the branch android-12.0 are somehow outdated with the security patch level from February 2022 . To get the code with the security patch level from June 2022 use the repositories for the branch android-12.1.

repo init -u https://github.com/omnirom/android.git -b android-12.1

# Then to sync up:

repo sync

Be aware that the repositories need about 140 GB disk space so this will take a while.

Please note that I did not install any additional tool to compile the OmniROM - everything neccessary was installed by synching the repositories.


When the download is finished you must add the repositories for the vendor and device tree for your phone to the manifests in the local repositories. This should be done in the file .repo/local_manifests/local_manifest.xml . For the ASUS Zenfone 8 the file should look like this:

[xtrnaw7@t15g /data/develop/android/OmniROM]$ cat .repo/local_manifests/local_manifest.xml
<?xml version="1.0" encoding="UTF-8"?>

<manifest>
<remote name="DonkeyCoyote"
fetch="https://github.com/DonkeyCoyote" />

<project path="device/asus/zenfone8" name="android_device_asus_zenfone8" remote="omnirom" revision="android-12.0" />
<project path="vendor/asus" name="proprietary_vendor_asus" remote="DonkeyCoyote" revision="android-12.1" />
</manifest>

After updating the file local_manifest.xml do another

repo sync

to sync the added repositories. This time the download will be faster because only the two additional repos will be downloaded.

Now you can start the build with these commands:

. build/envsetup.sh

brunch omni_zenfone8-user

in the directory with the downloaded repositories (use the command "breakfast" instead of "brunch" to see other possible targets)

Note that the first time the compile process will take 4 hours or more. And keep in mind that the compile process will need additional 140 GB for the temporary output files.


If everything is fine there will be a ZIP file with your own CustomROM image in the directory ./out/target/product/zenfone8/ when the compile script is done, e.g:

[xtrnaw7@t15g /data/develop/android/OmniROM]$ ls -ltr out/target/product/zenfone8/omni-12-20220621-zenfone8-HOMEMADE.zip
-rw-rw-r--. 25 xtrnaw7 xtrnaw7 1253703465 Jun 25 10:03 out/target/product/zenfone8/omni-12-20220621-zenfone8-HOMEMADE.zip
[xtrnaw7@t15g /data/develop/android/OmniROM]$

That image can now be installed on the Zenfone 8 using the LineageOS Recovery or TWRP.

Update 29.09.2022 /bs

There seems to be a bug or feature in the scripts to compile OmniROM:

Old versions of the ZIP file with the image in the output directory will always be overwritten or replaced with the current image, e.g.:


[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ ls -l  /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220*zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220728-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220729-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220804-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220809-zenfone8-HOMEMADE.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220810-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220820-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220828-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220904-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220911-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220918-zenfone8-MICROG.zip
-rw-rw-r--. 20 xtrnaw7 xtrnaw7 1256445293 Sep 27 18:12 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220927-zenfone8-MICROG.zip
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ cksum /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220*zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220728-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220729-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220804-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220809-zenfone8-HOMEMADE.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220810-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220820-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220828-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220904-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220911-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220918-zenfone8-MICROG.zip
592627598 1256445293 /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20220927-zenfone8-MICROG.zip
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ]



Update 03.11.2022 /bs

When compiling aborts with error messages like these

error: vendor/qcom/opensource/interfaces/wifi/supplicant/aidl/Android.bp:16:23: unrecognized property "versions_with_info"
error: vendor/qcom/opensource/interfaces/bluetooth_audio/aidl/Android.bp:17:23: unrecognized property "versions_with_info"

Do a forced repo sync:

repo sync --force-sync

and start the compile process again .


How to create an OmniROM image with MicroG
How to create an OmniROM image with MicroG

To create an OmniROM image with MicroG these additional steps are neccesarry:

Add the repo with the prebuild MicroG apks, android_prebuilts_prebuiltapks, to your local_manifests.xml file, e.g:

[xtrnaw7@t15g /data/develop/android/OmniROM]$ cat .repo/local_manifests/local_manifest.xml
<?xml version="1.0" encoding="UTF-8"?>

<manifest>

<remote name="DonkeyCoyote"
fetch="https://github.com/DonkeyCoyote" />
<project path="device/asus/zenfone8" name="android_device_asus_zenfone8" remote="omnirom" revision="android-12.0" />
<project path="vendor/asus" name="proprietary_vendor_asus" remote="DonkeyCoyote" revision="android-12.1" />
<project path="vendor/microg" name="android_prebuilts_prebuiltapks" remote="omnirom" revision="android-12.0" />
</manifest>
[xtrnaw7@t15g /data/develop/android/OmniROM]$

Do another repo sync to download the repository for MicroG:

repo sync

and create the CustomROM using these instructions:

export ROM_BUILDTYPE=MICROG

brunch omni_zenfone8-user

This will create an OmniROM Image with MicroG already installed, e.g:

[xtrnaw7@t15g /data/develop/android/OmniROM]$ ls -ltr out/target/product/zenfone8/omni-12-20220621-zenfone8-MICROG.zip
-rw-rw-r--. 25 xtrnaw7 xtrnaw7 1253703465 Jun 25 10:03 out/target/product/zenfone8/omni-12-20220621-zenfone8-MICROG.zip
[xtrnaw7@t15g /data/develop/android/OmniROM]$
   



How to use the Hardware Test App from ASUS on a Zenfone running a CustomROM


URL:  https://forum.xda-developers.com/t/how-to-use-the-hardware-test-app-from-asus-on-a-zenfone-running-a-customrom.4461533/

How to use the Hardware Test App from ASUS on a Zenfone running a CustomROM
How to use the Hardware Test App from ASUS on a Zenfone running a CustomROM

The Hardware Test App from ASUS for the Zenfone 8 can also be used on a ASUS Zenfone 8 running a CustomROM.

But to get the App working on a CustomROM some efforts are neccessary.

The ASUS Hardware Test App needs the permission to modify system settings. To get this permission the app must be signed with the platform certificate used for the running Android OS.
And because these certificates are not available for the public for all public available CustomROMs (for some very good reasons ...) you must compile your own CustomROM for using the ASUS Hardware Test App on a CustomROM.


So -- for those who still want to continue: Here are the steps neccessary to run the Hardware Test App from ASUS for the Zenfone 8 on a phone running a CustomROM :


First copy the apk with the ASUS Hardware Test App from a Zenfone 8 running the original Android 12 from ASUS to your PC.


This is the file

/system/apps/SMMI_TEST/SMMI_TEST.apk

on the phone running the ASUS Android OS.

Now it's neccessary to sign the App with the platform key from your CustomROM:


In the OmniROM (and I assume that's also true for other AOSP based CustomROMs) the neccessary files, platform.pk8 and platform.x509.pem, are in the directory

./build/make/target/product/security

in your build tree for the CustomROM, e.g.

[xtrnaw7@t15g /data/develop/android/OmniROM]$ ls -l ./build/make/target/product/security/platform.*
-rw-------. 1 xtrnaw7 xtrnaw7 1219 Jun 25 09:39 ./build/make/target/product/security/platform.pk8
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 1460 Jun 25 09:39 ./build/make/target/product/security/platform.x509.pem
[xtrnaw7@t15g /data/develop/android/OmniROM]$


Now re-sign the apk file using these commands:

# remove the current certificate files from the apk (probably not neccessary -- but shouldn't harm)
#
zip -d SMMI_TEST.apk META-INF/CERT.SF META-INF/CERT.RSA

# do a zip align for the apk and write the output to SMMI_TEST1.apk
#
# (zipalign is part of the OTA tools)
#
/data/develop/android/otatools/bin/zipalign 4 SMMI_TEST.apk SMMI_TEST1.apk


# test the result (there should be NO output from this command)
#
/data/develop/android/otatools/bin/zipalign -c 4 SMMI_TEST1.ap


# and now sign the apk with the platform key from the development tree for your self compiled CustomROM
# (apksigner.jar is part of the Sdk from Google for Android)
#
java -jar ./Android/Sdk/build-tools/33.0.0/lib/apksigner.jar sign --key platform.pk8 --cert platform.x509.pem SMMI_TEST1.apk


That's it . The re-signed apk file SMMI_TEST1.apk should work on your CustomROM


To test the signed apk do:

Copy the signed apk to the phone running your self compiled CustomROM and issue

ls -l /sdcard/Download/SMMI_TEST1.apk
cat /sdcard/Download/SMMI_TEST1.apk | pm install -S 25733396



If you get an error like this

ASUS_I006D:/ # ls -l /sdcard/Download/SMMI_TEST1.apk
-rw-rw---- 1 u0_a111 media_rw 25733396 2022-08-04 19:02 /sdcard/Download/SMMI_TEST1.apk
ASUS_I006D:/ #
ASUS_I006D:/ # cat /sdcard/Download/SMMI_TEST1.apk | pm install -S 25733396
Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE: Reconciliation failed...: Reconcile failed: Package com.asus.atd.smmitest has no signatures that match those in shared user android.uid.system; ignoring!]
1|ASUS_I006D:/ #   


something went wrong signing the apk. Just do it again.


To install the apk do:

If not already done:

Compile your own CustomROM (using the certificates used to sign the ASUS Hardware Test App!) and install it on the ASUS Zenfone 8. (see How to compile the OmniROM for the ASUS Zenfone 8 for how to compile the OmniROM)

Install Magisk on your phone running the self compiled CustomROM

Simulate a Magisk Module using these commands:


adb shell su - -c mkdir -p /data/adb/modules/SMMI_TEST/system/app/SMMI_TEST

adb push SMMI_TEST1.apk /sdcard/Download/

adb shell su - -c cp /sdcard/Download/SMMI_TEST1.apk /data/adb/modules/SMMI_TEST/system/app/SMMI_TEST/SMMI_TEST1.apk

adb shell su - -c chmod o+r /data/adb/modules/SMMI_TEST/system/app/SMMI_TEST/SMMI_TEST1.apk

and reboot the phone

adb reboot


After the reboot the ASUS Hardware Test App should be visible in the directory /system/app, e.g.

130|ASUS_I006D:/ # find /system/app/SMMI_TEST/
/system/app/SMMI_TEST/
/system/app/SMMI_TEST/SMMI_TEST1.apk
ASUS_I006D:/ #

To execute the ASUS Hardware Test App do

# open a shell on the ASUS Zenfone 8 (either local or via "adb shell"), become root user

su -

and execute

# switch SELinux to permissive mode (I currently do not know the neccessary SELinux policies to avoid this step ...)
#
setenforce 0

# and start the ASUS Hardware Test App from within the shell
#
am start -n com.asus.atd.smmitest/com.asus.atd.smmitest.main.MAIN

Do not forget to enable SELinux enforcing mode after the tests are done (or reboot the phone ...)

# switch SELinux to permissive mode (I currenlty do not know the neccessary SELinux policies to avoid this step ...)
#
setenforce 


Sample Script to start the ASUS Hardware Test App


# simple script to start the ASUS Hardware App

CUR_SELINUX_STATUS="$( getenforce )"
CUR_USERID=$( id -u -n )
RESTORE_SELINUX_STATE="false"

if [ "${CUR_SELINUX_STATUS}"x != "Permissive"x ] ; then

if [ "${CUR_USERID}"x != "root"x ] ; then
echo "Either start this script as user \"root\" or set the SELinux mode to \"permissive\" using the command \"setenforce 0\" before starting this script"
exit 1
fi
echo "Setting the SELinux mode to \"Permissive\" ..."
setenforce 0
RESTORE_SELINUX_STATE="true"
else
echo "The SELinux mode is already \"Permissive}\" "
fi

am start -n com.asus.atd.smmitest/com.asus.atd.smmitest.main.MAIN 2>&1 | tee /sdcard/Download/test.out

if [ ${RESTORE_SELINUX_STATE} = true ] ; then
echo "Press return when done with the tests .."
read USER_INPUT
echo "Setting the SElinux mode back to \"Enforcing\" ...."
setenforce 1
else
echo "Do not forget to set the SELinxu mode back to \"Enforcing\" using the command \"setenforce 1\" "
fi

Trouble Shooting

If the boot process of the phone hangs after adding the pseudo Magisk Module for the ASUS Hardware Test App do:

# connect to the phone via adb (that should work!)
#
adb shell

# become root user
#
su -

# delete the file with the AUS Hardware Test App
#
rm /data/adb/modules/SMMI_TEST/system/app/SMMI_TEST/SMMI_TEST1.apk

# and reboot the phone
#
reboot

Most probably something went wrong signing the app or you did not do the zip align (there should be some messages visible in logcat to find the reason for the boot loop; check the output of logcat before rebooting the phone)

A useful test to find reason for the boot loop is :

# try to install the apk as root user
#
su -

# and then
#
setenforce 0
pm install /sdcard/Download/SMMI_TEST.apk

If booting the phone works but the App does not start make sure that the SELinux status is permissive.
 



How to run a script at every boot using Magisk


URL:  https://forum.xda-developers.com/t/how-to-run-at-script-a-every-boot-using-magisk.4454493/

How to run at script a every boot using Magisk

How to run a script at every boot using Magisk

Note:


I tested the instructions below with Magisk 24.3 . They may or may not work with other versions of Magisk


Another very useful feature from Magisk that can also be used without creating a Magisk Module is the ability to start scripts after booting the phone:

To execute scripts after rebooting the phone just copy them to one of the directories

/data/adb/post-fs-data.d
/data/adb/service.d

on the phone, make the script executable (chmod +x scriptname) and reboot the phone.

(See also https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md#boot-scripts)


Because the mapping will be done while booting the phone it will also survive updating the OS on the phone to a new version as long as the user data is not deleted and Magisk is installed.

To remove the script just delete the script and reboot the phone.

In case the phone does not boot anymore after adding the script just boot the phone from a recovery with adb support, for example from the TWRP:

# boot the phone into the bootloader and then do

sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.6.1_12-1-I006D.img

Then connect via "adb shell" to the phone and delete the script .

I used this method to correct the SELinux context for the NFC device in the first version of AospExtended 9.0 for the ASUS Zenfone 8 (the device tree is recreated from scratch each time the phone boots so the change must be done after every reboot):

cat /data/adb/service.d/correct_dev_pn553.sh
#!/system/bin/sh

NFC_DEVICE="/dev/pn553"

echo ""
echo "The SELinux context for \"${NFC_DEVICE}\" is now:"
ls -lZ ${NFC_DEVICE}
echo ""
echo "Correcting the SELinux context for the NFC device \"${NFC_DEVICE}\" ..."
chcon -v u:object_r:nfc_device:s0 /dev/pn553
echo ""
echo "The SELinux context for \"${NFC_DEVICE}\" is now:"
ls -lZ ${NFC_DEVICE}

Again, if the script is tested and working you should create a Magisk module for it.

 

 

How to install Magisk into the boot partition using a script


URL:  https://forum.xda-developers.com/t/how-to-install-magisk-into-the-boot-partition-using-a-script.4456621/

How to install Magisk into the boot partition using a 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 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



Test Environment

Tested on an ASUS Zenfone 8 and with


Details



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

 
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




How to change files in the directory /system with Magisk


URL:  https://forum.xda-developers.com/t/how-to-change-files-in-the-directory-system-with-magisk.4454489/

How to change files in the directory /system with Magisk
How to change files in the directory /system with Magisk

Note:


I tested the instructions below with Magisk 24.3, 25.0, and 25.2 on an ASUS Zenfone 8 running OmniROM (Andorid 12).


The filesystem for /system is normally mounted read-only in Android 12 and it's difficult to change files in that filesystem.


One possible solution to change files in /system is to create a Magisk module for this task.

For testing if a Magisk Module would work like expected you can create a dummy Magisk module. The steps to implement the Dummy Magisk Module are:


Install Magisk, enable root access for the shell in Magisk, and enable access via adb (this can be done in the "Developer Options" in the system settings)


Next connect to the phone using the command

adb shell

and become root user

su -

Now create a sub directory in the directory

/data/adb/modules

e.g.

ASUS_I006D:/ # ls -ld /data/adb/modules/*
drwxrws--- 3 u0_a118 media_rw 3452 2022-05-30 12:12 /data/adb/modules/playstore
ASUS_I006D:/ #


Then copy the new files for the directory /system to that directory (/data/adb/modules/<modulename>/ is equal here to / on the phone)

Example:

ASUS_I006D:/ # find /data/adb/modules/playstore
/data/adb/modules/playstore
/data/adb/modules/playstore/system
/data/adb/modules/playstore/system/etc
/data/adb/modules/playstore/system/etc/permissions
/data/adb/modules/playstore/system/etc/permissions/com.android.vending.xml
/data/adb/modules/playstore/system/priv-app
/data/adb/modules/playstore/system/priv-app/Phonesky
/data/adb/modules/playstore/system/priv-app/Phonesky/lib
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libbrotli.so
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libconscrypt_jni.so
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libcronet.85.0.4181.5.so
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libgame_sdk_device_info_jni.so
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libphonesky_data_loader.so
/data/adb/modules/playstore/system/priv-app/Phonesky/lib/arm/libtensorflowlite_jni.so
/data/adb/modules/playstore/system/priv-app/Phonesky/Phonesky.apk
/data/adb/modules/playstore/system/priv-app/FakeStore
/data/adb/modules/playstore/system/priv-app/FakeStore/oat
/data/adb/modules/playstore/system/priv-app/FakeStore/oat/arm64
/data/adb/modules/playstore/system/priv-app/FakeStore/oat/arm64/FakeStore.odex
/data/adb/modules/playstore/system/priv-app/FakeStore/oat/arm64/FakeStore.vdex
/data/adb/modules/playstore/system/priv-app/FakeStore/FakeStore.apk
ASUS_I006D:/ #


Now reboot the phone.

After the reboot all files in /data/adb/modules/<modulename> will be mapped to /system by Magisk, e.g:

ASUS_I006D:/ # find /system/priv-app/Phonesky/
/system/priv-app/Phonesky/
/system/priv-app/Phonesky/lib
/system/priv-app/Phonesky/lib/arm
/system/priv-app/Phonesky/lib/arm/libbrotli.so
/system/priv-app/Phonesky/lib/arm/libconscrypt_jni.so
/system/priv-app/Phonesky/lib/arm/libcronet.85.0.4181.5.so
/system/priv-app/Phonesky/lib/arm/libgame_sdk_device_info_jni.so
/system/priv-app/Phonesky/lib/arm/libphonesky_data_loader.so
/system/priv-app/Phonesky/lib/arm/libtensorflowlite_jni.so
/system/priv-app/Phonesky/Phonesky.apk
ASUS_I006D:/ #

ASUS_I006D:/ # find /system/priv-app/FakeStore/
/system/priv-app/FakeStore/
/system/priv-app/FakeStore/oat
/system/priv-app/FakeStore/oat/arm64
/system/priv-app/FakeStore/oat/arm64/FakeStore.vdex
/system/priv-app/FakeStore/oat/arm64/FakeStore.odex
/system/priv-app/FakeStore/FakeStore.apk
ASUS_I006D:/ #

Because the mapping will be done while booting the phone it will also survive updating the OS on the phone to a new version as long as the user data is not deleted and Magisk is installed.


Notes:

Magisk will "replace" existing directories and files in /system with the files in that directory by using bind mounts.

You can not add additional files or directories to /system using this method -- it's only possible to add additional files and directories to the existing sub directories in /system.

This method to replace files in /system simulates a module in Magisk so if everything is okay I strongly recommend to create a "real" Magisk Module with the new files for /system

This Magisk feature can also be used to make files in sub directories in /system writable -- see How to make files in system writable for detailed instructions.


This method can also be used to replace files in the directories /vendor, /product, or /system_ext:

from https://topjohnwu.github.io/Magisk/guides.html:


The system folder​

All files you want to replace/inject should be placed in this folder. This folder will be recursively merged into the real /system; that is: existing files in the real /system will be replaced by the one in the module’s system, and new files in the module’s system will be added to the real /system.

If you place a file named .replace in any of the folders, instead of merging its contents, that folder will directly replace the one in the real system. This can be very handy for swapping out an entire folder.

If you want to replace files in /vendor, /product, or /system_ext, please place them under system/vendor, system/product, and system/system_ext respectively. Magisk will transparently handle whether these partitions are in a separate partition or not

But this seems to work only to replace existing files in the directories /vendor, /product, or /system_ext.


See How to change any file or directory using Magisk for another approach to change files on read-only mounted filesystems.



Trouble Shooting


If something does not work check the Magisk log file in /cache :

/cache/magisk.log






History of this entry
History

Update 10.06.2022

added the infos about how to update files /vendor, /product, /system-ext


Update 12.06.2022

The Magisk Module to install the patched Playstore used in this example can be downloaded here

http://bnsmb.de/files/public/Android/PlayStore_for_MicroG.zip

see also here


Update 03.10.2022

The sentence about how to update files in /vendor, /product, /system-ext was missing here --fixed
Added a note about what can not be done using this method


Update 04.11.2022

Added the hint for the post to make files in /system writable
Doing some cleanup to make the instructions more clear


Update 18.01.2023

Added a link to the new post How to change any file or directory using Magisk




How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)


URL:  https://forum.xda-developers.com/t/how-to-backup-the-data-from-the-phone-using-rsync-and-ssh-including-some-hints-for-using-sshd-on-an-android-phone.4462327/

How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)
How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)

Like for all computer it's important to have a backup of the data on the phone.

For those who like me don't like to save their private data in one of the suspicious clouds there is a solution with standard Linux tools:

Use rsync and ssh to backup the data from the phone to your local workstation (see the man page for rsync for details regarding rsync and why it is useful for this task)


The neccessary tools for Android for this method can be installed with the Magisk Module MagiskSSH.

Download the Magisk Module with MagiskSSH from here

https://gitlab.com/d4rcm4rc/MagiskSSH_releases

Copy the ZIP file with the Magisk Module to the phone :

adb push magisk_ssh_v0.14.zip /sdcard/Download/

and install it via the module installation from within the Magisk App or manuell using :

adb shell su - -c /data/adb/magisk/magisk64 --install-module /sdcard/Download/magisk_ssh_v0.14.zip

Sample output of the installation:

ASUS_I006D:/ # /data/adb/magisk/magisk64 --install-module /sdcard/Download/magisk_ssh_v0.14.zip
- Current boot slot: _a
- Device is system-as-root
*******************************
OpenSSH for Android
*******************************
[0/7] Preparing module directory
[1/7] Extracting architecture unspecific module files
[2/7] Extracting libraries and binaries for arm64
[3/7] Configuring library path wrapper
[4/7] Recreating symlinks
[5/7] Creating SSH user directories
[6/7] Found sshd_config, will not copy a default one
[7/7] Cleaning up
- Setting permissions
- Done
ASUS_I006D:/ #

A reboot is required now.

adb shell reboot

For the next tasks open a adb shell and become root user.


Next create the authorized_keys file for the user root :

touch /data/ssh/root/.ssh/authorized_keys
chmod 600 /data/ssh/root/.ssh/authorized_keys

and add your public ssh key to the file /data/ssh/root/.ssh/authorized_keys.

To make sure that the keys and other data files for the MagiskSSH module are not removed while deinstalling the module you should create the file /data/ssh/KEEP_ON_UNINSTALL:

touch /data/ssh/KEEP_ON_UNINSTALL

The MagiskSSH module also installs a service to start sshd after each reboot: to disable this start create the file /data/ssh/no-autostart:

touch /data/ssh/no-autostart

To manually start or stop the sshd use the script /data/adb/modules/ssh/opensshd.init :

# start the sshd (as user root)
#
/data/adb/modules/ssh/opensshd.init start

# to stop the sshd (as user root)
#
/data/adb/modules/ssh/opensshd.init stop

Now test the access via ssh from your Linux workstation:


ssh -l root <phone_ip_address>  id

To retrieve the IP address of the phone this command can be used:

PHONE_IP_ADDRESS=$( adb shell ifconfig wlan0 | grep "inet addr:" | sed -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )


example :

[xtrnaw7@t15g ~]$ ssh -l root ${PHONE_IP_ADDRESS} id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
[xtrnaw7@t15g ~]$

Now you can use rsync to backup the data from the phone, e.g. to backup the photos from the phone do :


# on your local Linux workstation do:
# start the sshd on the phone via adb if not already running
#
adb shell su - -c /data/adb/modules/ssh/opensshd.init start

# retrieve the current IP address from the phone
#
PHONE_IP_ADDRESS=$( adb shell ifconfig wlan0 | grep "inet addr:" | sed -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )

# backup the new photos from the phone to the Linux workstation (rsync only copies new files from the phone)
# to the local directory /data/backup/ASUS_ZENFONE8/DCIM
#
rsync -av --rsync-path /data/adb/modules/ssh/usr/bin/rsync root@${PHONE_IP_ADDRESS}:/sdcard/DCIM/ /data/backup/ASUS_ZENFONE8/DCIM

# optional stop the sshd on the phone via adb
#
adb shell su - -c /data/adb/modules/ssh/opensshd.init stop

Note: The sshd configuration file used is /data/ssh/sshd_config


Sample script to backup all data in the directory /sdcard
##!/bin/bash
#
# simple script to backup the data of an phone using adb, ssh, and rsync
#
# History
#  27.06.2022 /bs
#    initial release
#


# for testing
#
#RSYNC_OPTIONS="${RSYNC_OPTIONS} --dry-run"

RSYNC_OPTIONS="${RSYNC_OPTIONS} --del "

# default is to backup the phone connected via adb over LAN
#
[ $# -ne 0 ] && ADB_OPTIONS="$*" || ADB_OPTIONS="-e"

# retrieve the serial number of the attached phone
#
SERIAL_NO="$( adb ${ADB_OPTIONS} shell getprop  ro.serialno )"
if [ "${SERIAL_NO}"x = ""x ] ; then
  echo "ERROR: Can not read the serial number of the connected phone"
  exit 89
fi

VENDOR_MODEL="$( adb ${ADB_OPTIONS} shell getprop  ro.product.vendor.model )"

BACKUP_DIR="/data/backup/ASUS_ZENFONE8/data_backup/${VENDOR_MODEL}_${SERIAL_NO}"

if [ ! -d "${BACKUP_DIR}" ] ; then
  echo "ERROR: The directory \"${BACKUP_DIR}\" does not exist"
  exit 99
fi

PHONE_IP_ADDRESS="$( adb ${ADB_OPTIONS} shell ifconfig wlan0 | grep "inet addr:" | sed  -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )"
if [ "${PHONE_IP_ADDRESS}"x = ""x ] ; then
  echo "ERROR: Can not detect the IP address of the phone"
  exit 100
fi
 
echo "Updating a backup of the data on the phone with the serial number \"${SERIAL_NO}\" and the IP \"${PHONE_IP_ADDRESS}\" to the directory \"${BACKUP_DIR}\" ..."
 
set -x

# start the sshd if neccessary
#
adb ${ADB_OPTIONS} shell su - -c /data/adb/modules/ssh/opensshd.init start

# do the backup
#
time rsync ${RSYNC_OPTIONS} -av --rsync-path /data/adb/modules/ssh/usr/bin/rsync   root@${PHONE_IP_ADDRESS}:/sdcard/  "${BACKUP_DIR}/"

# stop the sshd
#
adb ${ADB_OPTIONS} shell su - -c /data/adb/modules/ssh/opensshd.init stop
set +x
 


How to enable access via ssh for non-root user
How to enable access via ssh for non-root user

In the standard configuration installed by MagiskSSH ssh access is only allowed as user root because the ssh keys are in the directory /data and all non-root user can not read files in the directory /data. Therefor some efforts are neccessary to add ssh access for non-root user.

e.g. To enable the ssh access for the user shell do:

To configure ssh access for the user shell we must create a .ssh directory for the user shell in a directory tree owned by the user shell. The only directory on the phone owned by the user shell that can be used for this purpose is /storage :

ASUS_I006D:/ # ls -ld /storage
drwx--x--- 4 shell everybody 80 2022-06-26 18:37 /storage
ASUS_I006D:/ #

But unfortunately all files and directories in this directory are temporary and will be deleted after a reboot of the phone.

Therefor we configure a startup script in Magisk to create this directory tree after each reboot, e.g.

/data/adb/service.d/create_ssh_dir_for_shell.sh:

# /data/adb/service.d/create_ssh_dir_for_shell.sh
#
mkdir -p /storage/shell/.ssh

chmod -R 700 /storage/shell/

touch /storage/shell/.ssh/authorized_keys

echo "<ssh_public_key>" > /storage/shell/.ssh/authorized_keys

chmod 600 /storage/shell/.ssh/authorized_keys

chown -R shell:shell /storage/shell

To test the script just execute it one time manually as user root.

su - -c sh data/adb/service.d/create_ssh_dir_for_shell.sh

Now create a backup of the sshd config file

su - -c cp /data/ssh/sshd_config /storage/ssh/sshd_config.org.$$

and add these lines at the end of the file /data/ssh/sshd_config

Match User shell
AuthorizedKeysFile /storage/shell/.ssh/authorized_keys

Restart the sshd if it's already running


Now test the access as user shell, example:

[xtrnaw7@t15g ~]$ ssh -l shell 192.168.1.148 id
uid=2000(shell) gid=2000(shell) groups=2000(shell) context=u:r:magisk:s0
[xtrnaw7@t15g ~]$

The reason for this config is the setting "StrictMode yes" in the sshd config file /data/ssh/sshd_config (see the man page for sshd_config for details). So another "solution" is to change this setting:

With the setting "StrictModes no" in the file sshd_config the directory with the authorized_keys file for the non-root users can be anywhere (for example in /sdcard/shell)

Execute as user root:


sed -i -e "s/.*StrictModes.*//g" -e "s/UsePrivilegeSeparation/StrictModes no\nUsePrivilegeSeparation/g" /data/ssh/sshd_config

and change the entry in the file /data/ssh/sshd_config for the authorized_keys file for the user shell, for example:

Match User shell
AuthorizedKeysFile /sdcard/shell/.ssh/authorized_keys

Afterwards restart the sshd:

/data/adb/modules/ssh/opensshd.init stop
/data/adb/modules/ssh/opensshd.init start


Now create the directories and files neccessary for the ssh access (see above) in the directory /sdcard/shell:

SUS_I006D:/ # find /sdcard/shell -exec ls -ld {} \;
drwxrws--- 3 u0_a118 media_rw 3452 2022-06-26 18:32 /sdcard/shell
drwxrws--- 2 u0_a118 media_rw 3452 2022-06-26 18:32 /sdcard/shell/.ssh
-rw-rw---- 1 u0_a118 media_rw 408 2022-06-26 18:32 /sdcard/shell/.ssh/authorized_keys
ASUS_I006D:/ #

and the access as user shell via ssh should work   



How to install packages (apk files) for Android via script


URL:  https://forum.xda-developers.com/t/how-to-install-packages-apk-files-for-android-via-script.4464987/

How to install packages (apk files) for Android via script
Sometimes, for example after a reinstallation of the phone from scratch, it's quite useful to install all the apps via an script.

The Android command to do that is

pm install [apk_file]


But unfortunately that does not work, neither as user shell (due to missing file access rights) nor as user root:


ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/ # pm install /sdcard/Download/EssentialApps/MiXplorer_v6.58.4-API29_B22020920.apk                                                    
avc:  denied  { read } for  scontext=u:r:system_server:s0 tcontext=u:object_r:fuse:s0 tclass=file permissive=0
System server has no access to read file context u:object_r:fuse:s0 (from path /sdcard/Download/EssentialApps/MiXplorer_v6.58.4-API29_B22020920.apk, context u:r:system_server:s0)
Error: Unable to open file: /sdcard/Download/EssentialApps/MiXplorer_v6.58.4-API29_B22020920.apk
Consider using a file under /data/local/tmp/
Error: Can't open file: /sdcard/Download/EssentialApps/MiXplorer_v6.58.4-API29_B22020920.apk

Exception occurred while executing 'install':
java.lang.IllegalArgumentException: Error: Can't open file: /sdcard/Download/EssentialApps/MiXplorer_v6.58.4-API29_B22020920.apk
    at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:572)
    at com.android.server.pm.PackageManagerShellCommand.doRunInstall(PackageManagerShellCommand.java:1337)
    at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:1303)
    at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:193)
    at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)
    at android.os.ShellCommand.exec(ShellCommand.java:38)
    at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:24812)
    at android.os.Binder.shellCommand(Binder.java:950)
    at android.os.Binder.onTransact(Binder.java:834)
    at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:4818)
    at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:8887)
    at android.os.Binder.execTransactInternal(Binder.java:1184)
    at android.os.Binder.execTransact(Binder.java:1143)
255|ASUS_I006D:/ #



After a little searching on the Internet I found a very useful workaround on this page:

https://stackoverflow.com/questions/50540334/install-apk-using-root-handling-new-limitations-of-data-local-tmp-folder


pm install also supports the installation of a package piped to stdin, e.g


cat [package_file] | pm install -S [package_size]


Example:

127|ASUS_I006D:/sdcard/Download # ls -l OmniStore.apk
-rw-rw---- 1 u0_a120 media_rw 4601419 2022-07-03 16:37 OmniStore.apk
ASUS_I006D:/sdcard/Download #
ASUS_I006D:/sdcard/Download # cat OmniStore.apk  | pm install -S 4601419                                                                          
Success
ASUS_I006D:/sdcard/Download #



Or, on the PC:

[xtrnaw7@t15g /data/backup/Android]$ adb shell id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid) context=u:r:shell:s0
[xtrnaw7@t15g /data/backup/Android]$

[xtrnaw7@t15g /data/backup/Android]$ cat EssentialApps/F-Droid.apk | adb shell pm install -S $( ls -l EssentialApps/F-Droid.apk | awk '{ print $5 }' )
Success
[xtrnaw7@t15g /data/backup/Android]$



So I wrote a little script to install one or more packages on an Android phone using this method.

The script can run on a PC with a connected phone with a working adb connection or in a shell on the phone

The usage of the script is:

#
# Usage on a phone:
#
# install_apk.sh [apk1|dir1 ... apk#|dir#]
#
# Usage on a PC:
#
# install_apk.sh [options_for_adb --] [apk1|dir1 ... apk#|dir#]
#
# apk# is the name of an apk file to install; dir# is a directory with apk files
# If a parameter is a directory the script will install all files with the extension .apk from that directory
#
# The options for adb are optional; the script will not check the options for adb
# the number of apk files or directories is only limited by the maxium parameter supported by the used shell.
#
# Set the variable PM_INSTALL_OPTIONS with additional options for the "pm install" command before starting the
# script if neccessary
#
# Prerequisites
#   The packages to install must exist as file either on the PC or on the phone
#   A shell on the phone or via adb command is required
#   root access is NOT neccessary
#



Sample script output for running on a phone:
ASUS_I006D:/storage/emulated/0/Download $ sh ./install_apk.sh myapps/ OmniStore.apk                                                               
Running on a phone
Directory found in the parameter: Installing all apk files found in the directory "myapps/"
The apks will be installed on the phone model ASUS_I006D with the serial number M6AIB760D0939LX
Installing these apks

 myapps//Magisk-25.1_25100.apk
myapps//Magisk-v25.0.apk
myapps//MiXplorer_v6.58.4-API29_B22020920.apk
myapps//Notecase_Pro_1.2.2.apk
myapps//com.keramidas.TitaniumBackupAddon_v1.apk
myapps//com.keramidas.TitaniumBackup_v417.apk
OmniStore.apk


Installing the apk "myapps//Magisk-25.1_25100.apk" ...
Success
"myapps//Magisk-25.1_25100.apk" succcessfully installed

Installing the apk "myapps//Magisk-v25.0.apk" ...
Failure [INSTALL_FAILED_VERSION_DOWNGRADE: Package Verification Result]
ERROR: Error installing the apk  "myapps//Magisk-v25.0.apk"

Installing the apk "myapps//MiXplorer_v6.58.4-API29_B22020920.apk" ...
Success
"myapps//MiXplorer_v6.58.4-API29_B22020920.apk" succcessfully installed

Installing the apk "myapps//Notecase_Pro_1.2.2.apk" ...
Success
"myapps//Notecase_Pro_1.2.2.apk" succcessfully installed

Installing the apk "myapps//com.keramidas.TitaniumBackupAddon_v1.apk" ...
Success
"myapps//com.keramidas.TitaniumBackupAddon_v1.apk" succcessfully installed

Installing the apk "myapps//com.keramidas.TitaniumBackup_v417.apk" ...
Success
"myapps//com.keramidas.TitaniumBackup_v417.apk" succcessfully installed

Installing the apk "OmniStore.apk" ...
Success
"OmniStore.apk" succcessfully installed

Installation summary
====================

6 package(s) successfully installed:
 
myapps//Magisk-25.1_25100.apk
myapps//MiXplorer_v6.58.4-API29_B22020920.apk
myapps//Notecase_Pro_1.2.2.apk
myapps//com.keramidas.TitaniumBackupAddon_v1.apk
myapps//com.keramidas.TitaniumBackup_v417.apk
OmniStore.apk


1 package(s) not installed:
 
myapps//Magisk-v25.0.apk


ASUS_I006D:/storage/emulated/0/Download $
 


Sample script output for running on a PC:
[xtrnaw7@t15g /data/backup/Android/myapps]$ /data/develop/android/install_apk.sh -d  -- /data/backup/Android/myapps/ ./Notecase/Notecase_Pro_1.2.1.apk
Running on a PC
Using adb with the options " -d " to install the packages
Directory found in the parameter: Installing all apk files found in the directory "/data/backup/Android/myapps/"
The apks will be installed on the phone model ASUS_I006D with the serial number M6AIB760D0939LX
Installing these apks

 /data/backup/Android/myapps//com.android.keepass_196.apk
/data/backup/Android/myapps//com.keramidas.TitaniumBackup_v417.apk
/data/backup/Android/myapps//com.matoski.adbm_v27.apk
/data/backup/Android/myapps//F-Droid.apk
/data/backup/Android/myapps//FoxMagiskModuleManager_0.4.0-rc1.apk
/data/backup/Android/myapps//Magisk-25.1_25100.apk
/data/backup/Android/myapps//MiXplorer_v6.58.4-API29_B22020920.apk
/data/backup/Android/myapps//Notecase_Pro_1.2.2.apk
./Notecase/Notecase_Pro_1.2.1.apk


Installing the apk "/data/backup/Android/myapps//com.android.keepass_196.apk" ...
Success
"/data/backup/Android/myapps//com.android.keepass_196.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//com.keramidas.TitaniumBackup_v417.apk" ...
Success
"/data/backup/Android/myapps//com.keramidas.TitaniumBackup_v417.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//com.matoski.adbm_v27.apk" ...
Success
"/data/backup/Android/myapps//com.matoski.adbm_v27.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//F-Droid.apk" ...
Success
"/data/backup/Android/myapps//F-Droid.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//FoxMagiskModuleManager_0.4.0-rc1.apk" ...
Success
"/data/backup/Android/myapps//FoxMagiskModuleManager_0.4.0-rc1.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//Magisk-25.1_25100.apk" ...
Success
"/data/backup/Android/myapps//Magisk-25.1_25100.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//MiXplorer_v6.58.4-API29_B22020920.apk" ...
Success
"/data/backup/Android/myapps//MiXplorer_v6.58.4-API29_B22020920.apk" succcessfully installed

Installing the apk "/data/backup/Android/myapps//Notecase_Pro_1.2.2.apk" ...
Success
"/data/backup/Android/myapps//Notecase_Pro_1.2.2.apk" succcessfully installed

ERROR: The file "./Notecase/Notecase_Pro_1.2.1.apk" does not exist or is not readable

Installation summary
====================

8 package(s) successfully installed:
 
/data/backup/Android/myapps//com.android.keepass_196.apk
/data/backup/Android/myapps//com.keramidas.TitaniumBackup_v417.apk
/data/backup/Android/myapps//com.matoski.adbm_v27.apk
/data/backup/Android/myapps//F-Droid.apk
/data/backup/Android/myapps//FoxMagiskModuleManager_0.4.0-rc1.apk
/data/backup/Android/myapps//Magisk-25.1_25100.apk
/data/backup/Android/myapps//MiXplorer_v6.58.4-API29_B22020920.apk
/data/backup/Android/myapps//Notecase_Pro_1.2.2.apk


1 package(s) not found:
 
./Notecase/Notecase_Pro_1.2.1.apk


[xtrnaw7@t15g /data/backup/Android/myapps]$ adb push /data/develop/android/install_apk.sh /sdcard/Download/
/data/develop/android/install_apk.sh: 1 file pushed, 0 skipped. 76.3 MB/s (5906 bytes in 0.000s)
[xtrnaw7@t15g /data/backup/Android/myapps]$ 




Download the script from here install_apk.sh

Notes:

Another method to install packages via script from an PC is

adb install [package]

This command copies the file to a temporary location on the phone and installs it then via "pm install".

e.g

[xtrnaw7@t15g /data/backup/Android/myapps]$ adb install F-Droid.apk
Performing Streamed Install
Success
[xtrnaw7@t15g /data/backup/Android/myapps]$



Another ugly and insecure method onthe phone to install an apk file using pm install as root is

pm install (insecure method)
130|ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ # 
ASUS_I006D:/ # getenforce
Enforcing
ASUS_I006D:/ # setenforce 0
ASUS_I006D:/ #
ASUS_I006D:/ # getenforce                                                                                                                         
Permissive
ASUS_I006D:/ #
ASUS_I006D:/ # pm install /sdcard/Download/EssentialApps/com.matoski.adbm_v27.apk                                                                 
avc:  denied  { read } for  scontext=u:r:system_server:s0 tcontext=u:object_r:fuse:s0 tclass=file permissive=1
Success
ASUS_I006D:/ # setenforce 1
ASUS_I006D:/ #
ASUS_I006D:/ # getenforce
Enforcing
ASUS_I006D:/ #



pm install supports a lot of options -- to get the usage help for pm use

adb shell pm help

or on the phone

pm help
 
or see here: https://developer.android.com/studio/command-line/adb#pm (Google Account neccessary) or here: http://adbcommand.com/adbshell/pm
 


How to replace the Fake Store from OmniROM with MicroG with a patched Playstore


URL:  https://forum.xda-developers.com/t/magisk-module-to-replace-the-fakestore-in-the-omnirom-with-a-patched-playstore-from-nanodroid.4456421/

How to replace the Fake Store from OmniROM with MicroG with a patched Playstore
The OmniROM with MicroG contains a perfect integrated MicroG but it does not contain the Playstore. Therefor apps that rely on the License functionality of the Playstore (like for example the DWD Warnwetter App) work only partial.

Fortunately there is already a patched PlayStore that works with MicroG in the NanoDroid image.

Therefor I created a Magisk Module to replace the FakeStore in OmniROM with MicroG with the patched PlayStore from NanoDroid.

The Module will replace the apk for the FakeStore from MicroG with an empty file and add the directory

/system/priv-apps/Phonesky

with the patched PlayStore from NanoDroid.

The patched Playstore works with MicroG so that apps using the license functionality from the Playstore can also be used in ROMs using MicroG .

The ZIP file with the Magisk Module can be downloaded from here:

http://bnsmb.de/files/public/Android/PlayStore_for_MicroG.zip

Note:

Update 09.07.2023

MicroG and the Playstore used in NanoDroid are now outdated and do not work anymore on the newest Android version . Therefor I recommend to use the Magisk Module microG-GApps to install MicroG and a working Playstore on CustomROMs without the Google stack.


see also How to install a more recent version of the Playstore in OmniROM 
 


How to change the home directory for the user root on an Android phone


URL:  https://forum.xda-developers.com/t/how-to-change-the-home-directory-for-the-user-root-on-an-android-phone.4469305/

How to change the home directory for the user root on an Android phone
When working a lot in a shell via adb on an Android phone it's usefull to be able to store some user dependent config files in the home directory of the user. So let's see how that can be implemented on a phone running Android.

Note: The config was done on OmniROM (based on Android 12)

The home directory for all user used in an (adb) shell on Android is the root directory "/".

Using a home directory for more then one user is not really usefull and in addition on Android "/" is mounted read-only so more or less useless as home directory.

Therefor this should be changed. Unfortunately there is no /etc/passwd file in Android to configure the home directory for a user.

Well, there is an /etc/passwd file (probably for compatibily reasons) but it's empty:


shell@ASUS_I006D:/ $ ls -l /etc/passwd                                                                                                   
-rw-r--r-- 1 root root 0 2009-01-01 01:00 /etc/passwd
shell@ASUS_I006D:/ $


And as far as I know there is no other config file to configure the home directory of the user in the Android OS.

So we must implement some work around to get this working.


The shell on Android behaves like normal shells on Linux and executes the file /etc/profile when starting a new session.

In Android /etc is a symbolic link to /system/etc:


root@ASUS_I006D:/ # ls -ld /etc
lrw-r--r-- 1 root root 11 2009-01-01 01:00 /etc -> /system/etc
root@ASUS_I006D:/ #



And in the default config there is no file called profile in that directory:

shell@ASUS_I006D:/ $ ls -l /etc/system/profile
ls: /etc/system/profile: No such file or directory
shell@ASUS_I006D:/ $



Because /system is also mounted read-only we need the magic tool Magisk again to create the file /system/etc/profile.

To create the file /system/etc/profile create these directories and files on the phone as user root (assuming Magisk is already installed on the phone).


root@ASUS_I006D:/ # find /data/adb/modules/initshell/
/data/adb/modules/initshell/
/data/adb/modules/initshell/system
/data/adb/modules/initshell/system/etc
/data/adb/modules/initshell/system/etc/profile
root@ASUS_I006D:/ #



and reboot the phone. After the reboot there should be the writable file /etc/profile:

root@ASUS_I006D:/ # ls -l /etc/profile
-rw-rw-rw- 1 root root 1100 2022-07-14 14:51 /etc/profile
root@ASUS_I006D:/ #


which is in reality the file

/data/adb/modules/initshell/system/etc/profile


Now you can edit the file /etc/profile as user root until it fullfills your requirements (be aware that all changes in that file are now persistent).
To test the changes open a new adb session (you should not close the current adb session to be able to fix an error in the profile if opening a new adb session fails).


This file can now be used to define the home directory for the user. The home directory should be on one of the writable filesystem, e.g. in /data:

# add in /etc/profile

HOME="/data/home/root"

export HOME


and the result is:

root@ASUS_I006D:/ #  id
uid=0(root) gid=0(root) groups=0(root),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid) context=u:r:su:s0

root@ASUS_I006D:/ # echo $HOME
/data/home/root
root@ASUS_I006D:/ #



Note that there is not really a writable filesystem for the user shell so this approach to change the home directory is mainly usable for the user root.

The file /etc/profile can also be used to init some other settings for (adb) sessions (e.g. change the PATH, etc) . This feature can be used for sessions for non-root users also.

For additional user dependent configs you can also create the file .profile in the home directory of the user; the file ${HOME}/.profile will be executed after the execution of the file /etc/profile.


Notes:

Be aware that creating a profile for the user root and changing the home directory for the user root might have some side effects on other processes using the shell!

Therefor it's recommended to use the command "tty -s" to test if the profile is executed in an interactive session:

#
# check if we're running in an interactive session
#
if ! tty -s; then
#
# this is not an interactive session - so we're just doing nothing at all
#
  :
else
#
# running in an interactive session
#
...
fi

You can also check if the parent process is the adb daemon to not do anything in non-adb sessions:

ps -fp $PPID| grep adbd >/dev/null
if [ $? -ne 0 ] ; then
#
# not running in an adb session
:
else
#
# running in an adb session
...
fi


For testing purpose I suggest to open at least two shells via adb to be able to fix an error in case opening a new shell fails due to a bug in the profile.

If opening a shell session fails due to an error in the /etc/profile and you do not have another open shell either delete the file /data/adb/modules/initshell/system/etc/profile with a filemanager with root access on the phone or boot the phone from a recovery image (like TWRP) and delete or edit the file /data/adb/modules/initshell/system/etc/profile.


I initially looked at this feature to enable a persistent command history for the shell on the Android phone. But after some google searches I found out that persistent history shells are disabled for the shell binary on Android. So this will not work without recompiling the shell binary.


The attached example for the profile will use the directory /data/home/root as home directory for the user root and will not change the home directory for other users. The profile will do nothing if not running in an interactive session.
The directory /data/home/root will be created by the profile if it does not yet exist.


As always, if the config is working you should create a real Magisk Module for the profile.

The Magisk Module attached, initshell.zip, can be used to create a Magisk Module with your own profile:

To create a new Magisk Module with your own /etc/profile do:


# create an empty working directory
#
TEMPDIR="/tmp/newdir"

mkdir "${TEMPDIR}"
cd "${TEMPDIR}"

# unpack the zip file in the new directory
#
unzip ../initshell.zip

# now edit the file ${TEMPDIR}/system/etc/profile

# also (optional) edit the files config.sh and module.prop in the new directory to document your changes

# the script customize.sh from the module will be executed once when the module is installed. You might add the code
# create the home directories or any other code here
#

# and recreate the zip file
#
zip -r ../initshell.zip .


 



Some hints for using Magisk on Android phones


URL:  https://forum.xda-developers.com/t/some-hints-for-using-magisk-on-android-phones.4471857/

Some hints for using Magisk on Android phones
In this post I summarize everything I found out about using Magisk (https://topjohnwu.github.io/Magisk) until now. A lot of the infos in this post are already in my other posts in this board but hopefully this summary is useful anyway.

My main purpose is to configure Magisk as much as possible via scripts so this tutorial is mostly about using Magisk via CLI commands in an adb shell or from within scripts.

I will update the post whenever I found something new and interesting. A history of changes in this post is at the end of the post


PreRequisites

Most of the instructions below need an enabled adb access for the phone and root permissions for the adb shell. In addition, a working Recovery image that is able to mount the /data partition (like for example TWRP) is strongly recommended before starting to play with Magisk.

The Magisk versions used for this tutorial are Magisk 25.1 and Magisk 25.2; the ROM used was OmniROM (Android 12 based).
The phone used was an ASUS Zenfone 8 but the instructions should work on other phones also.


Installation

Magisk consists out of two parts:

The Magisk App and the Magisk files in the boot partition. Without the files in the boot partition Magisk can do more or less nothing.

The Magisk App can be installed using the standard Android tools for installing apps.

The installation of the Magisk files into the boot partition can then be done using the Magisk App.

The official instructions to install Magisk into the boot partition are here: 

https://topjohnwu.github.io/Magisk/install.html

Magisk can also be installed via script without user intervention into the boot partition -- see here: How to install Magisk into the boot partition using a script

Update 18.04.2023/bs

see How to install Magisk v26.0 or newer via script for how to install Magisk v26 via script


Note that the Magisk files for the boot partition must be reinstalled after an OS upgrade.


Uninstalling Magisk

Use the Button "Uninstall Magisk" in the Magisk app. The Magisk App can also remove Magisk from the boot partition.

To uninstall the Magisk App only via script do

adb shell pm uninstall com.topjohnwu.magisk

To remove Magisk from the boot partition manually I suggest to flash the image of the original boot partition.


Installing Magisk Modules

Magisk Modules are extensions for Magisk to for example replace or add files to /system (and much more ... I suggest to study the documentation for Magisk for those that are interested in using Magisk)

Magisk Modules are distributed via ZIP files.

As of Magisk v25 or newer the official method to install Magisk Modules is to manually copy the ZIP file with the Magisk Module to the phone and then use the button "Install from storage" in the "Modules" sub menu in the Magisk App.

In previous versions of Magisk a Module browser to list and install Magisk Modules was part of the Magisk App but unfortunately the developer dropped that feature. Therefor you must search and download the available Magisk Modules using a browser on the phone or a PC.

Repositories with Magisk Modules are for example:

https://github.com/Magisk-Modules-Repo/

and

https://github.com/Magisk-Modules-Alt-Repo


A XDA thread about Magisk Modules (including a list of Magisk Modules) is here:

https://forum.xda-developers.com/t/collection-of-magisk-modules-v2.3575758/


There is now a new App for Android called "Fox's Magisk Module Manager" that can be used to list and download Magisk Modules to the phone
(see https://github.com/Fox2Code/FoxMagiskModuleManager)

It's also possible to install a Magisk Module via script. The commands to install an Magisk Module via script are:

# copy the Magisk Module to the phone

adb push /data/backup/Android/MagiskModules/Nano_for_Android_NDK-6_3-6300.zip /sdcard/Download/

# install the Module
#
# Note:
#
# The command can also be executed in a script runnning on the phone (without "adb shell")
#
adb shell magisk --install-module  /sdcard/Download/Nano_for_Android_NDK-6_3-6300.zip

# reboot the phone

adb reboot


Example:
[xtrnaw7@t15g ~]$ adb push  /data/backup/Android/MagiskModules/Nano_for_Android_NDK-6_3-6300.zip /sdcard/Download/
/data/backup/Android/MagiskModules/Nano_for_Android_NDK-6_3-6300.zip: 1 file pushed, 0 skipped. 37.6 MB/s (380554 bytes in 0.010s)

[xtrnaw7@t15g ~]$ adb shell magisk --install-module     /sdcard/Download/Nano_for_Android_NDK-6_3-6300.zip
- Current boot slot: _a
- Device is system-as-root
*****************************
 Nano for Android NDK
 by osm0sis @ xda-developers
*****************************
*******************
 Powered by Magisk
*******************
Archive:  /storage/emulated/0/Download/Nano_for_Android_NDK-6_3-6300.zip
  inflating: diffusion_config.sh
  inflating: module.prop
 
Mounting...
 
Extracting files...
Archive:  /storage/emulated/0/Download/Nano_for_Android_NDK-6_3-6300.zip
  inflating: META-INF/com/google/android/updater-script
  inflating: META-INF/com/google/android/update-binary
  inflating: sbin/nano
...
  inflating: etc/terminfo/l/linux
  inflating: customize.sh
  inflating: update.json
  inflating: bin/nano
  inflating: bin/nano.bin
  inflating: README.md
  inflating: diffusion_config.sh
  inflating: module.prop
 
Installing...
 
Installing nano to /data/adb/modules_update/nano-ndk/system/bin ...
Installing terminfo to /data/adb/modules_update/nano-ndk/system/etc ...
 
Unmounting...
 
Done!
[xtrnaw7@t15g ~]$  adb reboot
[xtrnaw7@t15g ~]$
 


Uninstalling a Magisk Module

Magisk Modules should be uninstalled via Magisk App if possible because there is no parameter for the magisk binary to uninstall a Magisk Module.

To force Magisk to remove the module after the next reboot create the file

remove

in the module directory.

If uninstalling the Magisk Module does not work or if you want to uninstall a Magisk Module via script you can just delete the directory for the Magisk Module in /data/adb/modules and reboot the phone. This can be done in an adb shell or if that does not work anymore after rebooting the phone from a Recovery Image like TWRP, e.g.

# uninstall the module "dummy_module" via adb shell commands
#

# check for an uninstall script for this module

if [ -x /data/adb/modules/dummy_module/uninstall.sh ] ; then
  echo "Executing the module uninstall script ...."

  /data/adb/modules/dummy_module/uninstall.sh
fi

adb shell rm -rf /data/adb/modules/dummy_module/

adb reboot


Note:

You should reboot the phone immediately after removing the directory with the Module.

This method is not really recommended.



List all installed Magisk Modules

The Magisk App will list all installed Magisk Modules

To list the installed Magisk Modules via CLI command use either

adb shell ls -d /data/adb/modules/* | cut -f5 -d "/"

e.g.

[xtrnaw7@t15g ~]$ adb shell su - -c ls -d /data/adb/modules/* | cut -f5 -d "/"
PlayStore_for_MicroG
ccbins
dummy_module
initshell
nano-ndk
terminalmods
[xtrnaw7@t15g ~]$


or

[xtrnaw7@t15g /data/develop/android/NewModules/initshell]$ for i in $( adb shell su - -c ls -d /data/adb/modules/* ); do printf "%-30s %s\n"  "${i##*/}" "$(  adb shell su - -c grep name= $i/module.prop 2>/dev/null | cut -f2 -d "=" | tr -s " " )" ; done
MiXplorer                      MiXplorer
PlayStore_for_MicroG           Patched Playstore from NanoDroid for MicroG (for ARM64 CPUs only)
ccbins                         Cross Compiled Binaries
dummy_module                   Dummy Module for testing a new module
initshell                      Create writable config files (/etc/profile) for sh
nano-ndk                       Nano for Android NDK
terminalmods                   Terminal Modifications
[xtrnaw7@t15g /data/develop/android/NewModules/initshell]$



or on the phone:

root@ASUS_I006D:/ # echo; for i in /data/adb/modules/*; do printf "%-30s %s\n"  "${i##*/}" "$(  grep name= $i/module.prop 2>/dev/null | cut -f2 -d "=" | tr -s " " )" ; done       

PlayStore_for_MicroG           Patched Playstore from NanoDroid for MicroG (for ARM64 CPUs only)
ccbins                         Cross Compiled Binaries
dummy_module                  
initshell                      Create writable config files (/etc/profile) for sh
nano-ndk                       Nano for Android NDK
terminalmods                   Terminal Modifications
root@ASUS_I006D:/ #


How to disable a Magisk Module

To disable a Magisk Module via script create a file called disable in the Module directory, e.g.:

touch /data/adb/modules/dummy_module/disable

The Magisk Module will then be disabled after the next reboot.

To re-enable the Magisk Module just delete the file disable in the Module directory and reboot the phone.

To list all disabled Magisk Modules do

adb shell ls /data/adb/modules/*/disable | cut -f5 -d "/"


Replacing files in /system using a Dummy Magisk Module

One feature of Magisk Modules is the possibility to change files in the sub directories in the directory /system or also add new files to the sub directories in the directory /system. /system is mounted read-only and in the current Android version it is not possible to remount /system read-write anymore so without Magisk it's very difficult to change files in /system.
Please note that you can not create new files or directories in the directory /system using this Magisk feature.

To test this feature you do not need to create a Magisk Module : It's sufficient to simulate a Magisk Module.
For that just create the necessary directory structure and files manually.

Magisk Modules are are installed in the directory /data/adb/modules. Each Magisk Module use an uniqe sub directory in that directory. To simulate a Magisk Module open an adb shell as user root and create the directories for your Dummy Magisk Module, e.g.:

mkdir /data/adb/modules/dummy_module

# Next create the sub directory system in that new directory

mkdir /data/adb/modules/dummy_module/system


Now copy the files for /system to that directory: The files in that directory will overwrite existing files in /system after the next reboot. Files in that directory that not already exist in /system will be created in /system.

e.g

# contents of my Dummy Magisk Module
#
root@ASUS_I006D:/data/adb/modules/dummy_module/system # pwd
/data/adb/modules/dummy_module/system

root@ASUS_I006D:/data/adb/modules/dummy_module/system # find .
.
./bin
./bin/my_new_binary
./etc
./etc/my_new_file_for_systme
root@ASUS_I006D:/data/adb/modules/dummy_module/system #                  
                                                                                                    


After rebooting the phone the files are visible in /system:

root@ASUS_I006D:/ # ls -l /system/etc/my_new_file_for_systme                                                                                                                 
-rw-rw-rw- 1 root root 20 2022-07-20 16:55 /system/etc/my_new_file_for_systme

root@ASUS_I006D:/ # cat /system/etc/my_new_file_for_systme                                                                                                                   
This is my new file
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # ls -l /system/bin/my_new_binary                                                                                                                          
-rwxr-xr-x 1 root root 41 2022-07-20 16:57 /system/bin/my_new_binary

root@ASUS_I006D:/ # my_new_binary                                                                                                                                            
Hello world from my dummy binary
root@ASUS_I006D:/ #


The permissions for the new files in /system are equal to the permissions of the original files in /data/adb/modules/dummy_module so if the original files are writable you can also edit the new files in /system, e.g

root@ASUS_I006D:/ # cat /system/etc/my_new_file_for_systme                                                                                                                   
This is my new file
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # echo "Test Test" >> /system/etc/my_new_file_for_systme                                                                                                   
root@ASUS_I006D:/ # cat /system/etc/my_new_file_for_systme                                                                                                                   
This is my new file
Test Test
root@ASUS_I006D:/ #


These changes are persistent (because they change the original file in /data/adb/modules/<modulename>). You can also edit the files in
/data/adb/modules/<modulename> but be aware that you can not use tools that create temporary files for changing the files like for example sed: Changes done with these kind of tools are only visible after a reboot of the phone.

To change the file via script I recommend to create a new file and then replace the existing file with the new file using cp.

This Magisk feature can also be used to make files in sub directories in /system writable -- see How to make files in system writable for detailed instructions.


Notes:

Magisk does not really change the files in /system - instead it uses "bind mounts" to replace the files with others. Therefor you can always restore the original files by deleting the files in /data/adb/modules/<module_name> and rebooting the phone. Another big advantage of this technique is that you do not have to take care about an update of the OS:

Changes done via Magisk will also work after an OS upgrade (assuming you reinstalled Magisk to the boot partition after installing the OS update)

Make sure that the new files for /system are readable for the user using them; e.g some files (like for example apk files) are used by the user system). IMHO it's recommended to add the read permission (chmod o+r) to all new files for /system.

Please be careful when changing existing files in /system.

To add the necessary infos for the Magisk App to list your dummy module properly just create the file module.prop for your Dummy Magisk Module, e.g.

root@ASUS_I006D:/ # cat /data/adb/modules/dummy_module/module.prop                                                                                                           
id=dummy-module
name=Dummy Module for testing a new module
version=1.0
versionCode=1000
author=otto.meier@mynetwork.com
description=Dummy Module ..
root@ASUS_I006D:/ #



For some examples for this technique see :

How to change files in the directory /system with Magisk

How to replace the Fake Store from OmniROM with MicroG with a patched Playstore

How to change the home directory for the user root on an Android phone



How to use this technique to disable an app that can not be uninstalled

Apps that can not be uninstalled can be disabled by creating empty files for the app.

Example:

My Magisk Module for the patched Playstore must disable the installed app for the fake store
 

That's done by creating an empty file in the Magisk Module for the apk file, the result looks like this:

root@ASUS_I006D:/ # ls -l /data/adb/modules/PlayStore_for_MicroG/system/priv-app/FakeStore/FakeStore.apk                                                                     
-rw-r--r-- 1 root root 1 2022-07-15 11:54 /data/adb/modules/PlayStore_for_MicroG/system/priv-app/FakeStore/FakeStore.apk
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # ls -l /system/priv-app/FakeStore/FakeStore.apk                                                                                                           
-rw-r--r-- 1 root root 1 2022-07-15 11:54 /system/priv-app/FakeStore/FakeStore.apk
root@ASUS_I006D:/ #

In case there are multiple files in the folder for the app you can also hide the entire folder:

From the original documentation from https://topjohnwu.github.io/Magisk/guides.html:

If you place a file named .replace in any of the folders, instead of merging its contents, that folder will directly replace the one in the real system. This can be very handy for swapping out an entire folder.
 


How to access the files replaced by Magisk

Use another bind mount to access the files replaced by Magisk.

Example:
root@ASUS_I006D:/ # head /data/adb/modules/fmradio/system/etc/public.libraries.txt                                                                                                             
# See https://android.googlesource.com/platform/ndk/+/master/docs/PlatformApis.md
#
# 04.08.2022 /bs
#  added the library libqcomfm_jni.so
#
libandroid.so
libaaudio.so
libamidi.so
libbinder_ndk.so
libc.so
root@ASUS_I006D:/ #
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # head /system/etc/public.libraries.txt                                                                                                                                      
# See https://android.googlesource.com/platform/ndk/+/master/docs/PlatformApis.md
#
# 04.08.2022 /bs
#  added the library libqcomfm_jni.so
#
libandroid.so
libaaudio.so
libamidi.so
libbinder_ndk.so
libc.so
root@ASUS_I006D:/ #

# -> The file /system/etc/public.libraries.txt is replaced by a file from a Magisk Module

# To access the original version of the file /system/etc/public.libraries.txt do

root@ASUS_I006D:/ #
root@ASUS_I006D:/ # mkdir -p /data/test
root@ASUS_I006D:/ #
root@ASUS_I006D:/ # mount -o bind /system /data/test

#
# -> /data/test is now "bind" mounted to /system
#
# -> /data/test/etc/public.libraries.txt is the original version of that file:
#

root@ASUS_I006D:/ #

root@ASUS_I006D:/ # head /data/test/etc/public.libraries.txt                                                                                                                                   
# See https://android.googlesource.com/platform/ndk/+/master/docs/PlatformApis.md
libandroid.so
libaaudio.so
libamidi.so
libbinder_ndk.so
libc.so
libcamera2ndk.so
libdl.so
libEGL.so
libGLESv1_CM.so
root@ASUS_I006D:/ #



Replacing files in /vendor, /product, or /system_ext using a Dummy Magisk Module

The (dummy) Magisk Module can also be used to replace files in the directories /vendor, /product, or /system_ext:

from the Magisk documentation at https://topjohnwu.github.io/Magisk/guides.html:

If you want to replace files in /vendor, /product, or /system_ext, please place them under system/vendor, system/product, and system/system_ext respectively. Magisk will transparently handle whether these partitions are in a separate partition or not.

Note that this works only to replace existing files in these directories - you can not add new files to these directories using this method.

Example:
root@ASUS_I006D:/ # cd /data/adb/modules/dummy_module/system                                                                                                                 

#
# files in the Magisk Module
#
root@ASUS_I006D:/data/adb/modules/dummy_module/system # find . -type f
./bin/my_new_binary
./etc/my_new_file_for_systme
./product/mynewfile
./product/media/audio/alarms/Krypton.ogg
./vendor/mynewfile
./vendor/etc/sap.conf
./system_ext/mynewfile
./system_ext/etc/dpm/dpm.conf
root@ASUS_I006D:/data/adb/modules/dummy_module/system #
#
#  the new files for /product, /vendor, and /system_ext in the Magisk Module do not exist:
#
root@ASUS_I006D:/data/adb/modules/dummy_module/system # ls -l /product/mynewfile /vendor/mynewfile /system_ext/mynewfile
ls: /product/mynewfile: No such file or directory
ls: /vendor/mynewfile: No such file or directory
ls: /system_ext/mynewfile: No such file or directory

#
# The existing files in /product, /vendor, and /system_ext are replaced with the files from the Magisk Moduel
#
root@ASUS_I006D:/data/adb/modules/dummy_module/system # cksum ./product/media/audio/alarms/Krypton.ogg  /product/media/audio/alarms/Krypton.ogg                              
4294967295 0 ./product/media/audio/alarms/Krypton.ogg
4294967295 0 /product/media/audio/alarms/Krypton.ogg
root@ASUS_I006D:/data/adb/modules/dummy_module/system #

root@ASUS_I006D:/data/adb/modules/dummy_module/system # cksum ./vendor/etc/sap.conf /vendor/etc/sap.conf
1967598015 7121 ./vendor/etc/sap.conf
1967598015 7121 /vendor/etc/sap.conf
root@ASUS_I006D:/data/adb/modules/dummy_module/system #

root@ASUS_I006D:/data/adb/modules/dummy_module/system # cksum  ./system_ext/etc/dpm/dpm.conf /system_ext/etc/dpm/dpm.conf
1970692378 2925 ./system_ext/etc/dpm/dpm.conf
1970692378 2925 /system_ext/etc/dpm/dpm.conf
root@ASUS_I006D:/data/adb/modules/dummy_module/system #

root@ASUS_I006D:/data/adb/modules/dummy_module/system # grep mynewfile /cache/magisk.log                                                                                     
01-01 00:11:51.764   767   769 W : Unable to add: /product/mynewfile, skipped
01-01 00:11:51.771   767   769 W : Unable to add: /system_ext/mynewfile, skipped
01-01 00:11:51.771   767   769 W : Unable to add: /vendor/mynewfile, skipped
root@ASUS_I006D:/data/adb/modules/dummy_module/system #
 


A work around to create new files in sub directories in /product, /vendor, or /system_ext:

Copy the complete existing folder to the sub directory in /data/adb/modules/<magisk_module>/system/[product|vendor|system_ext]/; create the file .replace in that directory and add the new file(s) to that directory.

Example:
root@ASUS_I006D:/ # find /data/adb/modules/testmodule/                                                                                                                                         
/data/adb/modules/testmodule/
/data/adb/modules/testmodule/system
/data/adb/modules/testmodule/system/product
/data/adb/modules/testmodule/system/product/lib
/data/adb/modules/testmodule/system/product/lib/.replace
/data/adb/modules/testmodule/system/product/lib/libframesequence.so
/data/adb/modules/testmodule/system/product/lib/libgiftranscode.so
/data/adb/modules/testmodule/system/product/lib/newfile001
/data/adb/modules/testmodule/system/vendor
/data/adb/modules/testmodule/system/vendor/app
/data/adb/modules/testmodule/system/vendor/app/TimeService
/data/adb/modules/testmodule/system/vendor/app/TimeService/TimeService.apk
/data/adb/modules/testmodule/system/vendor/app/TimeService/oat
/data/adb/modules/testmodule/system/vendor/app/TimeService/oat/arm64
/data/adb/modules/testmodule/system/vendor/app/TimeService/oat/arm64/TimeService.vdex
/data/adb/modules/testmodule/system/vendor/app/TimeService/oat/arm64/TimeService.odex
/data/adb/modules/testmodule/system/vendor/app/TimeService/newfile002
/data/adb/modules/testmodule/system/vendor/app/TimeService/.replace
/data/adb/modules/testmodule/system/system_ext
/data/adb/modules/testmodule/system/system_ext/app
/data/adb/modules/testmodule/system/system_ext/app/FM2
/data/adb/modules/testmodule/system/system_ext/app/FM2/.replace
/data/adb/modules/testmodule/system/system_ext/app/FM2/FM2.apk
/data/adb/modules/testmodule/system/system_ext/app/FM2/lib
/data/adb/modules/testmodule/system/system_ext/app/FM2/lib/arm64
/data/adb/modules/testmodule/system/system_ext/app/FM2/lib/arm64/libqcomfm_jni.so
/data/adb/modules/testmodule/system/system_ext/app/FM2/oat
/data/adb/modules/testmodule/system/system_ext/app/FM2/oat/arm64
/data/adb/modules/testmodule/system/system_ext/app/FM2/oat/arm64/FM2.odex
/data/adb/modules/testmodule/system/system_ext/app/FM2/oat/arm64/FM2.vdex
/data/adb/modules/testmodule/system/system_ext/app/FM2/newfile003
root@ASUS_I006D:/ #

root@ASUS_I006D:/ #   find /data/adb/modules/testmodule/system | grep newfile

/data/adb/modules/testmodule/system/product/lib/newfile001
/data/adb/modules/testmodule/system/vendor/app/TimeService/newfile002
/data/adb/modules/testmodule/system/system_ext/app/FM2/newfile003
root@ASUS_I006D:/ # 

root@ASUS_I006D:/ # pwd
/
root@ASUS_I006D:/ # ls -l $( find /data/adb/modules/testmodule/system | grep newfile | cut -f6- -d "/" )

-rw-r--r-- 1 root root 0 2022-08-07 14:34 system/product/lib/newfile001
-rw-r--r-- 1 root root 0 2022-08-07 14:24 system/system_ext/app/FM2/newfile003
-rw-r--r-- 1 root root 0 2022-08-07 14:36 system/vendor/app/TimeService/newfile002
root@ASUS_I006D:/ #



See How to change any file or directory using Magisk for another approach to change files on read-only mounted filesystems.


Executing scripts while booting the phone

Magisk also supports executing additional scripts while booting the phone.

The scripts to be executed must be copied to one of these directories

/data/adb/post-fs-data.d/   

/data/adb/service.d/     

see the description in the original Magisk documentation: https://topjohnwu.github.io/Magisk/details.html

Working examples for this technique are described in these posts:

How to run a script at every boot using Magisk

How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8

How to create or change a swap device in the OmniROM 12 using Magisk



Another example for using this technique I found here https://gist.github.com/niikoo/3f6bd13a69f2d68f3dd51cc667e79bdc

# Boot logging
# Create the file: /data/adb/post-fs-data.d/0001logcatboot

#!/system/bin/sh
mkdir -p /cache/logs
/system/bin/logcat -r 1024 -n 9 -v threadtime -f /cache/logs/log >info.log 2>err.log &



Note that I added commands to create / cleanup the log directory and changed the logcat command on my phone:

#!/system/bin/sh
m
kdir -p /cache/logs
rm -rf /cache/logs/*
/system/bin/logcat -r 102400 -n 9 -v threadTime -f /cache/logs/log >/cache/logs/info.log 2>/cache/logs/err.log &


To execute additional scripts earlier in the boot process you must add them to the boot image -- see the section Using Magisk to unpack and repack the boot image below for details.


Creating Magisk Modules

The official documentation for creating a Magisk Module is here: https://topjohnwu.github.io/Magisk/guides.html

Creating Magisk Modules that only use the standard features isn't that difficult and the official documentation should be sufficient.

I suggest to download an existing simple Magisk Module; unzip the ZIP file and study the contents.




Using Magisk to unpack and repack the boot image

The binary magiskboot that is part of the Magisk package can be used to unpack and repack the boot partition for every phone supported by Magisk

For details see here: How to change files in the boot image using Magisk 


Magisk
also supports changing files in the root directory via Root Directory Overlays (see https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md):

This feature can be used to create additional start or stop services for the Android OS:

Android uses init *rc files to define the services to start when booting and also to define the services to run when doing the shutdown (like the systemd or initd in other Linux implementations).
These files are read early in the boot process and therefore reside only in the ramdisk on the boot partition. To add new init* rc files the boot image must be modified. This can be done with Magisk:

Just add the new init*rc files and optimal other scripts or files to the boot image using Magisk . Magisk will then take care of processing the new init*rc files by the Android operating system when booting the phone.

See these posts:

How to run a script at shutdown
 
How to trigger an action when a property is changed .

How to enable root access using Magisk in a script

for examples for using that feature.



There are also magiskboot binaries for x86 in the Magisk apk file that can be used on a PC running the Linux OS:

see How to process Android boot image files on a PC running the Linux OS for more details about this feature.



Using Magisk to change the active slot

The Magisk App can also be used to change the active slot on phones with A/B slots -- for details see here :

How to switch the active slot 



Directories and files used by Magisk

The directories and files used by Magisk are documented here https://topjohnwu.github.io/Magisk/guides.html

Some important directories and files for developing and trouble shooting Magisk Modules are:

The logfile used by Magisk is 

/cache/magisk.log 

In case something goes wrong with a Magisk Module you should first check that file.

The base directory for the data files for Magisk is

/data/adb


The config setttings for Magisk are stored in the SQLite database

/data/adb/magisk.db


To view (or probably) change the database entries via script you can either use sqlite3 binary (if installed).

Example:
ASUS_I006D:/ #  sqlite3 /data/adb/magisk.db .tables

denylist  policies  settings  strings

ASUS_I006D:/ #

ASUS_I006D:/ # sqlite3 /data/adb/magisk.db                                                                                                                                   
SQLite version 3.7.6.3-Titanium
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

sqlite> .headers on
sqlite> .mode column
sqlite>
sqlite> .tables
denylist  policies  settings  strings
sqlite>
sqlite> .schema
CREATE TABLE denylist (package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));
CREATE TABLE policies (uid INT, policy INT, until INT, logging INT, notification INT, PRIMARY KEY(uid));
CREATE TABLE settings (key TEXT, value INT, PRIMARY KEY(key));
CREATE TABLE strings (key TEXT, value TEXT, PRIMARY KEY(key));
sqlite>
sqlite> select * from denylist ;
sqlite>
sqlite> select * from policies ;
uid         policy      until       logging     notification
----------  ----------  ----------  ----------  ------------
2000        2           0           1           1          
10134       1           0           1           1          
sqlite>
sqlite> select * from settings ;
key         value    
----------  ----------
denylist    0        
su_biometr  0        
sqlite>
sqlite> select * from strings ;
sqlite>



or use the magisk binary, e.g:

ASUS_I006D:/ # magisk --sqlite  'select * from policies'                                                                                                                    

logging=1|notification=1|policy=2|uid=2000|until=0

ASUS_I006D:/ #

ASUS_I006D:/ # magisk --sqlite  'select * from settings'                                                                                                                    
key=denylist|value=0
key=su_biometric|
value=0
ASUS_I006D:/ #  

There is a handy little script to list all apps with root access in the XDA thread https://forum.xda-developers.com/t/magisk-general-support-discussion.3432382/page-2681 :

list_apps_with_root_access.sh
#!/system/bin/sh

GRANTED=$(magisk --sqlite "SELECT uid FROM policies WHERE policy = '2';")

echo 'Packages with root granted:'

for UID in $GRANTED
do
UID=$(echo $UID | sed 's!^uid=!!g')
pm list packages --uid $UID
done


How to create the Magisk database manually

To create the Magisk database manually (e.g. after installing Magisk manually via script) these commands can be used:

# start the Magisk daemon if necessary
#
MAGISK_DAEMON_IS_RUNNING=true
/data/adb/magisk/magisk64 -v || /data/adb/magisk/magisk64 --daemon && MAGISK_DAEMON_IS_RUNNING=false
#
# force Magisk to create the Magisk database if it not yet exists
#
/data/adb/magisk/magisk64 --sqlite "PRAGMA user_version"

# stop the Magisk daemon again
#
[ ${MAGISK_DAEMON_IS_RUNNING} = false ] && /data/adb/magisk/magisk64 --stop



To create the Magisk database using sqlite3 use these commands:

.open ${CUR_MAGISK_DATABASE_FILE}
CREATE TABLE IF NOT EXISTS policies (uid INT, policy INT, until INT, logging INT,notification INT, PRIMARY KEY(uid));
CREATE TABLE IF NOT EXISTS settings (key TEXT, value INT, PRIMARY KEY(key));
CREATE TABLE IF NOT EXISTS strings (key TEXT, value TEXT, PRIMARY KEY(key));
CREATE TABLE IF NOT EXISTS denylist  (package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));
 PRAGMA user_version=6;
.exit


Note:

The Magisk database will be upgraded to the latest version automatically by the Magisk daemon after the next restart




Magisk root access configuration details


Magisk uses the table policies in the sqlite database /data/adb/magisk.db to store the list of root enabled apps.
See this post for details about this feature and how to enable root access via Magisk using a script.




Magisk App installation directory

Use

pm list packages -f | grep magisk

to get the directory where the Magisk App is installed.

e.g.

ASUS_I006D:/dev/hP3B # pm list packages -f | grep magisk
package:/data/app/~~oYhS9Dm94EFcjhYnyguTHg==/com.topjohnwu.magisk-2Ckcx1nejY3zYAklqVhBhw==/base.apk=com.topjohnwu.magisk
ASUS_I006D:/dev/hP3B #


Credits:  https://forum.xda-developers.com/t/magisk-general-support-discussion.3432382/page-2689#post-87694851


The Magisk App Configuration files

The Magisk App stores the configuration (e.g. the app settings) in the default directory for App Settings, on my phone this is

/data/user_de/0/com.topjohnwu.magisk/shared_prefs

The file in that directory used to store the settings is

com.topjohnwu.magisk_preferences.xml

For more details about the file see the post Where does the Magisk App store the settings?.


Temporary Magisk config directory

While running Magisk uses a temporary directory for the configuration.

The path to the config directory can be retrieved via magisk binary:

ASUS_I006D:/dev/hP3B # magisk --path
/dev/hP3B
ASUS_I006D:/dev/hP3B#



The contents of that directory look like :

ASUS_I006D:/dev/hP3B # ls -al /dev/hP3B/
total 568
drwx------  3 root root    200 1970-01-01 04:08 .
drwxr-xr-x 27 root root   5480 2022-11-06 18:32 ..
drwxr-xr-x  8 root root    180 1970-01-01 04:08 .magisk
lrwxrwxrwx  1 root root     10 1970-01-01 04:08 magisk -> ./magisk64
-rwxr-xr-x  1 root root      0 1970-01-01 04:08 magisk32
-rwxr-xr-x  1 root root 247168 1970-01-01 04:08 magisk64
-rwxr-xr-x  1 root root 328240 1970-01-01 04:08 magiskpolicy
lrwxrwxrwx  1 root root      8 1970-01-01 04:08 resetprop -> ./magisk
lrwxrwxrwx  1 root root      8 1970-01-01 04:08 su -> ./magisk
lrwxrwxrwx  1 root root     14 1970-01-01 04:08 supolicy -> ./magiskpolicy

ASUS_I006D:/dev/hP3B # ls -al /dev/hP3B/.magisk/
total 7
drwxr-xr-x 8 root root  180 1970-01-01 04:08 .
drwx------ 3 root root  200 1970-01-01 04:08 ..
d--------- 2 root root  160 1970-01-01 04:08 block
drwxr-xr-x 2 root root 7240 1970-01-01 04:08 busybox
---------- 1 root root  127 1970-01-01 04:08 config
d--------- 7 root root  220 1970-01-01 04:08 mirror
drwxr-xr-x 3 root root 3452 2022-11-06 18:32 modules
drwxr-xr-x 2 root root    0 1970-01-01 04:08 pts
d--------- 2 root root   80 1970-01-01 04:08 selinux

ASUS_I006D:/dev/hP3B # cat /dev/hP3B/.magisk/config
KEEPVERITY=false
KEEPFORCEENCRYPT=false
PATCHVBMETAFLAG=false
RECOVERYMODE=false
SHA1=1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/dev/hP3B #



Backups of boot partitions

Magisk creates backups of the boot partitions in sub directories in /data/adb with the name in the format

magisk_backup_[uniq_string]

e.g.:

Magisk creates backups of the boot partitions in /data/adb e.g.:

root@ASUS_I006D:/data/adb # ls -l /data/magisk_backup_*
/data/magisk_backup_79f3370cd83d03441325998a8875888780c3182f:
total 31712
-rwxr-xr-x 1 root root 32436260 2022-09-26 12:20 boot.img.gz

/data/magisk_backup_a0c712541fd002c331c25772a3b8609ae2fba546:
total 31712
-rwxr-xr-x 1 root root 32436965 2022-09-27 19:30 boot.img.gz
root@ASUS_I006D:/data/adb #



uniq_string in the name of the backup files is the SHA-1 from the boot image that was patched to install Magisk:

e.g.

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./install_magisk_via_twrp.sh
install_magisk_via_twrp.sh version - v2.0.0.1 - add Magisk to the boot partition of a phone running Android using TWRP
....
Creating the boot image file "/sdcard/Download/boot_b.1086412.img" from the partition "/dev/block/by-name/boot_b" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 0.313082 s, 307 M/s

Checking the result ...
-rw-rw---- 1 root media_rw 100663296 2022-11-06 16:01 /sdcard/Download/boot_b.1086412.img
...
OK, patching the boot partition "/dev/block/by-name/boot_b" was successfull
....
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$


The image file patched by Magisk in this example is /sdcard/Download/boot_b.1086412.img.


The backup of the boot partition on the phone created by Magisk for this installation is:

ASUS_I006D:/ # ls -ld /data/magisk_backup*
drwxr-xr-x 2 root root 3452 2022-11-06 17:14 /data/magisk_backup_1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/ #

ASUS_I006D:/ # ls -l /data/magisk_backup_1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
total 50692
-rw-r--r-- 1 root root 51852324 2022-11-06 17:14 boot.img.gz
ASUS_I006D:/ #


1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb is the SHA-1 from the image file used for the installation of Magisk:

ASUS_I006D:/ # sha1sum /sdcard/Download/boot_b.1086412.img
1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb  /sdcard/Download/boot_b.1086412.img
ASUS_I006D:/ #

# or

ASUS_I006D:/data/adb/workdir/unpack/ramdisk # /data/adb/magisk/magiskboot  sha1 /sdcard/Download/boot_b.1086412.img                                                                                             
1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/data/adb/workdir/unpack/ramdisk #



The SHA1 from the previous boot image is stored in the file /dev/hP3B/.magisk/config used by Magisk while running, e.g.:

ASUS_I006D:/data/adb/workdir/unpack/ramdisk # cat /dev/hP3B/.magisk/config
KEEPVERITY=false
KEEPFORCEENCRYPT=false
PATCHVBMETAFLAG=false
RECOVERYMODE=false
SHA1=1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/data/adb/workdir/unpack/ramdisk #                     
                                                                                                                                                  

/dev/hP3B is a directory on the temporary ramdisk used by Magisk while it is running.

Use the command magisk --path to retrieve the path for the current ramdisk while Magisk is running, e.g.:

ASUS_I006D:/data/adb/workdir/unpack/ramdisk # magisk --path
/dev/hP3B
ASUS_I006D:/data/adb/workdir/unpack/ramdisk #



The SHA1 from the previous boot image is also stored in the file .backup/.magisk in the ramdisk of a boot image, e.g:

# Note: unpack the boot image and the ramdisk from the boot image to get that file
#
ASUS_I006D:/data/adb/workdir/unpack/ramdisk # cat .backup/.magisk
KEEPVERITY=false
KEEPFORCEENCRYPT=false
PATCHVBMETAFLAG=false
RECOVERYMODE=false
SHA1=1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/data/adb/workdir/unpack/ramdisk #


To do it all in once use:

grep "^SHA1=" $( magisk --path )/.magisk/config | cut -f2 -d


e.g.:

ASUS_I006D:/data/adb/workdir/unpack/ramdisk # grep "^SHA1=" $( magisk --path )/.magisk/config | cut -f2 -d "="                                                                                                      
1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb
ASUS_I006D:/data/adb/workdir/unpack/ramdisk # #




Backup the Magisk config

To backup the Magisk config just copy the directories in /data/adb except the directory /data/adb/magisk (that directory is used for the binaries only), e.g.:

adb shell tar --exclude data/adb/magisk/ -czf /sdcard/Download/magisk_config_$( date +%Y-%m-%d).$$.tar /data/adb/

To restore the backup unpack the tar file on the phone and reboot the phone.

To also create a backup of the settings from the Magisk App create a backup of the directory with the Magisk App Settings (see above for details).


Start/Stop the Magisk App


To start the Magisk App via CLI command use

am start -n com.topjohnwu.magisk/.ui.MainActivity

To stop the Magisk App via CLI command use:

am force-stop  com.topjohnwu.magisk


Start/Stop the Magisk Daemon


To stop the Magisk Daemon use

magisk --stop

Be aware that stopping the magisk daemon will remove all bind mounts for files in /system. To re-enable these bind mounts a reboot is required.


To start the Magisk Daemon use

/data/adb/magisk/magisk64 --daemon 

To check if the Magisk Daemon is running use

/data/adb/magisk/magisk64 -v 

Example output:

# Magisk Daemon is running

ASUS_I006D:/ # /data/adb/magisk/magisk64 -v
25.2:MAGISK:R
ASUS_I006D:/ #

# Magisk Daemon is not running

|ASUS_I006D:/ # /data/adb/magisk/magisk64 -v
No daemon is currently running!
1|ASUS_I006D:/ #



Building Magisk

The Magisk source code is available at

https://github.com/topjohnwu/Magisk

There are also instructions how to create a local copy of the repository and compile Magisk on that page. I've successfully build Magisk using these instructions.


Miscellaneous


magiskboot can also be used compress or decompress files:

1|ASUS_I006D:/data/adb/magisk # ./magiskboot                                                                                                                                                
MagiskBoot - Boot Image Modification Tool

Usage: ./magiskboot <action> [args...]

Supported actions:
...

  compress[=format] <infile> [outfile]
    Compress <infile> with [format] to [outfile].
    <infile>/[outfile] can be '-' to be STDIN/STDOUT.
    If [format] is not specified, then gzip will be used.
    If [outfile] is not specified, then <infile> will be replaced
    with another file suffixed with a matching file extension.
    Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg

  decompress <infile> [outfile]
    Detect format and decompress <infile> to [outfile].
    <infile>/[outfile] can be '-' to be STDIN/STDOUT.
    If [outfile] is not specified, then <infile> will be replaced
    with another file removing its archive format file extension.
    Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg

1|ASUS_I006D:/data/adb/magisk #



magiskboot is also used in TWRP to unpack and repack the boot image for installing TWRP into the boot partition -- see How to add additional files to an TWRP image.
 


Using the magisk binary while the phone is booted into TWRP


If the used TWRP can mount the volume for /data you can also use the binary magisk while in TWRP.  The magisk binary is not in the path while booted into TWRP - therefor you must use the fully qualified filename:

This is

/data/adb/magisk/magisk64

for 64 Bit CPUs and

/data/adb/magisk/magisk32

for 32 Bit CPUs.

Some functions of Magisk are only usable if the Magisk daemon is running. To start the Magisk daemon the Magisk binary can also be used - example:

# read the policies table from the Magisk squlite database
#
ASUS_I006D:/ # /data/adb/magisk/magisk64 --sqlite "select * from policies ;"                                                                               
No daemon is currently running!

#
# -> the Magisk daemon is not running -> start it
#
1|ASUS_I006D:/ # /data/adb/magisk/magisk64 --daemon
ASUS_I006D:/ #

ASUS_I006D:/ # /data/adb/magisk/magisk64 --sqlite "select * from policies ;"                                                                                 
logging=1|notification=1|policy=2|uid=2000|until=0
logging=1|notification=1|policy=2|uid=10135|until=0
logging=1|notification=1|policy=2|uid=10143|until=0
logging=1|notification=1|policy=2|uid=10055|until=0
logging=1|notification=1|policy=2|uid=10142|until=0
ASUS_I006D:/ #



Use

/data/adb/magisk/magisk64 -V

to check if the Magisk daemon is running

Use

/data/adb/magisk/magisk64 --stop

to stop the Magisk Daemon, e.g.:

ASUS_I006D:/ # /data/adb/magisk/magisk64 -V
25200

# -> The Magisk Daemon is running

ASUS_I006D:/ # /data/adb/magisk/magisk64 --stop                                                                                                              
ASUS_I006D:/ #

ASUS_I006D:/ # /data/adb/magisk/magisk64 -V                                                                                                                  
No daemon is currently running!
1|ASUS_I006D:/ # 2D




Trouble Shooting

An error like this

08-06 18:41:39.341 +0000  1356  1726 W ziparchive: Unable to open '/system/app/AsusFMRadio/AsusFMRadio.apk': Permission denied
08-06 18:41:39.341 +0000  1356  1726 E system_server: Failed to open APK '/system/app/AsusFMRadio/AsusFMRadio.apk': I/O error
08-06 18:41:39.354 +0000  1356  1356 W PackageManager: Failed to parse /system/app/AsusFMRadio: Failed to parse /system/app/AsusFMRadio/AsusFMRadio.apk


is most of the time caused by missing read permissions for the file.

Use

chmod o+r /system/app/AsusFMRadio/AsusFMRadio.apk

to fix it.

If something went wrong and booting the phone does not work anymore after installing a MagiskModule just remove the files in
/data/adb/modules/<modulename> and reboot the phone :

Either connect via adb to the not booting phone (this should be possible in most cases even if the boot process does not finish), delete the files, and reboot the phone. Or reboot the phone from a Recovery image like TWRP, delete the files in /data/adb/modules/<modulename>, and reboot the phone.

The same procedure can be used if booting the phone does not work anymore after adding another init script - just delete the new script in

/data/adb/post-fs-data.d


or

/data/adb/service.d

and reboot the phone


To catch errors from a script executed by Magisk you might use this technique

# redirect STDERR of all commands in the script to a file
#
exec 2>/data/script_stderr.log
set -x

... rest of your script



To remove all installed Magisk Modules using the official method use:


magisk --remove-modules  

to remove all modules (but not the new init scripts!) and reboot the phone

According to the FAQ Magisk will not start if the phone is booted into safe mode (see https://topjohnwu.github.io/Magisk/faq.html)

Be aware that after rebooting the phone again in normal mode all Magisk Modules are disabled and must be enabled again either using the Magisk App or via CLI command:

To reenable all Magisk Modules via shell command do

adb shell rm /data/adb/modules/*/disable



History of this entry

07.08.2022 /bs

Added additional infos about the permissions for new files for /system.

Added additional commands to the script for catching the OS logs while booting the phone

Added infos about a workaround to add new files to /product, /vendor, or /system_ext

Added infos about how to access file replaced by a Magisk Module

20.09.2022 /bs

Added new links to posts about configuring swap devices via Magisk script

Added a link to the post about how to use Magisk to unpack and repack the boot image

28.09.2022 /bs

Added infos about the backups of the boot partitions created by Magisk

30.09.2022 /bs

Added a short info about using Magisk Overlays to change files in the root filesystem

Added the URL for the post with how to change the active slot using the Magisk App

02.10.2022 /bs

Added a short info about Root Directory Overlay system from Magisk

04.10.2022 /bs

Add an URL to another post to use the Root Directory Overlay system from Magsik

Fixed some spelling errors and also some errors in the code examples

Added more details about changing files in /system

26.10.2022 /bs

Added the section Miscellaneous 

28.10.2022/bs

Added the section Start/Stop the Magisk App

Added the section Start/Stop the Magisk Daemon 


02.11.2022 /bs


added the infos about the x86 version of the magiskboot executables in the Magisk apk file

fixed some spelling and formatting errors

04.11.2022 /bs

corrected some formatting errors
add the link to the Howto about making a file in /system writable

06.11.2022/bs

added more details about the boot partition backups created by Magisk

07.11.2022/bs

added more details about the boot partition backups created by Magisk
added the section about the temporary Magisk config directory

08.11.2022/bs

added the infos about how to get the Magisk app installation directory

25.11.2022/bs

added infos about how to build Magisk using a local copy of the repository

02.12.2022 /bs

added the infos about using the magisk binary while the phone is booted into TWRP

06.12.2022 /bs

added the section about Magisk root access configuration details
added infos about the files used to store the settings for the Magisk App

30.12.2022 /bs

added more infos about how to add new start / stop services in Android

07.05.2023 /bs

add infos about how to create the Magisk database manually






 

How to change files in the boot image using Magisk


URL:  https://forum.xda-developers.com/t/how-to-change-files-in-the-boot-image-using-magisk.4495645/

How to changes files in the boot image using Magisk
Sometime it's necessary to change a file in the boot image used for booting an Android phone (for example to change the default properties that are used before Magisk is loaded)

To change or add files to the boot image for an Android phone the Magisk binary magiskboot can be used

magiskboot is

A tool to unpack / repack boot images, parse / patch / extract cpio, patch dtb, hex patch binaries, and compress / decompress files with multiple algorithms.

and can handle the boot images for all phones supported by Magisk.

Warning

Be aware that changes to the boot image only survive until the installation of the next OS update. So it's strongly recommended to use Magisk to change files from the OS if possible.


Anyway -- to change a file in the boot image open a shell as user root on the phone and do:

Notes:

The following instructions were done on Zenfone 8 running OmniROM 12 but should also work for other ROMs.

At the end of this post are the commands to unpack the boot partition and to recreate the boot partition listed for cut & paste.



#
# first create the directory tree for the work
#
# Note: 
#
# This directory tree must be on a Unix filesystem like that mounted to /data; a filesystem like that mounted to /sdcard does not work
#
ASUS_I006D:/data/adb # cd /data/adb
ASUS_I006D:/data/adb # mkdir workdir
ASUS_I006D:/data/adb # mkdir workdir/backup
ASUS_I006D:/data/adb # mkdir workdir/ramdisk
ASUS_I006D:/data/adb # mkdir workdir/boot_files
ASUS_I006D:/data/adb #

Next get the current boot slot:

ASUS_I006D:/data/adb/workdir # getprop ro.boot.slot_suffix
_b


and create an image of the current boot partition:

#
ASUS_I006D:/ # cd /data/adb/workdir
ASUS_I006D:/data/adb/workdir # 

ASUS_I006D:/data/adb/workdir # dd if=/dev/block/by-name/boot_b of=./boot_b.img
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 0.374815 s, 256 M/s
ASUS_I006D:/data/adb/workdir # 


Create a backup of the original boot image file:

ASUS_I006D:/data/adb/workdir # cp boot_b.img backup/
ASUS_I006D:/data/adb/workdir # 


Note: 

You will need the boot image file just created to restore the boot partition in case the phone will not boot anymore with your changed boot partition. Therefor you should also copy the boot image file to your PC.


Next unpack the boot image using magiskboot into an empty directory:

ASUS_I006D:/data/adb/workdir # cd boot_files

ASUS_I006D:/data/adb/workdir/boot_files # ls 
ASUS_I006D:/data/adb/workdir/boot_files # 

ASUS_I006D:/data/adb/workdir/boot_files # /data/adb/magisk/magiskboot unpack  ../boot_b.img
Parsing boot image: [../boot_b.img]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [15093897]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
ASUS_I006D:/data/adb/workdir/boot_files # 


magiskboot will split the boot.img file into the various parts -- for most phones with Android 12 this should be the kernel and the ramdisk:

ASUS_I006D:/data/adb/workdir/boot_files # ls -l
total 71264
-rw-r--r-- 1 root root 42025472 2022-09-18 18:22 kernel
-rw-r--r-- 1 root root 30864336 2022-09-18 18:22 ramdisk.cpio
ASUS_I006D:/data/adb/workdir/boot_files # 


The files for the ramdisk used while booting the phone are in an cpio archive so to change them we must unpack the cpio archive with the ramdisk contents:

#
ASUS_I006D:/ # cd /data/adb/workdir/ramdisk
ASUS_I006D:/data/adb/workdir/ramdisk #

130|ASUS_I006D:/data/adb/workdir/ramdisk # cpio -idm  <../boot_files/ramdisk.cpio

ASUS_I006D:/data/adb/workdir/ramdisk #  ls -ltr
total 1868
-rw-r--r-- 1 root root   20700 1970-01-01 01:00 vendor_property_contexts
-rw-r--r-- 1 root root   89884 1970-01-01 01:00 vendor_file_contexts
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 vendor
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 tmp
-rw-r--r-- 1 root root    5783 1970-01-01 01:00 system_ext_property_contexts
-rw-r--r-- 1 root root    3347 1970-01-01 01:00 system_ext_file_contexts
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 system_ext
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 sys
drwxr-x--x 2 root root    3452 1970-01-01 01:00 storage
-rw-r--r-- 1 root root 1027673 1970-01-01 01:00 sepolicy
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 second_stage_resources
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 sdcard
-rw-r--r-- 1 root root   31390 1970-01-01 01:00 prop.default
-rw-r--r-- 1 root root    1721 1970-01-01 01:00 product_property_contexts
-rw-r--r-- 1 root root     306 1970-01-01 01:00 product_file_contexts
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 product
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 proc
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 postinstall
-rw-r--r-- 1 root root   76958 1970-01-01 01:00 plat_property_contexts
-rw-r--r-- 1 root root   40490 1970-01-01 01:00 plat_file_contexts
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 oem
-rw-r--r-- 1 root root       0 1970-01-01 01:00 odm_property_contexts
-rw-r--r-- 1 root root       0 1970-01-01 01:00 odm_file_contexts
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 mnt
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 metadata
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 linkerconfig
-rwxr-x--- 1 root root    2025 1970-01-01 01:00 init.recovery.qcom.rc
-rwxr-x--- 1 root root  491528 1970-01-01 01:00 init
lrwxrwxrwx 1 root root      11 1970-01-01 01:00 etc -> /system/etc
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 dev
lrwxrwxrwx 1 root root      12 1970-01-01 01:00 default.prop -> prop.default
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 debug_ramdisk
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 data_mirror
drwxr-x--x 2 root root    3452 1970-01-01 01:00 data
lrwxrwxrwx 1 root root      17 1970-01-01 01:00 d -> /sys/kernel/debug
dr-xr-xr-x 2 root root    3452 1970-01-01 01:00 config
lrwxrwxrwx 1 root root      50 1970-01-01 01:00 bugreports -> /data/user_de/0/com.android.shell/files/bugreports
lrwxrwxrwx 1 root root      11 1970-01-01 01:00 bin -> /system/bin
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 batinfo
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 asdf
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 apex
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 acct
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 APD
drwxr-xr-x 2 root root    3452 1970-01-01 01:00 ADF
drwxr-xr-x 4 root root    3452 2022-09-18 18:28 first_stage_ramdisk
drwxr-x--- 3 root root    3452 2022-09-18 18:28 overlay.d
drwxr-xr-x 2 root root    3452 2022-09-18 18:28 odm_dlkm
drwxr-xr-x 2 root root    3452 2022-09-18 18:28 odm
drwxr-xr-x 3 root root    3452 2022-09-18 18:28 res
drwxr-xr-x 5 root root    3452 2022-09-18 18:28 system
drwxr-xr-x 2 root root    3452 2022-09-18 18:28 vendor_dlkm
ASUS_I006D:/data/adb/workdir/ramdisk # 



Now you can change the files from the ramdisk; add new files, or do anything necessary.

Be aware that most of the directories in the ramdisk are mount points to mount the other partitions. Therefor it's quite pointless to add files in these directories in the ramdisk.


Changing files in the ramdisk from the boot image


Changing one or more properties

Warning:

Please note that I am not an expert on Android internals and the following may be completely wrong.

(Any comments or corrections are welcome)


There are some files with property definitions in the boot image:

ASUS_I006D:/data/adb/workdir/ramdisk # find . -name "*.prop"
./default.prop
./system/etc/ramdisk/build.prop
ASUS_I006D:/data/adb/workdir/ramdisk #


To change or add a property it should be sufficient to add or change it in the file default.prop in the ramdisk from the boot image. But that does not work (at least in my tests...) So to change or add a property it must be added to the file 

/system/etc/ramdisk/build.prop

in the ramdisk for the boot image.

For example to add a swap device with 2 GB in the OmniROM these properties can be used:

#
# define the size for the device zram0 (use the postfix G for values in Gigabytes or M for values in Megabytes)
#
vendor.zram.disksize=2147483648
vendor.zram.enable=1



The result after repacking and installing the boot image :

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/data/adb/workdir/ramdisk # getprop vendor.zram.disksize
2147483648
ASUS_I006D:/data/adb/workdir/ramdisk # vendor.zram.enable
1
ASUS_I006D:/data/adb/workdir/ramdisk #
1|ASUS_I006D:/data/adb/workdir/ramdisk # free
        total        used        free      shared     buffers
Mem:       7612493824  4858556416  2753937408    12169216    15933440
-/+ buffers/cache:     4842622976  2769870848
Swap:      2147479552           0  2147479552
ASUS_I006D:/data/adb/workdir/ramdisk # 


Notes:

These new properties defined in the file /system/etc/ramdisk/build.prop in the ramdisk are only visible for the root user. Most probably a SELinux definition in one of the *context* files in the boot image is required to make the property visible for all users (but I don't know the necessary permissions for the properties to make them visible for all user)

The current SELinx context for these properties is defined in the file vendor_property_contexts in the ramdisk from boot image:

ASUS_I006D:/data/adb/workdir/ramdisk # grep zram vendor_property_contexts 
vendor.zram.disksize   u:object_r:vendor_exported_system_prop:s0 
vendor.zram.enable     u:object_r:vendor_exported_system_prop:s0 
persist.vendor.zram    u:object_r:vendor_mpctl_prop:s0
vendor.asus.zram       u:object_r:vendor_mpctl_prop:s0
vendor.zram.           u:object_r:vendor_mpctl_prop:s0
ASUS_I006D:/data/adb/workdir/ramdisk # 




To add a new property without a specific SELInux context it seems to be sufficient to add the property to the file /system/etc/ramdisk/build.prop in the ramdisk from the boot image
(this does not work for properties starting with vendor. ):


# add the property to the file
#
130|ASUS_I006D:/ # tail -3 /data/adb/workdir/ramdisk/system/etc/ramdisk/build.prop
build.mysettings.variable01.value=5555
#
# end of file
ASUS_I006D:/ # 


After the reboot the property is visible for non-root user:

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $ 

ASUS_I006D:/ $ getprop build.mysettings.variable01.value     
5555
ASUS_I006D:/ $ 



But the new property can only be changed by the user root:

1|ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $ 
ASUS_I006D:/ $ setprop build.mysettings.variable01.value 7777                                                                                                             
Failed to set property 'build.mysettings.variable01.value' to '7777'.
See dmesg for error reason.
1|ASUS_I006D:/ $ 
1|ASUS_I006D:/ $ su -
ASUS_I006D:/ # 
ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ # 
ASUS_I006D:/ # setprop build.mysettings.variable01.value 7777   
ASUS_I006D:/ # 
ASUS_I006D:/ # getprop build.mysettings.variable01.value
7777
ASUS_I006D:/ # 
ASUS_I006D:/ # ^D
ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $ getprop build.mysettings.variable01.value
7777
ASUS_I006D:/ $
 


(see here https://source.android.com/docs/core/architecture/configuration/add-system-properties for details about defining properties)


When done with your changes repack the ramdisk.

First we recreate the cpio archive:

ASUS_I006D:/data/adb/workdir # cd /data/adb/workdir/ramdisk 
ASUS_I006D:/data/adb/workdir/ramdisk # 

ASUS_I006D:/data/adb/workdir/ramdisk # find . | cpio -o  >../boot_files/ramdisk.cpio                                                                                
ASUS_I006D:/data/adb/workdir/ramdisk # 

ASUS_I006D:/data/adb/workdir/ramdisk # ls -l ../boot_files/ramdisk.cpio
-rw-r--r-- 1 root root 30865560 2022-09-18 18:31 ../boot_files/ramdisk.cpio
ASUS_I006D:/data/adb/workdir/ramdisk # 


And then we recreate the boot image with the kernel and the changed ramdisk using magiskboot:

ASUS_I006D:/data/adb/workdir/ramdisk # cd /data/adb/workdir/boot_files/                                                                                                   
ASUS_I006D:/data/adb/workdir/boot_files # 

130|ASUS_I006D:/data/adb/workdir/boot_files # /data/adb/magisk/magiskboot repack ../boot_b.img ../boot_b.img.new                                                    
Parsing boot image: [../boot_b.img]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [15093897]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
Repack to boot image: [../boot_b.img.new]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [15094405]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
ASUS_I006D:/data/adb/workdir/boot_files # 

ASUS_I006D:/data/adb/workdir/boot_files # ls -l ../boot_b.img ../boot_b.img.new
-rw-r--r-- 1 root root 100663296 2022-09-18 18:20 ../boot_b.img
-rw-r--r-- 1 root root 100663296 2022-09-18 18:34 ../boot_b.img.new
ASUS_I006D:/data/adb/workdir/boot_files #  
                                                                                                                              

The new boot image file can now be written to the boot partition (either via dd or fastboot):

ASUS_I006D:/data/adb/workdir # cd /data/adb/workdir

ASUS_I006D:/data/adb/workdir # dd if=./boot_b.img.new  of=/dev/block/by-name/boot_b                                                                                       
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 5.324559 s, 18 M/s
ASUS_I006D:/data/adb/workdir # 


That's it -- now the new boot image should be used for the next reboots of the phone.


Additional infos

Please note that the file /system/etc/ramdisk/build.prop does not exist in the ramdisk from the boot disk used in the Original Android 12 for the ASUS Zenfone 8 . So this approach to change the properties does not work in that OS.
But the general approach used in these instructions to change files in the ramdisk used for the boot image does also work in the Original Android 12 for the ASUS Zenfone 8.


In the OmniROM for the Zenfone 8 and also in the Original Android 10 for the Zenfone 8 the swap device is created and configured with this init script: /vendor/bin/init.asus.zram.sh

See here

How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8

for a method using Magisk to change the swap device config in the Original ASUS Android 12

See here

How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8


for a method using Magisk to change the swap device config in the OnmiROM 12


Replace files in the root directory

Magisk also supports changing files in the root directory via an overlay system -- see the section Root Directory Overlay System in this page:

https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md

To use this feature it's also neccessary to unpack and repack the boot partition.

An example about how to use this feature is here: Magisk overlay - execute a script or copy files  

Another example is in this thread: https://github.com/topjohnwu/Magisk/issues/2978
Including some sample code to use magiskboot to change files in the ramdisk image from the boot partition:

./magiskboot unpack boot_root.img
./magiskboot cpio ramdisk.cpio \
"mkdir 0700 overlay.d" \
"add 0700 overlay.d/init.custom.rc init.custom.rc" \
"mkdir 0700 overlay.d/sbin" \
"add 0700 overlay.d/sbin/custom.sh init.custom.sh"
./magiskboot repack boot_root.img



Another example for this techniquie is in this post:

How to enable access via adb on a new installed OS



Trouble Shooting

If the phone does not boot anymore with the changed boot partition restore the boot partition from the boot image file created at the beginning either via dd after booting from a recovery image like TWRP or using fastboot to flash the boot paritition.

It's important to at least unpack the cpio with the ramdisk contents on an Unix filesystem (e.g. ext3, ext4, etc) - this can not be done in the device mounted to /sdcard.

Instructions for cut & paste

The instructions below are for cut & paste.  Be aware that there is no error checking.

Instructions to copy the boot partition to a boot image file and to unpack the boot image and the ramdisk


CUR_SLOT=$(  getprop ro.boot.slot_suffix )


cd /data/adb

mkdir /data/adb/workdir
mkdir /data/adb/workdir/backup
mkdir /data/adb/workdir/ramdisk
mkdir /data/adb/workdir/boot_files
cd /data/adb/workdir
dd if=/dev/block/by-name/boot${CUR_SLOT} of=./boot${CUR_SLOT}.img
cp boot_b.img backup/
cd boot_files
/data/adb/magisk/magiskboot unpack  ../boot${CUR_SLOT}.img
cd /data/adb/workdir/ramdisk
cpio -idm  <../boot_files/ramdisk.cpio
pwd
ls -ltr



Instructions to repack the ramdisk and the boot image and to rewrite the boot partition

CUR_SLOT=$( getprop ro.boot.slot_suffix )

cd /data/adb/workdir/ramdisk/
find . | cpio -o  >../boot_files/ramdisk.cpio
cd /data/adb/workdir/boot_files/
/data/adb/magisk/magiskboot repack ../boot${CUR_SLOT}.img ../boot${CUR_SLOT}.img.new
cd /data/adb/workdir
dd if=./boot${CUR_SLOT}.img.new  of=/dev/block/by-name/boot${CUR_SLOT}

reboot





History of this entry
History

30.09.2022/bs


add infos about changing files in the root directory via Magisk overlays

02.11.2022/bs

see How to process Android boot image files on a PC running the Linux OS for instructions about using magiskboot on a machine running the Linux OS

30.12.2022 /bs

Added a link to the new post to enable adb access




How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8


URL:  https://forum.xda-developers.com/t/how-to-disable-or-change-the-swap-device-in-the-android-12-from-asus-for-the-zenfone-8.4495085/

How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8
In the original Android 12 from ASUS for the Zenfone 8 a swap device is configured by default:

ASUS_I006D:/ # getprop   ro.product.build.fingerprint
asus/WW_I006D/ASUS_I006D:12/SKQ1.210821.001/31.1010.0411.113:user/release-keys
ASUS_I006D:/ # 

ASUS_I006D:/ # getprop ro.product.build.date 
Wed Jul 27 02:56:23 CST 2022
ASUS_I006D:/ # 

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7389499392   223170560    84582400    12218368
-/+ buffers/cache:     7377281024   235388928
Swap:      4294963200           0  4294963200
ASUS_I006D:/ # 



Note:

All commands in these instructions must be done by the user root.


To disable the swap device in the Android 12 from ASUS open a shell and execute:

setprop vendor.zram.enable 0      


Example:

ASUS_I006D:/data/adb/workdir/ramdisk # free
        total        used        free      shared     buffers
Mem:       7612669952  7334137856   278532096    82571264    19410944
-/+ buffers/cache:     7314726912   297943040
Swap:      4294963200           0  4294963200
root@ASUS_I006D:/data/adb/workdir/ramdisk # 

ASUS_I006D:/ # setprop vendor.zram.enable 0                                                                                                                               
ASUS_I006D:/ # 

ASUS_I006D:/ # free                                                                                                                                                       
        total        used        free      shared     buffers
Mem:       7612669952  7392669696   220000256    84586496    12263424
-/+ buffers/cache:     7380406272   232263680
Swap:               0           0           0
ASUS_I006D:/ # 



Note:

To execute the command via adb use

adb shell su - -c setprop vendor.zram.enable 0  


To re-enable the swap device execute as user root :

setprop vendor.zram.enable 1

e.g.

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7392796672   219873280    84598784    12288000
-/+ buffers/cache:     7380508672   232161280
Swap:               0           0           0
ASUS_I006D:/ # 

ASUS_I006D:/ # setprop vendor.zram.enable 1                                                                                                                               
ASUS_I006D:/ # 

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7397068800   215601152    84598784    12292096
-/+ buffers/cache:     7384776704   227893248
Swap:      4294963200           0  4294963200
ASUS_I006D:/ # 



To change the size of the swap device change the value for the variable 

vendor.zram.disksize

and disable and re-enable the swap device. The default unit for the value of the variable vendor.zram.disksize is byte; use the postfix M for megabytes or G for gigabytes.

e.g. to use a swap device with 2 GB do:

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7398703104   213966848    84631552    12365824
-/+ buffers/cache:     7386337280   226332672
Swap:      4294963200           0  4294963200
ASUS_I006D:/ # 

ASUS_I006D:/ # setprop vendor.zram.enable 0
ASUS_I006D:/ # 

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7394570240   218099712    84631552    12361728
-/+ buffers/cache:     7382208512   230461440
Swap:               0           0           0
ASUS_I006D:/ # 

ASUS_I006D:/ # setprop vendor.zram.disksize 2G
ASUS_I006D:/ # 

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7533645824    79024128    86020096    12369920
-/+ buffers/cache:     7521275904    91394048
Swap:               0           0           0
ASUS_I006D:/ # 

ASUS_I006D:/ # setprop vendor.zram.enable 1                                                                                                                               
ASUS_I006D:/ # 

ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612669952  7514472448    98197504    85999616    12374016
-/+ buffers/cache:     7502098432   110571520
Swap:      2147479552           0  2147479552
ASUS_I006D:/ # 



Note:

This can also be done in a Magisk script to make the new config for the swap device persistent:

e.g.

cat <<EOT >/data/adb/service.d/0010reconfigure_swap
#
# change the size of the swap volume to 2 GB
#
setprop vendor.zram.enable 0
setprop vendor.zram.disksize 2G
setprop vendor.zram.enable 1
EOT

chmod 755 /data/adb/service.d/0010reconfigure_swap



Result after a reboot:

ASUS_I006D:/ $ free
        total        used        free      shared     buffers
Mem:       7612669952  6047076352  1565593600    58441728    17780736
-/+ buffers/cache:     6029295616  1583374336
Swap:      2147479552           0  2147479552
ASUS_I006D:/ $ 



To disable the swap device completely via Magisk script use these commands:

cat <<EOT >/data/adb/service.d/0010disable_swap
#
# disable the swap device
#
setprop vendor.zram.enable 0
EOT

chmod 755 /data/adb/service.d/0010disable_swap


Result after a reboot:

shell@ASUS_I006D:/ $ free
        total        used        free      shared     buffers
Mem:       7612669952  5930426368  1682243584    55574528    17719296
-/+ buffers/cache:     5912707072  1699962880
Swap:               0           0           0
shell@ASUS_I006D:/ $



Note that this approach does not work in the OmniROM 12 for the ASUS Zenfone 8.


Update 20.09.2022/bs

see How to create or change a swap device in the OmniROM using Magisk for how to change or disable the swap device in the OmniROM.


Update 04.10.2022 /bs


See How to trigger an action when a property is changed for how to implement enabling or disabling the swap device on ZRAM via a property value in the OmniROM or other OS




How to create a swap device in the OmniROM 12 using Magisk


URL:  https://forum.xda-developers.com/t/how-to-create-or-change-a-swap-device-in-the-omnirom-using-magisk.4495415/

How to create a swap device in the OmniROM 12 using Magisk
The init script in the OmniROM running on the ASUS Zenfone 8 to create a swap device is:

/vendor/bin/init.asus.zram.sh

This script will create a swap device while booting the OS if that functionality is enabled.

To check if there is a swap device configured use the OS command free:

root@ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612493824  4732076032  2880417792    12857344    15298560
-/+ buffers/cache:     4716777472  2895716352
Swap:      4294963200           0  4294963200
root@ASUS_I006D:/ # 



If the result of the script is not okay you can create a Magisk script to correct the config for the swap device.

The script /vendor/bin/init.asus.zram.sh uses the property 

vendor.zram.enable

to determine if a swap device should be created and the property

vendor.zram.disksize

for the (new) size for the swap device. 

If the property vendor.zram.enable is set to 1 but the property vendor.zram.disksize is not set the script uses a default value for the size of the swap device depending on the amount of installed memory.

To create a swap device via a Magisk Script (or change the config of the swap device created by default) it's sufficient to just set the two properties to the approbiate values and execute the init script , e.g.

Note:

This must be done as user root.

cat >/data/adb/service.d/0010create_swap_device.sh <<EOT
#!/system/bin/sh

# there is a missing leading slash (/) in one of the commands in the file /vendor/bin/init.asus.zram.sh
# therefor the working directory when executing the script must be /
#
cd /

#
# enable creating the swap device
#
setprop vendor.zram.enable 1

#
# use a 512 MB swap device
# The default unit for vendor.zram.disksize is bytes; use the prefix M for megabytes or G for gigabytes

# Do not set this property to use the default value for the swap device (which is depending on the installed memory)
#
setprop vendor.zram.disksize 512M

#
# now (re)create the swap device
#
/vendor/bin/init.asus.zram.sh

EOT

chmod 755 /data/adb/service.d/0010create_swap_device.sh



To disable the swap device at all use this Magisk script:

cat  >/data/adb/service.d/0010disable_swap_device.sh <<EOT
#!/system/bin/sh

# there is a missing leading slash (/) in one of the commands in the file /vendor/bin/init.asus.zram.sh
# therefor the working directory when executing the script must be /
#
cd /

#
# enable creating the swap device (-> necessary to delete an existing swap device)
#
setprop vendor.zram.enable 1

# new size for the swap device (0 = do not configure a swap device)
#
setprop vendor.zram.disksize 0

#
# now (re)create the swap device (-> only delete an existing swap device)
#
/vendor/bin/init.asus.zram.sh

EOT

chmod 755 /data/adb/service.d/0010disable_swap_device.sh


Note:

See  How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8 for how to reconfigure swap in the Original Android 12 from ASUS for the Zenfone 8.


Please note that the device /dev/block/zram0 is created in the RAM but the memory for the device /dev/block/zram0 device is only allocated on demand - e.g.:

#
# free memory with a free swap device on zram0 with 6 GB is about 2783735808 (about 2 GB)
#
root@ASUS_I006D:/ # free
            total        used        free      shared     buffers
Mem:       7612489728  4828753920  2783735808    13639680    15556608
-/+ buffers/cache:     4813197312  2799292416
Swap:      6442446848           0  6442446848
root@ASUS_I006D:/ # 
root@ASUS_I006D:/ # setprop vendor.zram.disksize 0

root@ASUS_I006D:/ # /vendor/bin/init.asus.zram.sh                                                                                                                         
root@ASUS_I006D:/ # 

#
# free memory with no swap device on zram0 is about 2802741248 (about 2 GB)
#
root@ASUS_I006D:/ # free
           total        used        free      shared     buffers
Mem:       7612489728  4809748480  2802741248    13750272    15577088
-/+ buffers/cache:     4794171392  2818318336
Swap:               0           0           0
root@ASUS_I006D:/ # 


root@ASUS_I006D:/ # setprop vendor.zram.disksize 4G                                                                                                                       
root@ASUS_I006D:/ # 

root@ASUS_I006D:/ # /vendor/bin/init.asus.zram.sh                                                                                                                         
Swapspace size: 4194300k, UUID=4a914375-e28f-4181-850a-87cc792eacbe
root@ASUS_I006D:/ # 

#
# free memory with a free swap device on zram0 with 4 GB is about 2773835776 (about 2 GB)
#
root@ASUS_I006D:/ # free
           total        used        free      shared     buffers
Mem:       7612489728  4838653952  2773835776    13877248    15581184
-/+ buffers/cache:     4823072768  2789416960
Swap:      4294963200           0  4294963200
root@ASUS_I006D:/ #



Update 04.10.2022 /bs

See How to trigger an action when a property is changed for how to implement enabling or disabling the swap device on ZRAM via a property value in the OmniROM or other OS





How to switch the active slot


URL:  https://forum.xda-developers.com/t/how-to-manually-switch-the-active-slot.4499789/

How to switch the active slot
Switching the active slot on phones with A/B slots is normally only used by the updater to install a new OS update (see https://source.android.com/docs/core/ota/ab for details)

This functionality can also be quite useful if you have two different operating systems on your phone and want to switch between them without reinstalling.

Since the data on all partitions that do not belong to the A/B partitions is used by both operating systems (the one in slot A and the one in slot B), this functionality is limited by the installed operating systems.

The data in most partitions that are only present once should be usable for all operating systems (see here), but this does not apply to the data in the partition that is mounted to /data.
 
Switching the active slot does work if the OS (including the OS version) installed in slot A and B are equal. It does work most of the time if the OS installed in slot A and B are equal but with different OS versions.

And it should work if the OS installed in slot A and slot B use different directories in /data, e.g. in slot A is a Android based OS installed and in slot B a not on Android based OS (like for example
pocketMarketOS)

It does not work (most of the times) if there are different Android distributions in slot A and B: e.g. I did not get it to work if the OS in slot A is the original Android 12 from ASUS and the OS in slot B is OmniROM 12. (Probably it would work after removing one or more OS related directories from /data but I do not know which - infos on how to use these OS in parallel are welcome)


The instructions below where done on an ASUS Zenfone 8 but I believe they should work with other phones with A/B slots also.

Please note that you should have a working TWRP image for your phone on hand to restore the active slot in case of boot problems.



To get the current slot while the OS is running use the shell command getprop, e.g. execute in a shell on the phone or in an adb shell:

root@ASUS_I006D:/ # getprop ro.boot.slot_suffix
_a
root@ASUS_I006D:/ #



To get the active slot in fastboot mode or while the phone is booted into the bootloader execute on the PC connected to the phone:

fastboot getvar current-slot

e.g.

[ OmniRomDev - xtrnaw7@t15g /data/develop/android ] $ sudo fastboot getvar current-slot
current-slot: b
Finished. Total time: 0.001s
[ OmniRomDev - xtrnaw7@t15g /data/develop/android ] $



There are various methods to manual switch the active slot:


1. Switching the slot using fastboot


Switching the slot via fastboot works if TWRP is installed in the boot partition.

To use it connect the phone to an USB port of the PC and issue these commands on the PC:


# boot the phone into fastboot mode either if in the bootloader
#
sudo fastboot reboot fastboot

#
# or if the phone is booteind into the recovery or in the normal mode
#
adb reboot fastboot

# wait until the phone is booted in fastboot mode

# get the current slot
#
sudo fastboot getvar current-slot

# change the current slot
#
sudo fastboot set_active b

# check the result
#
sudo fastboot getvar current-slot

# reboot the phone from new current-slot
#
sudo fastboot reboot


Example
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot reboot
Rebooting                                          OKAY [  0.000s]
Finished. Total time: 0.201s
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ adb reboot fastboot
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot getvar current-slot
current-slot: a
Finished. Total time: 0.001s
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot set_active b
Setting current slot to 'b'                        OKAY [  0.042s]
Finished. Total time: 0.043s
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot getvar current-slot
current-slot: b
Finished. Total time: 0.000s
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot reboot
Rebooting                                          OKAY [  0.000s]
Finished. Total time: 0.251s

# check the result
#
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ adb shell getprop ro.boot.slot_suffix
_b
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$
 


Notes:

Switching the active slot in fastboot mode does not work if the boot image from the original Android 12 from ASUS or from the OmniROM is installed:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ sudo fastboot set_active b
Setting current slot to 'b'                        FAILED (remote: 'Unable to set slot')
fastboot: error: Command failed
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$


The same is true it the phone is booted into the bootloader:

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ sudo fastboot --set-active=a
Setting current slot to 'a'                        FAILED (remote: 'Slot Change is not allowed in Lock State')
fastboot: error: Command failed
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $


Changing the lock state via fastboot flashing lock|unlock will do a factory reset.



2. Switching the slot using TWRP

The active slot for the next reboot can also be changed in the reboot dialog from TWRP.
This can also be done if the phone is booted from the TWRP image.


Note:

This is the fallback to get the phone booting again in case the reboot of the other slot fails for whatever reason.



3. Switching the Slot via Magisk

The active slot can also be changed in the Magisk App. This is used to update the boot partition after an OTA (see here https://topjohnwu.github.io/Magisk/ota.html for details) but can also be used to manual change the active slot.

Open the Magisk App and choose the "install" button to install Magisk into the boot partition. In the next dialog use the method "Install to inactive Slot (After OTA)".

This works also if no update (OTA) was installed and it does not change the Magisk config - it just replaces the Magisk files in the inactive boot partition and activates the inactive boot partition. To reboot the phone then from the now active partition press the "Reboot" button after the update of the boot partition by Magisk is done.

This only works if the OS in both slots is supported by Magisk, of course.


4. Using the binary bootctl

There is binary called bootctl that can be used to switch the active slot. The source code of bootctl is part of the source for the Android OS but as far as I know the binary is not compiled by default and therefor also not installed on the phone.

But there is Magisk module to install the bootctl binary; that Magisk Module can be downloaded from here:

https://github.com/roihershberg/bootctl-binary

Unfortunately the current version of the Magisk Module with bootctl does not install and work out of the box on Android 12 or OmniROM 12 for the ASUS Zenfone 8. Therefor I created a corrected version of the Magisk Module.

The corrected Magisk Module can be downloaded here:

https://bnsmb.de/files/public/Android/bootctl-binary-v2.1.1.zip


The Changelog for the corrected Magisk module for bootctl is:

bootctl changelog
Update 21.09.2022 /bs

- changed the max. API version in customize.sh to 32 so that the module can be installed in Android 12

- renamed the binary to bootctl.bin

- added the wrapper script bootctl to start bootctl because the directory /vendor/lib64 with the necessary libraries for the binary is not in the default library path in Android 12 


bootctl must be executed by the root user in a shell on the phone or in an adb shell.


To change the active slot with bootctl use

bootctl set-active-boot-slot SLOT

SLOT is either 0 (for slot A) or 1 (for slot B); the next reboot will then boot from that slot.


To get the active booted slot with bootctl use

bootctl get-current-slot

Notes:

The usage help for bootctl is:

root@ASUS_I006D:# bootctl -h
./bootctl - command-line wrapper for the boot HAL.

Usage:
  ./bootctl COMMAND

Commands:
  hal-info                       - Show info about boot_control HAL used.
  get-number-slots               - Prints number of slots.
  get-current-slot               - Prints currently running SLOT.
  mark-boot-successful           - Mark current slot as GOOD.
  get-active-boot-slot           - Prints the SLOT to load on next boot.
  set-active-boot-slot SLOT      - On next boot, load and execute SLOT.
  set-slot-as-unbootable SLOT    - Mark SLOT as invalid.
  is-slot-bootable SLOT          - Returns 0 only if SLOT is bootable.
  is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
  get-suffix SLOT                - Prints suffix for SLOT.
  set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
                                   partition is in progress. Valid STAT values
                                   are: none, unknown, snapshotted, merging,
                                   or cancelled.
  get-snapshot-merge-status      - Prints the current snapshot-merge status.

SLOT parameter is the zero-based slot-number.


The source code for bootctl is here:

https://android.googlesource.com/platform/system/extras/+/master/bootctl/bootctl.cpp

https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/boot_control.h

There is also a simple app to change the active slot via the bootctl binary: Switch-My-Slot-Android

Note that the app can only be installed after the Magisk Module for bootctl is installed.


Trouble Shooting

If you get an error like this after rebooting from the previously inactive slot

Cannot load Android System. Your data may be corrupt. if you continue o get this message, you may need to perform a factory data reset and erase all user data storeed on this device

or if the phone boots only in the bootloader after the reboot from the previously inactive slot boot the phone from the TWRP image to activate the previous active slot again.

e.g. for the phone ASUS Zenfone 8:

sudo fastboot boot  /data/backup/ASUS_ZENFONE8/twrp/twrp-3.6.1_12-1-I006D.img

Then in TWRP select to reboot the phone and in the dialog to reboot switch the slot back to the previous active slot.

You can ignore the error messages printed by TWRP after changing the active slot - just continue rebooting the phone.

Note:

On the ASUS Zenfone 8 Press the buttons Up, Down, and Power at the same time for about 10 or more seconds to turn off the phone. Afterwards you can boot into the bootloader.

History

06.11.2022 /bs

The section about changing the slot in fastboot mode was not correct: fastboot mode and bootloader mode are differents states of the phone.


06.11.2022 /bs (2)

Switching the slot in fastboot mode does only work if TWRP is installed in the boot partition




How to run a script at shutdown


URL:  https://forum.xda-developers.com/t/how-to-run-a-script-at-shutdown.4500719/

How to run a script at shutdown

To define additional startup scripts via Magisk the Magisk directories /data/adb/service.d and /data/adb/post-fs-data.d can be used. Unfortunately there is no equivalent for scripts that should be executed during shutdown.

So we must use other methods to implement these kind of scripts.


Using the overlay feature of Magisk to run a script at shutdown

Introduction

in Android it is possible to define actions that will be executed when certain conditions are satisfied. These definitions are done in the file init.rc (and other .rc files) using the Android Init Language.

And this feature can be used to execute a command when the phone is shutting down.

Note:

For details about the Android Init Language used for these files see here https://android.googlesource.com/platform/system/core/+/master/init/README.md


The .rc files used by Android are in the directories
/system/etc/init
/vendor/etc/init
/odm/etc/init

Note: The first .rc file read is /system/etc/init/hw/init.rc

Unfortunately it's useless to change the .rc files in these directories using the Magisk features to change files in the directory /system because these files are processed by the OS before the new files are "created" by Magisk.

Therefor the overlay functionality from Magisk must be used to create additional .rc files (see the section Root Directory Overlay System on this page https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md for details about this Magisk Feature).



Preparation

To be able to restore the original boot partition in case of an error create an image of the original boot partition from the phone on your PC before starting the development:

CUR_SLOT=$( adb shell getprop ro.boot.slot_suffix )
adb shell su - -c dd if=/dev/block/by-name/boot${CUR_SLOT}  | cat >boot${CUR_SLOT}


e.g.

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ CUR_SLOT=$( adb shell getprop ro.boot.slot_suffix )

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ echo ${CUR_SLOT}
_b
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ adb shell su - -c dd if=/dev/block/by-name/boot${CUR_SLOT}  | cat >boot${CUR_SLOT}.img
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 2.668147 s, 36 M/s
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ]

[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $ ls -ltr  boot${CUR_SLOT}.img
-rw-r--r--. 1 xtrnaw7 xtrnaw7 100663296 Oct  1 12:13 boot_b.img
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/test ] $



To trouble shoot issues with this approach it is highly recommended to create an Magisk init script in the directory

/data/adb/post-fs-data.d

to fetch and store the Android logs into a persistent file. Use these commands to create the script:

cat >/data/adb/post-fs-data.d/0002logcatboot <<-EOT

mkdir -p /cache/logs

# backup the OS logs from before the reboot:
#
[ -r /cache/logs/log ] && mv /cache/logs/log /cache/logs/oldlog

/system/bin/logcat -r 102400 -n 9 -v threadTime -f /cache/logs/log >/cache/logs/info.log 2>/cache/logs/err.log &
EOT

chmod 755 /data/adb/post-fs-data.d/0001logcatboot

Using this script the log messages from before the last reboot are stored in the file /cache/logs/oldlog.
To activate the script the phone must be rebooted.

Check the contents of the directory /cache/logs/log after the reboot as user root to be sure that it works.

shell@ASUS_I006D:/ $ su - -c ls -ltr /cache/logs
total 205008
-rw-rw-rw- 1 root root       0 1970-01-06 08:16 info.log
-rw-rw-rw- 1 root root       0 1970-01-06 08:16 err.log
-rw-r----- 1 root root 4707523 2022-10-01 17:29 log
shell@ASUS_I006D:/ $




Details


The trigger in the .rc files for the action that should be done while shutting down is

on shutdown

The trigger can be used more then once; the OS will execute all defined actions for the trigger in the order they are found in the rc files.


The action to run an executable in the .rc file is

exec [ <seclabel> [ <user> [ <group>\* ] ] ] -- <command> [ <argument>\* ]

Fork and execute command with the given arguments. The command starts after “--” so that an optional security context, user, and supplementary groups can be provided. No other commands will be run until this one finishes. seclabel can be a - to denote default. Properties are expanded within argument. Init halts executing commands until the forked process exits.


In Android SELinux is enabled by default. Therefor it's neccessary to use the correct SELinux context for the files used.

(Note: The SELinux context for the init process executing the action is u:r:init:0.)

It's quite difficult to find the correct SELinux contexts in Android for this approach therefor it's better to use the general SELinux context defined by Magisk: u:r:magisk:s0 .


Implementation

Note:

All commands must be done as user root in an session on the phone or in an adb session.



So first create the neccessary directories and files:

mkdir -p /data/init_scripts

mkdir -p /data/init_scripts/log


Create the script to execute on shutdown:

cat >/data/init_scripts/my_shutdown.sh <<-\EOT
#!/system/bin/sh

SHUTDOWN_LOG="/data/init_scripts/log/myshutdown.$$.log"

echo "$0: Shutdown with parameter \"$*\" started at $( date ) " >>${SHUTDOWN_LOG}

echo "*** id : "  >>${SHUTDOWN_LOG} 2>&1
id  >>${SHUTDOWN_LOG} 2>&1

# ... add necessary commands ...

EOT
chmod 755 /data/init_scripts/my_shutdown.sh



Correct the SELinux context:

chcon -R u:r:magisk:s0 /data/init_scripts/


Check the result

root@ASUS_I006D:/ # find /data/init_scripts/ -exec ls -ld {}  \;
drwxr-xr-x 3 root root u:r:magisk:s0  3452 2022-10-01 16:12 /data/init_scripts/
-rwxr-xr-x 1 root root u:r:magisk:s0  637 2022-10-01 16:12 /data/init_scripts/my_shutdown.sh
drwxr-xr-x 2 root root u:r:magisk:s0  3452 2022-10-01 16:16 /data/init_scripts/log
root@ASUS_I006D:/ #



Create a working dir:

#
# create a working directory
#
mkdir -p /data/adb/workdir
cd /data/adb/workdir



Now create the additional .rc file:

#
# change the current directory to the working directory
#
cd /data/adb/workdir

cat >init.custom.rc <<-\EOT
on shutdown
    exec u:r:magisk:s0 -- /system/bin/sh /data/init_scripts/my_shutdown.sh 0008

on early-init
   setprop my_custom_rc_file loaded
EOT


Note:

The additional trigger is for testing the new .rc file (see the trouble shooting section below for details)
Magisk supports more then one .rc file; the name of the .rc file is meaningless but the extension must be .rc


And now add the new file to the ramdisk on the boot partition:

#
# change the current directory to the working directory
#
cd /data/adb/workdir

# get the current active slot
#
CURRENT_SLOT=$( getprop ro.boot.slot_suffix )

echo "The current active slot is: ${CURRENT_SLOT}"

# copy the boot partition from the active slot to a file
#
dd if=/dev/block/by-name/boot${CURRENT_SLOT} of=./boot_root.img

# unpack the image file
#
/data/adb/magisk/magiskboot unpack ./boot_root.img

# add the new dirs and files to the ramdisk from the boot partition
#
/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"mkdir 0700 overlay.d" \
"add 0700 overlay.d/init.custom.rc init.custom.rc"

# recreate the image file for the boot partition
#
/data/adb/magisk/magiskboot repack boot_root.img

# write the corrected image file to the boot partition
#
dd if=./new-boot.img of=/dev/block/by-name/boot${CURRENT_SLOT}



Note:

The commands to unpack and pack the ramdisk manually using the cpio command are (if NOT using the Magisk binary magiskboot):

RAMDISK=$PWD/ramdisk

mkdir ${RAMDISK}
cd ${RAMDISK}

# unpack the ramdisk
#
cpio -idm  <../ramdisk.cpio

# ... do what ever is necessary with the files/dirs in ${RAMDISK}

# pack the ramdisk again
#
cd ${RAMDISK}
find . | cpio -o  >../ramdisk.cpio



Now reboot the phone to activate the new .rc config and after the reboot check that the .rc file was processed

getprop my_custom_rc_file

e.g

shell@ASUS_I006D:/ $ getprop  my_custom_rc_file
loaded
shell@ASUS_I006D:/ $


If the property defined in the .rc file, my_custom_rc_file, is not set something went wrong and you should check the OS logs and double check your config.

If the new property is defined you can test the shutdown action by rebooting the phone again and check the output directory.

While doing this reboot the new shutdown script should be executed and after the reboot is done there should be the log files from the shutdown script:

shell@ASUS_I006D:/ $ su -       

root@ASUS_I006D:/ # ls -l /data/init_scripts/log
total 0
-rw------- 1 root root 179 2022-10-01 18:23 myshutdown.4617.log

root@ASUS_I006D:/ # cat /data/init_scripts/log/myshutdown.4617.log                                                                                                                                                       
/data/init_scripts/my_shutdown.sh: Shutdown with parameter "0008" started at Sat Oct  1 18:23:14 CEST 2022
*** id :
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
root@ASUS_I006D:/ #


That's it.

Note that you can change the script executed while doing the shutdown without changing the boot image again.
But you should always test the script before rebooting -- an error in your script may stop the reboot.

To change the additional .rc files it's necessary to recreate the ramdisk and boot partition.

The filesystems for /data and for /sdcard are still mounted while executing the actions for the trigger "on shutdown" .

To log the current environment while executing the shutdown script you can add code like this to the script:

(
echo
echo "*** Environment while executing the shutdown script ..."
echo

echo "*** pwd: "
pwd
echo

echo "*** id: "
id
echo

echo "*** df -h: "
df -h
echo

echo "*** ps -efZ : "
ps -efZ
echo

echo "*** env: "
env
echo

echo "*** set: "
set
echo
) >>/data/init_scripts/log/myshutdown_env.log 2>&1



To create a directory in which other actions from the .rc file (like write) can write with enabeld SELinux use one of the SELInux contexts the init process can write to, e.g:

mkdir /data/system_data
chcon u:object_r:system_data_file:s0 /data/system_data


Now the .rc config

on shutdown
  write /data/system_data/myshutdown.log Shutdown_started\n


will work.

See the file /plat_file_contexts in the ramdisk from the boot partition for other existing SELinux contexts:

root@ASUS_I006D:/data/adb/test # /data/adb/magisk/magiskboot cpio ramdisk.cpio "extract plat_file_contexts plat_file_contexts"
Loading cpio: [ramdisk.cpio]
Extract [plat_file_contexts] to [plat_file_contexts]

root@ASUS_I006D:/data/adb/test # ls -l plat_file_contexts                                                                                              
-rw-r--r-- 1 root root 40490 2022-10-03 16:27 plat_file_contexts
root@ASUS_I006D:/data/adb/test #


Please be aware that these changes will be gone after the next OS update. But on the other hand it's quite easy to create a script to re install the shutdown script without user intervention.

For further hints regarding *rc files see also Hints for adding files to the boot partition 


Trouble Shooting

The main reason for problems with this approach are invalid SELinux contexts. Therefor you should test your script in permissive SELinux mode if it does not work like expected. To do that temporary disable SELinux before rebooting (SELinux will be automatically enabled again after the reboot), e.g.:

# set SELinux to permissive
#
setenforce 0

reboot


and check the log messages in the directory /cache/logs/oldlog for SELinux related messages:

su - -c grep deny /cache/logs/oldlog

Note that you can not disable SELinux in an action in an .rc file.



To check if your additional .rc file is processed by Magisk add a statement like these to the custom .rc file in the overlay directory:

on early-init
    setprop sys.example.foo bar


If this statement is processed by Magisk and Android the property sys.example.foo should be defined after the reboot, e.g.:

root@ASUS_I006D:/ #  getprop sys.example.foo
bar
root@ASUS_I006D:/ #



To check if the "on shutdown" trigger is processed use :

on shutdown
   write /sdcard/Download/myshutdown.log Shutdown_started\n


and reboot with disabled SELinux

setenforce 0
reboot


If the "on shutdown" trigger in your .rc file is processed there should exist the file

/sdcard/Download/myshutdown.log

after the reboot

If the shutdown of the phone hangs open another adb session to the phone and kill the script (the adb daemon should still run while the shutdown script is running)

If the phone does not boot anymore with the new shutdown script reboot the phone from the TWRP image and fix / delete the new shutdown script. Or reflash the boot partition with the image file created before starting the development.


In general you should carefully check your .rc file for syntax errors -- entries in the file after the first syntax error will be ignored


Useful URLs

I used ideas and code from the web pages listed below for this HowTo:

How to run an executable on boot and keep it running?

How to run an Android init service with superuser SELinux context?

Magisk overlay - execute a script or copy files

History

03.10.2022 /bs

added code about to extract a single file (plat_file_contexts) from the ramdisk cpio image using magiskboot 






How to trigger an action when a property is changed


URL:  https://forum.xda-developers.com/t/how-to-trigger-an-action-when-a-property-is-changed.4501489/

How to trigger an action when a property is changed
In Android it's possible to trigger various actions by changing the value of a property. This feature is quite handy and the implementation using Magisk is not really difficult.

As an example

In the original Android from ASUS for the Zenfone 8 you can disable and enable the swap on a ZRAM device by changing the value for the property vendor.zram.enable:

e.g.:

To turn the swap on use

setprop vendor.zram.enable 1

and to turn the swap off use

setprop vendor.zram.enable 0

To get the current value use

getprop vendor.zram.enable

or

check the output of the OS command free.

(see How to disable or change the swap device in the Android 12 from ASUS for the Zenfone 8 for details)

This feature is not implemented in the OmniROM for the ASUS Zenfone 8 but quite useful so let's see how to implement it in the OmniROM.

The Triggers and Action for this Android feature are configured in the init.rc files in the root filesystem for the OS (see https://android.googlesource.com/platform/system/core/+/master/init/README.md for details). The root filesystem for Android is read-only mounted so without creating your own Android image for the phone it's not possible to add the functionality to the OS.

But we can use the Root Directory Overlay System from Magisk (see https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md for the documentation) to implement it.

The detailed process for creating additional *.rc files for Android via Magisk is described here: 

How to run a script at shutdown

Therefore I will not go into the details here. But please read that post before you continue

First we check how this feature is implemented in the Original Android for the Zenfone:

Enabling and disabling the swap device on ZRAM is configured in the .rc file

/vendor/etc/hw/init.asus.debugtool.rc

using these settings in the original Android OS for the Zenfone 8:

service asus_zram /system/vendor/bin/sh /vendor/bin/init.asus.zram.sh
    user root
    group root
    disabled
    seclabel u:r:vendor_qti_init_shell:s0
    oneshot

on property:persist.vendor.zram.enable=1
    setprop vendor.zram.enable "1"
    setprop vendor.zram.disksize ${persist.vendor.zram.disksize}

on property:persist.vendor.zram.enable=0
    setprop vendor.zram.enable "0"
    setprop vendor.zram.disksize ${persist.vendor.zram.disksize}

on property:vendor.zram.enable=*
    start asus_zram


The script used to enable or disable the swap device on ZRAM in the original Android for the Zenfone 8 is:

/vendor/bin/init.asus.zram.sh

The script is quite simple (see also below):

It uses two properties to configure the swap device:

vendor.zram.enable : if this property is set to 1 the script enables the swap device and if the property is set to 0 it disables the swap device

vendor.zram.disksize : the value of this property is the size of the ramdisk.


Now we can implement this feature for the OmniROM:

Note:

All commands must be done as user root in a shell on the phone or in an adb shell


First we check the prerequisites for the feature:

The used shell for the service exists in the OmniROM:

root@ASUS_I006D:/data/adb/workdir # ls -Zl /vendor/bin/sh
-rwxr-xr-x 1 root shell u:object_r:vendor_shell_exec:s0  318216 2009-01-01 01:00 /vendor/bin/sh
root@ASUS_I006D:/data/adb/workdir #


The script to toggle the ramdisk on the swap device also already exists in the OmniROM:

root@ASUS_I006D:/data/adb/test # ls -l /vendor/bin/init.asus.zram.sh
-rwxr-xr-x 1 root shell 1127 2009-01-01 01:00 /vendor/bin/init.asus.zram.sh
root@ASUS_I006D:/data/adb/test #


And the necessary SELinux contexts are also already defined in the OmniROM.

So, let's start:

# create a temporary directory
#
mkdir /data/adb/workdir
cd /data/adb/workdir

# create the additional .rc file
#
cat >init.asus.zram.rc <<-\EOT
#
# Note:
#
# The service definition for an OS without the script init.asus.zram.sh should be
#
# service asus_zram /system/bin/sh /system/sbin/init.asus.zram.sh
#
service asus_zram /system/vendor/bin/sh /vendor/bin/init.asus.zram.sh
    user root
    group root
    disabled
    seclabel u:r:vendor_qti_init_shell:s0
    oneshot
#
# these properties can be used to define the initial state of the ramdisk on ZRAM
#
on property:persist.vendor.zram.enable=1
    setprop vendor.zram.enable "1"
    setprop vendor.zram.disksize ${persist.vendor.zram.disksize}

on property:persist.vendor.zram.enable=0
    setprop vendor.zram.enable "0"
    setprop vendor.zram.disksize ${persist.vendor.zram.disksize}

on property:vendor.zram.enable=*
    start asus_zram

# The property persist.vendor.zram.enable is not defined in the OmniROM. If necessary you can add another trigger in the .rc file,
# e.g to enable the swap device by default use
#
on early-init
   setprop persist.vendor.zram.enable 1
EOT


# get the current active slot
#
CURRENT_SLOT=$( getprop ro.boot.slot_suffix )
echo "The current active slot is: ${CURRENT_SLOT}"

# copy the boot partition from the active slot to a file

dd if=/dev/block/by-name/boot${CURRENT_SLOT} of=./boot_root.img

# unpack the image file

/data/adb/magisk/magiskboot unpack ./boot_root.img

# add the new dir and file to the ramdisk from the boot partition

/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"mkdir 0700 overlay.d" \
"add 0700 overlay.d/init.custom.rc init.asus.zram.rc"

# recreate the image file for the boot partition

/data/adb/magisk/magiskboot repack boot_root.img

# write the corrected image file to the boot partition

dd if=./new-boot.img of=/dev/block/by-name/boot${CURRENT_SLOT}


That's it.

After the next reboot switching the ramdisk on the ZRAM device via an property should be active:

root@ASUS_I006D:/ # reboot

# .....
#  start a new adb session
# ....
shell@ASUS_I006D:/ $ getprop ro.omni.version
12-20220703-zenfone8-MICROG


#
# Note: The property is only visible to the user root
#
shell@ASUS_I006D:/ $ su -

root@ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612493824  4835229696  2777264128    14585856    14487552
-/+ buffers/cache:     4820742144  2791751680
Swap:      4294963200           0  4294963200
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # getprop  vendor.zram.enable
1
root@ASUS_I006D:/ #

# -> now disable the swap on ZRAM

root@ASUS_I006D:/ # setprop  vendor.zram.enable 0
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # getprop  vendor.zram.enable
0
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612493824  4840824832  2771668992    14524416    14483456
-/+ buffers/cache:     4826341376  2786152448
Swap:               0           0           0
root@ASUS_I006D:/ #

# -> now enable the swap on ZRAM again

root@ASUS_I006D:/ # setprop  vendor.zram.enable 1
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # getprop vendor.zram.enable
1
root@ASUS_I006D:/ #

root@ASUS_I006D:/ # free
        total        used        free      shared     buffers
Mem:       7612493824  4844777472  2767716352    14524416    14512128
-/+ buffers/cache:     4830265344  2782228480
Swap:      4294963200           0  4294963200
root@ASUS_I006D:/ #



See the post How to enable access via adb on a new installed OS for another usage example for this technique.


Workarounds for other configuration

Find below some workarounds for OS versions without the prerequisites for implementing this feature.

1. The script to toggle the swap /vendor/bin/init.asus.zram.sh does not exist

If the script /vendor/bin/init.asus.zram.sh does not exist in your OS create a Magisk Module for the script

To create a (dummy) Magisk Module for the script do:

mkdir -p /data/adb/modules/toggle_ram/system/bin

#
# create the script to toggle the ramdisk on ZRAM
#
cat >/data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh <<-\EOT

lahaina_set=`getprop vendor.asus.zram_setting`
if test "$lahaina_set" != "1"; then
    echo  "[asus_zram] init.kernel.post_boot-lahaina.sh not finished yet!"> /dev/kmsg
    exit 0
fi
disksize=`getprop vendor.zram.disksize`
zram_enable=`getprop vendor.zram.enable`

MemTotalStr=`cat /proc/meminfo | grep MemTotal`
MemTotal=${MemTotalStr:16:8}
let RamSizeGB="( $MemTotal / 1048576 ) + 1"

if test "$disksize" = ""; then
    disksize="4096M"
fi
echo "[asus_zram]RamSizeGB=${RamSizeGB}" > /dev/kmsg
if test "$zram_enable" = "1"; then
    if [ $RamSizeGB -le 7 ]; then  #this is for 6G; or the value will be 4G(8G,12G,16G,18G,etc)
        disksize="( $RamSizeGB * 1024 ) / 2""M"
    fi
    swapoff /dev/block/zram0 2>/dev/kmsg
    echo 1 > sys/block/zram0/reset 2>/dev/kmsg
    sleep 1
    echo lz4 > /sys/block/zram0/comp_algorithm
    echo $disksize > /sys/block/zram0/disksize 2>/dev/kmsg
    mkswap /dev/block/zram0 2>/dev/kmsg
    swapon /dev/block/zram0 -p 32758 2>/dev/kmsg
    echo "[asus_zram]write zram disksize=${disksize}" > /dev/kmsg
fi
if test "$zram_enable" = "0"; then
    swapoff /dev/block/zram0 2>/dev/kmsg
    echo "[asus_zram]turn off the zram" > /dev/kmsg
fi
EOT

chmod 755 /data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh

chown root:shell /data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh

chcon u:object_r:vendor_file:s0 /data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh 



Check the result:

root@ASUS_I006D:/ # ls -lZ /data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh                                                                   
-rwxr-xr-x 1 root shell u:object_r:vendor_file:s0  1109 2022-10-03 21:14 /data/adb/modules/toggle_ram/system/bin/init.asus.zram.sh
root@ASUS_I006D:/ #
  

Then change the script to execute in the service definition in the .rc file to /system/bin/init.asus.zram.sh:

service asus_zram /system/vendor/bin/sh /system/bin/init.asus.zram.sh

The rest of the instructions can be be used without changes.


2. The binary /system/vendor/bin/sh does not exist

If the shell /system/vendor/bin/sh does not exist in the OS create an Magisk Module with an approbiate shell:

Note:

The shell /system/bin/sh can not be used because it's configured with another SELinux context:

root@ASUS_I006D:/ # ls -lZ /system/vendor/bin/sh
-rwxr-xr-x 1 root shell u:object_r:vendor_shell_exec:s0  318216 2009-01-01 01:00 /system/vendor/bin/sh
root@ASUS_I006D:/ #

# but

root@ASUS_I006D:/data/adb/workdir # ls -Zl /system/bin/sh
-rwxr-xr-x 1 root shell u:object_r:shell_exec:s0  307768 2009-01-01 01:00 /system/bin/sh
root@ASUS_I006D:/data/adb/workdir #



To create a (dummy) Magisk Module for the necessary shell do:

mkdir -p /data/adb/modules/vendorshell/system/bin
cp /system/bin/sh /data/adb/modules/vendorshell/system/bin/sh
chcon u:object_r:vendor_shell_exec:s0 /data/adb/modules/vendorshell/system/bin/vendor_sh



Check the result:

root@ASUS_I006D:/data/adb/workdir # ls  -lZ /data/adb/modules/vendorshell/system/bin/vendor_sh
-rwxr-xr-x 1 root root u:object_r:vendor_shell_exec:s0  307768 2022-10-03 20:41 /data/adb/modules/vendorshell/system/bin/vendor_sh
root@ASUS_I006D:/data/adb/workdir #


Now change the shell in the service definition in the .rc file to /system/bin/vendor_sh:

service asus_zram /system/bin/vendor_sh /vendor/bin/init.asus.zram.sh

The rest of the instructions can be be used without changes.


3. The necessary shell /system/vendor/bin/sh and the script /vendor/bin/init.asus.zram.sh do both not exist

If the necessary shell /system/vendor/bin/sh and the script /vendor/bin/init.asus.zram.sh do not exist you can also create one (dummy) Magisk module for both.

See

https://topjohnwu.github.io/Magisk/guides.html

and

Some hints for using Magisk on Android phones

for instructions and infos about how to create a real Magisk Module.


4. Missing SELinux contexts

These instructions assume that all necessary SELinux contexts are already defined in the used OS.
If there are SELinux contexts missing it might not work without adding the missing SELinux contexts (that might be difficult and I must admit that I did not test that)


5. only for the records:

The magiskboot command can also be used to add new files to the /sbin directory in the root filesystem.

This can be can be configured like this:

/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"mkdir 0700 overlay.d" \
"add 0700 overlay.d/init.custom.rc init.asus.zram.rc" \
"mkdir 0700 overlay.d/sbin" \
"add 0700 overlay.d/sbin/my_new_script.sh my_script.sh"


This is useful for adding binaries, scripts. or data files for actions that should be executed while the other partitions are not yet mounted and therefor the files in these filesystems not yet available. But for the action defined in this post this is not necessary.

And be aware that the files added to the root filesystem ( /sbin/my_new_script.sh in this example) are only available while booting the phone.


6. Other problems

In case of problems please check the post

How to run a script at shutdown

again.  Also check the restrictions for this approach documented in that post










How to install TWRP via script


URL: https://forum.xda-developers.com/t/how-to-install-twrp-via-script.4511355/

How to install TWRP via script

The recovery image TWRP is very useful for the maintenance of the Android OS. You can either boot the phone from the TWRP image without installing it using these commands:

adb reboot bootloader
sudo fastboot boot ./twrp-3.7.0_12-0-I006D.img

or install the TWRP image into the boot partition and use the command

adb reboot recovery


or

sudo fastboot boot recovery

to boot into TWRP. Depending on the OS used there is also an option in the reboot dialog in the GUI to reboot into the recovery.

While the phone is booted from TWRP you can open a shell as user root via adb.


The only official method to install TWRP into the boot partition of the phone is via the TWRP GUI, e.g.:
  1. boot the phone from the TWRP image (see above)
  2. install TWRP into the boot partition using the menu entry "Advanced" and then "Flash Current TWRP". TWRP will then install itself into both boot partitions.

There is no method to install TWRP without user interaction via a script (at least I did not found any ...). Therefor I wrote a shell script to install TWRP without user input:

install_twrp_from_within_twrp.sh

The script install_twrp_from_within_twrp.sh can be used to install TWRP either into a boot partition or into an image file for the boot partition. The script must be executed in a shell on the phone while the phone is booted from the TWRP image that should be installed.

The usage of the script is:

ASUS_I006D:/data/local/tmp $ ./install_twrp_from_within_twrp.sh -h

 install_twrp_from_within_twrp.sh - script to install TWRP into a boot partition or into a file

 Usage:

    install_twrp_from_within_twrp.sh [-f] [boot_image_file] [new_boot_image_file]

    boot_image can be either an image file from the boot partition or a boot partition block device
    (e.g. /dev/block/by-name/boot_a)
    new_boot_partition_file must be the absolute name of a file.

    Both parameter are optional
    If the script is called without a parameter TWRP will be installed in the current boot partition.
    If boot_image_file is a block device and new_boot_image_file is missing TWRP will be installed in the block device.
    If the parameter new_boot_image_file is used the script will only write a boot partition image with TWRP into that file.

    Use the parameter -f to ignore missing or invalid checksums.

    This script must run in a shell in TWRP

    Environment variables used by the script if set:

      BIN_DIR (Current value: "/system/bin")
      TMPDIR (Current value: "/data/local/tmp")


|ASUS_I006D:/data/local/tmp $



The commands used in the script install_twrp_from_within_twrp.sh to install TWRP are copied from the TWRP source file with the code to install TWRP:

https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp

Note that TWRP contains a copy of the binary magiskboot to unpack and repack the boot partition. Therefor the installation should work on all phones that are supported by Magisk.
Be aware that the installation of TWRP will wipe out the installation of Magisk (or any other tool for enabling root access). So for installing TWRP and Magisk they must be installed in this order:
  1. install TWRP
  2. install Magisk (or a similar tool)

Example usage:

Example for install_twrp_from_within_twrp.sh
ASUS_I006D:/ # /cache/install_twrp_from_within_twrp.sh /dev/block/by-name/boot_b

Installing TWRP into a boot image or boot partition via script

Checking the running OS ...
OK, running a shell in TWRP: "twrp_I006D" version "3.7.0_12-0"
Checking the prerequisites for installing TWRP ...
Using the partition "/dev/block/by-name/boot_b" as source for the installation of TWRP
Creating the new boot image with TWRP in the file "/data/local/tmp/img.568/new-boot.img"
Checking if the required executables exist ...
OK, the file "/system/bin/magiskboot" exists and is executable
OK, the file "/system/bin/cpio" exists and is executable
OK, the file "/system/bin/sha256sum" exists and is executable
OK, the file "/system/bin/getprop" exists and is executable
OK, the file "/system/bin/dd" exists and is executable
Checking if the requried data files exist ...
OK, the file "/ramdisk-files.sha256sum" exists
OK, the file "/ramdisk-files.txt" exists
OK, the file "/dev/block/by-name/boot_b" exists
Checking the check sums of the files for the new ramdisk ...
OK, the check sums of the files for the new ramdisk are okay
Directory "/data/local/tmp" successfully created
Directory "/data/local/tmp/img.568" successfully created
Directory "/data/local/tmp/img_files.568" successfully created
The temporary directory to unpack the boot image is "/data/local/tmp/img_files.568"
Unpacking the boot image from "/dev/block/by-name/boot_b" ...
Parsing boot image: [/dev/block/by-name/boot_b]
HEADER_VER      [3]
KERNEL_SZ       [42027520]
RAMDISK_SZ      [34080747]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
OK, "/dev/block/by-name/boot_b" successfully unpacked to "/data/local/tmp/img_files.568" :
Creating a backup of the original ramdisk ...
total 74412
-rw-rw-rw- 1 root root       50 2022-10-24 19:35 header
-rw-r--r-- 1 root root 42027520 2022-10-24 19:35 kernel
-rw-r--r-- 1 root root 34080747 2022-10-24 19:35 ramdisk.cpio.org
Creating the new ramdisk with TWRP ...
New ramdisk with TWRP "/data/local/tmp/img_files.568/ramdisk.cpio" successfully created.

-rw-rw-rw- 1 root root 85852380 2022-10-24 19:35 /data/local/tmp/img_files.568/ramdisk.cpio
-rw-r--r-- 1 root root 34080747 2022-10-24 19:35 /data/local/tmp/img_files.568/ramdisk.cpio.org

Repacking the boot image into the file "/data/local/tmp/img.568/new-boot.img" ...
+ cd /data/local/tmp/img_files.568
+ /system/bin/magiskboot repack /dev/block/by-name/boot_b /data/local/tmp/img.568/new-boot.img
Parsing boot image: [/dev/block/by-name/boot_b]
HEADER_VER      [3]
KERNEL_SZ       [42027520]
RAMDISK_SZ      [34080747]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
Repack to boot image: [/data/local/tmp/img.568/new-boot.img]
HEADER_VER      [3]
KERNEL_SZ       [42027520]
RAMDISK_SZ      [34080769]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
+ TEMPRC=0
+ set +x
OK, the new boot image "/data/local/tmp/img.568/new-boot.img" successfully created
-rw-r--r-- 1 root root 100663296 2022-10-24 19:35 /data/local/tmp/img.568/new-boot.img
Now rewriting "/dev/block/by-name/boot_b" using "/data/local/tmp/img.568/new-boot.img" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 4.498630 s, 21 M/s
TWRP successfully installed in "/dev/block/by-name/boot_b"
ASUS_I006D:/ #



To install TWRP using the script without user input these steps are neccessary:
  1.  reboot the phone into the bootloader
  2.  boot the phone from the TWRP image that should be installed on the phone
  3.  copy the script install_twrp_from_within_twrp.sh to the phone
  4.  execute the script install_twrp_from_within_twrp.sh on the phone in an adb shell

To automate these steps I wrote a wrapper script :

install_twrp.sh

The wrapper script install_twrp.sh must be executed on a machine running Linux with the phone attached via USB.

The wrapper script will check the current status of the phone, boot the phone from the TWRP image, copy the script install_twrp_from_within_twrp.sh to the phone and execute it on the phone to install TWRP into either the active or the inactive boot partition.

The script install_twrp_from_within_twrp.sh must be in the same directory as install_twrp.sh (if not set the variable TWRP_INSTALL_SCRIPT before executing the script -- see the script usage below).


The usage for the script install_twrp.sh is:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./install_twrp.sh  -h
./install_twrp.sh version - 1.0.0.0 - install TWRP to the boot partition of a phone running Android using TWRP

 Usage

    install_twrp_via_twrp.sh [boot_slot] [wait=n] [dd|fastboot]  [twrp_image]

 All parameter are optional

 The parameter "boot_slot" can be a, b, active, inactive, next, current; default is the current boot slot of the phone

 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 value for the parameter "wait" is the number of seconds to wait before starting the script "install_twrp_from_within_twrp.sh" on the phone
 This seems to be necessary to avoid errors while repacking the boot image. The default wait time is 10 seconds.

 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.

 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

 The script uses the script "install_twrp_from_within_twrp.sh" to install TWRP. The script install_twrp_from_within_twrp.sh must
 be in the same directory as this script. The script will be copied to the phone and then executed on the phone.
 Set the variable TWRP_INSTALL_SCRIPT to the name of the script to use before starting this script if another script should be used .

 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 TMP_DIR_ON_THE_PHONE to the temporary directory to use on the phone

 The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
 The default script executed on the phone to install TWRP is "./install_twrp_from_within_twrp.sh"

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$



Without any parameter the script install_twrp.sh will install TWRP into the active boot partition.


Example for install_twrp.sh
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./install_twrp.sh
./install_twrp.sh version - 1.0.0.0 - install TWRP 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 #46 SMP PREEMPT Sat Oct 15 08:38:07 CEST 2022 aarch64
... found a phone connected via USB with working adb access
The phone is booted in normal mode
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.500s]
Booting                                            OKAY [ 10.440s]
Finished. Total time: 12.983s
Waiting up to 60 seconds for the boot of the phone ....
Retrieving the current boot slot from the phone ...
The current boot slot is "_a"
The boot slot to patch is "_a"
The boot partition to patch is "boot_a"
Checking if the directory "/data/local/tmp" exists on the phone ....
/data/local/tmp
Copying the script "./install_twrp_from_within_twrp.sh" to the phone ...
./install_twrp_from_within_twrp.sh: 1 file pushed, 0 skipped. 216.6 MB/s (13141 bytes in 0.000s)
Creating the boot image file "/data/local/tmp/boot_a.327964.img" from the partition "/dev/block/by-name/boot_a" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 0.311720 s, 308 M/s

Checking the result ...
-rw-rw-rw- 1 root root 100663296 2022-10-24 18:02 /data/local/tmp/boot_a.327964.img
The check sums are:
The check sum of the boot partition "/dev/block/by-name/boot_a" on the phohe is  "728455636"
The check sum of the boot image file on the phone is "/data/local/tmp/boot_a.327964.img" is "728455636"  
Installing TWRP using the boot image file "/data/local/tmp/boot_a.327964.img"  ...

 ----------------------------------------------------------------------
[2022.10.24 20:02:43] Waiting now 10 seconds ...
+ adb -d -s M6AIB760D0939LX shell /data/local/tmp/install_twrp_from_within_twrp.sh /data/local/tmp/boot_a.327964.img /data/local/tmp/boot_a_witn_twrp.327964.img

Installing TWRP into a boot image or boot partition via script

Checking the running OS ...
OK, running a shell in TWRP: "twrp_I006D" version "3.7.0_12-0"
Checking the prerequisites for installing TWRP ...
Using the file "/data/local/tmp/boot_a.327964.img" as source for the installation of TWRP
Creating the boot image with TWRP in the file "/data/local/tmp/boot_a_witn_twrp.327964.img"
Checking if the required executables exist ...
OK, the file "/system/bin/magiskboot" exists and is executable
OK, the file "/system/bin/cpio" exists and is executable
OK, the file "/system/bin/sha256sum" exists and is executable
OK, the file "/system/bin/getprop" exists and is executable
Checking if the requried data files exist ...
OK, the file "/ramdisk-files.sha256sum" exists
OK, the file "/ramdisk-files.txt" exists
OK, the file "/data/local/tmp/boot_a.327964.img" exists
Checking the check sums of the files for the new ramdisk ...
OK, the check sums of the files for the new ramdisk are okay
Directory "/data/local/tmp" successfully created
Directory "/data/local/tmp/img.583" successfully created
Directory "/data/local/tmp/img_files.583" successfully created
The temporary directory to unpack the boot image is "/data/local/tmp/img_files.583"
Unpacking the boot image from "/data/local/tmp/boot_a.327964.img" ...
Parsing boot image: [/data/local/tmp/boot_a.327964.img]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [34080743]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
OK, "/data/local/tmp/boot_a.327964.img" successfully unpacked to "/data/local/tmp/img_files.583" :
Creating a backup of the original ramdisk ...
total 74412
-rw-rw-rw- 1 root root       50 2022-10-24 18:02 header
-rw-r--r-- 1 root root 42025472 2022-10-24 18:02 kernel
-rw-r--r-- 1 root root 34080743 2022-10-24 18:02 ramdisk.cpio.org
Creating the new ramdisk with TWRP ...
New ramdisk with TWRP "/data/local/tmp/img_files.583/ramdisk.cpio" successfully created.

-rw-rw-rw- 1 root root 85852380 2022-10-24 18:02 /data/local/tmp/img_files.583/ramdisk.cpio
-rw-r--r-- 1 root root 34080743 2022-10-24 18:02 /data/local/tmp/img_files.583/ramdisk.cpio.org

Repacking the boot image into the file "/data/local/tmp/boot_a_witn_twrp.327964.img" ...
+ cd /data/local/tmp/img_files.583
+ /system/bin/magiskboot repack /data/local/tmp/boot_a.327964.img /data/local/tmp/boot_a_witn_twrp.327964.img
Parsing boot image: [/data/local/tmp/boot_a.327964.img]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [34080743]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
KERNEL_FMT      [raw]
RAMDISK_FMT     [gzip]
VBMETA
Repack to boot image: [/data/local/tmp/boot_a_witn_twrp.327964.img]
HEADER_VER      [3]
KERNEL_SZ       [42025472]
RAMDISK_SZ      [34080758]
OS_VERSION      [12.0.0]
OS_PATCH_LEVEL  [2022-05]
PAGESIZE        [4096]
CMDLINE         []
+ TEMPRC=0
+ set +x
OK, the new boot image "/data/local/tmp/boot_a_witn_twrp.327964.img" successfully created
-rw-r--r-- 1 root root 100663296 2022-10-24 18:03 /data/local/tmp/boot_a_witn_twrp.327964.img
Note: Flashing the new image to the boot partition was not requested
+ set +x
 ----------------------------------------------------------------------
Checking the result ...
The patched boot image is "/data/local/tmp/boot_a_witn_twrp.327964.img"
-rw-r--r-- 1 root root 100663296 2022-10-24 18:03 /data/local/tmp/boot_a_witn_twrp.327964.img
[2022.10.24 20:03:05] Waiting now 5 seconds ...
Patching the partition "/dev/block/by-name/boot_a" from the patched boot image file "/data/local/tmp/boot_a_witn_twrp.327964.img" via dd ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 4.865411 s, 20 M/s
Checking the result ....
The check sums for the images and devices on the phone are:

Checksum   Size      File/Device name
-------------------------------------
728455636 100663296 /data/local/tmp/boot_a.327964.img
2360878166 100663296 /data/local/tmp/boot_a_witn_twrp.327964.img
2360878166 100663296 /dev/block/by-name/boot_a

OK, patching the boot partition "/dev/block/by-name/boot_a" was successfull

*** Press return to reboot the phone now

Rebooting the phone now ...
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$




Advanced Usage

To install TWRP and Magisk via script these commands can be used :


# install the Magisk App (if not already done)
#
cat Magisk-25.1_25100.apk | adb shell pm install -S $( ls -l Magisk-25.1_25100.apk  | awk '{ print $5}' )

# install TWRP and Magisk into the boot partition
#
REBOOT=no ./install_twrp.sh ; REBOOT=yes ./install_magisk_via_twrp.sh



To install a new OS image including TWRP and Magisk these commands can be used:

# boot the phone into the boot loader
#
adb reboot bootloader

# boot the phone from the TWRP image
#
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img

# copy the OS image to install to the phone
#
adb push /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20221015-zenfone8-MICROG.zip /sdcard/Download/


# install the OS image
#
adb shell twrp install  /sdcard/Download/omni-12-20221015-zenfone8-MICROG.zip


# reboot into the new OS
#
adb shell twrp reboot


# install the Magisk app
#
cat Magisk-v25.2.apk | adb shell pm install -S $( ls -l Magisk-v25.2.apk | awk '{ print $5}' )


# install TWRP and Magisk
#
REBOOT=no ./install_twrp.sh ; REBOOT=yes ./install_magisk_via_twrp.sh

The documentation for the script install_magisk_via_twrp.sh can be found here:  

How to install Magisk into the boot partition using a script.


Details

The script install_twrp_from_within_twrp.sh was tested on an ASUS Zenfone 8 with

OmniROM 12 (Android 12) and TWRP 3.7.0.12
OmniROM 12 (Android 12) and TWRP 3.6.1.12
Original Android 12 from ASUS and TWRP 3.7.0.12
OmniROM 13 (Android 13) and TWRP 3.7.0.12 (see note below)


Important Update 04.12.2022 /bs


In TWRP installed in the boot partition of an ASUS Zenfone 8 running early versions of OmniROM 13 the touch screen does not work. You can only use TWRP in an adb session in this configuration.
This bug was fixed in OmniROM 13 from 04.12.2022.

To get around this bug boot the phone from the image file with TWRP, e.g.:

sudo fastboot boot twrp-3.7.0_12-0-I006D.img

or, better, update OmniROM 13 to the image from 04.12.2022 or later.


The script install_twrp_from_within_twrp.sh does not create a backup of the boot partition before rewriting it.

The script install_twrp_from_within_twrp.sh creates temporary files in the directory ${TMPDIR} on the phone; the default value for this variable in the script is

TMPDIR="${TMPDIR:=/cache/install_twrp.$$}"

The default value for the variable in the tested TWRP versions is /data/local/tmp so without changes the temporary files will be created in that directory.
The script does not delete the temporary files so a manual cleanup of the directory might be necessary .


Trouble Shooting

For unknown reasons repacking the boot image fails sometimes and the script ends with an error message like this:

ERROR: The size of the new boot image file is 56713216 but the size of the original image file is 100663296 -- that does not match (RC=33)

To avoid this error increase the wait time using the script parameter "wait=n" (the default wait time is 10 seconds).

If increasing the wait time does not help just restart the wrapper script install_twrp.sh again while the phone is still booted from the TWRP image. So a workaround for this problem is :

install_twrp.sh ; [ $? = 33 ] && install_twrp.sh


The following error message appears when executing the script install_twrp_from_within_twrp.sh after booting the already installed TWRP:

ERROR: Error checking the check sums of the files for the new ramdisk

./system/etc/fstab.postinstall: FAILED


Installing TWRP via script only works if the phone is booted from the TWRP image.
To ignore this error the parameter -f can be used - but this is NOT recommended.



How to add additional files to an TWRP image


URL:  https://forum.xda-developers.com/t/how-to-add-additional-files-to-an-twrp-image.4511827/

How to add additional files to an TWRP image

The recovery image TWRP is very useful for the maintenance of the Android OS.

It's the Swiss Army knife for maintaining phones running the Android OS but sometimes you might miss some files in the TWRP recovery.

To add additional files to an TWRP image the instructions from the HowTo How to change files in the boot image can be used.

To make sure that the new files are also added to an TWRP installed in the boot partition additional steps are necessary.

To unpack and repack the TWRP image the Magisk binary magiskboot is necessary. This binary is part of TWRP so creating the new image should be done while the phone is booted from the TWRP image.

So to add additional files to an TWRP image do:

# boot into the TWRP image
#
adb reboot bootloader
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img



Wait until booting the TWRP image is done and open an adb shell

adb shell


Create some directories for the work on the phone:

mkdir -p /data/develop/workdir/twrp/ramdisk


Copy the TWRP image file from your PC to the working directory on the phone

adb push twrp-3.7.0_12-0-I006D.img /data/develop/workdir/twrp

Unpack the image file:

cd /data/develop/workdir/twrp         

magiskboot unpack -h twrp-3.7.0_12-0-I006D.img


e.g.

ASUS_I006D:/data/develop/workdir/twrp # magiskboot unpack -h twrp-3.7.0_12-0-I006D.img
Parsing boot image: [twrp-3.7.0_12-0-I006D.img]
HEADER_VER      [3]
KERNEL_SZ       [18627321]
RAMDISK_SZ      [34811448]
OS_VERSION      [99.87.36]
OS_PATCH_LEVEL  [2099-12]
PAGESIZE        [4096]
CMDLINE         [twrpfastboot=1]
KERNEL_FMT      [gzip]
RAMDISK_FMT     [gzip]
VBMETA
ASUS_I006D:/data/develop/workdir/twrp #

ASUS_I006D:/data/develop/workdir/twrp # ls -ltr
total 324223
-rw-rw---- 1 u0_a119 media_rw 100663296 2022-10-12 13:38 twrp-3.7.0_12-0-I006D.img
-rw-r--r-- 1 root    root     100663296 2022-10-28 15:55 new-boot.img
-rw-rw-rw- 1 root    root            66 2022-10-28 16:14 header
-rw-r--r-- 1 root    root      41943552 2022-10-28 16:14 kernel
-rw-r--r-- 1 root    root      88384000 2022-10-28 16:14 ramdisk.cpio
drwxrwxrwx 2 root    root          3452 2022-10-28 16:15 ramdisk
ASUS_I006D:/data/develop/workdir/twrp #



Unpack the ramdisk from the TWRP image:

cd /data/develop/workdir/twrp/ramdisk         
cpio -idm  <../ramdisk.cpio


The directory /data/develop/workdir/twrp/ramdisk now contains the contents of the ramdisk used for TWRP.

Now add the new files for the image to the directory /data/develop/workdir/twrp/ramdisk:

In this example we will add the binaries zsh, sqlite3, and vi.
 
cd /data/develop/workdir/twrp/ramdisk

cp ../../newfiles/sqlite3 ./system/bin/
cp ../../newfiles/zsh ./system/bin/
cp ../../newfiles/vi ./system/bin/


Note:


In case you want to add new binaries :

To check if the binaries will work in TWRP execute the binaries while the phone is booted from the original TWRP image. If the binaries need additional libraries not included in TWRP just also add them to the TWRP image into the directory ./system/lib64 of the image.


The next step is necessary to make sure that TWRP will copy the new files also to a TWRP installation in a boot partition:

For each new file add an entry to the files

./ramdisk-files.sha256sum 
./ramdisk-files.txt

in the directory with the contents from the ramdisk.

The file ramdisk-files.txt contains the name and path of the files used in the ramdisk for the boot partition and the file ramdisk-files.sha256sum contains the checksum for each of the files for the ramdisk.

Note:

If you change an existing file that is mentioned in the file ./ramdisk-files.sha256sum you must also correct the entry in that file.

So do for each new file:

echo <file_with_path_not_starting_with_a_dot_or_slash> >>ramdisk-files.txt ; done

sh256sum <file_with_path_startinge with_a_slash> >>ramdisk-files.sha256sum



e.g:

ASUS_I006D:/data/develop/workdir/twrp/ramdisk # for i in system/bin/sqlite3 system/bin/vi system/bin/zsh ; do sha256sum ./$i  >>ramdisk-files.sha256sum ; echo "$i" >>ramdisk-files.txt ; done
ASUS_I006D:/data/develop/workdir/twrp/ramdisk #


Check the result.

ASUS_I006D:/data/develop/workdir/twrp/ramdisk # tail -4 ramdisk-files.sha256sum
8a8ff76cbda445f08ba3552598085089883f9d594cd7716e90f3411a8b82f8a0  ./twres/fonts/DroidSansMono.ttf
09fcec146d9ecba0f2a0b7362b52d2f87607a3c5760c94ba5fcd900e423e16f8  ./system/bin/sqlite3
3a221d10ffc275cadaf79fb1a00c39c9083ce672ea111205301e277091a04b51  ./system/bin/vi
354e415da42503ed4211907adf93ed8bfb14ce2e2d8418fcd7a3227873b4b53f  ./system/bin/zsh
ASUS_I006D:/data/develop/workdir/twrp/ramdisk #

ASUS_I006D:/data/develop/workdir/twrp/ramdisk # tail -4 ramdisk-files.txt
twres/fonts/DroidSansMono.ttf
system/bin/sqlite3
system/bin/vi
system/bin/zsh
ASUS_I006D:/data/develop/workdir/twrp/ramdisk #


Correct the entry for the file ramdisk-files.txt in the file ramdisk-files.sha256sum :

cd /data/develop/workdir/twrp/ramdisk

cp ./ramdisk-files.sha256sum ./ramdisk-files.sha256sum.org
NEW_ENTRY="$( sha256sum ./ramdisk-files.txt )" && sed -i -e "s#.*\./ramdisk-files.txt#${NEW_ENTRY}#g" ./ramdisk-files.sha256sum 



Check the entries in the file ramdisk-files.sha256sum

sha256sum --status -c ./ramdisk-files.sha256sum ; echo $?

If the output of that command is not 0 use

sha256sum -c ./ramdisk-files.sha256sum   | grep -v OK

to get the entry with the invalid checksum. Fix the entry and check again.


             
Now repack the ramdisk:

cd /data/develop/workdir/twrp/ramdisk
find . | cpio -o  >../ramdisk.cpio


And recreate the image file:

cd /data/develop/workdir/twrp
magiskboot repack twrp-3.7.0_12-0-I006D.img

e.g

ASUS_I006D:/data/develop/workdir/twrp #  magiskboot repack twrp-3.7.0_12-0-I006D.img
Parsing boot image: [twrp-3.7.0_12-0-I006D.img]
HEADER_VER      [3]
KERNEL_SZ       [18627321]
RAMDISK_SZ      [34811448]
OS_VERSION      [99.87.36]
OS_PATCH_LEVEL  [2099-12]
PAGESIZE        [4096]
CMDLINE         [twrpfastboot=1]
KERNEL_FMT      [gzip]
RAMDISK_FMT     [gzip]
VBMETA
Repack to boot image: [new-boot.img]
HEADER_VER      [3]
KERNEL_SZ       [18618540]
RAMDISK_SZ      [37358888]
OS_VERSION      [99.87.36]
OS_PATCH_LEVEL  [2099-12]
PAGESIZE        [4096]
CMDLINE         [twrpfastboot=1]
ASUS_I006D:/data/develop/workdir/twrp #


The file new-boot.img contains the TWRP image with the additional files.:

ASUS_I006D:/data/develop/workdir/twrp # ls -ltr new-boot.img
-rw-r--r--  1 root    root     100663296 2022-10-28 16:28 new-boot.img
ASUS_I006D:/data/develop/workdir/twrp #


To test the image, rename the file, copy the file to an PC and boot the phone from the image:

mv new-boot.img twrp-3.7.0_12-0-I006D-enhanced.img

Execute on the PC:

adb pull /data/develop/workdir/twrp/twrp-3.7.0_12-0-I006D-enhanced.img

adb reboot bootloader

sudo fastboot boot twrp-3.7.0_12-0-I006D-enhanced.img



Connect via adb to the phone booted from the new TWRP image and check the result:

ASUS_I006D:/ # ls -l /system/bin/zsh /system/bin/sqlite3 /system/bin/vi
-rwxr-xr-x 1 root root 1714720 2022-10-28 16:19 /system/bin/sqlite3
-rwxr-xr-x 1 root root 1815152 2022-10-28 16:19 /system/bin/vi
-rwxr-xr-x 1 root root 2004808 2022-10-28 16:19 /system/bin/zsh
ASUS_I006D:/ #

ASUS_I006D:/ #  /system/bin/sqlite3  --version
3.39.3 2022-09-05 11:02:23 4635f4a69c8c2a8df242b384a992aea71224e39a2ccab42d8c0b0602f1e826e8
ASUS_I006D:/ #

ASUS_I006D:/ # /system/bin/vi --version | head -1
VIM - Vi IMproved 8.1 (2018 May 18)
ASUS_I006D:/ #

ASUS_I006D:/ # /system/bin/zsh --version
zsh 5.9 (aarch64-unknown-linux-android)
ASUS_I006D:/ #



Advanced Usage

To be able to distinguish the new image from the original one you can change or add values in the file ./default.prop in the ramdisk for the image, e.g.:

# before creating the new ramdisk

cd /data/develop/workdir/twrp/ramdisk
echo "ro.product.type=enhanced" >>default.prop


Note:

The file default.prop is not listed in the checksum file ramdisk-files.sha256sum for the image.

Then when booted from the image you can check the new property, e.g.:


ASUS_I006D:/ # getprop ro.product.type
enhanced
ASUS_I006D:/ #


Notes:

Adding new files to ramdisk-files.sha256sum is not mandatory but recommended

To add files to the image that should not be copied to an TWRP installation in a boot partition just add them to the ramdisk but not to the file ramdisk-files.txt.

see also How to install TWRP via script 

Udpate 28.11.2022 /bs

see also How to compile TWRP for the ASUS Zenfone 8  









How to process Android boot image files on a PC running the Linux OS


URL:  https://forum.xda-developers.com/t/how-to-process-android-boot-image-files-on-a-pc-running-the-linux-os.4513845/

How to process Android boot image files on a PC running the Linux OS
To unpack and repack the boot image for phones running the Android OS the Magisk binary magiskboot is necessary (well, there might be other tools for this task but magiskboot seems to be one of the best tools for this task). Therefor these kind of tasks will usually be done on a phone running the Android OS.

But there is also a magiskboot binary for x86 CPUs in the Magisk apk file and that executable can be used to work with boot images for Android phones on a PC running the Linux OS on a machine with X86 CPU. To retrieve the x86 version of magiskboot from the Magisk apk file do:

# to use the 32 bit version
#
unzip  Magisk-v25.2.apk lib/x86/libmagiskboot.so

mv lib/x86/libmagiskboot.so  ./magiskboot
chmod 755 ./magiskboot


or

# to use the 64 bit version
#
unzip  Magisk-v25.2.apk lib/x86_64/libmagiskboot.so

mv lib/x86_x64/libmagiskboot.so  ./magiskboot
chmod 755 ./magiskboot



Example:

[xtrnaw7@t15g /data/develop/android/magisk]$ unzip Magisk-v25.2.apk lib/x86_64/libmagiskboot.so
Archive:  Magisk-v25.2.apk
version=25.2
versionCode=25200
  inflating: lib/x86_64/libmagiskboot.so 
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ mv lib/x86_64/libmagiskboot.so  ./magiskboot
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ chmod 755 magiskboot
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ file ./magiskboot
./magiskboot: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=b2d42d7bab2f70a0d66fc606a1fb2726a0559fed, stripped
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ ldd ./magiskboot
    not a dynamic executable
[xtrnaw7@t15g /data/develop/android/magisk]$

[xtrnaw7@t15g /data/develop/android/magisk]$ ./magiskboot
MagiskBoot - Boot Image Modification Tool

Usage: ./magiskboot <action> [args...]

Supported actions:
...

[xtrnaw7@t15g /data/develop/android/magisk]$


The binary magiskboot from the Magisk apk file is a statically linked executable for 32 bit or 64 bit. Therefor it should run on any Linux distribution that supports executing x86 executables . I tested the binary from the apk file with Magisk v25.2 on these distributions:


Unpacking a boot image file using magiskboot on a PC running the Linux OS can be done using this command;

./magiskboot unpack boot_a.img

To change files in the ramdisk from the boot image use the magiskboot parameter cpio :

[xtrnaw7@t15g /data/develop/android/test2]$ ./magiskboot 
MagiskBoot - Boot Image Modification Tool

Usage: ./magiskboot <action> [args...]

Supported actions:
...

  cpio <incpio> [commands...]
    Do cpio commands to <incpio> (modifications are done in-place)
    Each command is a single argument, add quotes for each command
    Supported commands:
      exists ENTRY
        Return 0 if ENTRY exists, else return 1
      rm [-r] ENTRY
        Remove ENTRY, specify [-r] to remove recursively
      mkdir MODE ENTRY
        Create directory ENTRY in permissions MODE
      ln TARGET ENTRY
        Create a symlink to TARGET with the name ENTRY
      mv SOURCE DEST
        Move SOURCE to DEST
      add MODE ENTRY INFILE
        Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
      extract [ENTRY OUT]
        Extract ENTRY to OUT, or extract all entries to current directory
      test
        Test the current cpio's status
        Return value is 0 or bitwise or-ed of following values:
        0x1:Magisk    0x2:unsupported    0x4:Sony
      patch
        Apply ramdisk patches
        Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
      backup ORIG
        Create ramdisk backups from ORIG
      restore
        Restore ramdisk from ramdisk backup stored within incpio
      sha1
        Print stock boot SHA1 if previously backed up in ramdisk
...

[xtrnaw7@t15g /data/develop/android/test2]$ 


E.g. to change the file system/etc/ramdisk/build.prop in the ramdisk, extract the file from the ramdisk

./magiskboot cpio ramdisk.cpio  "extract system/etc/ramdisk/build.prop build.prop"

edit the file, and add the file back to the ramdisk using this command:

./magiskboot cpio ramdisk.cpio "add 0644 system/etc/ramdisk/build.prop build.prop"


Notes


Note that I did not yet manage to create a working boot image with magiskboot running in the Linux OS after extracting, changing, and recreating the ramdisk via the Linux cpio command (as Linux user root, of course):

Creating a new boot image this way with magiskboot running in the LInux OS works without error but booting the new created boot image always ends with a ramdump error on the phone. So extracting the ramdisk via Linux cpio command can only be used to view the files in a boot image file for an Android Phone from wthin the Linux OS.


Trouble Shooting

To test the new boot image without installing it use this approach:

# boot the phone into the bootloader
#
adb reboot bootloader

# boot the phone from the patched image file
#
sudo fastboot boot ./new-boot.img

If booting the phone works and everything is fine flash the image to the active boot partition

In case you get a ramdump error after booting the phone from the new boot partition just restart the phone into the bootloader mode and flash the old image back to the boot partition.





How to make files in /system writable


URL:  https://forum.xda-developers.com/t/how-to-make-files-in-system-writable.4514603/

How to make files in /system writable

In Android 12 /system is mounted read-only can not be remounted read-write anymore.

Sometimes it's useful that one or more files in /system are writable (for example for develop tasks or for testing)

This can be implemented using Magisk (see How to change files in the directory /system for more details)

Example :

Make the file /system/etc/vimrc writable

# create a dummy Magisk module
#
mkdir -p /data/adb/modules/writable_system/system/etc

# copy the file that should be writable to the Magisk module directory
#
cp /system/etc/vimrc /data/adb/modules/writable_system/system/etc/

# make the file in the Magisk module directory writable
#
chmod +w /data/adb/modules/writable_system/system/etc/vimrc


Now reboot the phone.

After the reboot the file /system/etc/vimrc is writable by the user root, Example:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #

ASUS_I006D:/ # ls -l /system/etc/vimrc
-rw-r--r-- 0 root root 3350 2022-11-04 11:36 /system/etc/vimrc

ASUS_I006D:/ # tail -2 /system/etc/vimrc
          \ | wincmd p | diffthis
endif
ASUS_I006D:/ #

ASUS_I006D:/ # echo '" Test Comment' >>/system/etc/vimrc
ASUS_I006D:/ #

ASUS_I006D:/ # tail -2 /system/etc/vimrc
endif
" Test Comment
ASUS_I006D:/ #

Only the user root can access the directory /data/adb. Therefor the files configured using this approach are only writable by the user root.


To make a file in /system writable for non-root users use this method:

Open a (adb) shell and execute as user shell:

#
# create a directory that is writable for the user shell
#
mkdir /data/local/tmp/writable_system

mkdir /data/local/tmp/writable_system/etc

#
# copy the file that should be writable to that directory
#
cp /system/etc/vimrc /data/local/tmp/writable_system/etc 

The next commands must be executed as user root:

# create dummy Magisk module
#
mkdir -p /data/adb/modules/writable_system/system/etc
#
# create a symbolic link to the file in the writable directory in the directory with the dummy Magisk module
#
ln -s /data/local/tmp/writable_system/etc/vimrc /data/adb/modules/writable_system/system/etc

Now reboot the phone.

After the reboot the file /system/etc/vimrc is writable by the user shell, Example:

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $

ASUS_I006D:/ $ tail -2 /system/etc/vimrc
          \ | wincmd p | diffthis
endif
ASUS_I006D:/ $

ASUS_I006D:/ $ echo '" Test Comment' >>/system/etc/vimrc
ASUS_I006D:/ $

ASUS_I006D:/ $ tail -2 /system/etc/vimrc
endif
" Test Comment
ASUS_I006D:/ $


Important:

The writable directory can also be in a sub directory in /sdcard. But be aware that /sdcard is mounted late in the boot process so it might be that the overwritten file in /system will be used by the OS when the bind mount points to a non-existent file if using a sub directory in /sdcard.


The changes to the file done using these methods are "persistent" as long as Magisk is installed in the boot partition.

To restore the file with the original contents after each new reboot of the phone without removing the writable config open a (adb) shell as user root and execute:

#
# restore the file /data/adb/modules/writable_system/system/etc/vimrc from the original file /system/etc/vimrc
#
# this must be done before Magisk creates the bind mounts
#
echo "cp /system/etc/vimrc /data/adb/modules/writable_system/system/etc/vimrc">/data/adb/post-fs-data.d/restore_vimrc.sh
chmod 755 /data/adb/post-fs-data.d/restore_vimrc.sh


Now the file in the dummy Magisk module will be restored with the contents of the original file from /system after each reboot

To temporary access the original file from /system just stop the Magisk daemon, Example:

ASUS_I006D:/ #  echo '"Test Test' >>/etc/vimrc
ASUS_I006D:/ #

ASUS_I006D:/ # tail -1 /etc/vimrc
"Test Test
ASUS_I006D:/ #

ASUS_I006D:/ # id   
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #

ASUS_I006D:/ # magisk --stop
ASUS_I006D:/ #

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0

ASUS_I006D:/ #
ASUS_I006D:/ # tail -1 /etc/vimrc
endif
ASUS_I006D:/ #


Note

Stopping the Magisk daemon will disable all bind mounts done by Magisk.
Restarting the Magisk daemon will not re-create the bind mount - to re-activate the bind mount for the writable file after stopping the Magisk daemon the phone must be rebooted.



To make more then one file writable in a sub directory in /system you can also replace the complete folder using these commands as user root:

#
# make all files in /system/etc writable by the user root
#
mkdir -p  /data/adb/modules/writable_system/system/etc

cd /system/etc
find . | cpio -pdum /data/adb/modules/writable_system/system/etc/

touch /data/adb/modules/writable_system/system/etc/.replace

Now Magisk will replace the directory /system/etc with the directory /data/adb/modules/writable_system/system/etc after the next reboot


Notes

You should test these commands with a not important file like /system/etc/vimrc before changing important files.

It is NOT recommended to use this approach on productive phones.

See How to change any file or directory using Magisk for another approach to change files on read-only mounted filesystems.



Trouble Shooting

As always: If something does not work like expected check the Magisk log file /cache/magisk.log and also check the infos in this post.

 


How to boot a phone from a TWRP recovery image via script

URL:  https://forum.xda-developers.com/t/how-to-boot-a-phone-from-a-twrp-recovery-image-via-script.4527547/

How to boot a phone from a TWRP recovery image via script
For installing and configuring an Android phone via script it is often required to boot the phone from an TWRP recovery image.

Because of this I wrote a Linux shell script to reboot an Android phone from an TWRP recovery image:

boot_phone_from_twrp.sh

The script boot_phone_from_trwp.sh will detect the current status of the attached phone and use the approbiate commands (fastboot and/or adb) to reboot the phone from the TWRP recovery image.

The usage for the script is:

boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image

    boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]

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



Use the parameter -H to get a detailed usage help:

detailed usage help for boot_phone_from_twrp.sh
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./boot_phone_from_twrp.sh -H
boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image

 Usage

    boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]

 All parameter are optional

 Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help

 Use the parameter "serial=#sn" to define the serialnumber of the phone to process. This parameter is only necessary if there
 are more then one phones connected via USB and the environment variable SERIAL_NUMBER is not set.

 The value for the parameter "wait" is the maximum number of seconds to wait after booting from the TWRP image until
 the adb deamon is ready to use. The default value is 10 seconds.

 Use the parameter "force" to reboot the phone from the TWRP image even if it's booted from a TWRP installed in
 the boot or recovery partition.

 Use the parameter "noforce" to disable rebooting the phone from the TWRP image if it's booted from a TWRP installed in
 the boot or recovery partition. This is the default setting.

 Use the parameter "reboot" to reboot the phone from the TWRP image even if it's already booted from an TWRP image.

 Use the parameter "noreboot" to disable rebooting the phone from the TWRP image if it's already booted from an TWRP image
 This is the default setting.

 Use the parameter "checkonly" to only retrieve the current boot mode of the phone; the script will also return
 the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.

 Use the parameter "status" to only print the current boot mode of the phone; the script will return
 the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.

 The parameter "twrp_image" can be used to define another TWRP image to use. The parameter is optional - the
 default for "twrp_image" is the value of the environment variable TWRP_IMAGE; the default for the variable
 TWRP_IMAGE is hardcoded in the script in the variable DEFAULT_TWRP_IMAGE (= the TWRP image for the ASUS Zenfone 8)

 Parameter overwrite the values defined via environment variables.

 Returncodes:

     0 - the phone was successfully booted from the TWRP image

     1 - the phone is already booted from the TWRP image
     2 - the phone is booted from TWRP installed in the boot or recovery partition
     3 - the phone is booted into the Android OS
     4 - the phone is booted into bootloader
     5 - the phone is booted into the fastbootd
     6 - the phone is booted into the safe mode of the Android OS
     7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition

     9 - too many phones connected
    10 - usage help printed and exited

   100 - invalid parameter found
   101 - TWRP image not found
   102 - booting the phone into the bootloader failed
   103 - booting the phone into the TWRP failed

   252 - access to the phone failed
   253 - requirement check failed (e.g. one or more required executables not found, etc)
   254 - unknown error
   

 The phone to boot must be attached via USB.
 The phone can be either in fastboot mode, in normal mode with enabled adb support, in the boot loader, or already booted from an installed TWRP or an 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 TWRP_IMAGE to the name of the TWRP image file that should be used (the parameter of the script will overwrite the variable)

   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 FAST_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the bootloader (default are 60 seconds)
   Set the environment variable ADB_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the TWRP image (default are 60 seconds)

   Set the environment variable ADB to the adb executable that should be used; default: search adb in the PATH
   Set the environment variable FASTBOOT to the fastboot executable that should be used; default: search fastboot in the PATH
   Set the environment variable TIMEOUT to the timeout executable that should be used; default: search timeout in the PATH


 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 ] $



When more than one phone is connected via USB use the parameter serial=#sn or the environment variable SERIAL_NUMBER to define the serial number of the phone to use.

To only retrieve the current status of the attached phone execute the script with the parameter checkonly
 
./boot_phone_from_twrp.sh checkonly

The script will then only check the status of the phone and return the current status in the return code:

     1 - the phone is booted from the TWRP image
     2 - the phone is booted from TWRP installed in the boot or recovery partition
     3 - the phone is booted into the Android OS
     4 - the phone is booted into bootloader
     5 - the phone is booted into the fastbootd
     6 - the phone is booted into the safe mode of the Android OS
     7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition


If the parameter status is used the script will also print a message with the current status of the phone,e.g:

e.g.

./boot_phone_from_twrp.sh checkonly|status
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh checkonly ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image

The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
RC is 6
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$


[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh status ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image

The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
The phone is currently booted into the safe-mode of the Android OS
RC is 6
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$



The script boot_phone_from_twrp.sh can also be used as include file using

.  ./boot_phone_from_twrp.sh

Using this method these functions are available in the script that includes boot_phone_from_twrp.sh:

Please check the comments at the start of the script for details about using the script in this way; the command

grep "#S#" boot_phone_from_twrp.sh  | cut -c4-

prints the hints for using the script as include file.

Details for using the script as include file
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ grep "#S#" boot_phone_from_twrp.sh  | cut -c4-
 ---------------------------------------------------------------------
 To use the script as include file add the statement

 . boot_phone_from_twrp.sh

 to your file

 If used as include file the script will

 - define these constants if not already defined:

   __TRUE
   __FALSE

 - define these functions if not already defined:

   LogMsg
   LogError
   isNumber
   wait_some_seconds

 - define these functions (overwriting existing functions with the same name)

   check_prereqs_for_boot_phone_from_twrp
   init_global_vars_for_boot_phone_from_twrp

   set_serial_number

   retrieve_phone_status
   print_phone_status
   boot_phone_from_the_TWRP_image
   reboot_phone
   wait_for_phone_to_be_in_the_bootloader
   wait_for_phone_with_a_working_adb_connection
   wait_for_the_adb_daemon
 

 - the variable PHONE_BOOT_ERROR contains the error code (102 or 103) if rebooting the phone fails


 The script including this script can optional define these variables before
 including this script (see the source code below for the meaning of these variables):

    SUDO_PREFIX
    CUR_USER

    FORCE_BOOT_INTO_TWRP_IMAGE
    FORCE_REBOOT_INTO_TWRP_IMAGE

    TWRP_IMAGE
    SERIAL_NUMBER

    ADB_DAEMON_WAIT_TIME
    FAST_BOOT_WAIT_TIME
    ADB_BOOT_WAIT_TIME

    FASTBOOT_OPTIONS
    ADB_OPTIONS

    ADB
    FASTBOOT
    TIMEOUT

    CHECKONLY
   

 The global variables listed above will be defined after including this script

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$




Note:

After including the script you should use the command

${ADB} ${ADB_OPTIONS}

to run the adb binary and the command

${FASTBOOT} ${FASTBOOT_OPTIONS}

to run the fastboot binary.


Examples:

To boot the phone from the TWRP image use this code:

. ./boot_phone_from_twrp.sh

retrieve_phone_status

boot_phone_from_the_TWRP_image



To boot the phone into the Android OS use this code:

. ./boot_phone_from_twrp.sh

retrieve_phone_status

reboot_phone


Simple Test script
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ cat ./test.sh
echo "*** Loading the functions to boot the phone ..."

. ./boot_phone_from_twrp.sh

echo ""
echo "*** Checking the status of the phone"

retrieve_phone_status
print_phone_status

echo ""
echo "*** Booting the phone into the TWRP image ..!"
boot_phone_from_the_TWRP_image

echo ""
echo "*** Press return to boot the phone into the Android OS ..."
read USER_INPUT
reboot_phone



[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$  ./test.sh
*** Loading the functions to boot the phone ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use

*** Checking the status of the phone
The phone is currently booted from the Android OS

*** Booting the phone into the TWRP image ..!
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
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.761s]
Booting                                            OKAY [ 10.361s]
Finished. Total time: 13.167s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready  ..
... the adb daemon is ready after  second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"

*** Press return to boot the phone into the Android OS ...

Booting the Android OS using the command "adb reboot " now ...
Waiting up to 60 seconds for the boot of the phone .....
Waiting up to 10 seconds for the adb daemon to get ready  ..........
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$




The script boot_phone_from_twrp.sh is used as include script in the script init_magisk_db.sh - see also this post How to enable root access using Magisk in a script.
The script is also used in the script nstall_os_via_twrp.sh see this post How to install an OS image using the twrp command in a script

The script is also available on my homepage:

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


Changelog for the script boot_phone_from_twrp.sh
#
# History
#   05.11.2022 v1.0.0.0 /bs  #VERSION
#     inital release
#
#   01.12.2022 v2.0.0.0 /bs  #VERSION
#     script rewritten using functions
#     added the parameter --serial=#sn
#
#   07.12.2022 v2.1.0.0 /bs #VERSION
#     added a check for the Android OS safe mode
#     missing shift command in the code to process the parameter for the serial number -- fixed
#
#   17.12.2022 v2.2.0.0 /bs #VERSION
#     added support for the LineageOS recovery
#


History
History

17.12.2022 /bs


Updated the post with the infos for the script version 2.2.0.0

18.12.2022 /bs


Added some details for using the script as include file
Added a link to another script using this script
 





How to restore the boot partition to remove Magisk


URL: https://forum.xda-developers.com/t/how-to-restore-the-boot-partition-to-remove-magisk.4515759/

How to restore the boot partition to remove Magisk
Magisk creates a backup of the existing boot partition before rewriting the new boot partition.

This one-liner can be used as user root on the phone to manually remove Magisk from the boot partition by restoring the boot partition from the backup created by Magisk:

gzip -cd /data/magisk_backup_$( grep SHA1 $( magisk --path )/.magisk/config | cut -f2 -d "=" )/boot.img.gz | dd of=/dev/block/by-name/boot$( getprop ro.boot.slot_suffix )


Note:

In production environments a script with some more error checking should be used for this task, of course.

 



How to compile the OmniROM 13 for the ASUS Zenfone 8


URL:  https://forum.xda-developers.com/t/how-to-compile-the-omnirom-13-for-the-android-zenfone-8.4518733/

How to compile the OmniROM 13 for the ASUS Zenfone 8

Compiling the CustomROM OmniROM 13 from within Linux is again a very simple and straight forward process -- the developer for the OmniROM did once again a really amazing good job setting up the build environment for the OmniROM 13.


Notes:

As of 22.01.2023 there is an additional repository required for compiling OmniROM 13 -- see below for details. I also added some more details at the end of this post.

This HowTo is about how to compile the OmniROM 13 for the ASUS Zenfone 8 -- see How to compile the OmniROM for the ASUS Zenfone 8 for how to compile the OmniROM 12 for the ASUS Zenfone 8.

Most (but not all) of the necessarry steps to sync the repositories are described in the section "Getting Started" here: https://github.com/omnirom/android.


Prerequisites

Some of the repositories for OmniROM 13 are on https://gitlab.com and the access to these repositories for the repository sync is done via ssh. Therefor to sync these repositories a user account on gitlab.com with configured public ssh key is required. The user account on gitlab.com is free of charge and the public ssh key can be uploaded into the user profile on gitlab.com (see here: https://docs.gitlab.com/ee/user/ssh.html)

The repositories on http://github.com are currently accessed via https but that might change in the future. Therefor it's recommended to create a user on github.com and add the ssh public key to the profile of that user also.

Note:

To check how the access to the repositories is configured use this command:

xtrnaw7@t15g /data/develop/android/OmniROM_13.0/.repo]$ grep fetch $( find . -name "*xml"  )
./local_manifests/local_manifest.xml:     fetch="https://github.com/DonkeyCoyote" />
./manifests/omni-aosp.xml:           fetch="https://github.com/" />
./manifests/omni-aosp.xml:           fetch="ssh://git@gitlab.com/omnirom" />
./manifests/omni-aosp.xml:           fetch="https://github.com/omnirom"
./manifests/omni-caf.xml:           fetch="https://github.com/" />
./manifests/omni-caf.xml:           fetch="https://github.com/omnirom"
./manifests/omni-caf.xml:           fetch="https://git.codelinaro.org/clo/la"
./manifests/omni-private.xml:           fetch="ssh://git@github.com/omnirom"
./manifests/default.xml:           fetch="https://android.googlesource.com"
[xtrnaw7@t15g /data/develop/android/OmniROM_13.0/.repo]$


The tool repo is required to maintain the repositories for the OmniROM 13; you can download the tool "repo" from https://gerrit.googlesource.com/git-repo

It's not necessary to install additional tools for creating an OmniROM image -- all necessary tools to create the image are in the repositories for the OmniROM 13.



Creating the local copies of the repositories for the OmniROM 13

To create a local copy of the repositories for OmniROM 13 these steps are required:


First create a new directory for the repositories and change the working directory to the new directory, e.g.:

mkdir /data/develop/android/OmniROM_13.0
cd /data/develop/android/OmniROM_13.0


Init the local repositories using the command:
repo init -u https://github.com/omnirom/android.git -b android-13.0

Note that the command repo init only creates a hidden directory called .repo.


Next do the initial sync of the repositories using the command:
repo sync

Be aware that the repositories need about 160 GB disk space so this will take a while.


The initial repository sync will download all repositories necessary to create the OmniROM image except the hardware dependent repositories (these repos are different for the various phone models).

Therefor you must add the repositories for the vendor and device tree for your phone to the manifests in the local repositories after the download finished.

This should be done in the file .repo/local_manifests/local_manifest.xml (if the directory .repo/local_manifests does not exist create it).

For the ASUS Zenfone 8 the file should look like this:

[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$ cat .repo/local_manifests/local_manifest.xml 
<?xml version="1.0" encoding="UTF-8"?>

<manifest>
<remote name="DonkeyCoyote"
fetch="https://github.com/DonkeyCoyote" />

<project path="device/asus/zenfone8" name="android_device_asus_zenfone8" remote="omnirom" revision="android-13.0" />
<project path="vendor/asus" name="proprietary_vendor_asus" remote="DonkeyCoyote" revision="android-13.0" />
<project path="vendor/microg" name="android_prebuilts_prebuiltapks" remote="omnirom" revision="android-13.0" />
<project path="vendor/images/asus" name="android_vendor_images_asus" remote="gitlab" revision="android-13.0" />
</manifest>
[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$

Note:

The repository android_prebuilts_prbuiltapks is only necessary for OmniROM images that should include the MicroG packages.

Update 22.01.2023

The repository android_vendor_images_asus is now also required; currently this repository only contains the ASUS Galery.
I also added some more details at the end of this post.


After updating the file local_manifest.xml do another

repo sync

to sync the added repositories. This time the download will be faster because only the two (or three) additional repos will be downloaded.


Now you can start the build with these commands:

# optional:
#
# to create an OmniROM image with MicroG packages set the environment variable ROM_BUILDTYPE to MICROG before starting the build
#
# to create an OmniROM image with GAPPS packages set the environment variable ROM_BUILDTYPE to GAPPS
before starting the build
#
(that will only work if the repositories for the GAPPS are configured, of course)
#
export ROM_BUILDTYPE=MICROG

. build/envsetup.sh

brunch omni_zenfone8-user


in the directory with the downloaded repositories (use the command "breakast" instead of "brunch" to see other possible targets)

Note that the first time the compile process will take 4 hours or more. And keep in mind that the compile process will need additional 140 GB for the temporary output files.


If everything is fine there will be a ZIP file with your own CustomROM image in the directory ./out/target/product/zenfone8/ when the compile script is done, e.g. for images with the MicroG packages:

[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$ ls -l out/target/product/zenfone8/omni-13-2022*-zenfone8-*.zip
-rw-rw-r--. 3 xtrnaw7 xtrnaw7 1258920226 Nov 14 11:55 out/target/product/zenfone8/omni-13-202211141148-zenfone8-MICROG.zip
[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$


The image can now be installed on the ASUS Zenfone 8 using the LineageOS Recovery or TWRP.


Notes


Be aware that each new ROM image created with brunch will overwrite the existing ROM images (even if the name of the image is different), e.g.

[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$ ls -li out/target/product/zenfone8/omni-13-2022*-zenfone8-*.zip
77928215 -rw-rw-r--. 3 xtrnaw7 xtrnaw7 1258920226 Nov 14 11:55 out/target/product/zenfone8/omni-13-202211131612-zenfone8-MICROG.zip
77928215 -rw-rw-r--. 3 xtrnaw7 xtrnaw7 1258920226 Nov 14 11:55 out/target/product/zenfone8/omni-13-202211141148-zenfone8-MICROG.zip
[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$

[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$ cksum out/target/product/zenfone8/omni-13-2022*-zenfone8-*.zip
2429251380 1258920226 out/target/product/zenfone8/omni-13-202211131612-zenfone8-MICROG.zip
2429251380 1258920226 out/target/product/zenfone8/omni-13-202211141148-zenfone8-MICROG.zip
[xtrnaw7@t15g /data/develop/android/OmniROM_13.0]$


Therefor you should move the zip file with the new image to a new directory before building new image



For OS images that should be used on the phone for daily use I strongly recommend to create your own certificates for the OS.

In the OmniROM development directory tree the neccessary files, platform.pk8 and platform.x509.pem, are in the directory

./build/make/target/product/security

in your build tree for the CustomROM, e.g.

[xtrnaw7@t15g /data/develop/android/OmniROM]$ ls -l ./build/make/target/product/security/platform.*
-rw-------. 1 xtrnaw7 xtrnaw7 1219 Jun 25 09:39 ./build/make/target/product/security/platform.pk8
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 1460 Jun 25 09:39 ./build/make/target/product/security/platform.x509.pem
[xtrnaw7@t15g /data/develop/android/OmniROM]$

Replace these files with your own certificate files and recreate the image using the command brunch (see above).


See here https://source.android.com/devices/tech/ota/sign_builds for how to create your own certificate.

To manually create the keys use these commands:

Android uses 2048-bit RSA keys with public exponent 3. You can generate certificate/private key pairs using the openssl tool from openssl.org:

# generate RSA key
#
[xtrnaw7@t15g /data/develop/android/test1]$ openssl genrsa -3 -out temp.pem 2048
[xtrnaw7@t15g /data/develop/android/test1]$ ls -ltr
total 4
-rw-------. 1 xtrnaw7 xtrnaw7 1704 Nov 14 13:24 temp.pem


# create a certificate with the public part of the key
#
[xtrnaw7@t15g /data/develop/android/test1]$ openssl req -new -x509 -key temp.pem -out releasekey.x509.pem -days 10000 -subj  '/C=DE/ST=Hessen/L=Frankfurt am Main/O=Android/OU=Android/CN=Android/emailAddress=bnsmb01@gmail.com'
[xtrnaw7@t15g /data/develop/android/test1]$

[xtrnaw7@t15g /data/develop/android/test1]$ ls -ltr
total 8
-rw-------. 1 xtrnaw7 xtrnaw7 1704 Nov 14 13:24 temp.pem
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 1456 Nov 14 13:25 releasekey.x509.pem
[xtrnaw7@t15g /data/develop/android/test1]$


# create a PKCS#8-formatted version of the private key
#
[xtrnaw7@t15g /data/develop/android/test1]$ openssl pkcs8 -in temp.pem -topk8 -outform DER -out releasekey.pk8 -nocrypt
[xtrnaw7@t15g /data/develop/android/test1]$


[xtrnaw7@t15g /data/develop/android/test1]$ ls -ltr
total 12
-rw-------. 1 xtrnaw7 xtrnaw7 1704 Nov 14 13:24 temp.pem
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 1456 Nov 14 13:25 releasekey.x509.pem
-rw-------. 1 xtrnaw7 xtrnaw7 1218 Nov 14 13:25 releasekey.pk8
[xtrnaw7@t15g /data/develop/android/test1]$

# securely delete the temp.pem file
#
[xtrnaw7@t15g /data/develop/android/test1]$ shred --remove temp.pem
[xtrnaw7@t15g /data/develop/android/test1]$

[xtrnaw7@t15g /data/develop/android/test1]$ ls -ltr
total 8
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 1456 Nov 14 13:25 releasekey.x509.pem
-rw-------. 1 xtrnaw7 xtrnaw7 1218 Nov 14 13:25 releasekey.pk8
[xtrnaw7@t15g /data/develop/android/test1]$

From the documentation:

The openssl pkcs8 command given above creates a .pk8 file with no password, suitable for use with the build system. To create a .pk8 secured with a password (which you should do for all actual release keys), replace the -nocrypt argument with -passout stdin; then openssl will encrypt the private key with a password read from standard input. No prompt is printed, so if stdin is the terminal the program will appear to hang when it's really just waiting for you to enter a password. Other values can be used for the-passout argument to read the password from other locations; for details, see the openssl documentation.

The temp.pem intermediate file contains the private key without any kind of password protection, so dispose of it thoughtfully when generating release keys. In particular, the GNUshred utility may not be effective on network or journaled filesystems. You can use a working directory located in a RAM disk (such as a tmpfs partition) when generating keys to ensure the intermediates are not inadvertently exposed.


Updating the local repositories [Update 22.01.2023]

To update the local repositories change the working directory to the directory with the repositories for OmniROM 13 and execute

repo sync

After the synchronization is complete, create a new operating system image using the commands described above.

It is recommended to create a new image at least once a month after the new security patches for Android are added to the repositories for OmniROM.

To view the date of the security patch in the source files for the OmniROM do:

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/OmniROM_13.0 ] $ grep "^[[:space:]]*PLATFORM_SECURITY_PATCH[[:space:]]*:=" ./build/make/core/version_defaults.mk
    PLATFORM_SECURITY_PATCH := 2023-01-05
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/OmniROM_13.0 ] $


Moving the repositories to another directory [Update 22.01.2023]

In case you want to move the repositories to another directory or rename the directory with the repositories:

Be aware that the make scripts create some absolute links in the output directory; to list these absolute links use while in the directory with the repositories:

find . -type l -ls | grep " $PWD"
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/OmniROM_13.0 ] $ find . -type l -ls | grep " $PWD"
 79960553      4 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        93 Dec 18 18:49 ./out/soong/.intermediates/prebuilts/build-tools/prebuilt_py3-launcher-autorun/linux_glibc_x86_64/py3-launcher-autorun -> /data/develop/android/OmniROM_13.0/prebuilts/build-tools/linux-x86/bin/py3-launcher-autorun64
 79960558      4 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        73 Dec 18 18:46 ./out/soong/.intermediates/prebuilts/build-tools/m4/linux_glibc_x86_64/m4 -> /data/develop/android/OmniROM_13.0/prebuilts/build-tools/linux-x86/bin/m4
 78782027      0 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        53 Jan 22 09:07 ./out/target/product/zenfone8/obj/DTBO_OBJ/source -> /data/develop/android/OmniROM_13.0/kernel/asus/sm8350
 78931423      0 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        53 Jan 22 09:07 ./out/target/product/zenfone8/obj/DTB_OBJ/source -> /data/develop/android/OmniROM_13.0/kernel/asus/sm8350
 78792528      0 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        53 Jan 15 11:56 ./out/target/product/zenfone8/obj/PACKAGING/kernel_modules_intermediates/lib/modules/5.4.147-Omni-qgki-perf-g3e8f0551c618/source -> /data/develop/android/OmniROM_13.0/kernel/asus/sm8350
 78792529      4 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        77 Jan 15 11:56 ./out/target/product/zenfone8/obj/PACKAGING/kernel_modules_intermediates/lib/modules/5.4.147-Omni-qgki-perf-g3e8f0551c618/build -> /data/develop/android/OmniROM_13.0/out/target/product/zenfone8/obj/KERNEL_OBJ
 84806159      0 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        53 Jan 22 09:13 ./out/target/product/zenfone8/obj/PACKAGING/kernel_modules_intermediates/lib/modules/5.4.147-Omni-qgki-perf-g875131b91949/source -> /data/develop/android/OmniROM_13.0/kernel/asus/sm8350
 84806160      4 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        77 Jan 21 11:25 ./out/target/product/zenfone8/obj/PACKAGING/kernel_modules_intermediates/lib/modules/5.4.147-Omni-qgki-perf-g875131b91949/build -> /data/develop/android/OmniROM_13.0/out/target/product/zenfone8/obj/KERNEL_OBJ
 78788578      0 lrwxrwxrwx   1 xtrnaw7  xtrnaw7        53 Jan 22 09:10 ./out/target/product/zenfone8/obj/KERNEL_OBJ/source -> /data/develop/android/OmniROM_13.0/kernel/asus/sm8350
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/OmniROM_13.0 ] $



Before starting another build after moving the directories with the repositories you must correct these links.





 

How to change files in Android XML Binary format


URL: https://forum.xda-developers.com/t/how-to-change-files-in-android-xml-binary-format.4521513/

How to change global settings via script in Android
In the OmniROM (tested in OmniROM 12 and OmniROM 13) - and I assume in other Android distributions also - global settings are stored the file

/data/system/users/0/settings_global.xml


The file settings_global.xml is in the Android Binary XML format and can not be edited via text editor. To change the entries in that file via script it must be converted to text format and
after the necessary changes are done in the file in human readable format converted back from text format to the Android Binary XML format. This can be done with the Android executables abx2xml and xml2abx:

ASUS_I006D:/ # abx2xml -h
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

usage: abx2xml [-i] input [output]
usage: xml2abx [-i] input [output]

Converts between human-readable XML and Android Binary XML.

When invoked with the '-i' argument, the output of a successful conversion
will overwrite the original input file. Input can be '-' to use stdin, and
output can be '-' to use stdout.

ASUS_I006D:/ #


Note:


See here https://www.cclsolutionsgroup.com/post/android-abx-binary-xml or here https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/com/android/internal/util/BinaryXmlSerializer.java for details about the Android Binary XML file format.

A lot of xml files in Android are in the Android Binary XML format (there may be others on other phones):

ASUS_I006D:/ # find /data/system -name "*.xml" -exec file {} \; 2>/dev/null | grep  "Android Binary XML" | wc -l
31
ASUS_I006D:/ #


find /data/system -name "*.xml" -exec file {} \; 2>/dev/null | grep "Android Binary XML"
ASUS_I006D:/ # find /data/system -name "*.xml" -exec file {} \; 2>/dev/null | grep  "Android Binary XML"                                             
/data/system/users/0/package-restrictions.xml: Android Binary XML v0
/data/system/users/0/registered_services/android.content.SyncAdapter.xml: Android Binary XML v0
/data/system/users/0/registered_services/android.accounts.AccountAuthenticator.xml: Android Binary XML v0
/data/system/users/0/settings_system.xml: Android Binary XML v0
/data/system/users/0/settings_secure.xml: Android Binary XML v0
/data/system/users/0/settings_global.xml: Android Binary XML v0
/data/system/users/0/wallpaper_info.xml: Android Binary XML v0
/data/system/users/0/appwidgets.xml: Android Binary XML v0
/data/system/users/0.xml: Android Binary XML v0
/data/system/users/userlist.xml: Android Binary XML v0
/data/system/appops/history/900000.xml: Android Binary XML v0
/data/system/appops/history/9900000.xml: Android Binary XML v0
/data/system/appops/history/99900000.xml: Android Binary XML v0
/data/system/package-watchdog.xml: Android Binary XML v0
/data/system/display-manager-state.xml: Android Binary XML v0
/data/system/packages.xml: Android Binary XML v0
/data/system/overlays.xml: Android Binary XML v0
/data/system/sensor_privacy.xml: Android Binary XML v0
/data/system/watchlist_settings.xml: Android Binary XML v0
/data/system/cachequota.xml: Android Binary XML v0
/data/system/job/jobs.xml: Android Binary XML v0
/data/system/device_policies.xml: Android Binary XML v0
/data/system/shortcut_service.xml: Android Binary XML v0
/data/system/inputmethod/subtypes.xml: Android Binary XML v0
/data/system/install_sessions.xml: Android Binary XML v0
/data/system/appops.xml: Android Binary XML v0
/data/system/log-files.xml: Android Binary XML v0
/data/system/notification_policy.xml: Android Binary XML v0
/data/system/netpolicy.xml: Android Binary XML v0
/data/system/sensor_privacy_impl.xml: Android Binary XML v0
/data/system/batterystats-daily.xml: Android Binary XML v0
ASUS_I006D:/ #



So to change the settings in the file settings_global.xml these commands are necessary:


Example:

# convert the file into a human readable format
#
ASUS_I006D:/ # abx2xml /data/system/users/0/settings_global.xml /data/local/tmp/setttings_global.xml.human                                            
ASUS_I006D:/ #

ASUS_I006D:/ # ls -l /data/local/tmp/setttings_global.xml.human
-rw-r--r-- 1 root root 15425 2022-11-20 14:30 /data/local/tmp/setttings_global.xml.human
ASUS_I006D:/ #

ASUS_I006D:/ # file /data/local/tmp/setttings_global.xml.human
/data/local/tmp/setttings_global.xml.human: ASCII text
ASUS_I006D:/ #

ASUS_I006D:/ # head -5 /data/local/tmp/setttings_global.xml.human                                                                                        
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<settings version="210">
  <setting id="140" name="adb_wifi_enabled" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="86" name="mobile_data1" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" />
  <setting id="91" name="mobile_data2" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
ASUS_I006D:/ #


Now edit the file /data/local/tmp/setttings_global.xml.human and convert it back to Android Binary XML format

# conver the XML file in human readable format into the Android Binary XML format
#
ASUS_I006D:/ # xml2abx /data/local/tmp/setttings_global.xml.human /data/system/users/0/settings_global.xml                                           
ASUS_I006D:/ #

ASUS_I006D:/ # ls -l /data/system/users/0/settings_global.xml
-rw------- 1 system system 10697 2022-11-20 15:34 /data/system/users/0/settings_global.xml
ASUS_I006D:/ #



To activate the new settings just reboot the phone.


Notes:

The global settings in the file settings_global.xml in the OmniROM 13 are (there may be others on other phones):

cat /data/local/tmp/setttings_global.xml.human
ASUS_I006D:/ # cat /data/local/tmp/setttings_global.xml.human                                                                                            
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<settings version="210">
  <setting id="140" name="adb_wifi_enabled" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="86" name="mobile_data1" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" />
  <setting id="91" name="mobile_data2" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="41" name="low_battery_sound_timeout" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="33" name="car_undock_sound" value="/product/media/audio/ui/Undock.ogg" package="android" defaultValue="/product/media/audio/ui/Undock.ogg" defaultSysSet="true" />
  <setting id="131" name="enable_freeform_support" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="128" name="show_notification_channel_warnings" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="103" name="time_remaining_estimate_based_on_usage" value="0" package="com.android.systemui" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="142" name="development_settings_enabled" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="10" name="window_animation_scale" value="1.0" package="android" defaultValue="1.0" defaultSysSet="true" />
  <setting id="136" name="stylus_handwriting_enabled" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="129" name="force_allow_on_external" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="106" name="multi_sim_data_call" value="2" package="com.android.phone" defaultValue="2" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="122" name="tether_offload_disabled" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" />
  <setting id="105" name="battery_estimates_last_update_time" value="1668412978004" package="com.android.systemui" defaultValue="1668412978004" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="9" name="mode_ringer" value="2" package="android" defaultValue="2" defaultSysSet="true" />
  <setting id="36" name="default_install_location" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="22" name="wifi_display_on" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="44" name="device_name" value="ASUS_I006D" package="android" defaultValue="ASUS_I006D" defaultSysSet="true" />
  <setting id="13" name="wifi_networks_available_notification_on" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="29" name="dock_sounds_enabled_when_accessbility" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="14" name="bluetooth_on" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="115" name="bugreport_in_power_menu" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="18" name="mobile_data" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="160" name="network_watchlist_last_report_time" value="1668898800000" package="android" defaultValue="1668898800000" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="135" name="autofill_logging_level" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="46" name="database_creation_buildid" value="SQ3A.220605.009.A1" package="android" defaultValue="SQ3A.220605.009.A1" defaultSysSet="true" />
  <setting id="118" name="art_verifier_verify_debuggable" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" />
  <setting id="37" name="emergency_tone" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="114" name="adb_allowed_connection_time" value="604800000" package="com.android.settings" defaultValue="604800000" defaultSysSet="true" />
  <setting id="53" name="max_sound_trigger_detection_service_ops_per_day" value="1000" package="android" defaultValue="1000" defaultSysSet="true" />
  <setting id="47" name="add_users_when_locked" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="34" name="dock_audio_media_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="117" name="enable_gpu_debug_layers" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="143" name="adb_enabled" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="25" name="trusted_sound" value="/product/media/audio/ui/Trusted.ogg" package="android" defaultValue="/product/media/audio/ui/Trusted.ogg" defaultSysSet="true" />
  <setting id="24" name="unlock_sound" value="/product/media/audio/ui/omni_unlock_phone.ogg" package="android" defaultValue="/product/media/audio/ui/omni_unlock_phone.ogg" defaultSysSet="true" />
  <setting id="130" name="force_resizable_activities" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="2" name="airplane_mode_radios" value="cell,bluetooth,wifi,nfc,wimax" package="android" defaultValue="cell,bluetooth,wifi,nfc,wimax" defaultSysSet="true" />
  <setting id="21" name="wifi_max_dhcp_retry_count" value="9" package="android" defaultValue="9" defaultSysSet="true" />
  <setting id="126" name="always_finish_activities" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="3" name="airplane_mode_toggleable_radios" value="bluetooth,wifi,nfc" package="android" defaultValue="bluetooth,wifi,nfc" defaultSysSet="true" />
  <setting id="43" name="heads_up_notifications_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="78" name="low_power" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="58" name="charging_started_sound" value="/product/media/audio/ui/ChargingStarted.ogg" package="android" defaultValue="/product/media/audio/ui/ChargingStarted.ogg" defaultSysSet="true" />
  <setting id="64" name="zen_mode_ringer_level" value="2" package="android" defaultValue="2" defaultSysSet="true" />
  <setting id="65" name="user_disabled_hdr_formats" value="" package="android" defaultValue="" defaultSysSet="true" />
  <setting id="39" name="preferred_network_mode" value="26,26" package="android" defaultValue="26,26" defaultSysSet="true" />
  <setting id="48" name="bluetooth_disabled_profiles" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="31" name="desk_undock_sound" value="/product/media/audio/ui/Undock.ogg" package="android" defaultValue="/product/media/audio/ui/Undock.ogg" defaultSysSet="true" />
  <setting id="55" name="zen_duration" package="android" />
  <setting id="121" name="mobile_data_always_on" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="97" name="debug_app" package="android" preserve_in_restore="true" />
  <setting id="8" name="wifi_sleep_policy" value="2" package="android" defaultValue="2" defaultSysSet="true" />
  <setting id="28" name="dock_sounds_enabled" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="107" name="wifi_scan_always_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="127" name="cached_apps_freezer" value="device_default" package="com.android.settings" defaultValue="device_default" defaultSysSet="true" />
  <setting id="125" name="overlay_display_devices" package="com.android.settings" />
  <setting id="87" name="data_roaming1" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" />
  <setting id="84" name="data_roaming2" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" />
  <setting id="138" name="enable_back_animation" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="82" name="network_recommendations_enabled" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="133" name="enable_non_resizable_multi_window" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="102" name="bluetooth_sanitized_exposure_notification_supported" value="1" package="com.android.bluetooth" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="32" name="car_dock_sound" value="/product/media/audio/ui/Dock.ogg" package="android" defaultValue="/product/media/audio/ui/Dock.ogg" defaultSysSet="true" />
  <setting id="54" name="sound_trigger_detection_service_op_timeout" value="15000" package="android" defaultValue="15000" defaultSysSet="true" />
  <setting id="79" name="updatable_driver_production_denylist" value="" package="android" defaultValue="" defaultSysSet="true" />
  <setting id="26" name="power_sounds_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="111" name="stay_on_while_plugged_in" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="104" name="average_time_to_discharge" value="-1" package="com.android.systemui" defaultValue="-1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="45" name="lid_behavior" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="159" name="zen_mode_config_etag" value="1249128678" package="android" defaultValue="1249128678" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="35" name="set_install_location" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="116" name="debug_view_attributes" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="62" name="zen_mode" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="11" name="transition_animation_scale" value="1.0" package="android" defaultValue="1.0" defaultSysSet="true" />
  <setting id="16" name="data_roaming" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="80" name="sysui_tuner_demo_on" value="0" package="com.android.systemui" defaultValue="0" defaultSysSet="true" />
  <setting id="123" name="debug.force_rtl" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="1" name="theater_mode_on" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="15" name="cdma_cell_broadcast_sms" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="137" name="ingress_rate_limit_bytes_per_second" value="-1" package="com.android.settings" defaultValue="-1" defaultSysSet="true" />
  <setting id="0" name="airplane_mode_on" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="132" name="force_desktop_mode_on_external_displays" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="50" name="default_restrict_background_data" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="27" name="low_battery_sound" value="/product/media/audio/ui/omni_lowbattery1.ogg" package="android" defaultValue="/product/media/audio/ui/omni_lowbattery1.ogg" defaultSysSet="true" />
  <setting id="56" name="apply_ramping_ringer" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="95" name="wait_for_debugger" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="19" name="netstats_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="49" name="wifi_wakeup_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="112" name="ota_disable_automatic_update" value="1" package="com.android.settings" defaultValue="1" defaultSysSet="true" />
  <setting id="63" name="wifi_migration_completed" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="101" name="time_remaining_estimate_millis" value="-1" package="com.android.systemui" defaultValue="-1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="40" name="subscription_mode" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="4" name="assisted_gps_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="23" name="lock_sound" value="/product/media/audio/ui/omni_lock_phone.ogg" package="android" defaultValue="/product/media/audio/ui/omni_lock_phone.ogg" defaultSysSet="true" />
  <setting id="120" name="non_persistent_mac_randomization_force_enabled" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="6" name="auto_time_zone" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="134" name="enable_gnss_raw_meas_full_tracking" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="88" name="wifi_on" value="1" package="android" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="57" name="wireless_charging_started_sound" value="/product/media/audio/ui/WirelessChargingStarted.ogg" package="android" defaultValue="/product/media/audio/ui/WirelessChargingStarted.ogg" defaultSysSet="true" />
  <setting id="83" name="device_provisioned" value="1" package="com.android.provision" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="119" name="wifi_display_certification_on" value="0" package="com.android.settings" defaultValue="0" defaultSysSet="true" />
  <setting id="81" name="sysui_demo_allowed" value="0" package="com.android.systemui" defaultValue="0" defaultSysSet="true" />
  <setting id="59" name="hdmi_control_auto_device_off_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="5" name="auto_time" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="20" name="usb_mass_storage_enabled" value="1" package="android" defaultValue="1" defaultSysSet="true" />
  <setting id="38" name="call_auto_retry" value="0" package="android" defaultValue="0" defaultSysSet="true" />
  <setting id="77" name="audio_safe_volume_state" value="3" package="android" defaultValue="3" defaultSysSet="true" />
  <setting id="141" name="boot_count" value="4" package="android" defaultValue="4" defaultSysSet="true" preserve_in_restore="true" />
  <setting id="30" name="desk_dock_sound" value="/product/media/audio/ui/Dock.ogg" package="android" defaultValue="/product/media/audio/ui/Dock.ogg" defaultSysSet="true" />
  <setting id="124" name="animator_duration_scale" value="1.0" package="android" defaultValue="1.0" defaultSysSet="true" />
</settings>
ASUS_I006D:/ #

 


Working with files in Android Binary XML format on a PC


I do not know a tool to convert a file in Android Binary XML format to human readable format on a PC.

There is a tool to convert binary XML files used in apk files in the AMXL format to human readable format and vice versa called xml2axml (https://github.com/hzw1199/xml2axml)
XML files used in apk files are in AMXL format, which is also a binary format for XML files, but different from the Android Binary XML format.
I used xml2axml successfully to convert XML files used in apk files but xml2axml does not work for files in the Android Binary XML format.

I did not test it but I assume that's also true for other tools to work with apk files like apktool or dex2jar.

Other tools for files in Android Binary XML format I found are:

The abx converter from here https://github.com/cclgroupltd/android-bits/tree/main/ccl_abx claims to be a tool to convert Android Binary XML files but seems not to work for Android Binary XML files from OmniROM 13:

[xtrnaw7@t15g /data/develop/android/android-bits-main/ccl_abx]$ python3 ccl_abx.py ../../test2/settings_global.xml
Traceback (most recent call last):
  File "/data/develop/android/android-bits-main/ccl_abx/ccl_abx.py", line 292, in <module>
    main(sys.argv[1:])
  File "/data/develop/android/android-bits-main/ccl_abx/ccl_abx.py", line 284, in main
    doc = reader.read(is_multi_root=multi_root)
  File "/data/develop/android/android-bits-main/ccl_abx/ccl_abx.py", line 266, in read
    raise NotImplementedError(f"unexpected XML type: {xml_type}")
NotImplementedError: unexpected XML type: 7
[xtrnaw7@t15g /data/develop/android/android-bits-main/ccl_abx]$


Update 23.11.2022 /bs

ccl_abx.py works for the Android Binary XML files from OmniROM 12, e.g.

[xtrnaw7@t15g /data/develop/android/android-bits-main/ccl_abx]$ python3 ccl_abx.py ../../test2/settings_global_a12.xml  -mr | sed "s/>/>\n/g" | more
<root>
<settings version="205">
<setting id="67" name="adb_wifi_enabled" value="0" package="android" defaultValue="0" defaultSysSet="true" />
<setting id="91" name="mobile_data1" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
<setting id="87" name="mobile_data2" value="0" package="com.android.phone" defaultValue="0" defaultSysSet="true" />
<setting id="41" name="low_battery_sound_timeout" value="0" package="android" defaultValue="0" defaultSysSet="true" />
<setting id="33" name="car_undock_sound" value="/product/media/audio/ui/Undock.ogg" package="android" defaultValue="/product/media/audio/ui/Undock.ogg" defaultSysSet="true" />
<setting id="101" name="time_remaining_estimate_based_on_usage" value="0" package="com.android.systemui" defaultValue="0" defaultSysSet="true" preserve_in_restore="true" />
....
<setting id="85" name="wifi_on" value="1" package="android" defaultValue="1" defaultSysSet="true" preserve_in_restore="true" />
<setting id="57" name="wireless_charging_started_sound" value="/product/media/audio/ui/WirelessChargingStarted.ogg" package="android" defaultValue="/product/media/audio/ui/WirelessChargingStarted.ogg" defaultSysSet="true" />
[xtrnaw7@t15g /data/develop/android/android-bits-main/ccl_abx]$ 



There is a github repo with an abx parser in go https://github.com/SagerNet/abx-go (I did not test this)



Example:

I wrote a little script to use the method described above to enable WiFi in the persistent settings:

enable_wifi.sh

The script will enable WiFi in the persistent settings if currently disabled; if WiFi is already enabled the script will change nothing.

The script must be executed by the user root on the phone.

Example output of the script
Output if WiFi is disabled:

ASUS_I006D:/ # /data/local/tmp/enable_wifi.sh
Checking WiFi status in the persistent global settings ...
Converting the file "/data/system/users/0/settings_global.xml" to an XML file in text format "data/local/tmp/setttings_global.xml.human.9574" ...
WiFi is currently disabled -- enabling WiFi now :..
... WiFi enabled in the persistent global settings.

To activate the change reboot the phone
ASUS_I006D:/ #

Output if Wifi is already enabled:

ASUS_I006D:/ # /data/local/tmp/enable_wifi.sh
Checking WiFi status in the persistent global settings ...
Converting the file "/data/system/users/0/settings_global.xml" to an XML file in text format "data/local/tmp/setttings_global.xml.human.9613" ...
WiFi is already enabled in the persistent global settings
ASUS_I006D:/ #
 

The script is attached to this post ; it can also be downloaded from here: enable_wifi.sh



Important Note

After writing this HowTo I discovered that there is a native Android command to change the settings stored in the file /data/system/users/0/settings_global.xml:

settings


settings usage
ASUS_I006D:/ $ settings
Settings provider (settings) commands:
 help
     Print this help text.
 get [--user <USER_ID> | current] NAMESPACE KEY
     Retrieve the current value of KEY.
 put [--user <USER_ID> | current] NAMESPACE KEY VALUE [TAG] [default]
     Change the contents of KEY to VALUE.
     TAG to associate with the setting.
     {default} to set as the default, case-insensitive only for global/secure namespace
 delete [--user <USER_ID> | current] NAMESPACE KEY
     Delete the entry for KEY.
 reset [--user <USER_ID> | current] NAMESPACE {PACKAGE_NAME | RESET_MODE}
     Reset the global/secure table for a package with mode.
     RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive
 list [--user <USER_ID> | current] NAMESPACE
     Print all defined keys.
     NAMESPACE is one of {system, secure, global}, case-insensitive
255|ASUS_I006D:/ $



settings example
e.g to set or get the WiFi status:

ASUS_I006D:/ $ settings  get global wifi_on                                                                                 
1
ASUS_I006D:/ $

ASUS_I006D:/ $ settings  put global wifi_on  0                                                                              
ASUS_I006D:/ $

ASUS_I006D:/ $ settings  get global wifi_on                                                                                      
0
ASUS_I006D:/ $

ASUS_I006D:/ $ settings  put global wifi_on   1                                                                                  
ASUS_I006D:/ $

ASUS_I006D:/ $ settings  get global wifi_on                                                                                      
1
ASUS_I006D:/ $


A description of the known settings is here:

https://developer.android.com/reference/android/provider/Settings.Global
https://developer.android.com/reference/android/provider/Settings.Secure
https://developer.android.com/reference/android/provider/Settings.System

But since this HowTo is about editing files in Android Binary XML format and the change of the Wifi status is only meant as an example, I leave it like this.






How to compile TWRP for the ASUS Zenfone 8


URL: https://forum.xda-developers.com/t/how-to-compile-twrp-for-the-asus-zenfone-8.4523817/

How to compile TWRP for the ASUS Zenfone 8
There are a lot of web pages about building your own TWRP image in the internet but at least I did not find a HowTo with all necessary instructions and infos in one place.

Therefor I wrote this little HowTo about building a TWRP image for the ASUS Zenfone 8. The instructions in the HowTo can also be used to create TWRP images for other phones supported by TWRP.

Note that this HowTo can only be used for phones that are officially supported by TWRP. The HowTo is for a PC running the Linux OS.


In the end, creating a TWRP image is also quite easy once you know how it works, thanks to the well-done setup of the development environment by the TWRP developer.


Prereqs

The repositories with the manifests for TWRP are here https://github.com/minimal-manifest-twrp and the repositories with the TWRP files and the device trees for the phones supported by TWRP are here: https://github.com/TeamWin. So you need access to github for creating your own TWRP image.

The tool repo is required to maintain the repositories for TWRP; you can download repo from https://gerrit.googlesource.com/git-repo

It's not necessary to install additional tools for creating an TWRP image -- all necessary tools to create the image are in the repositories for TWRP.


Note

To check if there is a repository with the device tree for another phone for TWRP use the search function on the page https://github.com/TeamWin and search for the device ID of the phone.

To search via script use (I006D is the device ID for the ASUS Zenfone 8):

wget "https://api.github.com/search/repositories?q=I006D" -O ./repository_list


search example for the ASUS Zenfone 8
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrpX ] $ wget "https://api.github.com/search/repositories?q=I006D" -O ./repository_list
--2022-11-27 09:20:27--  https://api.github.com/search/repositories?q=I006D
Resolving api.github.com (api.github.com)... 140.82.121.5
Connecting to api.github.com (api.github.com)|140.82.121.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: ‘./repository_list’

./repository_list                                      [ <=>                                                                                                            ]  46.30K  --.-KB/s    in 0.01s  

2022-11-27 09:20:28 (3.55 MB/s) - ‘./repository_list’ saved [47414]

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

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrpX ] $ grep "full_name"  ./repository_list
      "full_name": "CaptainThrowback/android_device_asus_I006D",
      "full_name": "nguyenhung9xdev2022/android_kernel_asus_I006D",
      "full_name": "nguyenhung9xdev2022/android_vendor_asus_I006D",
      "full_name": "nguyenhung9xdev2022/android_device_asus_I006D",
      "full_name": "asus-development/android_device_asus_I006D",
      "full_name": "OpenPecha-Data/I006D4C7E",
      "full_name": "dmd79/android_device_asus_I006D",
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrpX ] $



To get the device ID for a phone execute :

adb shell getprop ro.product.system.device

e,g.

#
# on a PC for a connected ASUS Zenfone 8
#
[xtrnaw7@t15g /data/develop/android/twrp]$ adb shell getprop ro.product.system.device
I006D
[xtrnaw7@t15g /data/develop/android/twrp]$

#
# on the ZUK Z2 Pro
#
z2_row:/ # getprop ro.product.system.device
z2_row
z2_row:/ #


The device ID is required for the lunch command used to copy the repositories with the device trees (see below).



Building the ROM

First copy the necessary repositories for building TWRP to your local machine using these commands:

#
# create a new directory for the repositories (optional)
#
mkdir twrp
cd twrp

# init the local repositories
#
# As of 27.11.2022 the latest version of the TWRP repositories is 12.1 ;
# to download the default repository version you can omit the parameter "
-b twrp-12.1"
# As of 27.11.2022 the repositories for TWRP 12.1 use about 45 GB;
# to save some space (about 12 GB) you could add the parameter
--depth=1
#
repo init -u https://github.com/minimal-manifest-twrp/platform_manifest_twrp_aosp.git -b twrp-12.1

# sync the repositories (Parameter -j8 = use 8 parallel jobs; adjust this parameter to the number of CPU available on your PC)
#
repo sync -c -j8 --force-sync --no-clone-bundle --no-tags dd



Next add the device dependent repositories for the ASUS Zenfone 8.

The format of the parameter for the lunch command is

twrp_<device_id>-eng

So the parameter for the ASUS Zenfone 8 is twrp_I006D-eng.


#
# init the environment to build the TWRP image
#
. build/envsetup.sh

export ALLOW_MISSING_DEPENDENCIES=true

lunch twrp_I006D-eng



lunch example
TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $ lunch twrp_I006D-eng
In file included from build/make/core/config.mk:313:
In file included from build/make/core/envsetup.mk:312:
build/make/core/product_config.mk:160: error: Can not locate config makefile for product "twrp_I006D".
11:35:37 dumpvars failed with: exit status 1
Device I006D not found. Attempting to retrieve device repository from TeamWin Github (http://github.com/TeamWin).
Found repository: android_device_asus_I006D
Checking branch info
Calculated revision: android-12.1
Adding dependency:
Repository: android_device_asus_I006D
Branch: android-12.1
Remote: TeamWin
Path: device/asus/I006D

Syncing repository to retrieve project.
Fetching: 100% (1/1), done in 12.846s
NOT Garbage collecting: 0% (0/1), done in 0.001s
repo sync has finished successfully.
Repository synced!
Looking for dependencies
Adding dependencies to manifest

Adding dependency:
Repository: android_device_asus_sm8350-common
Branch: android-12.1
Remote: TeamWin
Path: device/asus/sm8350-common

Syncing dependencies
Fetching: 100% (1/1), done in 15.848s
NOT Garbage collecting: 0% (0/1), done in 0.001s
repo sync has finished successfully.
Looking for dependencies
device/asus/sm8350-common has no additional dependencies.
Done

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=99.87.36
TARGET_PRODUCT=twrp_I006D
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-6.0.9-200.fc36.x86_64-x86_64-Fedora-Linux-36-(MATE-Compiz)
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=SP2A.220405.004
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/asus/sm8350-common
============================================
[ TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $



And final build the TWRP image

mka bootimage


mka bootimage example
[ TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $ time mka bootimage
build/make/core/soong_config.mk:197: warning: BOARD_PLAT_PUBLIC_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS instead.
build/make/core/soong_config.mk:198: warning: BOARD_PLAT_PRIVATE_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS instead.
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=99.87.36
TARGET_PRODUCT=twrp_I006D
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=generic
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-6.0.9-200.fc36.x86_64-x86_64-Fedora-Linux-36-(MATE-Compiz)
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=SP2A.220405.004
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/asus/sm8350-common
============================================
[ 78% 246/312] including bootable/recovery/Android.mk ...
bootable/recovery/prebuilt/Android.mk:437: warning: vendor_hw: bootable/recovery/prebuilt/relink.sh out/target/product/I006D/recovery/root/vendor/bin/hw
[ 95% 299/312] including system/sepolicy/Android.mk ...
system/sepolicy/Android.mk:57: warning: BOARD_PLAT_PUBLIC_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS instead.
system/sepolicy/Android.mk:62: warning: BOARD_PLAT_PRIVATE_SEPOLICY_DIR has been deprecated. Use SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS instead.
[ 30% 8637/28297] //bionic/libc:libc_bionic clang arch-arm64/bionic/setjmp.S
....
Removing unneeded service: keymaster-4-1-citadel

Service_Cleanup script complete.

[100% 28297/28297] Target boot image from recovery: out/target/product/I006D/boot.img

#### build completed successfully (14:50 (mm:ss)) ####


real 14m50.110s
user 182m6.217s
sys 11m58.492s
[ TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $
 


Note:

For devices without A/B devices use mka recoveryimage.


The TWRP image will be created in file boot.img in the directory ./out/target/product/I006D, e.g.:

[ TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $   ls -ltr out/target/product/I006D/boot.img
-rw-rw-r--. 1 xtrnaw7 xtrnaw7 100663296 Nov 27 12:06 out/target/product/I006D/boot.img
[ TWRP Dev - xtrnaw7@t15g /data/develop/android/twrp ] $ 


To test the image just boot the phone from the image

adb reboot bootloader
sudo fastboot boot out/target/product/I006D/boot.img


And check if the boot was succesfull
:

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrpX ] $  adb shell getprop ro.bootmode
unknown
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/twrpX ] $

unknown is the expected value here.


Trouble Shooting

On some pages I found a hint that the build scripts require python2 and will not work with python3. But this is most probably not true anymore because building the image with python3 worked without problems.


It's strongly recommended to use separate sessions for compiling OmniROM or TWRP or even different versions of TWRP:

All these build environments define the same commands in the file ./build/envsetup.sh but with different functionality.


As of 27.11.2022 there are only 2 branches in the repository with the manifests for TWRP : version 11 and version 12.1.

If there is no branch for one of these versions in the repositories for the device tree of the phone for which you want to create a TWRP image the lunch command will fail.


The repositories with the manifests for TWRP prior to version 11 can be fetched with this command:

# Minimal manifest for building TWRP for devices shipped with Android 5.1 through Android 9.0
#
repo init -u https://github.com/minimal-manifest-twrp/platform_manifest_twrp_omni.git -b twrp-9.0


Note that the lunch command in the old repositories does work different 


Notes:

The URL to the repository with the device tree for a phone is also on the page for the device on the TWRP device list: https://twrp.me/Devices/

See https://unofficialtwrp.com/build-compile-twrp-recovery/ for other possible settings for the build

see here for How to add additional files to an TWRP image

XDA Forum thread about compiling TWRP: How to compile TWRP touch recovery 

 

Some hints for using TWRP


URL: https://forum.xda-developers.com/t/some-hints-using-twrp.4525833/

Some hints using TWRP
This HowTo contains some hints for using TWRP in general and some tips for using CLI commands in TWRP.

I will update this post in case I find something new & useful regarding TWRP and update the history section at the end of this post.

The test environment for the instructions below was

TWRP 3.6.2.12-0 for the ASUS Zenfone 8
TWRP 3.7.0_12-0 for the ASUS Zenfone 8

Note:

This is NOT a general HowTo for TWRP.




How to test a TWRP image?

It's recommended to boot from the TWRP image file before installing it into the boot or recovery partition of your phone. To do this do

# connect the phone via USB to your PC

# boot the phone into the bootloader - e.g. via
#
adb reboot bootloader

# boot the phone from the TWRP image (this will not change anything on the phone)
#
sudo fastboot boot <twrpimage_file>



If everything works you can install the TWRP image into the boot or recovery partition using the approbiate menu entry in TWRP.



How to connect via adb to an TWRP session?

TWRP supports adb session as user root, therefor an

adb shell

on your PC is sufficient to open an adb shell as user root on the phone booted into TWRP.

In case you get a permission denied error kill the running adb daemon on the PC and restart the adb command. If that does not fix the error disconnect and reconnect the USB cable between the PC and the Phone.

Be aware that TWRP starts the adb daemon twice after booting. Therefor an adb session to the phone started to early after the boot into TWRP might be killed. In this case just open a new adb shell.



How to check if the phone booted into TWRP ?

Check the value of the property ro.twrp.boot; this property is only set if TWRP is running

e.g:

ASUS_I006D:/ # getprop ro.twrp.boot                                           
1
ASUS_I006D:/ #




How to check if the running TWRP was booted from an TWRP image or from a boot or recovery partition?

Check the value of the property ro.bootmode; the value of this property is "unknown" if TWRP was booted from an image file via "fastboot boot <twrp_image_file>", e.g.

ASUS_I006D:/ # getprop ro.bootmode
unknown
ASUS_I006D:/ #


-> TWRP was booted from an TWRP image file

ASUS_I006D:/ # getprop ro.bootmode
recovery
ASUS_I006D:/ #


-> TWRP was booted from the boot or recovery partition



How to get the version of the running TWRP?

Check the value of the property getprop ro.twrp.version,  e.g.:

ASUS_I006D:/ # getprop ro.twrp.version

3.7.0_12-0
ASUS_I006D:/ #




Where is the log file from TWRP?

The logfile with the messages written while booting TWRP is /data/recovery/log.gz.

e.g.


130|ASUS_I006D:/ # ls -ltr /data/recovery/l*gz
-rw------- 1 system system 8022 2022-12-26 14:03 /data/recovery/log.gz
ASUS_I006D:/ #

ASUS_I006D:/ # gzip -cd /data/recovery/log.gz | head -10                                                                                                                                            

Starting TWRP 3.7.0_12-0-d07fdb3c on Sat Jan 10 00:53:15 1970
 (pid 403)
I:Lun file '/config/usb_gadget/g1/functions/mass_storage.0/lun.0/file'
PRODUCT_USE_DYNAMIC_PARTITIONS := true
TW_INCLUDE_CRYPTO := true
I:TW_BRIGHTNESS_PATH := /proc/lcd_brightness
I:Found brightness file at '/proc/lcd_brightness'
I:TWFunc::Set_Brightness: Setting brightness control to 420
I:TW_EXCLUDE_ENCRYPTED_BACKUPS := true
I:LANG: en
ASUS_I006D:/ #



The current logfile of the running TWRP is /tmp/recovery.log, e.g.:

ASUS_I006D:/ # ls -l /tmp/recovery.log
-rw-rw-rw- 1 root root 43303 2023-05-19 20:11 /tmp/recovery.log
ASUS_I006D:/ #




Where are the settings from TWRP stored?

The settings from TWRP are stored in the file

/data/media/0/TWRP/.twrps

This is a binary file.

Note that on phones with active encryption that file is only accessible after /data is decrypted.



How to edit a file while booted into TWRP?

The editor nano is part of TWRP and can be used to edit files in an adb session.



How to mount a partition while inside TWRP?

Use

twrp mount <mountpoint>

to mount a partition while TWRP is running.

Note that you should use this command with caution in adb sessions because the adb session may be killed after the mount


How to decrypt an encrypted data partition form within a booted TWRP

Use

twrp decrypt <password>

to decrypt an encrypted /data partition, e.g.:

ASUS_I006D:/ # ls /data/media/0                                                                                                                                   
+lnodCAAAAAnYFLObBoWS5ZNb5yVMpze  8KdHMCAAAAQOgtDHq,XdSfFPW+XURA2M  KlfTJCAAAAwL93jkH2ugnU4eUG3Zzm7M  W9W9SCAAAAgh6lwoP0mNiEK9VIWJoH6L
0nUvhAAAAAQ9ptz+L5qPQrpzcAt4G06K  DWk4KDAAAAA,B1xVDmT3dZI87g2C0IS8  M6jcmCAAAAAU,4gn9o0zoBZmkysyXy49  bE7t0CAAAAQ2YuHL9styNaf,iSf2cCO,
4nFjMBAAAAwv2oUczXEpKUDfRIOi+nR5  EcHrdAAAAAglBtbhNWl0NvyX8aW+3CHN  O5ZVrDAAAAAlKd7PPpo7CaQmkFbZzKRm  haPcnBAAAAw9o2lLF97zpPTaC+byP92s
5yL8gCAAAAgekBDBUi4,W78rJAkgFcMF  FA1aZAAAAAg2XNF+M859lpPY,3rUC2cY  Vda+LCAAAAwR+g6Tav35+h,Epc8latGR  mSKc8BAAAAwm+12sMClHPf+zwtHAeW+Q
8CsF+DAAAAwI3tImfOqIS9aHCQ3S7Cdg  JpuFgAAAAAwdEzuQerx4681dzGLjlK3X  VvEV4DAAAAgRgoqCfrypNqryZxRACnfr  rMJEjAAAAAgwTgx,vA5ewWMVizc2LAos
ASUS_I006D:/ #

ASUS_I006D:/ # twrp decrypt 12345                                                                                                                             
Attempting to decrypt data partition or user data via command line.
Attempting to decrypt FBE for user 0...
User 0 Decrypted Successfully
Data successfully decrypted
Updating partition details...
...done
ASUS_I006D:/ #

ASUS_I006D:/ # ls /data/media/0                                                                                                                                   
Alarms  Android  Audiobooks  DCIM  Documents  Download  Movies  Music  Notifications  Pictures  Podcasts  Recordings  Ringtones  TWRP
ASUS_I006D:/ #


see the TWRP Commandline Guide for the known parameter for this command

Note that existing adb sessions will be stopped after sucessfully executing this command.


How to check if the /data partition is encrypted from within a booted TWRP

To check if the data partition is encrypted in a script executed in a running TWRP use code like this:


PASSWORD="1234"

CRYPTO_READY=$( getprop crypto.ready )

TWRP_DECRYPT_DONE=$( getprop twrp.decrypt.done )


if [ "${TWRP_DECRYPT_DONE}"x = "true"x ] ; then

  echo "The data partition is not encrypted or already decrypted"

elif [ "${CRYPTO_READY}"x = "1"x ] ; then

  echo "The data partition is encrypted"

# sample commands to decrypt the data partition
#
#  echo "Decryptiong the data partition now ..."

#  twrp decrypt "${PASSWORD}" 

elif [ "${CRYPTO_READY}"x = "0"x ] ; then

  echo "The data partition is not encrypted"

else

  echo "ERROR: The value of the property crypto.ready \"${CRYPTO_READY}\" is not known"

fi




How to install a ZIP file via CLI command from within a running TWRP?

Copy the zip file to the phone via adb push and then use

twrp install /path/to/update.zip

to install a ZIP file from within TWRP

Note that the parameter must be a fully qualified filename.

Using this method to update the OS only works in a terminal session on the phone; it does not work in a adb session (at least in my tests until now ...)



What commands are available in TWRP?

Most of the Unix commands are available in TWRP:

ls /bin/
ASUS_I006D:/ # \ls  /bin/                                                                                                                                    
[                                            env               load_policy          pigz                             swapon
acpi                                         erase_image       log                  pkill                            sync
adbd                                         expand            logcat               pmap                             sysctl
android.hardware.boot@1.0-service            expr              logd                 prepdecrypt.sh                   tac
android.hardware.boot@1.1-service            fallocate         logname              printenv                         tail
android.hardware.boot@1.2-service            false             losetup              printf                           tar
android.hardware.gatekeeper@1.0-service-qti  fastbootd         lpdump               privapp-permissions-twrpapp.xml  taskset
android.hardware.health@2.0-service          fatlabel          lpdumpd              ps                               tee
android.hardware.health@2.1-service          fgrep             lptools              pwd                              test
android.hardware.keymaster@4.1-service-qti   file              ls                   qcom_decrypt-timestamp           time
avbctl                                       find              lsattr               qcom_decrypt_fbe-timestamp       timeout
awk                                          flash_image       lsmod                qseecomd                         toolbox
base64                                       flock             lsof                 readelf                          top
basename                                     fmt               lspci                readlink                         touch
bash                                         free              lsusb                realpath                         toybox
bash_twrp-timestamp                          fsck.exfat        magiskboot           reboot                           tr
bc                                           fsck.f2fs         make_f2fs            recovery                         true
blockdev                                     fsck.fat          md5sum               renice                           truncate
bootctl                                      fscryptpolicyget  me.twrp.twrpapp.apk  resetprop                        tty
bu                                           fsync             microcom             resize2fs                        tune2fs
cal                                          getconf           minadbd              restorecon                       twrp
cat                                          getenforce        mkdir                rm                               tzdata_twrp-timestamp
charger                                      getevent          mke2fs               rmdir                            ueventd
chattr                                       getprop           mkexfatfs            rmmod                            ulimit
chcon                                        grep              mkfifo               rtcwake                          umount
chgrp                                        groups            mkfs.fat             runcon                           uname
chmod                                        gunzip            mknod                sed                              uniq
chown                                        gzip              mkswap               sendevent                        unix2dos
chroot                                       head              mktemp               seq                              unlink
chrt                                         hostname          modinfo              servicemanager                   unpigz
cksum                                        hwclock           modprobe             setenforce                       unshare
clear                                        hwservicemanager  more                 setprop                          unzip
cmp                                          i2cdetect         mount                setsid                           update_engine_sideload
comm                                         i2cdump           mountpoint           sgdisk                           uptime
cp                                           i2cget            mv                   sh                               usleep
cpio                                         i2cset            nano                 sha1sum                          uudecode
cut                                          iconv             nano_twrp-timestamp  sha224sum                        uuencode
date                                         id                nc                   sha256sum                        uuidgen
dd                                           ifconfig          netcat               sha384sum                        vmstat
debuggerd                                    init              netstat              sha512sum                        vndservicemanager
devmem                                       inotifyd          nice                 simg2img                         vold_prepare_subdirs
df                                           insmod            nl                   sleep                            watch
diff                                         install           nohup                sload_f2fs                       watchdogd
dirname                                      ionice            nproc                sort                             wc
dmesg                                        iorenice          nsenter              split                            which
dos2unix                                     keystore2         od                   start                            whoami
du                                           keystore_cli_v2   orsin                stat                             xargs
dump_image                                   kill              orsout               stop                             xxd
e2fsck                                       killall           paste                strace                           yes
e2fsdroid                                    linker64          patch                strings                          zcat
echo                                         linker_asan64     pgrep                stty                             zipinfo
egrep                                        ln                pidof                swapoff                          ziptool
ASUS_I006D:/ #





Be aware that a lot of Unix commands in TWRP are implemented using a general binary called toybox (toybox is something like busybox) and do not support all parameter like the original Linux commands

ASUS_I006D:/ # ls -l /bin/  | wc -l                                                                                                                          
261
ASUS_I006D:/ # ls -l /bin/ | grep toybox  | wc -l                                                                                                            
175
ASUS_I006D:/ # 




What other TRWP specific commands can be used while in a running TWRP?


twrp and a few others like bootctl (see below) or magiskboot (see below).

twrp usage
ASUS_I006D:/ # twrp
TWRP openrecoveryscript command line tool, TWRP version 3.7.0_12-0

Allows command line usage of TWRP via openrecoveryscript commands.
Some common commands include:
  install /path/to/update.zip
  backup <SDCRBAEM> [backupname]
  restore <SDCRBAEM> [backupname]
  wipe <partition name>
  format data
  sideload
  set <variable> [value]
  decrypt <password> [USER ID]
  remountrw
  fixperms
  mount <path>
  unmount <path>
  listmounts
  print <value>
  mkdir <directory>
  reboot [recovery|poweroff|bootloader|download|edl]

See more documentation at https://twrp.me/faq/openrecoveryscript.html
ASUS_I006D:/ #


see also How to install an OS image using the TWRP binary twrp?


Can I use the twrp binary in my Android OS or another recovery?

The twrp executable does not really do all the work : it just writes the commands to the figo /system/bin/orsout and reads the result from the fifo /system/bin/orsin. The real work is done by the TWRP daemon. Without the running TWRP daemon the twrp executable does not work. The TWRP daemon does not work in the Android OS and therefor it useless to copy the twrp executable to the Android OS.


How to change the active slot for the next reboot while in TWRP?

Use bootctl, e.g.

ASUS_I006D:/ # bootctl get-current-slot                                                                                                                    
1
ASUS_I006D:/ # bootctl set-active-boot-slot 0                                                                                                                


The usage for the command bootctl is

bootctl usage
ASUS_I006D:/ # /bin/bootctl                                                                                                                              
/bin/bootctl - command-line wrapper for the boot HAL.

Usage:
  /bin/bootctl COMMAND

Commands:
  hal-info                       - Show info about boot_control HAL used.
  get-number-slots               - Prints number of slots.
  get-current-slot               - Prints currently running SLOT.
  mark-boot-successful           - Mark current slot as GOOD.
  get-active-boot-slot           - Prints the SLOT to load on next boot.
  set-active-boot-slot SLOT      - On next boot, load and execute SLOT.
  set-slot-as-unbootable SLOT    - Mark SLOT as invalid.
  is-slot-bootable SLOT          - Returns 0 only if SLOT is bootable.
  is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
  get-suffix SLOT                - Prints suffix for SLOT.
  set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
                                   partition is in progress. Valid STAT values
                                   are: none, unknown, snapshotted, merging,
                                   or cancelled.
  get-snapshot-merge-status      - Prints the current snapshot-merge status.

SLOT parameter is the zero-based slot-number.
ASUS_I006D:/ #      


see also How to switch the active slot 



How to use Magisk binaries while booted into TWRP


Magisk is another great tool for Android phones -- for details about Magisk see here:

https://github.com/topjohnwu/Magisk


The Magisk binary for manipulating the boot partition, magiskboot, is part of TWRP.  This binary can be used to unpack and repack boot partitions or boot partition images, ramdisks, or compress/decompress files. The binary works without problems while booted into TWRP.

magiskboot usage
1|ASUS_I006D:/ # magiskboot
MagiskBoot - Boot Image Modification Tool

Usage: magiskboot <action> [args...]

Supported actions:
  unpack [-n] [-h] <bootimg>
    Unpack <bootimg> to, if available, kernel, kernel_dtb, ramdisk.cpio,
    second, dtb, extra, and recovery_dtbo into current directory.
    If '-n' is provided, it will not attempt to decompress kernel or
    ramdisk.cpio from their original formats.
    If '-h' is provided, it will dump header info to 'header',
    which will be parsed when repacking.
    Return values:
    0:valid    1:error    2:chromeos

  repack [-n] <origbootimg> [outbootimg]
    Repack boot image components from current directory
    to [outbootimg], or new-boot.img if not specified.
    If '-n' is provided, it will not attempt to recompress ramdisk.cpio,
    otherwise it will compress ramdisk.cpio and kernel with the same format
    as in <origbootimg> if the file provided is not already compressed.
    If env variable PATCHVBMETAFLAG is set to true, all disable flags will
    be set in the vbmeta header.

  hexpatch <file> <hexpattern1> <hexpattern2>
    Search <hexpattern1> in <file>, and replace with <hexpattern2>

  cpio <incpio> [commands...]
    Do cpio commands to <incpio> (modifications are done in-place)
    Each command is a single argument, add quotes for each command
    Supported commands:
      exists ENTRY
        Return 0 if ENTRY exists, else return 1
      rm [-r] ENTRY
        Remove ENTRY, specify [-r] to remove recursively
      mkdir MODE ENTRY
        Create directory ENTRY in permissions MODE
      ln TARGET ENTRY
        Create a symlink to TARGET with the name ENTRY
      mv SOURCE DEST
        Move SOURCE to DEST
      add MODE ENTRY INFILE
        Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
      extract [ENTRY OUT]
        Extract ENTRY to OUT, or extract all entries to current directory
      test
        Test the current cpio's status
        Return value is 0 or bitwise or-ed of following values:
        0x1:Magisk    0x2:unsupported    0x4:Sony
      patch
        Apply ramdisk patches
        Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
      backup ORIG
        Create ramdisk backups from ORIG
      restore
        Restore ramdisk from ramdisk backup stored within incpio
      sha1
        Print stock boot SHA1 if previously backed up in ramdisk

  dtb <input> <action> [args...]
    Do dtb related actions to <input>
    Supported actions:
      print [-f]
        Print all contents of dtb for debugging
        Specify [-f] to only print fstab nodes
      patch
        Search for fstab and remove verity/avb
        Modifications are done directly to the file in-place
        Configure with env variables: KEEPVERITY

  split <input>
    Split image.*-dtb into kernel + kernel_dtb

  sha1 <file>
    Print the SHA1 checksum for <file>

  cleanup
    Cleanup the current working directory

  compress[=format] <infile> [outfile]
    Compress <infile> with [format] (default: gzip), optionally to [outfile]
    <infile>/[outfile] can be '-' to be STDIN/STDOUT
    Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg

  decompress <infile> [outfile]
    Detect format and decompress <infile>, optionally to [outfile]
    <infile>/[outfile] can be '-' to be STDIN/STDOUT
    Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg

1|ASUS_I006D:/ #



The Magisk binary magisk can be used while booted into TWRP if the used TWRP is able to mount the volume for /data; for details see the section Using the magisk binary while the phone is booted into TWRP in the post Some hints for using Magisk on Android phones 


If Magisk is installed in the Android OS on the phone and the booted TWRP is able to mount the volume for /data the Magisk binary magisk can also be used in an session in TWRP.
For details see the section Using the magisk binary while the phone is booted into TWRP in the post Some hints for using Magisk on Android phones.


magisk usage
1|ASUS_I006D:/ # /data/adb/magisk/magisk64
Magisk - Multi-purpose Utility

Usage: magisk [applet [arguments]...]
   or: magisk [options]...

Options:
   -c                        print current binary version
   -v                        print running daemon version
   -V                        print running daemon version code
   --list                    list all available applets
   --remove-modules          remove all modules and reboot
   --install-module ZIP      install a module zip file

Advanced Options (Internal APIs):
   --daemon                  manually start magisk daemon
   --stop                    remove all magisk changes and stop daemon
   --[init trigger]          callback on init triggers. Valid triggers:
                             post-fs-data, service, boot-complete, zygote-restart
   --unlock-blocks           set BLKROSET flag to OFF for all block devices
   --restorecon              restore selinux context on Magisk files
   --clone-attr SRC DEST     clone permission, owner, and selinux context
   --clone SRC DEST          clone SRC to DEST
   --sqlite SQL              exec SQL commands to Magisk database
   --path                    print Magisk tmpfs mount path
   --denylist ARGS           denylist config CLI

Available applets:
    su, resetprop

1|ASUS_I006D:/ #





How to install TWRP into the boot partition using CLI commands from within a booted TWRP?

Use the script

install_twrp_from_within_twrp.sh

Download from here: http://bnsmb.de/files/public/Android/install_twrp_from_within_twrp.sh


How to install TWRP after an OS Upgrade from within the running Android OS?

see How to Install TWRP into the boot partition again after the installation of an OS update 



How to add additional files to an existing TWRP image?

see How to add additional files to an TWRP image 



How to install TWRP into the boot partition via script?

see How to install TWRP via script



How to build a TWRP image?

see How to compile TWRP for the ASUS Zenfone 8 



How to install an OS image using the twrp command in a script

see How to install an OS image using the TWRP binary twrp?



How to use TWRP if LineageOS 20.x is installed

As of 26.06.2023 booting the phone from the TWRP recovery image using the command

sudo fastboot boot <twrp_image>

Does not work anymore if LineageOS 20.x is installed on the phone. 
See How to use TWRP if LineageOS 20.x is installed for a workaround





History of this entry
History


02.12.2022 /bs
  initial release

17.12.2022 /bs
  added infos about how to use the binary twrp to install an OS image

26.12.2022 /bs
  added infos about the logile used by TWRP

23.04.2023 /bs
  added info about how to decrypt the data partition

30.04.2023 /bs
  added infos about the file used for the TWRP settings

12.03.2023 /bs
  added infos about using the twrp executable outside of TWRP

19.05.2023 /bs

  added infos about how to check if the data partition is encrypted
  updated the infos about the log file used by TWRP
  added infos about how to install TWRP from within a running Android OS

26.06.2023 /bs
  added the infos about how to use TWRP with LineageOS 20.x


 

How to enable root access using Magisk in a script


URL: https://forum.xda-developers.com/t/how-to-enable-root-access-using-magisk-in-a-script.4527035/

How to enable root access using Magisk in a script
Magisk uses the table policies in the Magisk database /data/adb/magisk.db to store the list of root enabled apps.

This Howto is about how to change the entries in that table via script.

Caution:

Changing the table via script is necessary for the automatic installation and configuration of the Android OS. Of course you can do a lot of mischief with it and bypass the security checks of Android and Magisk i.e. this should only be used with caution!
On the other hand, who rooted her phone via Magisk should know what she is doing anyway ....


Anyway -- let's start:


Note:

Most of the commands in this HowTo must be done by the user root


magisk.db is a sqlite database so to list the table sqlite3 can be used, e.g.:

ASUS_I006D:/ # /data/adb/sqlite3 /data/adb/magisk.db 'select * from policies ;'
2000|2|0|1|1
10143|2|0|1|1
10134|2|0|1|1
10141|2|0|1|1
ASUS_I006D:/ #


or in a human readable format:

ASUS_I006D:/ # /data/adb/sqlite3 -table /data/adb/magisk.db 'select * from policies ;'
+-------+--------+-------+---------+--------------+
|  uid  | policy | until | logging | notification |
+-------+--------+-------+---------+--------------+
| 2000  | 2      | 0     | 1       | 1            |
| 10143 | 2      | 0     | 1       | 1            |
| 10134 | 2      | 0     | 1       | 1            |
| 10141 | 2      | 0     | 1       | 1            |
+-------+--------+-------+---------+--------------+
ASUS_I006D:/ #


Not all sqlite3 binaries support the parameter -table : for sqlite binaries not supporting the parameter -table the parameter -column -header can be used for a human readble output, e.g.:

ASUS_I006D:/ # /data/adb/sqlite3  -column -header /data/adb/magisk.db  "select * from policies ;"
uid    policy  until  logging  notification
-----  ------  -----  -------  ------------
2000   2       0      1        1          
10134  2       0      1        1          
10141  2       0      1        1          
10143  2       0      1        1          
10129  2       0      1        1          
10122  2       0      1        1          
10133  3       0      1        1          
ASUS_I006D:/ #


sqlite3 is not part of the Android OS; it must be installed either by copying the sqlite3 binary to the phone (see also this thread : https://forum.xda-developers.com/t/new-sqlite3-binary-v3-40-0-for-all-devices.4273049/ ) or by installing a Magisk module with sqlite3 like for example this one: https://github.com/stylemessiah/SQLite3UniversalBinaries

Fortunately the binary magisk from Magisk can also work with SQL databases. So the entries of the table policies can also be printed using the magisk binary, e.g:

ASUS_I006D:/ # magisk --sqlite 'select * from policies ;'
logging=1|notification=1|policy=2|uid=2000|until=0
logging=1|notification=1|policy=2|uid=10143|until=0
logging=1|notification=1|policy=2|uid=10134|until=0
logging=1|notification=1|policy=2|uid=10141|until=0
ASUS_I006D:/ #


To add another entry to the table policies either sqlite3 or magisk can be used, example:

ASUS_I006D:/ # /data/adb/sqlite3 -table /data/adb/magisk.db 'select * from policies ;'
+-------+--------+-------+---------+--------------+
|  uid  | policy | until | logging | notification |
+-------+--------+-------+---------+--------------+
| 2000  | 2      | 0     | 1       | 1            |
| 10143 | 2      | 0     | 1       | 1            |
| 10134 | 2      | 0     | 1       | 1            |
| 10141 | 2      | 0     | 1       | 1            |
+-------+--------+-------+---------+--------------+
ASUS_I006D:/ #


-> insert an entry for the UID 10888 in the table policies using magisk :

ASUS_I006D:/ # magisk --sqlite 'INSERT INTO policies (uid,policy,until,logging,notification) values(10888,2,0,1,1);'                                                           
ASUS_I006D:/ #

ASUS_I006D:/ # /data/adb/sqlite3 -table /data/adb/magisk.db 'select * from policies ;'
+-------+--------+-------+---------+--------------+
|  uid  | policy | until | logging | notification |
+-------+--------+-------+---------+--------------+
| 2000  | 2      | 0     | 1       | 1            |
| 10143 | 2      | 0     | 1       | 1            |
| 10134 | 2      | 0     | 1       | 1            |
| 10141 | 2      | 0     | 1       | 1            |
| 10888 | 2      | 0     | 1       | 1            |
+-------+--------+-------+---------+--------------+
ASUS_I006D:/ #


-> insert an entry for the UID 10999 in the table policies using sqlite3 :

ASUS_I006D:/ # /data/adb/sqlite3 -table /data/adb/magisk.db 'INSERT INTO policies (uid,policy,until,logging,notification) values(10999,2,0,1,1);'                                  
ASUS_I006D:/ #

ASUS_I006D:/ # /data/adb/sqlite3 -table /data/adb/magisk.db 'select * from policies ;'
+-------+--------+-------+---------+--------------+
|  uid  | policy | until | logging | notification |
+-------+--------+-------+---------+--------------+
| 2000  | 2      | 0     | 1       | 1            |
| 10143 | 2      | 0     | 1       | 1            |
| 10134 | 2      | 0     | 1       | 1            |
| 10141 | 2      | 0     | 1       | 1            |
| 10888 | 2      | 0     | 1       | 1            |
| 10999 | 2      | 0     | 1       | 1            |
+-------+--------+-------+---------+--------------+
ASUS_I006D:/ #



To change an existing entry use the SQL command REPLACE, e.g.

#
# using the magisk binary to change the entry for the UID 10888
#
magisk --sqlite 'REPLACE INTO policies (uid,policy,until,logging,notification) values(10888,2,0,1,1);'

#
# using the sqlite3 binary to change the entry for the UID 10999
#
/data/adb/sqlite3 -table /data/adb/magisk.db 'REPLACE INTO policies (uid,policy,until,logging,notification) values(10999,2,0,1,1);'


To delete an entry use the SQL command DELETE, e.g.:

#
# use magisk to delete the entry for the UID 10999
#
magisk --sqlite 'DELETE FROM policies where (uid=10999) ; '
 
#
# use sqlite3 to delete the entry for the UID 10888
#
/data/adb/sqlite3 -table /data/adb/magisk.db  'DELETE FROM policies where (uid=10888);'


The uid in the table policies is the UID from the packages installed on the phone.

To get the list of installed packages with UIDs use this command:

pm list packages -U

example:

130|ASUS_I006D:/ # pm list packages -U
package:org.microg.nlp.backend.nominatim uid:10071
package:com.qualcomm.qti.gpudrivers.lahaina.api30 uid:10079
package:com.android.theme.icon.teardrop uid:10008
....
package:com.qualcomm.qtil.aptxacu uid:10110
package:com.android.pacprocessor uid:10062
package:com.android.messaging uid:10091
package:com.android.theme.icon.square uid:10007
ASUS_I006D:/ #  



To get the UID for a specific package use

pm list packages -U "<appname>"

example:

ASUS_I006D:/ # pm list packages -U com.android.shell  | cut -f3 -d ":"                                                                                      
2000
ASUS_I006D:/ #



The value for policy in the table policies defines the root access status for an app: the value for root enabled is 2. So to list the UIDs of all apps with enabled root access use this command:

ASUS_I006D:/ #  magisk --sqlite "SELECT uid FROM policies WHERE policy = '2';"
uid=2000
uid=10143
uid=10134
uid=10141
ASUS_I006D:/ #

... or use the handy little script from this thread https://forum.xda-developers.com/t/magisk-general-support-discussion.3432382/page-2681

130|ASUS_I006D:/ # sh /sdcard/Download/list_magisk_root_permissions.sh
Packages with root granted:
package:com.android.shell uid:2000
package:com.mixplorer uid:10143
package:com.keramidas.TitaniumBackup uid:10134
package:io.github.muntashirakon.AppManager uid:10141
ASUS_I006D:/ #


Note:

The value for policy in the table policies for apps with disabled root access is 1.


Scripts

To automate enabling root access via changing the entries in the Magisk database I wrote a little script:

enable_root_access_via_magisk.sh

Usage:

ASUS_I006D:/ #  /data/adb/enable_root_access_via_magisk.sh -h

    enable_root_access_via_magisk.sh [-f] [-H|--help] [id1 ... id#]

1|ASUS_I006D:/ #


Verbose Usage:

enable_root_access_via_magisk.sh --help

ASUS_I006D:/ # /data/adb/enable_root_access_via_magisk.sh  --help

 enable_root_access_via_magisk.sh - script to enable root access by adding entries to the Magisk database

 Usage:

    enable_root_access_via_magisk.sh [-f] [-H|--help] [id1 ... id#]

 "id#" are the UIDs of the apps for which root access should be enabled
 If the script is running in the Android OS "id#" can also be the app name.

 The script was tested with these Magisk versions

   Magisk 24.3
   Magisk 25.1
   Magisk 25.2
 
 The format of the database entries for other Magisk versions may be different.
 Therefor to use it for databases created by other Magisk versions use the parameter -f

 The script can run in the Android OS or in an adb shell while booted from TWRP.

 The UIDs must be numeric if running in TWRP; to get the UID for an installed app use the command

   pm list packages -U <app_name>  | cut -f3 -d :

 e.g.

   pm list packages -U com.android.shell  | cut -f3 -d :
 
 while the Android OS is running.

 The script uses the binary sqlite3 if found (either in the PATH or anywhere in /data/adb or /sdcard/Download ).
 If no sqlite3 binary is found the magisk binary will be used.
 If the Magisk binary is available in the PATH, this is used, otherwise the Magisk binary /data/adb/magisk/magisk64 (or /data/adb/magisk/magisk32 for 32-bit CPUSs) is used.

 Environment variables used by the script if set:

   MAGISK_DATA_DIR (Current value: "/data/adb")
   BIN_DIR (Current value: "/system/bin")
   TMPDIR (Current value: "/data/local/tmp")
   SQLITE3 (Current value: "/system/xbin/sqlite3")

1|ASUS_I006D:/ #




enable_root_access_via_magisk.sh example

#
# running enable_root_acces_via_magisk.sh on the phone
#
ASUS_I006D:/ $ su - -c "sh /cache/enable_root_access_via_magisk.sh com.mixplorer com.keramidas.TitaniumBackup io.github.muntashirakon.AppManager com.matoski.adbm com.fox2code.mmm"

Enabling root access for apps by adding entries to the Magisk database

Running in the Android OS
Using the Magisk binary "/system/bin/magisk"
Using the Magisk database "/data/adb/magisk.db"
Checking the prerequisites ...
Checking if the required executables exist ...
OK, the file "/system/bin/magisk" exists and is executable
Checking if the required data files exist ...
OK, the file "/data/adb/magisk.db" exists
Searching the sqlite3 binary in /data/adb/ ...
Searching the sqlite3 binary in /sdcard/Download/ ...
No sqlite3 executable found - using the binary "/system/bin/magisk" to process the Magisk database
Checking if the Magisk daemon is already running ...
25.1:MAGISK:R
OK, the Magisk daemon is already running
Checking the parameter ...
The UID for "com.mixplorer" is "10143"
The UID for "com.keramidas.TitaniumBackup" is "10135"
The UID for "io.github.muntashirakon.AppManager" is "10142"
The UID for "com.matoski.adbm" is "10137"
The UID for "com.fox2code.mmm" is "10141"
OK, all parameter are okay
OK, the installed Magisk version "25.1" is supported
Creating a backup of the existing Magisk database in "/data/adb/magisk.db.4929.bkp" ...

Reading the current entries from the Magisk database ...
Current entries in the Magisk database table "policies":
-
logging=1|notification=1|policy=1|uid=10137|until=0
logging=1|notification=1|policy=2|uid=2000|until=0

Enabling the root access for these app UIDs:  10143 10135 10142 10137 10141

Processing the UID "10143" ...
Enabling root access for the UID "10143" now ...
OK, root access successfully enabled for the UID "10143"

Processing the UID "10135" ...
Enabling root access for the UID "10135" now ...
OK, root access successfully enabled for the UID "10135"

Processing the UID "10142" ...
Enabling root access for the UID "10142" now ...
OK, root access successfully enabled for the UID "10142"

Processing the UID "10137" ...
Enabling root access for the UID "10137" now ...
OK, root access successfully enabled for the UID "10137"

Processing the UID "10141" ...
Enabling root access for the UID "10141" now ...
OK, root access successfully enabled for the UID "10141"

Summary

root access enabled for 5 UID(s):
   10143 10135 10142 10137 10141

Current entries in the Magisk database table "policies":
-
logging=1|notification=1|policy=1|uid=10137|until=0
logging=1|notification=1|policy=2|uid=2000|until=0
logging=1|notification=1|policy=2|uid=10143|until=0
logging=1|notification=1|policy=2|uid=10135|until=0
logging=1|notification=1|policy=2|uid=10142|until=0
logging=1|notification=1|policy=2|uid=10141|until=0

ASUS_I006D:/ $





enable_root_access_via_magisk.sh
can run in the Android OS or in a adb shell in a running TWRP. If running in the Android OS the script needs an already enabled root access for the shell so it can not be used that way to configure the initial root access for the shell.

Therefor I wrote another script to automate enabling the root access for the shell after the initial installation:

init_magisk_db.sh
 
init_magisk_db.sh boots the phone from the TWRP image, copies the script enable_root_access_via_magisk.sh to the phone, and executes enable_root_access_via_magisk.sh then on the phone to enable the initial root access.
This is useful to automatically configure a root enabled shell that then can be used in other scripts to complete the installation and configuration of the phone.

Usage:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./init_magisk_db.sh -h
init_magisk_db.sh version - v1.0.0.0 - shell script to enable root access via Magisk for specified apps


    init_magisk_db.sh [-h|help|-H] [--list|-l] [--reboot|--noreboot] [apps=app1,..,app#]

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$


Verbose usage:

init_magisk_db.sh --help
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./init_magisk_db.sh  --help
init_magisk_db.sh version - v1.0.0.0 - shell script to enable root access via Magisk for specified apps

 Usage
 
    init_magisk_db.sh [-h|help|-H] [--list|-l] [--force|-f] [--reboot|--noreboot] [apps=app1,..,app#]

 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

 If the parameter "--list" is used the script will only print all apps for which root access would be enabled and exit
 
 Use the parameter "--force" if the used Magisk version is not official supported by the script enable_root_access_via_magisk.sh.

 If the parameter "--reboot" is used the script will reboot the phone after enabling the root access; to disable the automatic reboot use the parameter "--noreboot".
 Default is "--noreboot".

 The parameter "apps=app#" can be used to define the apps for which root access should be enabled. apps can be either the "name" of an app or the UID of an app.
 e.g.
 
     apps=com.android.shell,10123,com.keramidas.TitaniumBackup
 
 The value of the parameter "apps=" will overwrite the default list of apps to be root enabled hardcoded in the script; to append the apps to the list of
 hardcoded apps use a leading "+", e.g. apps="+app1,..,app#".
 The parameter can be used more then once; the leading "+" is mandatory in this case for all but the first occurance.

 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 enabling the root access
   Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access

 See also the source code of the script

   boot_phone_from_twrp.sh

 for environment variables supported by this script


 Notes

 Use the Android command
 
   pm list packages
 
 in an adb shell to get the names of the installed apps;
 
 use the Android command
 
   pm list packages -U <appname> | cut -f3 -d ":" 
 
 to get the UID of an app.
 

 Prerequisites

 - a phone with installed Magisk; the script was tested with Magisk v25.2
 - working adb and fastboot binaries on the PC
 - the phone must be connected via USB
 - access via adb must be enabled on the phone

 Details

 If the list of apps for enabling root access contains one or more app names the script will first boot the phone into the normal mode if not already running in that mode
 to get the UIDs for these apps.

 The helper scripts boot_phone_from_twrp.sh and enable_root_access_via_magisk.sh are required -- see the source code of the script

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$  ./boot_phone_from_twrp.sh -h
boot_phone_from_twrp.sh v2.0.0.0 - boot a phone from a TWRP image

    boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [twrp_image]

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$  ./boot_phone_from_twrp.sh --help
boot_phone_from_twrp.sh v2.0.0.0 - boot a phone from a TWRP image

 Usage

    boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [twrp_image]

 All parameter are optional

 Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help

 Use the parameter "serial=#sn" to define the serialnumber of the phone to process. This parameter is only necessary if there
 are more then one phones connected via USB and the environment variable SERIAL_NUMBER is not set.

 The value for the parameter "wait" is the maximum number of seconds to wait after booting from the TWRP image until
 the adb deamon is ready to use. The default value is 10 seconds.

 Use the parameter "force" to reboot the phone from the TWRP image even if it's booted from a TWRP installed in
 the boot or recovery partition.

 Use the parameter "noforce" to disable rebooting the phone from the TWRP image if it's booted from a TWRP installed in
 the boot or recovery partition. This is the default setting.

 Use the parameter "reboot" to reboot the phone from the TWRP image even if it's already booted from an TWRP image.

 Use the parameter "noreboot" to disable rebooting the phone from the TWRP image if it's already booted from an TWRP image
 This is the default setting.

 Use the parameter "checkonly" to only retrieve the current boot mode of the phone; the script will return
 the current state via return code; the possible values are 1 to 5 or one of the return codes indicating an error.

 The parameter "twrp_image" can be used to define another TWRP image to use. The parameter is optional - the
 default for "twrp_image" is the value of the environment variable TWRP_IMAGE; the default for the variable
 TWRP_IMAGE is hardcoded in the script in the variable DEFAULT_TWRP_IMAGE (= the TWRP image for the ASUS Zenfone 8)

 Parameter overwrite the values defined via environment variables.

 Returncodes:

     0 - the phone was successfully booted from the TWRP image

     1 - the phone is already booted from the TWRP image
     2 - the phone is booted from TWRP installed in the boot or recovery partition
     3 - the phone is booted into normal mode
     4 - the phone is booted into bootloader
     5 - the phone is booted into the fastbootd

     9 - too many phones connected
    10 - usage help printed and exited

   100 - invalid parameter found
   101 - TWRP image not found
   102 - booting the phone into the bootloader failed
   103 - booting the phone into the TWRP failed

   252 - access to the phone failed
   253 - requirement check failed (e.g. one or more required executables not found, etc)
   254 - unknown error
   

 The phone to boot must be attached via USB.
 The phone can be either in fastboot mode, in normal mode with enabled adb support, in the boot loader, or already booted from an installed TWRP or an 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 TWRP_IMAGE to the name of the TWRP image file that should be used (the parameter of the script will overwrite the variable)

   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 FAST_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the bootloader (default are 60 seconds)
   Set the environment variable ADB_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the TWRP image (default are 60 seconds)

   Set the environment variable ADB to the adb executable that should be used; default: search adb in the PATH
   Set the environment variable FASTBOOT to the fastboot executable that should be used; default: search fastboot in the PATH
   Set the environment variable TIMEOUT to the timeout executable that should be used; default: search timeout in the PATH


 The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./init_magisk_db.sh -h
init_magisk_db.sh version -  - shell script to enable root access via Magisk for specified apps

    init_magisk_db.sh [-h|help|-H] [--list|-l] [--force|-f] [--reboot|--noreboot] [apps=app1,..,app#]

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$






init_magisk_db.sh example
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./init_magisk_db.sh  apps=com.android.shell
init_magisk_db.sh version -  - shell script to enable root access via Magisk for specified apps

Using the TWRP helper script "./boot_phone_from_twrp.sh"
Using the Magisk helper script "./enable_root_access_via_magisk.sh"
The apps for which root access should be enabled are:
   com.android.shell
Apps that should be enabled are:  com.android.shell
UIDs that should be enabled are:
Found one more app names in the list of apps
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted from the Android OS
Retrieving the UIDs for the app names ...
Retrieving the UID for the app "com.android.shell" ...
The UID for the app "com.android.shell" is "2000"
Will enable the root access via Magisk for these UIDs:  2000
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
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.809s]
Booting                                            OKAY [ 10.360s]
Finished. Total time: 13.215s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready  ..
... the adb daemon is ready after  second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Copying the Magisk helper script "./boot_phone_from_twrp.sh" to  on the phone ...
./enable_root_access_via_magisk.sh: 1 file pushed, 0 skipped. 321.7 MB/s (22461 bytes in 0.000s)
Executing the Magisk helper script "/cache/enable_root_access_via_magisk.sh" on the phone now ...

Enabling root access for apps by adding entries to the Magisk database

OK, running in TWRP
TWRP is booted from an image file
Using the Magisk binary "/data/adb/magisk/magisk64"
Using the Magisk database "/data/adb/magisk.db"
Checking the prerequisites ...
Checking if the required executables exist ...
OK, the file "/data/adb/magisk/magisk64" exists and is executable
Checking if the required data files exist ...
OK, the file "/data/adb/magisk.db" exists
Searching the sqlite3 binary in /data/adb/ ...
Searching the sqlite3 binary in /sdcard/Download/ ...
No sqlite3 executable found - using the binary "/data/adb/magisk/magisk64" to process the Magisk database
Checking if the Magisk daemon is already running ...
No daemon is currently running!
The Magisk daemon is not running -- starting the Magisk Daemon now ...
Checking if the Magisk daemon start was successfull ...
25.1:MAGISK:R
OK, the Magisk daemon is now running
Checking the parameter ...
The parameter "2000" is okay
OK, all parameter are okay
OK, the installed Magisk version "25.1" is supported
Creating a backup of the existing Magisk database in "/data/adb/magisk.db.565.bkp" ...

Reading the current entries from the Magisk database ...
Current entries in the Magisk database table "policies":
-
logging=1|notification=1|policy=1|uid=10137|until=0

Enabling the root access for these app UIDs:  2000

Processing the UID "2000" ...
Enabling root access for the UID "2000" now ...
OK, root access successfully enabled for the UID "2000"

Summary

root access enabled for 1 UID(s):
   2000

Current entries in the Magisk database table "policies":
-
logging=1|notification=1|policy=1|uid=10137|until=0
logging=1|notification=1|policy=2|uid=2000|until=0

root access for the UIDs via Magisk successfully enabled

*** Press return to reboot the phone now

Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$

#
# Test
#
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ adb shell su - -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$



 
Notes:

init_magisk_db.sh needs a working adb access to the phone.

If one or more of the values for the parameter apps= for init_magisk_db.sh are app names init_magisk_db.sh will first boot the phone into the Android OS (if not already booted into the Android OS) to retrieve the UIDs for these apps (for this task no root access is necessarry).

init_magisk_db.sh uses the script boot_phone_from_twrp.sh for booting the phone into the TWRP image (the script is also attached to this post)

This method can also be used to give apps root rights that they do not require ... whether this is useful everyone must decide for themselves.

All three scripts mentioned in this post are also available on my home page:

boot_phone_from_twrp.sh

enable_root_access_via_magisk.sh

init_magisk_db.sh


Note that the Magisk App will silently remove all invalid entries from the table policies in the Magisk database; dito for entries for non-existent UIDs .




Where does the Magisk App store the settings?


URL: https://forum.xda-developers.com/t/where-does-the-magisk-app-store-the-settings.4527129/

Where does the Magisk App store the settings?
The Magisk App uses the standard location for storing the settings of the App - on my phone this is the directory

/data/user_de/0/com.topjohnwu.magisk/shared_prefs

e.g.
ASUS_I006D:/ # ls -l /data/user_de/0/com.topjohnwu.magisk/shared_prefs
total 8
-rw-rw---- 1 u0_a129 u0_a129 163 2022-12-06 12:20 com.topjohnwu.magisk_preferences.xml
-rw-rw---- 1 u0_a129 u0_a129 116 2022-12-04 15:09 su_timeout.xml
ASUS_I006D:/ #
[/CODE]
The files in that directory are standard xml files, e.g.
[CODE]
ASUS_I006D:/ # cat /data/user_de/0/com.topjohnwu.magisk/shared_prefs/com.topjohnwu.magisk_preferences.xml                         
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="update_channel">-1</string>
    <string name="su_auto_response">0</string>
</map>
ASUS_I006D:/ #


The file com.topjohnwu.magisk_preferences.xml is used to store the settings of the Magisk App.

To change one of the settings for the Magisk App via script just change the value for the corresponding entry in the XML file (or create the entry if it's missing).

For example the entry for the setting "Automatic Response" is "su_auto_response" and the known values for this entry are:   Grant = 2, Deny = 1 , Prompt = 0

If the Magisk App is currently running it must be stopped (for example via the Android Settings) to activate the new setting.

To stop the Magisk app via CLI command use

am force-stop  com.topjohnwu.magisk


The known values for this file are listed in the source code for the app

https://github.com/topjohnwu/Magisk/blob/master/app/src/main/java/com/topjohnwu/magisk/core/Config.kt

As of 06.12.2022 for the Magisk App version 25.2 the defined entries and values for the file are:

Known keys and vlaues for the settings xml file
object Key {
        // db configs
        const val ROOT_ACCESS = "root_access"
        const val SU_MULTIUSER_MODE = "multiuser_mode"
        const val SU_MNT_NS = "mnt_ns"
        const val SU_BIOMETRIC = "su_biometric"
        const val ZYGISK = "zygisk"
        const val DENYLIST = "denylist"
        const val SU_MANAGER = "requester"
        const val KEYSTORE = "keystore"

        // prefs
        const val SU_REQUEST_TIMEOUT = "su_request_timeout"
        const val SU_AUTO_RESPONSE = "su_auto_response"
        const val SU_NOTIFICATION = "su_notification"
        const val SU_REAUTH = "su_reauth"
        const val SU_TAPJACK = "su_tapjack"
        const val CHECK_UPDATES = "check_update"
        const val UPDATE_CHANNEL = "update_channel"
        const val CUSTOM_CHANNEL = "custom_channel"
        const val LOCALE = "locale"
        const val DARK_THEME = "dark_theme_extended"
        const val REPO_ORDER = "repo_order"
        const val SHOW_SYSTEM_APP = "show_system"
        const val DOWNLOAD_DIR = "download_dir"
        const val SAFETY = "safety_notice"
        const val THEME_ORDINAL = "theme_ordinal"
        const val BOOT_ID = "boot_id"
        const val ASKED_HOME = "asked_home"
        const val DOH = "doh"
    }

    object Value {
        // Update channels
        const val DEFAULT_CHANNEL = -1
        const val STABLE_CHANNEL = 0
        const val BETA_CHANNEL = 1
        const val CUSTOM_CHANNEL = 2
        const val CANARY_CHANNEL = 3
        const val DEBUG_CHANNEL = 4

        // root access mode
        const val ROOT_ACCESS_DISABLED = 0
        const val ROOT_ACCESS_APPS_ONLY = 1
        const val ROOT_ACCESS_ADB_ONLY = 2
        const val ROOT_ACCESS_APPS_AND_ADB = 3

        // su multiuser
        const val MULTIUSER_MODE_OWNER_ONLY = 0
        const val MULTIUSER_MODE_OWNER_MANAGED = 1
        const val MULTIUSER_MODE_USER = 2

        // su mnt ns
        const val NAMESPACE_MODE_GLOBAL = 0
        const val NAMESPACE_MODE_REQUESTER = 1
        const val NAMESPACE_MODE_ISOLATE = 2

        // su notification
        const val NO_NOTIFICATION = 0
        const val NOTIFICATION_TOAST = 1

        // su auto response
        const val SU_PROMPT = 0
        const val SU_AUTO_DENY = 1
        const val SU_AUTO_ALLOW = 2

        // su timeout
        val TIMEOUT_LIST = intArrayOf(0, -1, 10, 20, 30, 60)

        // repo order
        const val ORDER_NAME = 0
        const val ORDER_DATE = 1
    }


If in doubt what key or value to use just create a backup of the config file; change the approbiate key in the setting in the Magisk App and compare the config file with the just created backup file.





How to install an OS image using the TWRP binary twrp


URL: https://forum.xda-developers.com/t/how-to-install-an-os-image-using-the-twrp-binary-twrp.4531345/

How to install an OS image using the TWRP binary twrp
The most important step to automate the installation and configuration of the Android OS is the installation of an OS image via script without user intervention. One method to do that is using the binary twrp from the recovery TWRP .

The command to install an OS image with twrp is

/system/bin/twrp install /path/to/update.zip


The usage for the binary twrp is (see also here)

twrp usage help
I006D:/ # /system/bin/twrp --help
TWRP openrecoveryscript command line tool, TWRP version 3.7.0_12-0

Allows command line usage of TWRP via openrecoveryscript commands.
Some common commands include:
  install /path/to/update.zip
  backup <SDCRBAEM> [backupname]
  restore <SDCRBAEM> [backupname]
  wipe <partition name>
  format data
  sideload
  set <variable> [value]
  decrypt <password> [USER ID]
  remountrw
  fixperms
  mount <path>
  unmount <path>
  listmounts
  print <value>
  mkdir <directory>
  reboot [recovery|poweroff|bootloader|download|edl]

See more documentation at https://twrp.me/faq/openrecoveryscript.html
I006D:/ #



The binary twrp can also be used to wipe the data from the phone using the parameter wipe <partition_name>.

The command twrp install will not change the active boot slot for the next reboot so this must be done manually after installing the OS image.

This can be done using the binary bootctl which is also part of TWRP.

The usage for the command bootctl is:

bootctl usage help
ASUS_I006D:/ # /system/bin/bootctl -h
/system/bin/bootctl - command-line wrapper for the boot HAL.

Usage:
  /system/bin/bootctl COMMAND

Commands:
  hal-info                       - Show info about boot_control HAL used.
  get-number-slots               - Prints number of slots.
  get-current-slot               - Prints currently running SLOT.
  mark-boot-successful           - Mark current slot as GOOD.
  get-active-boot-slot           - Prints the SLOT to load on next boot.
  set-active-boot-slot SLOT      - On next boot, load and execute SLOT.
  set-slot-as-unbootable SLOT    - Mark SLOT as invalid.
  is-slot-bootable SLOT          - Returns 0 only if SLOT is bootable.
  is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
  get-suffix SLOT                - Prints suffix for SLOT.
  set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
                                   partition is in progress. Valid STAT values
                                   are: none, unknown, snapshotted, merging,
                                   or cancelled.
  get-snapshot-merge-status      - Prints the current snapshot-merge status.

SLOT parameter is the zero-based slot-number.
64|ASUS_I006D:/ #



Afterwards the phone can be rebooted to activate the new installed OS


So the commands for installing an OS image via twrp are

- boot the phone from the TWRP image

- (optional) use twrp to wipe the data

- copy the OS image file via adb push to the phone

- install the OS image file via the twrp binary

- change the next active boot slot via the binary bootctl.

- reboot the phone


I wrote a simple script to just do that :

install_os_via_twrp.sh

The usage of the script is :

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh  -h
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp

    install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]

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



Use the parameter --help or -H to view the detailed usage help

Detailed usage
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh  -H
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp

 Usage
 
    install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]

 All parameter are optional, except the parameter for the OS image to install "os_image_file".
 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

 If the parameter "--reboot" is used the script will reboot the phone after successfully installing the OS image; to disable the automatic reboot use the parameter "--noreboot".
 Default is to ask the user for confirmation to reboot the phone.

 Use the parameter "wipe" or "wipeall" to wipe /data, /cache, and Dalvik before installing the OS image.
 Use one or more of the parameter "wipe_cache", "wipe_data", or "wipe_dalvik" to only wipe some of the partitions used.

 Use the parameter "format_metadata" to format the meta data partition; this wipe is NOT included in "wipe_all"
 Use the parameter "format_data" to format the data partition; this wipe is NOT included in "wipe_all"

 Use the parameter "factory_reset" to do a factory reset before installing the OS image; a factory reset is done by formatting the data and the metadata partitions.

 Use the parameter "force" to ignore errors while wiping or formatting the data; without this parameter the script will abort if one of the wipe or format commands fails

 To change some of the values used by the script these environment variables can be set before starting the script:

   Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
   Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access

   Set the environment variable UPLOAD_DIR_ON_THE_PHONE to set the upload directory for the OS image file on the phone (default dir is /tmp; /tmp is mounted on a ramdiks)

 See also the source code of the script

   boot_phone_from_twrp.sh

 for environment variables supported by this script

 Prerequisites

 - the phone must be connected via USB
 - there must be a working connection to the phone using fastboot and adb
 - a working TWRP image for the phone must exist

 The script boot_phone_from_twrp.sh is required by this script -- see the source code of the script

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





Note:

The script needs the helper script boot_phone_from_twrp.sh 


Sample output of the script installing an new version of the same OS

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ REBOOT=yes /data/develop/android/scripts_on_linux/install_os_via_twrp.sh  /data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp

Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now  ...
Sending 'boot.img' (98304 KB)                      OKAY [  2.833s]
Booting                                            OKAY [ 10.359s]
Finished. Total time: 13.243s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready  ..
... the adb daemon is ready after  second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready 
... the adb daemon is ready after  second(s)

Coyping the OS image file "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip" to "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" on the phone ...
adb: error: failed to read copy response8-MICROG.zip
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 201.8 MB/s (1261116637 bytes in 5.959s)
adb: error: failed to read copy response

adb: device 'M6AIB760D0939LX' not found

Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1261116637 bytes in 37.056s)

Installing the OS image "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" into the slot "_b" ...
Installing zip file '/tmp/omni-13-202212180505-zenfone8-MICROG.zip'
Flashing A/B zip to inactive slot: B
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" successfully installed.
Now changing the next active slot to _b ...
... successfully changed the active slot for the next reboot
Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $







Sample output of the script installing another OS in the inactive slot
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ /data/develop/android/scripts_on_linux/install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp

Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now  ...
Sending 'boot.img' (98304 KB)                      OKAY [  2.840s]
Booting                                            OKAY [ 10.406s]
Finished. Total time: 13.286s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready  ..
... the adb daemon is ready after  second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready 
... the adb daemon is ready after  second(s)

Wiping all data including media ...
Done.
You may need to reboot recovery to be able to use /data again.
TWRP will not recreate /data/media on an FBE device. Please reboot into your rom to create /data/media
Formatting Metadata using mke2fs...
Done.
Done processing script file

Wiping the metadata ...
++ /usr/bin/adb -s M6AIB760D0939LX shell umount /dev/block/by-name/metadata
umount: /dev/block/sda22: Invalid argument
++ /usr/bin/adb -s M6AIB760D0939LX shell mke2fs -F -t ext4 /dev/block/by-name/metadata
mke2fs 1.45.4 (23-Sep-2019)
Discarding device blocks: done                           
Creating filesystem with 4096 4k blocks and 4096 inodes

Allocating group tables: done                           
Writing inode tables: done                           
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

Coyping the OS image file "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip" to "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" on the phone ...
adb: error: failed to read copy responsey-sake-signed.zip
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 313.7 MB/s (1114300349 bytes in 3.387s)
adb: error: failed to read copy response

adb: device 'M6AIB760D0939LX' not found

Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1114300349 bytes in 32.719s)

Installing the OS image "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" into the slot "_a" ...
Installing zip file '/tmp/lineage-19.1-20221216-nightly-sake-signed.zip'
Flashing A/B zip to inactive slot: A
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" successfully installed.
Now changing the next active slot to _a ...
... successfully changed the active slot for the next reboot

*** Press return to reboot the phone now

Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $



Testenvironment

I tested the script install_os_via_twrp.sh using this hardware and software:

Hardware:

ASUS Zenfone 8

TWRP

TWRP 3.7.0.12

OS Images installed via script

OmniROM 12
OmniROM 13
LineageOS 19

The script does not work for the images with the original Android from ASUS; the OS installation works but the phone does not boot from the new installation (I currently do not know why it does not work...).


The script install_os_via_twrp.sh is also available on my home page:

install_os_via_twrp.sh


Trouble Shooting

To update the installed OS to a new version in most cases neither a wipe nor a factory reset is necessary.

To install a different distribution on the phone in most cases a factory reset is required. (e.g installing LIneageOS if the current OS is OmniROM)

xxxx




How to "fix" the error "Cannot load Android system. Your data may be corrupt."


URL: https://forum.xda-developers.com/t/how-to-fix-the-error-cannot-load-android-system-your-data-may-be-corrupt.4534237/

How to fix the error Cannot load Android system. Your data may be corrupt.

If a serious error occurs when booting Android, Android boots the phone from the recovery installed on the phone. The standard Android recovery then aborts the boot process with an error message similar to this one:

Cannot load Android system. Your data may be corrupt.

and the only options to continue are "Try again" or "Factory data reset".


The error message is not really useful and most documentation on the Internet that I found recommends a factory reset to fix the problem. In most cases, factory reset works, but it has the disadvantage that you lose all the settings and data on the phone. So in most cases this is the worst option.
However, if the error is not caused by an invalid or missing file in the partitions for /data or /metadata, resetting to factory defaults will not fix the problem, but the data from the phone will still be gone.

Therefor I wrote this little HowTo.

This HowTo is not about how to fix that error -- it only contains some hints to find the reason for the error.

To get more information about the error and also the possibility to backup the phone's data, TWRP can be used:

Just poweroff the phone, reboot the phone into the bootloader, and then reboot the phone from the TWRP image. E.g.

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


Note:

In most cases, there is no option to turn off the phone in this dialog, so the phone must be turned off using the phone keys.

The key combination to turn off the phone during this dialog depends on the phone; on ASUS Zenfone 8, this key combination must be used:

Press Volume UP + Volume DOWN + Power for 20 or more seconds


After the phone booted into the TWRP image connect via adb to the phone and check the TWRP logfile for a more detailed error message. The logile used by TWRP is /data/recovery/log.gz. Search for the string "Android Rescue Party trigger" in the TWRP logfile.


Note:

/data/recovery/log.gz is a compressed file ; use gzip to uncompress it.


Example:

ASUS_I006D:/ # gzip -cd /data/recovery/log.gz | more
Starting TWRP 3.7.0_12-0-d07fdb3c on Sat Jan 10 00:53:15 1970
 (pid 403)
I:Lun file '/config/usb_gadget/g1/functions/mass_storage.0/lun.0/file'
PRODUCT_USE_DYNAMIC_PARTITIONS := true
TW_INCLUDE_CRYPTO := true
I:TW_BRIGHTNESS_PATH := /proc/lcd_brightness
I:Found brightness file at '/proc/lcd_brightness'
I:TWFunc::Set_Brightness: Setting brightness control to 420
I:TW_EXCLUDE_ENCRYPTED_BACKUPS := true
I:LANG: en
I:AB_OTA_UPDATER := true
Starting the UI...
Skipping adf graphics -- not present in build tree
setting DRM_FORMAT_XBGR8888 and GGL_PIXEL_FORMAT_RGBA_8888
setting DRM_FORMAT_XBGR8888 and GGL_PIXEL_FORMAT_RGBA_8888
....
I:Switching packages (TWRP)
boot command: boot-recovery
I:Startup Commands:

Android Rescue Party trigger! Possible solutions? Either:
 1. Wipe caches, and/or
 2. Format data, and/or
 3. Clean-flash your ROM.
 
The reported problem is:
 '--reason=set_policy_failed:/data/misc'

'--reason=set_policy_failed:/data/misc'
ro.boot.bootreason=shutdown
ro.boot.id.rf=1



If the error message is still not helpful in finding the reason for the error, try a Google search.

And if a factory reset is necessary, you can at least back up your data on the phone before performing the reset.


More infos about Android Rescue Party Trigger

The Android Rescue Party Trigger is an error handling process from Android (see here for more details about this Android functionality).

Android Rescue Party Trigger can also be triggered by Android while the OS is running :

In this case there should be some additional messages in the logfile /data/system/uiderrors.txt.

In addition, and if Magisk is installed, it might be useful to write the logcat messages to a file using the script from this post

https://gist.github.com/niikoo/3f6bd13a69f2d68f3dd51cc667e79bdc


File: /data/adb/post-fs-data.d/0001logcatboot

#!/system/bin/sh
mkdir -p /cache/logs
/system/bin/logcat -r 1024 -n 9 -v threadtime -f /cache/logs/log >info.log 2>err.log &
The script can also be "installed" after the error already occured, to do this:

Boot the phone from the TWRP image; copy the script to the directory /data/adb/post-fs-data.d; make it executable and reboot the phone again from the installed OS to trigger the error again.

Note that logcat is not yet running if the error occurs in the early boot phase.


Caution:

If possible, TWRP will mount /data into the partition used for /data in the Android OS. Therefore, you can view the recovery boot logs in /data/recovery even after restarting the Android operating system as the user root.
However, if mounting the partition for /data in TWRP does not work, /data will be a directory in the root file system, e.g.:

[CODE]
130|ASUS_I006D:/ # df -h /data                                                                                                                                                                                                                                 
Filesystem      Size Used Avail Use% Mounted on
rootfs          3.2G 101M  3.1G   4% /
ASUS_I006D:/ #
[/CODE]

In this case the contents of /data/reocvery are lost after rebooting the phone!



Notes:

The Android kernel uses the device /dev/block/by-name/misc to store the kernel parameter for the recovery kernel. The kernel from the recovery TWRP will clean the device /dev/block/by-name/misc . Therefor the error message is only visible in the logs of the first boot of TWRP.



The active log file for TWRP is /tmp/recovery.log; you can also use that file for the checks. Note that /tmp is mounted on a ramdisk.

On the page https://source.android.com/docs/core/tests/debug/rescue-party are instructions to force an Android Rescue Partry trigger by setting some properties but I did not get that to work neither in the OmniROM 13 nor in the ASUS Android 12.

To avoid the reboot from the TWRP image, install the TWRP recovery on the phone. In this case, Android will automatically boot into the installed TWRP recovery when a fatal error occurs. 



How to enable access via adb on a new installed OS


URL: https://forum.xda-developers.com/t/how-to-enable-access-via-adb-on-a-new-installed-os.4535165/

How to enable access via adb on a new installed OS
For automatic installation and configuration of a phone running the Android OS the access via adb is required. "Unfortunately" the access via adb is disabled in most Android distributions (in reality it is of course good that the access via adb is disabled in the default setting! See also the notes regarding the LineageOS at the end of this post)

There are (at least) two ways to enable access via adb:

Manually via the GUI of Android on the phone or via the Android command settings executed by the user root in a shell ,e.g.:

settings put global development_settings_enabled 1
settings put global adb_enabled 1


The latter can also be done in a script in an adb shell -- but of course we need a working adb connection ....


One way around this problem is to use Magisk:

In principle this could be done using an init script for Magisk:

Magisk allows to run scripts while booting the phone (see How to run a script at every boot using Magisk); this feature could be used to enable adb via script.

Unfortunately, this method no longer works in the latest Android versions (Androd 12 and Android 13; Android 11 not tested):

Android now uses encryption for most of the files in the subdirectories in /data and to initiate the encryption keys for the files in these directories, the phone must boot once from the installed OS.
To use a Magisk init script to enable the access via adb, we need to create files in the directories /data/adb and /data/misc/adb. If you create these files while booting into TWRP recovery without a configured Android operating system, these files will be created unencrypted, making it impossible to boot the installed Android operating system:

If the Android operating system finds unencrpyted files in these directories at the first reboot, it boots into the installed recovery with the famous error message "Cannot load Android system. Your data may be corrupt" (see How to fix the error "Cannot load Android system. Your data may be corrupt" for details)


Fortunately, the Android operating system does not encrypt files in the directory /data/recovery (and therefore continues booting if there are already files in that directory) and the part of Magisk in the boot partition does not need the Magisk files /data/adb.

So to enable the access via adb using Magisk, we can create a script in the directory /data/recovery and a new Init .rc file for the Android operating system to execute that script while the Android OS is booting - for details about how to do that see How to trigger an action when a property is changed.


The working method to enable adb via Magisk is as follows:

- Boot the phone from a recovery (like TWRP)

- Install the new OS image but do not reboot the phone (see How to install an OS image using the TWRP binary twrp for how to install the OS via the cli commands from TWRP)

- Install Magisk into the boot partition of the phone and delete all files in /data/adb afterwards (see How to install Magisk into the boot partition using a script for how to install Magisk via script)

- copy a script to the directory /data/recovery on the phone that will enable access via adb using the Android command settings and copy the public ssl keys for the access via adb to the directory /data/misc/adb on the phone

- install a new init .rc file via Magisk which executes the script in the direcory /data/recovery to enable the access via adb - this script will be executed while doing the 1st reboot of the new installed Android OS.
 (see How to trigger an action when a property is changed for how to add a new init .rc file using Magisk)

- reboot the phone


I wrote two scripts to enable adb using this method:


The script enable_adb_using_magisk.sh must run in an adb session in TWRP after the new OS was installed and after Magisk has been installed into the boot partition but before the first reboot of the new installed operating system.


The script enable_adb_using_magisk.sh

  - creates the script /data/recover/enable_adb_via_service.sh on the phone to enable adb and install the public ssl key for access via adb

  - creates a new init *rc file using Magisk that will run the script /data/recover/enable_adb_via_service.sh on the phone while doing the 1st reboot (after the file encryption for the files /data is initalized by the Android OS)


To use the script enable_adb_using_magisk.sh manually after installing the new OS and Magisk do while the phone is booted into TWRP:

#
# copy the script enable_adb_using_magisk.sh to the directory /tmp on the phone
#
adb push enable_adb_using_magisk.sh /tmp/enable_adb_using_magisk.sh

# copy the public ssl key for access via adb to the directory /tmp on the phone
#
adb push $HOME/.android/adbkey.pub /tmp/adbkey.pub

# execute the script /tmp/enable_adb_using_magisk.sh on the phone
#
adb shell sh /tmp/enable_adb_using_magisk.sh

# reboot the phone into the new installed OS
#
adb reboot



Notes:


The script enable_adb_using_magisk.sh installs the init *rc file into the boot partition from the slot for the next reboot: In this scenario this is the slot with the new installed OS.

To specify the boot partition to use add the boot partition as parameter, e.g.

adb shell sh /tmp/enable_adb_using_magisk.sh _a


The init *rc service used is:

init *rc service to enable adb
#
# additional RC service to enable the access via adb after the 1st boot into the new installed Android OS
#
service bnsmb_enable_adb /system/bin/sh /data/recovery/enable_adb_via_service.sh
    user root
    group root
    seclabel u:r:magisk:s0
    disabled
    oneshot

on zygote-start
    setprop sys.bnsmb_enable_adb_done 0
    start bnsmb_enable_adb

#
# Note: the following entries are for testing only!
#
on zygote-start
   write /data/recovery/semfile Here_I_am
   setprop sys.bnsmb.test.okay 0



The script enable_adb_using_magisk.sh creates the files adb_keys and adb_temp_keys.xml in the directory /data/misc/adb if the public ssl key /tmp/adbkey.pub exists on the phone.

Note that the file /data/misc/adb/db_temp_keys.xml is an Android Binary XML fIle (see How to change files in Android XML Binary format for details about Android Binary XML files)



To avoid the manual steps listed above I wrote another script:

enable_access_via_adb.sh

The script enable_access_via_adb.sh does all the steps listed above; the usage for enable_access_via_adb.sh is

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -h
enable_access_via_adb.sh version - v1.0.0.0 -  shell script to enable adb access via an init .rc file configured via Magisk

    enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]

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



Detailed usage
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -H
enable_access_via_adb.sh version - v1.0.0.0 -  shell script to enable adb access via an init .rc file configured via Magisk

 Usage
 
    enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]

 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

 If the parameter "--reboot" is used the script will reboot the phone after installing Init .rc file; to disable the automatic reboot use the parameter "--noreboot".
 Default is to ask the user for confirmation to reboot the phone.

 Use the parameter "--nopubkey" to disable configuring the public key of the current user on the PC for the access via adb; use the parameter "--pubkey" to configure
 the public key; default is to configure the public key.
 The default public ssl key used is the key in the file "${HOME}/.android/adbkey.pub".

 To change some of the values used by the script these environment variables can be set before starting the script:

   Set the environment variable PUBLIC_KEY_ON_PC to the file with the public ssl key to use for the access via adb if another public key should be used

   Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
   Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access

 See also the source code of the script

   boot_phone_from_twrp.sh

 for environment variables supported by this script


 Prerequisites

 - the phone must be connected via USB
 - Magisk must be already installed in the boot partition of the phone
 - the phone must be either booted into the fastbootd or bootloader with a working fastboot connection
   or already booted into a recovery image with working adb connnection


 The scripts boot_phone_from_twrp.sh and enable_adb_using_magisk.sh are required by this script -- see the source code of the script

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





So installing a new OS and enabling access via adb in a script without user intervention can be done using these commands:

#
# do a factory reset and install the OS image using the TRWP binary twrp
#
REBOOT=no  ./install_os_via_twrp.sh factory_reset  /data/backup/ASUS_ZENFONE8/omnirom_local/omni-13/omni-13-20221211-zenfone8-MICROG.zip 


# install Magisk into the boot sector of the partition with the new OS and delete all files in /data/adb afterwards
#
# The parameter delete_adb_dir for the script "install_magisk_via_twrp.sh" is only available in version 2.2.0.0 or newer of the script.
#
REBOOT=no  ./install_magisk_via_twrp.sh next  copy_apk  delete_adb_dir


# enable adb and copy the public keys for adb via new init *rc file using Magisk in the boot partition
#
REBOOT=no ./enable_access_via_adb.sh


# reboot the phone
#
adb reboot


# wait until the new OS is booted and adb is enabled
#
adb wait-for-device


# install the Magisk App
#
./install_apk.sh /data/backup/Android/EssentialApps/Magisk-v25.2.apk


# install the Magisk directories and binaries in /data/adb
#
REBOOT=no  ./install_magisk_via_twrp.sh adb_only


# (optional) enable root access for the adb shell
#
REBOOT=yes ./init_magisk_db.sh



Notes:


The script enable_access_via_adb.sh needs the helper script boot_phone_from_twrp.sh and the script enable_adb_using_magisk.sh .

The scripts can be downloaded from my homepage:

enable_access_via_adb.sh

enable_adb_using_magisk.sh

boot_phone_from_twrp.sh


The other scripts mentioned in ths post can also be downloaded from my home page:

install_os_via_twrp.sh (see How to install an OS image using the TWRP binary twrp for a descripton of this script)

install_magisk_via_twrp.sh (see How to install Magisk into the boot partition using a script for a description for this script)

install_apk.sh (see How to install packages (apk files) for Android via script for a description for this script)

init_magisk_db.sh (see How to enable root access using Magisk in a script for a description for this script)



Disabling the service to enable access via adb

To only run once, the script enable_adb_using_magisk.sh creates the file /data/recovery/adb_initialized exists on the phone. If that file already exists when enable_adb_using_magisk.sh starts, the script will do nothing and end immediately.
Therefor, it's not necessary to delete the new init *rc service.

It might even be useful to leave the service installed, since it can be used for other purposes later by modifying the script

However, if necessary, you can either delete the script used by the init *rc service enable_adb_using_magisk.sh or reinstall the original boot partition to get rid of the new service completely:

#
# while the phone is booted into the Android OS with enabled root access or while the phone is booted into TWRP
#

# use the parameter "yes" for the script to run without requesting user input
#
adb shell su - -c /data/recovery/work/restore_boot_partition.sh



Directories and files used

Name
Type
Content
Comment
/tmp/adbkey.pub
file
ssl public key that should be configured
/tmp is on a ramdisk so this file is lost after booting the phone




/data/recovery/work
Directory
temporary files used for enabling adb

/data/recovery/work/enable_adb_via_service.rc
file
init *rc file added to the boot partition via Magiks

/data/recovery/work/boot_a.img
file
image of the original boot partition

/data/recovery/work/original_boot_partition.img
SymLink
link to the image with the original boot partition

/data/recovery/work/restore_boot_partition.sh file
Script to restore the boot partition using the image of the original boot partition

/data/recovery/work/header
/data/recovery/work/kernel
/data/recovery/work/ramdisk.cpio
files
contents of the boot partition





/data/recovery/adbkey.pub
file
public ssl key to be configured for access via adb

/data/recovery/adb_temp_keys.xml.human
file
XML file with the configuration for the access via adb
(will be converted to an XML file in Android binary XML format in /data/misc/adb)

/data/recovery/enable_adb_via_service.sh
file
script to enable the access via adb and installl the public ssl key for the access via adb
The script is configured in the init*rc file /data/recovery/work/enable_adb_via_service.rc

/data/recovery/enable_adb_via_service.log
file
log file created by the script /data/recovery/enable_adb_via_service.sh
/data/recovery/adb_initialized
file
semphor file for the script /data/recovery/enable_adb_via_service.sh - if this file exists the script will do nothing

/data/recovery/semfile
file
test file created by the new init *rc service





/data/recovery/log.gz
/data/recovery/last_log.gz
/data/recovery/recovery.fstab
/data/recovery/storage.fstab
file
Files created by TWRP
only for information




Enabling access via adb in the LineageOS (Update 11.01.2023)


In the LineageOS 19 for the ASUS Zenfone 8 access via adb is enabled in the default configuration. Therefor these instructions are not necessary. The only configuration that must be done on a phone running the LineageOS is to copy the public ssl keys to /data/misc/adb to enable the access via adb. Unfortunately the method used for this described in this post seems not to work in the LineageOS.
In addition, root access via adb can be enabled in the developer settings in the LineageOS. Until now I did not find out how to enable the plain root access from the LineageOS from within a script.



Trouble Shooting

The script enable_adb_using_magisk.sh uses the log file /data/recovery/enable_adb_via_service.log if executed via init *rc service.

To check if the init *rc file was configured successfully check the properties

sys.bnsmb_enable_adb_done
sys.bnsmb.test.okay

in the running Android OS. Both properties should be defined with the value 0:

ASUS_I006D:/ # getprop sys.bnsmb_enable_adb_done
0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop sys.bnsmb.test.okay
0
ASUS_I006D:/ #


In addition, if everything worked there should exist the file /data/recovery/semfile.

ASUS_I006D:/ # ls -l /data/recovery/semfile
-rw------- 1 root root 9 2022-12-29 11:44 /data/recovery/semfile
ASUS_I006D:/ #


Use the Android command start to check if the service exists and can be started, e.g.

start bnsmb_enable_adb
ASUS_I006D:/ # start bnsmb_enable_adb ; dmesg | tail -10
[ 3069.861365] [ 3069.861365] (CPU:2-pid:1:init) [12:35:31.711593934] init: Service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) exited with status 0 waiting took 0.024000 seconds
[ 3069.861402] [ 3069.861402] (CPU:2-pid:1:init) [12:35:31.711630236] init: Sending signal 9 to service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) process group...
[ 3069.861618] [ 3069.861618] (CPU:2-pid:1:init) [12:35:31.711846069] libprocessgroup: Successfully killed process cgroup uid 1000 pid 5734 in 0ms
[ 3071.202534] [ 3071.202486] (CPU:2-pid:5077:wk:0xffffffe40) [12:35:33.052716433] [BAT][CHG]asus_jeita_cc_worker set BATTMAN_OEM_WORK_EVENT : WORK_JEITA_CC
[ 3071.220807] [ 3071.220807] (CPU:0-pid:350:wk:0xffffffe40) [12:35:33.071038100] [BAT][CHG]handle_message set property:16 successfully
[ 3071.570753] [ 3071.570753] (CPU:2-pid:1:init) [12:35:33.420982631] init: starting service 'bnsmb_enable_adb'...
[ 3071.575971] [ 3071.575971] (CPU:3-pid:1:init) [12:35:33.426199819] init: Control message: Processed ctl.start for 'bnsmb_enable_adb' from pid: 5735 (start bnsmb_enable_adb)
[ 3071.614570] [ 3071.614570] (CPU:3-pid:1:init) [12:35:33.464799298] init: Service 'bnsmb_enable_adb' (pid 5736) exited with status 0 oneshot service took 0.040000 seconds in background
[ 3071.614609] [ 3071.614609] (CPU:3-pid:1:init) [12:35:33.464837267] init: Sending signal 9 to service 'bnsmb_enable_adb' (pid 5736) process group...

[ 3071.614788] [ 3071.614788] (CPU:3-pid:1:init) [12:35:33.465016277] libprocessgroup: Successfully killed process cgroup uid 0 pid 5736 in 0ms
ASUS_I006D:/ #
 


If something does not work execute the script manually in an adb session to check :

/data/recovery/enable_adb_via_service.sh
ASUS_I006D:/ # rm -f /data/recovery/adb_initialized
ASUS_I006D:/ #
ASUS_I006D:/ #
ASUS_I006D:/ # sh -x /data/recovery/enable_adb_via_service.sh
+ tty -s
+ '[' 0 -ne 0 ']'
+ PATH=/system/bin:/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin
+ export PATH
+ '[' ! -r /data/recovery/adb_initialized ']'
+ echo 'Sleeping 30 seconds now ...'
Sleeping 30 seconds now ...
+ sleep 30
+ touch /data/recovery/adb_initialized
+ settings put global development_settings_enabled 1
+ settings put global adb_enabled 1
+ '[' ! -d /data/misc/adb ']'
+ '[' ! -r /data/misc/adb/adb_keys ']'
+ '[' -r /data/recovery/adbkey.pub ']'
+ >>/data/misc/adb/adb_keys
+ cat /data/recovery/adbkey.pub
+ '[' -r /data/recovery/adb_temp_keys.xml.human ']'
+ xml2abx /data/recovery/adb_temp_keys.xml.human /data/misc/adb/adb_temp_keys.xml
+ chmod 0600 /data/misc/adb/adb_temp_keys.xml
+ chown system:shell /data/misc/adb/adb_temp_keys.xml
+ chcon -v u:object_r:adb_keys_file:s0 /data/misc/adb/adb_temp_keys.xml
chcon '/data/misc/adb/adb_temp_keys.xml' to u:object_r:adb_keys_file:s0
ASUS_I006D:/ #











Some hints about the Android safe mode


URL: https://forum.xda-developers.com/t/some-hints-about-the-android-safe-mode.4536269/

Some hints about the Android safe mode
Safe mode is a feature in Android phones where all third-party apps on the device become inaccessible when enabled.

Doing a Google search on Android safe mode, I found a lot of information that is outdated and no longer applies to the current Android version.

So this small HowTo contains some infos about using the Android safe mode in Android 13 ; the infos should also be valid for Android 12 but I did not test that yet.


How to check if the phone is booted into safe mode in the Android GU

In safe mode Android prints the string "Safe mode" in the lower left corner


How to check if the phone is booted into safe mode in a shell as root user

The root user on the phone can check the value of the property persist.sys.safemode; this property is set to 1 in safe mode and does not exist if not in safe mode

in safe mode:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #

ASUS_I006D:/ # getprop persist.sys.safemode
1
ASUS_I006D:/ #

in normal mode:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/ #  getprop persist.sys.safemode
ASUS_I006D:/ #


How to check if the phone is booted into safe mode in a shell as non-root user

Non-root user can not read the property persist.sys.safemode. Non-root user can use the output of the command dumpsys to check if the phone is booted into safe mode using this command:

dumpsys display | grep mSafeMode

e.g.:

in safe mode:

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $
ASUS_I006D:/ $  dumpsys display | grep mSafeMode
  mSafeMode=true
ASUS_I006D:/ $
in normal mode;

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $
ASUS_I006D:/ $ dumpsys display | grep mSafeMode
  mSafeMode=false
ASUS_I006D:/ $


How to enable the safe mode for the next reboot in the Android GUI

To enable safe mode via the Android GUI do :

Presse the power button until the reboot dialog with Power Off, Restart, etc appears. Now long tap on the PowerOff button in that dialog until the dialog to reboot into safe mode appears and press ok in that dialog. The phone will then be booted into safe mode.


How to enable the safe mode for the next reboot via shell command

To enable safe mode for the next reboot of the Android OS when in the running Android OS open a shell (either local or via adb), become root user and, issue

setprop persist.sys.safemode 1

reboot


Note that Android will automatically clear this property after booting into the normal mode again


How to enable the safe mode for the next reboot when booted into the recovery

The current value for the property persist.sys.safemode is stored in the file

/data/property/persistent_properties

e.g:

ASUS_I006D:/ # grep safe /data/property/persistent_properties
persist.sys.safemode
ASUS_I006D:/ #


Unfortunately this is a binary file with an unknown format:

ASUS_I006D:/ # file /data/property/persistent_properties
/data/property/persistent_properties: data
ASUS_I006D:/ #

So it's not clear how to enable safe mode for the next reboot while booted in to recovery.


A very ugly workaround to enable safe mode for the next reboot while the phone is booted from the recovery can be implemented using a Magisk init script (if Magisk is installed on the phohe, of course - see How to run a script at every boot using Magisk for details):

ASUS_I006D:/ # cat /data/adb/post-fs-data.d/enable_safe_mode
/data/adb/magisk/magisk64 resetprop -n persist.sys.safemode 1
#
# make sure booting into safe mode is only a onetime job!
#
rm /data/adb/post-fs-data.d/enable_safe_mode
ASUS_I006D:/ #


Notes:

Using this method the propery persist.sys.safemode is not set after booting into safe mode and the safe mode can only be checked using the output of the dumpsys command, e.g.:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop persist.sys.safemode

ASUS_I006D:/ #
ASUS_I006D:/ # dumpsys display|grep mSafeMode
  mSafeMode=true
ASUS_I006D:/ #


Be aware that without deleting the Magisk init script for the workaround the phone will always boot into safe mode.


How to exit the safe mode

To exit the safe mode Just reboot the phone






How to install and configure the Android OS via Script


URL: https://forum.xda-developers.com/t/how-to-install-and-configure-the-android-os-via-script.4546375/


How to install and configure the Android OS via Script


Update 18.04.2023/bs

see How to install Magisk v26.0 or newer via script for how to install Magisk v26 via script


Note:


The instructions below work in my environment - they should work for others after adjusting the configuration. But of course I can't guarantee that.


After creating all the other scripts to install and configure the Android OS without user intervention I finally wrote a script to do all the steps necessary to install and configure the Android OS using these scripts.

For this purpose I used another script that I wrote some time ago and use also for a lot of other things: execute_tasks.sh

execute_tasks.sh reads a list of defined tasks from one or more include files and executes one or more of these tasks one after the other. One or more parameter for the tasks are supported. It's also possible to define task groups with one or more tasks that can be used like single tasks.

The usage for execute_tasks.sh is :

./execute_tasks.sh -h
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/scripts ] $ ./execute_tasks.sh -h
[17.01.2023 18:42 ] ### execute_tasks.sh started at 17.01.2023 18:42:44 (The PID of this process is 655162)
[17.01.2023 18:42 ] ### Processing the parameter ...
[17.01.2023 18:42 ] Tasks to execute are: ""
[17.01.2023 18:42 ] Parameter for the function init_tasks are: ""
 Usage:    execute_tasks.sh [-v|--verbose] [-q|--quiet] [-f|--force] [-o|--overwrite] [-y|--yes] [-n|--no] [-l|--logfile filename]
               [-d{:dryrun_prefix}|--dryrun{:dryrun_prefix}] [-D|--debugshell] [-t fn|--tracefunc fn] [-L]
               [-T|--tee] [-V|--version] [--var name=value] [--appendlog] [--nologrotate] [--noSTDOUTlog] [--disable_tty_check] [--nobackups]
               [--print_task_template [filename]] [--create_include_file_template [filename]] [--list [taskmask]] [--list_tasks [taskmask]]
               [--list_task_groups [groupmask]] [--list_default_tasks] [--abort_on_error] [--abort_on_task_not_found] [--abort_on_duplicates]
               [--checkonly] [--check] [--singlestep] [--unique] [--trace] [--info] [--print_includefile_help] [-i|--includefile [?]filename]
               [--no_init_tasks[ [--no_finish_tasks] [--only_list_tasks] [--disabled_tasks task1[...,task#]] [--list_disabled_tasks] [--enable_all_tasks]
               [task1] [... task#] [-- parameter_for_init_tasks]

Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes

[17.01.2023 18:42 ] ### The logfile used was /var/tmp/execute_tasks.sh.log
[17.01.2023 18:42 ] ### The start time was 17.01.2023 18:42:44, the script runtime is (day:hour:minute:seconds) 0:00:00:00 (= 0 seconds)
[17.01.2023 18:42 ] ### execute_tasks.sh ended at 17.01.2023 18:42:44 (The PID of this process is 655162; the RC is 0)
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/scripts ] $


To get a more detailed usage description either download the script and execute it with the parameter -H or use this link .

The include file with the tasks to install and configure the Android OS, prepare_phone.include, can be downloaded here.


To use that file either use

./execute_tasks.sh -i prepare_phone.include [task1 ... task#]

or create the symbolic link prepare_phone.sh for execute_tasks.sh :

ln -s ./execute_tasks.sh ./prepare_phone.sh

and use it like this

./prepare_phone.sh [task1 ... task#]

Note:

./prepare_phone.sh and prepare_phone.include must be in the same directory. In the rest of this post I use this method.


To list all defined tasks in the include file prepare_phone.include use the parameter --list, e.g:

./prepare_phone.sh --list -v

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android ] $ ./scripts_on_linux/prepare_phone.sh --list -v
[27.01.2023 12:25 ] ### prepare_phone.sh started at 27.01.2023 12:25:09 (The PID of this process is 1024201)
[27.01.2023 12:25 ] ### Processing the parameter ...
[27.01.2023 12:25 ] Tasks to execute are: ""
[27.01.2023 12:25 ] Parameter for the function init_tasks are: ""
[27.01.2023 12:25 ] ### The logfile used is /var/tmp/prepare_phone.sh.log
[27.01.2023 12:25 ] INFO: Searching the default include file "prepare_phone.include" ...
[27.01.2023 12:25 ] INFO: "/data/develop/android/scripts_on_linux/prepare_phone.include" found.

[27.01.2023 12:25 ] INFO: Checking and reading the include file(s) "/data/develop/android/scripts_on_linux/prepare_phone.include " now ...

[27.01.2023 12:25 ] Checking the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" ...
[27.01.2023 12:25 ] INFO: The version of the include file "/data/develop/android/scripts_on_linux/prepare_phone.include", "1.0.0.0", is okay.
[27.01.2023 12:25 ] INFO: Function "init_tasks" found in the include file "/data/develop/android/scripts_on_linux/prepare_phone.include"
[27.01.2023 12:25 ] INFO: Function "finish_tasks" found in the include file "/data/develop/android/scripts_on_linux/prepare_phone.include"
[27.01.2023 12:25 ] Reading the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" ...
[27.01.2023 12:25 ] INFO: Found this list of tasks to be excluded from  "all" in the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" :
[27.01.2023 12:25 ] INFO: Found this list of tasks for "all" in the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" :
[27.01.2023 12:25 ] INFO:   enable_disable_abort_on_error:enable
[27.01.2023 12:25 ] INFO:   boot_phone_from_twrp_image
[27.01.2023 12:25 ] INFO:   install_os:noreboot:factory_reset
[27.01.2023 12:25 ] INFO:   install_twrp:noreboot:next
[27.01.2023 12:25 ] INFO:   install_magisk_in_boot_partition_only:noreboot:next
[27.01.2023 12:25 ] INFO:   enable_adb:reboot
[27.01.2023 12:25 ] INFO:   #
[27.01.2023 12:25 ] INFO:   install_magisk_in_data_adb_only:noreboot:copy_apk
[27.01.2023 12:25 ] INFO:   enable_root_access_for_the_shell:reboot
[27.01.2023 12:25 ] INFO:   wait_for_access_via_adb:60:kill
[27.01.2023 12:25 ] INFO:   install_the_magisk_app
[27.01.2023 12:25 ] INFO:   enable_disable_abort_on_error:disable
[27.01.2023 12:25 ] INFO:   install_essential_scripts
[27.01.2023 12:25 ] INFO:   install_essential_apps
[27.01.2023 12:25 ] INFO:   install_essential_magisk_modules
[27.01.2023 12:25 ] INFO:   enable_root_access_for_apps:reboot
[27.01.2023 12:25 ] INFO:   execute_script_on_the_phone:ignorerc

Include files used are:

/data/develop/android/scripts_on_linux/prepare_phone.include

Tasks defined are:

Task: boot_phone_from_twrp_image                Usage: boot_phone_from_twrp_image [ignorerc] [twrp_image] parameter_for_the_script]
Task: boot_phone_into_android                   Usage: boot_phone_into_android [ignorerc] [force]
Task: boot_phone_into_bootloader                Usage: boot_phone_into_bootloader [ignorerc] 
Task: boot_phone_into_fastboot                  Usage: boot_phone_into_fastboot [ignorerc] 
Task: boot_phone_into_recovery                  Usage: boot_phone_into_recovery [ignorerc] 
Task: boot_phone_into_safemode                  Usage: boot_phone_into_safemode [ignorerc] [timeout=timeout_in_seconds] # Default: 30 seconds; Note: The phone must be booted into the AndroidOS for this task
Task: check_config                              Usage: check_config
Task: download_scripts                          Usage: download_scripts [target_dir|default|pwd] [backup|nobackup]  [ignorerc] # default: download the scripts to the current directory
Task: enable_adb                                Usage: enable_adb [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [parameter_for_the_script]  # default is: no reboot
Task: enable_disable_abort_on_error             Usage: enable_disable_abort_on_error [yes|no] # default is yes
Task: enable_root_access_for_apps               Usage: enable_root_access_for_apps [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [parameter_for_the_script]  # default is: no reboot
Task: enable_root_access_for_the_shell          Usage: enable_root_access_for_the_shell [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [parameter_for_the_script]  # default is: no reboot
Task: execute_script_on_the_phone               Usage: execute_script_on_the_phone [ignorerc] [script_to_execute] # default: post_install.sh in the current directory
Task: factory_reset_via_twrp                    Usage: factory_reset_via_twrp [reboot|noreboot|reboot=no|reboot=yes] [twrp_imagefile] [ignorerc] [force|noforce]
Task: install_apps                              Usage: install_apps [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [apkfile1 ... apkfile#]  # default is: no reboot
Task: install_essential_apps                    Usage: install_essential_apps [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [dir_with_the_apk_files_to_install]  # default is: no reboot
Task: install_essential_magisk_modules          Usage: install_essential_magisk_modules [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [dir_with_the_magisk_modules_to_install]  # default is: no reboot
Task: install_essential_scripts                 Usage: install_essential_scripts [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [dir_with_the_scripts_to_install]  # default is: no reboot
Task: install_magisk_in_boot_partition          Usage: install_magisk_in_boot_partition [slot] [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [magisk_apkfile] [parameter_for_the_script]  # default is: reboot
Task: install_magisk_in_boot_partition_only     Usage: install_magisk_in_boot_partition_only [slot] [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [magisk_apkfile] [parameter_for_the_script]  # default is: no reboot
Task: install_magisk_in_data_adb_only           Usage: install_magisk_in_data_adb_only [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [magisk_apkfile] [parameter_for_the_script]  # default is: no reboot
Task: install_magisk_modules                    Usage: install_magisk_modules [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [magisk_module1 ... magisk_module#]  # default is: no reboot
Task: install_os                                Usage: install_os [slot] [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [reset] [imagefile] [parameter_for_the_script] # default is: no reboot
Task: install_the_magisk_app                    Usage: install_the_magisk_app [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [magisk_apk_file]  # default is: no reboot
Task: install_twrp                              Usage: install_twrp [slot] [reboot|noreboot|reboot=no|reboot=yes] [ignorerc] [twrp_imagefile] [parameter_for_the_script]  # default is: no reboot
Task: kill_adb_daemon                           Usage: kill_adb_daemon [reboot|noreboot|reboot=no|reboot=yes] [ignorerc]
Task: restore_titanium_backup                   Usage: restore_titanium_backup [license=license_file] [zip=zipfile] [app=titanium_app] [ignorerc]
Task: set_os_image_to_install                   Usage: set_os_image_to_install [os_image] # there is no default value
Task: update_include_file                       Usage: update_include_file [target_dir|target_file] [ignorerc] [keep|nokeep] # default: nokeep (= delete temporary files)
Task: wait_for_access_via_adb                   Usage: wait_for_access_via_adb [ignorerc] [kill] [[timeout=]timeout_in_seconds] # default: 30 seconds
Task: wait_for_phone_state                      Usage: wait_for_phone_state [ignorerc] [state|state=#] [timeout=timeout_in_seconds] # default: 3 30 ; use 0 for infinite; set TIMEOUT_VALUE to define the timeout value via env variable
Task: wait_n_seconds                            Usage: wait_n_seconds [wait_time] [step_count] # default for wait_time is 60 and default for step count is 5
Task: wipe_dalvik_and_cache_via_twrp            Usage: wipe_dalvik_and_cache_via_twrp [reboot|noreboot|reboot=no|reboot=yes] [twrp_imagefile] [ignorerc] [force|noforce]

33 task(s) defined.

Defined Task groups are:

abort_on_error :
    task_enable_disable_abort_on_error:enable

install_magisk :
    install_the_magisk_app
    install_magisk_in_boot_partition:current
    wait_for_access_via_adb:60:kill
    enable_root_access_for_the_shell:reboot
    wait_for_access_via_adb:60:kill
    check_root_access
    install_essential_magisk_modules:reboot

no_abort_on_error :
    task_enable_disable_abort_on_error:disable

refresh :
    task_update_include_file

refresh_all :
    download_scripts:default
    task_update_include_file

update_os :
    enable_disable_abort_on_error:enable
    boot_phone_from_twrp_image
    install_os:noreboot
    install_twrp:noreboot:next
    install_magisk_in_boot_partition:reboot:next

wait_for_androidos :
    task_wait_for_phone_state:3

wait_for_bootloader :
    task_wait_for_phone_state:4

wait_for_fastboot :
    task_wait_for_phone_state:5

wait_for_lineageos_recovery :
    task_wait_for_phone_state:7

wait_for_safemode :
    task_wait_for_phone_state:6

wait_for_sideload :
    task_wait_for_phone_state:8

wait_for_twrp_image :
    task_wait_for_phone_state:1

wait_for_twrp_recovery :
    task_wait_for_phone_state:2

Note: use ":" to separate the task name and the parameter

[27.01.2023 12:25 ] ### The logfile used was /var/tmp/prepare_phone.sh.log
[27.01.2023 12:25 ] ### The start time was 27.01.2023 12:25:09, the script runtime is (day:hour:minute:seconds) 0:00:00:00 (= 0 seconds)
[27.01.2023 12:25 ] ### prepare_phone.sh ended at 27.01.2023 12:25:09 (The PID of this process is 1024201; the RC is 0)
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android ] $



The tasks in the include files use the other scripts I wrote to install and configure the Android OS. Therefor you should first download the current version of these scripts either manual (see below) or using the task download_scripts:

./prepare_phone.sh download_scripts

The task download_scripts will download all necessary scripts from https://bnsmb.de/files/public/Android to the current directory; to download the scripts to another directory add the target directory as task parameter, e.g. to download the scripts into the directory
/var/tmp/scripts use:

./prepare_phone.sh download_scripts:/var/tmp/scripts


Next either edit the default values for the various variables in the file prepare_phone.include or, better, create the config file prepare_phone.conf with the necessary config entries like this:

cat ./prepare.conf
# ---------------------------------------------------------------------
#
# config file for prepare_phone.include
#
# This file can be used to define the global variables used by the tasks in the file prepare_phone.include
#
# The format of the entries in this file is
#
# <varname>="<var_value>"
#
# empty lines and lines starting with a hash "#" will be ignored
#
# use "ksh -x -n prepare_phone.conf" to check this file for syntax errors
#

# ---------------------------------------------------------------------
# global variables used in prepare_phone.include
#
SCRIPT_DIR="/data/develop/android/scripts_on_linux"

#OS_IMAGE_TO_INSTALL="/data/develop/android/OmniROM_13.0/out/target/product/zenfone8/omni-13-20230115-zenfone8-MICROG.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/omnirom_local/omni-12/omni-12-20221103-zenfone8-MICROG.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/Lineage-19_Android12_with_MicroG/lineage-19.1-20221222-microG-sake.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20230414-nightly-sake-signed.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-20230430-zenfone8-MICROG.zip"
OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/omnirom_local/omni-13/omni-13-20230430-zenfone8-MICROG.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202304151702-zenfone8-MICROG.zip"
#OS_IMAGE_TO_INSTALL="/data/develop/android/OmniROM_13.0/out/target/product/zenfone8/omni-13-20230412-zenfone8-MICROG.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/ASUS_firmware/Android_13/UL-ASUS_I006D-ASUS-33.0210.0210.269-1.1.26-2303-user.zip"
#OS_IMAGE_TO_INSTALL="/data/backup/ASUS_ZENFONE8/omnirom/omni-12/omni-12-20220814-zenfone8-MICROG.zip"

TWRP_IMAGE="/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img"

MAGISK_v26_APK_FILE="/data/backup/Android/EssentialApps_Backup/Magisk-v26.1.apk"
MAGISK_v25_APK_FILE="/data/backup/Android/EssentialApps_Backup/Magisk-v25.2.apk"

MAGISK_APK_FILE="/data/backup/Android/EssentialApps_Backup/Magisk-v25.2.apk"

# use "none" or an empty directory to disable installing essential apps
#
ESSENTIAL_APPS_DIR="/data/backup/Android/EssentialApps"

# use "none" or an empty directory to disable installing the scripts
#
ESSENTIAL_SCRIPTS_DIR="/data/backup/Android/EssentialScripts/"

# use "none" or an empty directory to disable install the Magisk Modules
#
ESSENTIAL_MAGISK_MODULES_DIR="/data/backup/Android/EssentialMagiskModules"

UPLOAD_DIR_FOR_SCRIPTS_ON_THE_PHONE="/sdcard/Download/scripts"

UPLOAD_DIR_FOR_MAGISK_MODULES_ON_THE_PHONE="/sdcard/Download/MagiskModules"

APPS_FOR_ROOT_ACCESS="com.mixplorer,com.keramidas.TitaniumBackup,io.github.muntashirakon.AppManager,com.matoski.adbm,com.fox2code.mmm"


# ---------------------------------------------------------------------
# Titanium license file
#
TITANIUM_LICENSE_FILE="/data/backup/Android/Licenses/TitaniumBackup_license.txt"

# default Titanium update.zip file to restore the backups
#
TITANIUM_UPDATE_ZIP_FILE="/data/backup/ASUS_ZENFONE8/current_update.zip"

# Titanium files and directories on the phone
#
TITANIUM_LICENSE_FILE_ON_THE_PHONE="/sdcard/TitaniumBackup_license.txt"

# this variable can either contain a directory name ending with a slash "/" or a file name
#
TITANIUM_UPDATE_ZIP_FILE_ON_THE_PHONE="/sdcard/Download/"

# directory with the backups created by Titanium
#
TITANIUM_BACKUP_DIR_ON_THE_PHONE="/sdcard/TitaniumBackup"


# Titanium app
#
TITANIUM_APP="com.keramidas.TitaniumBackup"


# default post install script; scripts without a fully qualified filename are searched in the current directory
#
POST_INSTALL_SCRIPT="post_install.sh"

# default archive with config files for the Android OS
#
ARCHIVE_FILE="/data/develop/android/android_config.tar"

# ---------------------------------------------------------------------
# environment variables used by the scripts executed in the tasks
#
# (remove the leading hash "#" to activate the setting
#

# ---------------------------------------------------------------------
### Environment variables for the script ./boot_phone_from_twrp.sh  

#TWRP_IMAGE=""

#SERIAL_NUMBER=""

#ADB="/usr/bin/adb"
#FASTBOOT="/usr/bin/fastboot"
#TIMEOUT="/usr/bin/timeout"

# These variables are also used by the scripts install_apk.sh, install_magisk_via_twrp.sh, and install_twrp.sh
#
#ADB_OPTIONS=""
#FASTBOOT_OPTIONS=""

#FAST_BOOT_WAIT_TIME=55
#ADB_BOOT_WAIT_TIME=65

# ---------------------------------------------------------------------
### Environment variables for the script ./enable_access_via_adb.sh
#  
#PUBLIC_KEY_ON_PC=""

# ---------------------------------------------------------------------
### Environment variables for the script ./enable_root_access_via_magisk.sh

#MAGISK_DATA_DIR="/data/adb"
#BIN_DIR="/system/bin"
#TMPDIR="/cache/enable_root_access_via_magisk.sh.1115565"
#SQLITE3="/usr/bin/sqlite3"


# ---------------------------------------------------------------------
### Environment variables for the script ./install_os_via_twrp.sh

#UPLOAD_DIR_ON_THE_PHONE=""






It's also possible to define other variables used by the various scripts executed by the tasks in prepare_phone.include in this file, an example for the file prepare_phone.conf can be downloaded here.

The config file "prepare_phone.conf" is searched in the current directory, the home directory "${HOME}" and in the directory /etc (in this order),


When done use the task check_config to check the configuration, e.g.

./prepare_phone.sh check_config

Thet task check_config will check that all required variables are set and, if possible, that the values of these variables are valid. It also checks that all scripts used by the tasks in the include file exist.


The include file defines a task group called all that can be used to install and configure a phone attached via USB from scratch.

To list the tasks that will be done if the parameter all is used use the parameter --list-default-tasks, e.g.

./prepare_phone.sh --list_default_tasks
[25.01.2023 19:45 ] ### prepare_phone.sh ended at 25.01.2023 19:45:42 (The PID of this process is 508258; the RC is 0)
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ 
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./prepare_phone.sh --list_default_tasks
[25.01.2023 19:48 ] ### prepare_phone.sh started at 25.01.2023 19:48:03 (The PID of this process is 510292)
[25.01.2023 19:48 ] ### Processing the parameter ...
[25.01.2023 19:48 ] Tasks to execute are: ""
[25.01.2023 19:48 ] Parameter for the function init_tasks are: ""
[25.01.2023 19:48 ] ### The logfile used is /var/tmp/prepare_phone.sh.log


[25.01.2023 19:48 ] Checking the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" ...
[25.01.2023 19:48 ] Reading the include file "/data/develop/android/scripts_on_linux/prepare_phone.include" ...

Include files used are:

/data/develop/android/scripts_on_linux/prepare_phone.include


Tasks defined are


16 default task(s) defined:

enable_disable_abort_on_error:enable
boot_phone_from_twrp_image
install_os:noreboot:factory_reset
install_twrp:noreboot:next
install_magisk_in_boot_partition_only:noreboot:next
enable_adb:reboot
install_magisk_in_data_adb_only:noreboot:copy_apk
enable_root_access_for_the_shell:reboot
wait_for_access_via_adb:60:kill
install_the_magisk_app
enable_disable_abort_on_error:disable
install_essential_scripts
install_essential_apps
install_essential_magisk_modules
enable_root_access_for_apps:reboot
execute_script_on_the_phone:ignorerc

0 task(s) will be ignored if the parameter "all" is used:


[25.01.2023 19:48 ] ### The logfile used was /var/tmp/prepare_phone.sh.log
[25.01.2023 19:48 ] ### The start time was 25.01.2023 19:48:03, the script runtime is (day:hour:minute:seconds) 0:00:00:00 (= 0 seconds)
[25.01.2023 19:48 ] ### prepare_phone.sh ended at 25.01.2023 19:48:03 (The PID of this process is 510292; the RC is 0)
[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $


Hopefully the task names are self-explanatory ...

In principle the tasks executed if the parameter all is used will
  1. do a factory reset
  2. install the OS using TWRP
  3. install the TWRP recovery into the boot partition
  4. install Magisk into the boot partition
  5. enable access via adb using Magisk
  6. install Magisk
  7. enable root access for the shell via Magisk
  8. unpack an archive file with some config files on the phone
  9. copy all scripts from the local directory ${ESSENTIAL_SCRIPTS_DIR} to the directory ${SCRIPT_DIR_ON_THE_PHONE} on the phone
  10. install all apk files found in the local directory ${ESSENTIAL_APPS_DIR} 
  11. install all Magisk Modules found in the local directory ${ESSENTIAL_MAGISK_MODULES_DIR}
  12. enable root access for the applications listed in the variable ${APPS_FOR_ROOT_ACCESS} 
  13. copy the script post_install.sh from the current directory to the phone and execute it there (only if it exists)

To install and configure a phone attached via USB use the command:

./prepare_phone.sh all

Sample output of an installation and configuration done using the script with the parameter all can be found here.

Other task groups defined are:

Task group install_magisk

The tasks in this task group will
  1. install the Magisk app
  2. install Magisk into the boot partition
  3. enable root access for the shell.
  4. install the essential Magisk Modules found in the local directory ${ESSENTIAL_MAGISK_MODULES_DIR}

Task group update_os

The tasks in this task group will
  1. install the OS into the passive boot slot
  2. install TWRP into the boot partition for the passive boot slot
  3. install Magisk into the boot partition of the passive boot slot
  4. change the active slot and reboot the phone

The task group update_os can be used together with the task set_os_image_to_install to update an existing OS, e.g:

./prepare_phone.sh  set_os_image_to_install:/data/develop/android/OmniROM_13.0/out/target/product/zenfone8/omni-13-20230121-zenfone8-MICROG.zip update_os

Sample output of an OS update using these tasks can be found here.


To install and configure the phone manually using my other scripts use these commands:

Manual installation and configuration of the phone
  export OS_IMAGE_TO_INSTALL="/data/develop/android/OmniROM_13.0/out/target/product/zenfone8/omni-13-20230115-zenfone8-MICROG.zip"

  export TWRP_IMAGE="/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img"

  export MAGISK_APK_FILE="/data/backup/Android/EssentialApps/Magisk-v25.2.apk"


  cd /data/develop/android/scripts_on_linux

 ./boot_phone_from_twrp.sh

 ./install_os_via_twrp.sh factory_reset noreboot  $OS_IMAGE_TO_INSTALL

 ./install_twrp.sh next noreboot

 ./install_magisk_via_twrp.sh next copy_apk delete_adb_dir noreboot magisk_apk_file=$MAGISK_APK_FILE
 
 ./enable_access_via_adb.sh reboot

 adb wait-for-device

 ./install_magisk_via_twrp.sh adb_only copy_apk noreboot

 ./init_magisk_db.sh reboot

 ./install_apk.sh $MAGISK_APK_FILE
 adb shell pm grant com.topjohnwu.magisk android.permission.POST_NOTIFICATIONS


 # to install an app (example)

 ./install_apk.sh /data/backup/Android/EssentialApps/MiXplorer_v6.58.8_B23010320.apk

 # to install a Magisk Module (example)

 adb push /data/backup/Android/EssentialMagiskModules/addbin-v1.3.0.0.zip /sdcard/Download/MagiskModules/addbin-v1.3.0.0.zip
 adb shell su - -c magisk --install-module /sdcard/Download/MagiskModules/addbin-v1.3.0.0.zip

 # to enable root access for other apps (example)
 #
  ./init_magisk_db.sh apps=+com.mixplorer,com.keramidas.TitaniumBackup,io.github.muntashirakon.AppManager,com.matoski.adbm,com.fox2code.mmm




To update the OS manual using the scripts use these commands (assuming Magisk is already installed):

Manual update of the OS on the phone
  export OS_IMAGE_TO_INSTALL="/data/develop/android/OmniROM_13.0/out/target/product/zenfone8/omni-13-20230115-zenfone8-MICROG.zip"

  export TWRP_IMAGE="/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img"


  cd /data/develop/android/scripts_on_linux

 ./boot_phone_from_twrp.sh

 ./install_os_via_twrp.sh  noreboot  $OS_IMAGE_TO_INSTALL

 ./install_twrp.sh next noreboot

 ./install_magisk_via_twrp.sh next reboot
 



Notes:

Reinstalling an ASUS Zenfone 8 with OmniROM from scratch including a factory reset takes about 12 minutes with these scripts, and updating the operating system in the passive slot takes about 5 minutes.

Please note that while writing the global script I updated and corrected some of the other scripts. Therefor it's recommended to download the current version of the scripts.


Some hints for trouble shooting

Each task is a function in the include file with the name task_<taskname>, e.g. the function used to implement the task enable_adb is task_enable_adb.

Task parameter must be added to the taskname using the colon ":" as delimiter, example: install_magisk_in_boot_partition_only:noreboot:next

To execute the tasks in verbose mode use the parameter --info; to execute the complete script in verbose mode use the parameter --verbose.

To execute the tasks with "set -x" use the parameter --trace.

To execute the tasks in single step mode use the parameter --single-step.

The global initialisation of the script is done in the function init_tasks; all script parameter after "--" are parameter for the function init_tasks. To execute the function init_tasks with "set -x" use the parameter "--  trace"; to view the known parameter for the function init_tasks use the parameter "--  help".

To execute the tasks in dry-run mode use the init_tasks parameter " -- dryrun"


In the default configuration execute_tasks.sh will stop executing the remaining tasks if one of the tasks ends with an error. To disable this functionality for one task use the task parameter ignorerc; to disable this functionality global use the the task
enable_disable_abort_on_error:disable and to enable it again use the task enable_disable_abort_on_error:enable.

Use the parameter "-h -v" to get the detailed usage help


Notes

The scripts used by this script can be downloaded manually from these URLs:

https://bnsmb.de/files/public/Android/boot_phone_from_twrp.sh
https://bnsmb.de/files/public/Android/enable_access_via_adb.sh
https://bnsmb.de/files/public/Android/enable_adb_using_magisk.sh
https://bnsmb.de/files/public/Android/enable_root_access_via_magisk.sh
https://bnsmb.de/files/public/Android/init_magisk_db.sh
https://bnsmb.de/files/public/Android/install_apk.sh
https://bnsmb.de/files/public/Android/install_magisk_via_twrp.sh
https://bnsmb.de/files/public/Android/install_os_via_twrp.sh
https://bnsmb.de/files/public/Android/install_twrp_from_within_twrp.sh
https://bnsmb.de/files/public/Android/install_twrp.sh

The documentation for these scripts can be found on my home page https://bnsmb.de/My_HowTos_for_Android.html or in the various HowTos in the XDA Forum that I wrote.


Update 02.02.2023 /bs

See here for how to configure WiFi in Android via script

Update 18.04.2023 /bs

see How to install Magisk v26.0 or newer via script for how to install Magisk v26 via script



 



How to change any file or directory using Magisk


URL: https://forum.xda-developers.com/t/how-to-change-any-file-or-directory-using-magisk.4543103/

How to change any file or directory using Magisk
To change a file (or directory) in one of the read-only mounted filesystems in the Android OS Magisk can be used.
(see How to change files in the directory /system with Magisk and How to make files in /system writable )    
 
But this method only works for files in the directories /system, /vendor, /product, or /system_ext.

In current Android OS implementations there are a lot of other directories on read-only filesystems with files that must be changed or replaced to change the behaviour of Android.

For example on some Android implementations the boot animation is in the file /my_product/media/bootanimation/bootanimation.zip. The standard mechanism to change a file on a read-only mounted filesystem via Magisk can not be used to replace this file with another file.

But Magisk also supports executing scripts while booting the phone (see How to run a script at every boot using Magisk).


And the Magisk init scripts in the directory /data/adb/post-fs-data.d are executed early in the boot process. Therefor we can create a Magisk init script to replace any file with another file using the same method Magisk is using (that is a "mount -o bind ..." ):

Note

All commands in this Howto must be done by the user root.


Example

To replace the boot animation with another one copy the ZIP file with the new boot animation to the directory /data/adb (or any other read-write mounted directory available early in the boot process), e.g:

ASUS_I006D:/data # ls -l /data/adb/Earth_bootanimation.zip                                                                      
-rw-r--r-- 1 u0_a130 media_rw 13227720 2022-04-16 10:58 /data/adb/Earth_bootanimation.zip
ASUS_I006D:/data #
 



Make sure that the owner, group, permissions, and SELinux contexts for the file are okay using these commands:

# get the owner, group, permissions, and SELinux context of the file to replace
#
ASUS_I006D:/ # ls -lZ /system/media/bootanimation.zip
-rw-r--r-- 1 root root u:object_r:system_file:s0  4109852 2009-01-01 01:00 /system/media/bootanimation.zip
ASUS_I006D:/ #

# correct the config for the new file
#
chown root:root /data/adb/Earth_bootanimation.zip 


chmod 0644 /data/adb/Earth_bootanimation.zip 

chcon -v u:object_r:system_file:s0 /data/adb/Earth_bootanimation.zip 


Result:

ASUS_I006D:/data # ls -ldZ /data/adb/Earth_bootanimation.zip                                                                    
-rw-r--r-- 1 root root u:object_r:system_file:s0  13227720 2022-04-16 10:58 /data/adb/Earth_bootanimation.zip
ASUS_I006D:/data #


Now create a Magisk init script to replace the file via bind mount:

echo "mount -o bind /data/adb/Earth_bootanimation.zip  /system/media/bootanimation.zip " >post-fs-data.d/change_bootanimation.sh
chmod 755 post-fs-data.d/change_bootanimation.sh

That's it .. Reboot the phone and enjoy the new animation.

To remove the new animation just delete the script post-fs-data.d/change_bootanimation.sh and reboot the phone.


The same method can be used to replace a complete directory using these steps:

e.g. to make the directory /odm/etc writable do:

#
# create the new directory
#
mkdir /data/adb/odm_etc

#
# copy the existing files in /odm/etc to the new directory
#
cd /odm/etc && find . | cpio -pdum /data/adb/odm_etc

#
# create the Magisk init script to replace /odm/etc with the new directory /data/adb/odm_etc
#
echo "# make /odm writable
mount -o bind /data/adb/odm_etc /odm/etc
" >/data/adb/post-fs-data.d/make_odm_etc_writable
chmod 0755 /data/adb/post-fs-data.d/make_odm_etc_writable



Now reboot the phone and check the result:

ASUS_I006D:/ #
ASUS_I006D:/ # ls -l /odm/etc
total 15
-rw-r--r-- 1 root root 4961 2009-01-01 01:00 NOTICE.xml.gz
-rw------- 1 root root 1136 2009-01-01 01:00 build.prop
-r--r--r-- 1 root root    0 2009-01-01 01:00 fs_config_dirs
-r--r--r-- 1 root root    0 2009-01-01 01:00 fs_config_files
-rw-r--r-- 1 root root    0 2009-01-01 01:00 group
-rw-r--r-- 1 root root    0 2009-01-01 01:00 passwd
drwxr-xr-x 2 root root 3452 2023-01-18 16:30 selinux
ASUS_I006D:/ #

#
# create a new file in the directory used to replace /odm/etc
#
ASUS_I006D:/ # touch /data/adb/odm_etc/test.$$                                                                                                              

ASUS_I006D:/ # ls -l /data/adb/odm_etc/test.$$                                                                                                              
-rw-r--r-- 1 root root 0 2023-01-18 16:41 /data/adb/odm_etc/test.4597
ASUS_I006D:/ #

#
# check the contents of the directory /odm/etc
#
ASUS_I006D:/ # ls -l /odm/etc/test.$$                                                                                                                      
-rw-r--r-- 1 root root 0 2023-01-18 16:41 //odm/etc/test.4597
ASUS_I006D:/ #



Caution

I'm sure there is a reason why this feature is not implemented for all directories by default in Magisk
And, for example, if you make the directory /odm/etc writable using the method described above Android will complain after the next reboot with an error message that something is wrong with your phone.
Therefor please use with care! If possible only use this approach to make single files writable.


Trouble Shooting


If it looks like the "new" file is not used or something else went wrong, but the phone still boots into the Android system, this could be due to an incorrect SELinux context. To find this kind of error, look for the name of replaced file in the output of logcat with grep.


In case the phone does not boot anymore after replacing a file or directory using this method:

    Reboot the phone from a recovery with adb support (like TWRP) and delete the script in /data/adb/post-fs-data to fix the error


Notes


Files or directories that are used by Android before the Magisk init scripts are executed can not be changed using this method. These files must be replaced by changing the files in the ramdisk on the boot partition
(see How to change files in the boot image using Magisk and How to trigger an action when a property is changed )

There are various Magisk Modules available in the internet to replace the animation that use this technique


To test a boot animation while the Android OS is running do:

mount -o bind /data/adb/bootanimation_android12.zip  /system/media/bootanimation.zip                                                         
bootanimatio
n                                                                                                                                


Update 11.07.2023 /bs

Added more details about how to configure the SELinux context for the new file



Update 24.06.2023/bs

How to use an overlay filesystem to make filesystems writable

URL:  https://forum.xda-developers.com/t/how-to-change-any-file-or-directory-using-magisk.4543103/#post-88679517

How to use an overlay filesystem to make filesystems writable
Another method to change files in read-only filesystems is to use an overlay filesystem.

This method is implemented in the Magisk Module Magisk Overlayfs.
The source code for this Magisk module is available here: https://github.com/HuskyDG/magic_overlayfs

Using this Magisk Module every file in most of the read-only filesystems in Android can be changed; excerpt from the documentation

Make most parts of system partition (/system, /vendor, /product, /system_ext, /odm, /odm_dlkm, /vendor_dlkm, ...) become read-write.

I successfully tested this Magisk Module on an ASUS Zenfone 8 running OmniROM 13.


Notes:

There is no installable zip file in the repository for the Magisk Overlayfs . But the repository contains a script to create the zip file (build.sh)

Note that I had to change the code to create Magisk module zip file in the script build.sh to create an installable Magisk Module:

I replaced the line

zip -r9 out/magisk-module-release.zip out/magisk-module

with

cd out/magisk-module  && zip -r9 ../magisk-module-release.zip
 


How to configure the WiFI in Android via script


URL: https://forum.xda-developers.com/t/how-to-configure-the-wifi-in-android-via-script.4548489/

How to configure the WiFi in Android via script


Update 11.09.2023

see also here How to configure the WiFI in Android via CLI command



In the current version of Android the WiFi configuration is stored in the file

/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml

e.g.

ASUS_I006D:/ # ls -lZtr /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml                                        
-rw------- 1 system system u:object_r:apex_system_server_data_file:s0  4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
ASUS_I006D:/ #


The file is in plain ASCII xml format so it can be processed by any executable to change text files (editor, sed, etc).
But IMHO it's better to configure the WiFi manually via GUI and then use the file WifiConfigStore.xml with that configuration for configuring the WiFi via script.

Therefor I use this code in the post install script to install and configure my phone (see How to install and configure the Android OS via Script):

script commands to enable and configure WiFi
#
# sample post install script for the customizing of the phone
#
# This script will be copied to the phone and executed there
#
# The script is executed by the user shell; use "su - -c <command>" to execute commands as user root
#
echo ""
echo "*** Postinstall script is running ..."
echo ""

# ...  other customizations ....

#
# create the WiFi config file
#
# -rw------- 1 system system u:object_r:apex_system_server_data_file:s0  4884 2023-01-29 08:55 /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml
#
WIFI_CONFIG_FILE="/data/misc/apexdata/com.android.wifi/WifiConfigStore.xml"
WIFI_CONFIG_FILE_BACKUP="${WIFI_CONFIG_FILE}.$$.bkp"
NEW_WIFI_CONFIG_FILE="/sdcard/Download/WifiConfigStore.xml"

if [ -r "${WIFI_CONFIG_FILE}" ] ; then
  echo "Creating a backup of the file \"${WIFI_CONFIG_FILE}\" in \"${WIFI_CONFIG_FILE_BACKUP}\" ...."
  cp "${WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE_BACKUP}"
fi

echo "Creating the file \"${NEW_WIFI_CONFIG_FILE}\" ..."
cat >"${NEW_WIFI_CONFIG_FILE}" <<-\EOT

***  add here the contents of the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml from the phone after manually configuring the WLAN

EOT
if [ $? -eq 0 ] ; then
  echo "Creating the file \"${WIFI_CONFIG_FILE}\" ..."
  su - -c cp "${NEW_WIFI_CONFIG_FILE}" "${WIFI_CONFIG_FILE}" && \
    su - -c chmod 600 "${WIFI_CONFIG_FILE}" && \
    su - -c chown system:system "${WIFI_CONFIG_FILE}" && \
    su - -c chcon -v "u:object_r:apex_system_server_data_file:s0" "${WIFI_CONFIG_FILE}"
  if [ $? -ne 0 ] ; then
    echo "Error creating the file \"${WIFI_CONFIG_FILE}\" "
  fi
else
  echo "Error creating the file \"${NEW_WIFI_CONFIG_FILE}\" "
fi

# enable WiFi
#
echo "Enabling WiFi ..."

svc wifi enable

# optional: Disable mobile data connections
#
echo "Disabling mobile data ..."

svc data disable

# Now reboot the phone to activate the new WiFi config ..
#
echo "Waiting 15 seconds now before rebooting - press CTRL-C to abort ...."
i=0
while [ $i -lt 15 ] ; do
  (( i = i + 1 ))
  printf "."
  sleep 1
done
printf "\n"

echo "Now rebooting the phone ..."

reboot
#



Notes
:

Be aware the the file /data/misc/apexdata/com.android.wifi/WifiConfigStore.xml contains the passwords for all configured WLANs in plain text.


There should be a command to force Android to reread the file WifIConfigStore.xml but I don't know that.

Android can be forced to re-read the file WifIConfigStore.xml by killing the process system_server, e.g:

pkill system_server

But that is more or less a warm reboot and I do not know what other side effects that restart triggers.




There is an app to configure WiFi via adb command:

https://github.com/steinwurf/adb-join-wifi

example:

adb shell am start -n com.steinwurf.adbjoinwifi/.MainActivity -e ssid myssid -e password mywlan_password  -e password_type WPA 

The app works but unfortunately it does not support enabling the setting to use the device MAC instead of the random MAC so it's not usable in my environment.


Another app to configure WiFi via adb command is available here:

https://github.com/pr4bh4sh/adb-wifi-setting-manager

Unfortunately that app also does not support enabling choosing the MAC address for the connection.


There is also a script to convert an old Android wpa_supplicant.conf file to the newer (post-Oreo) WifiConfigStore.xml file (not tested):

https://github.com/mnalis/android-wifi-upgrade


It's also possible to use the command wpa_cli to configure WiFi but be aware the wpa_cli only works if selinux is (temporary) disabled, example:

wpa_cli example
ASUS_I006D:/ $ su -                                                                                                                                                                            
ASUS_I006D:/ #
ASUS_I006D:/ # setenforce 0
ASUS_I006D:/ #
ASUS_I006D:/ #
ASUS_I006D:/ # wpa_cli                                                                                                                                                                         
wpa_cli v2.10-devel-11
Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi> and contributors

This software may be distributed under the terms of the BSD license.
See README for more details.


Using interface 'wlan0'

Interactive mode

<3>Control interface command 'BSS RANGE=ALL MASK=0x2'
<3>Control interface command 'LIST_CREDS'
<3>Control interface command 'LIST_NETWORKS'
<3>Control interface command 'STA-FIRST'
>    
> list_networks
network id / ssid / bssid / flags
0    Zeilsheim    any    [CURRENT]
1        any    [DISABLED]
<3>Control interface command 'LIST_NETWORKS'
>
> quit
ASUS_I006D:/ #


Note: Use help to get list of known commands


Test Environment

I tested the WiFi config via script described above in these Android distributions:

OmniROM 13 (Android 13)
ASUS Android 12
ASUS Android 13

This method seems not to work in Android 11
 


How to configure the WiFI in Android via CLI command


URL: https://forum.xda-developers.com/t/how-to-configure-the-wifi-in-android-via-cli-command.4626435/

How to configure the WiFI in Android via CLI command
In the forum entry How to configure the WiFi in Android via Script I described some methods to configure the WLAN in Android via script. In the end I recommended to configure the WLAN manually via the GUI and then save the configuration file to copy it back later for a new configuration.

This was mainly because I couldn't find a way to define the MAC address to be used for configuring the WLAN interface.

Now I know that this can be done using another Android cli command:

cmd wifi connect-network


e.g.:

cmd wifi connect-network  mySSID wpa2 mypassword

To configure the randomization scheme for the MAC address (use the fixed MAC address or a random MAC address)  the parameter -r of the wifi cmd wifi sub command connect-network can be used:

    -r auto|none|persistent|non_persistent - MAC randomization scheme for the network

Using that command it's possible to configure the WLAN interface via script and the usage of the GUI is not necessary anymore.


Notes:

The command cmd wifi must be executed by the user root.

Use

cmd wifi


to get the usage help for the command

The syntax for the cmd wifi sub command connect-network is:

  connect-network <ssid> open|owe|wpa2|wpa3 [<passphrase>] [-x] [-m] [-d] [-b <bssid>] [-r auto|none|persistent|non_persistent]
    Connect to a network with provided params and add to saved networks list
    <ssid> - SSID of the network
    open|owe|wpa2|wpa3 - Security type of the network.
        - Use 'open' or 'owe' for networks with no passphrase
           - 'open' - Open networks (Most prevalent)
           - 'owe' - Enhanced open networks
        - Use 'wpa2' or 'wpa3' for networks with passphrase
           - 'wpa2' - WPA-2 PSK networks (Most prevalent)
           - 'wpa3' - WPA-3 PSK networks
    -x - Specifies the SSID as hex digits instead of plain text
    -m - Mark the network metered.
    -d - Mark the network autojoin disabled.
    -h - Mark the network hidden.
    -p - Mark the network private (not shared).
    -b <bssid> - Set specific BSSID.
    -r auto|none|persistent|non_persistent - MAC randomization scheme for the network




Test Environment

Tested with this Android versions:

OmniROM 13
ASUS Android 13

see also here https://stackoverflow.com/questions/75294766/android-device-how-to-connect-wifi-by-adb-shell-cmd-wifi-command   



How to install Magisk v26.0 or newer via script


URL:  https://forum.xda-developers.com/t/how-to-install-magisk-into-the-boot-partition-using-a-script.4456621/post-88429939

How to install Magisk v26.0 or newer via script
In Magisk v26.0 the developer changed the code so that it's not possible anymore to install Magisk into the boot partition while booted from a recovery image.

from the Magisk change log:

"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."


The installation of Magisk v26.x while booted into the recovery (for example using my script install_magisk_via_twrp.sh; see How to install Magisk into the boot partition using script) will succeed but executing the Magisk app after the necessary reboot will end with this dialog: