My HowTos for Android

Last Update: 26.11.2022/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
    5. Links
      1. Files mentioned in the HowTos
      2. General Android LInks
      3. TWRP related links
      4. Magisk related links
      5. Links for the OmniROM
      6. Links for ROM Development
      7. Links for useful Tools
      8. Links for the ASUS Zenfone 8
      9. Misc Links



Introduction


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

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


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/





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/


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 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/


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/



TWRP related articles


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/



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 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/




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


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.


The usage for the script is:

[xtrnaw7@t15g /data/backup/Android/scripts_on_linux]$ ./install_magisk_via_twrp.sh -h
./install_magisk_via_twrp.sh version - 2.0.0.0 - add Magisk to the boot partition of a phone running Android using TWRP

Usage

install_magisk_via_twrp.sh [boot_slot] [wait=n] [dd|fastboot] [twrp_image] [cleanup] [use_apk]

All parameter are optional. The parameter can be used in any order.

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.

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.

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 /sdcard/Download on the phone before starting the script.

Use the parameter "cleanup" to delete the directory /data/MagiskInst at script end

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)


The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"

[xtrnaw7@t15g /data/backup/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




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.


Trouble Shooting


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

/cache/magisk.log


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



 


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:

For Custom ROMs without MicroG I recommend to use the ZIP file from NanoDroid to install MicroG with the patched Playstore.
 


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
 
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:/ #



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):

See these posts:

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

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


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



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.


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.
 


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 save 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



 

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




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




 

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.



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 triger 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:/ #



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 26.11.2022 /bs


The touch screen in TWRP 3.7.0.12 does NOT work if TWRP is installed in the boot partition for an OmniROM 13 on an ASUS Zenfone 8 - you can only use TWRP in an adb session in this configuration.

To use TWRP 3.7.0.12 with OmniROM 13 on an ASUS Zenfone 8 boot the phone from the image file for TWRP,  e.g.:

sudo fastboot boot twrp-3.7.0_12-0-I006D.img


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 








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.


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:  not yet published

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 (see for example How to install TWRP via script or How to install Magisk into the boot partition using a script).

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 state of the attached phone and use the approbiate commands to reboot the phone from the TWRP recovery image.


The usage for the script is:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh help
boot_phone_from_twrp.sh v1.0.0.0 - boot a phone from a TWRP image

 Usage

    boot_phone_from_twrp.sh [-h|help|-H] [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

 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.

 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.

 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 4 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)

 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]$



The script can also be used to only retrieve the current state of the phone without rebooting it using the parameter checkonly. In this case the script will return the current state of the phone via return code:
 
    RC  phone state
    ----------------------------------------------------------------------------------------------------------
     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



Notes

The script boot_phone_from_twrp.sh can also be used as include file using

.  ./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-

print the hints for using the script as include file.


The script is also available on my homepage:

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



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:

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" />
</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.

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.




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 

 
 


Links


 

Files mentioned in the HowTos


URL
Content
Comment
install_apk.sh How to install packages (apk files) for Android via script
install_magisk_via_twrp.sh
How_to_install_Magisk_into_the_boot
PlayStore_for_MicroG.zip
How to replace the Fake Store from OmniROM with MicroG with a patched Playstore 
initshell.zip
How to change the home directory for the user root on an Android phone
profile
How to change the home directory for the user root on an Android phone
bootctl-binary-v2.1.1.zip How to switch the active slot
The original repository

https://github.com/roihershberg/bootctl-binary

is not maintained anymore by the author.
install_twrp_from_within_twrp.sh
How to install TWRP via script 

install_twrp.sh How to install TWRP via script
boot_phone_from_twrp.sh
How to boot a phone from a TWRP recovery image via script

enable_wifi.sh
How to change files in Android XML Binary format 





General Android LInks


URL
Content
Comment
https://forum.xda-developers.com/
XDA Forum ; a very good source for Android phone related infos

https://microg.org/ MicroG homepage
https://nanolx.org/ NanoDroid homepage
https://downloads.nanolx.org/NanoDroid/Stable/ NanoDroid Downloads
https://f-droid.org/ F-Droid homepage
http://adbcommand.com/
adb commands manual
https://android-doc.github.io/tools/help/adb.html
adb command documentation
https://developer.android.com/studio/command-line/adb#shellcommands
adb shell command overview
Google account neccessary
https://blog.testproject.io/2021/08/10/useful-adb-commands-for-android-testing/
Useful ADB commands for Android testing

https://developer.android.com/studio/command-line/adb#pm usage help for the command "pm"
Google account neccessary
https://developer.android.com/studio/command-line/adb#am usage help for the command "am"
Google account neccessary



https://developer.android.com/studio/releases/platform-tools
Android SDK Platform tools (fastboot, adb,etc)

https://developer.android.com/studio/releases/platform-tools.html
Android SDK Platform tools Downloads



https://github.com/roihershberg/bootctl-binary
bootctl binary to check or change the current slot

https://android.googlesource.com/platform/system/extras/+/master/bootctl/bootctl.cpp

https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/boot_control.h
source code for bootctl

https://github.com/gibcheesepuffs/Switch-My-Slot-Android
app with a GUI for bootctl

https://source.android.com/docs/core/ota/ab
A/B System updates documentation

https://dev.to/larsonzhong/most-complete-adb-commands-4pcg
ADB documentation




https://source.android.com/docs/core/architecture/bootloader/partitions
Partition usage in Android

https://developer.android.com/reference/android/provider/Settings.Global Global system settings that can be changed with the command settings


https://developer.android.com/reference/android/provider/Settings.Secure
Secure system settings that can be changed with the command settings
https://developer.android.com/reference/android/provider/Settings.System
System settings, containing miscellaneous system preferences, that can be changed with the command settings
https://www.cclsolutionsgroup.com/post/android-abx-binary-xml
Descrsiption of the Android Binary XML files




TWRP related links



URL
Content
Comment
https://twrp.me/
TWRP home page

https://www.xda-developers.com/how-to-install-twrp/ Instructions for installing TWRP
https://twrp.me/faq/openrecoveryscript.html
TWRP command line guide







https://forum.xda-developers.com/t/dev-how-to-compile-twrp-touch-recovery.1943625/
XDA Forum: How to compile TWRP touch recovery

https://www.hovatek.com/forum/thread-27922.html
not tested yet












https://github.com/TeamWin/Team-Win-Recovery-Project TWRP source code
https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp Source code from TWRP for installing TWRP into the boot partition
https://github.com/minimal-manifest-twrp/ Manifests for building TWRP

https://github.com/TeamWin/android_device_asus_I006D

TWRP device tree for ASUS ZenFone 8 a.k.a. "sake" a.k.a. ZS590KS





https://unofficialtwrp.com Support for TWRP on devices offical not supported by TWRP
https://unofficialtwrp.com/build-compile-twrp-recovery/ Instructions how to build TWRP








Magisk related links



URL
Content
Comment
https://github.com/topjohnwu/Magisk
Magisk home page
Magisk the must have for every Android phone
https://topjohnwu.github.io/Magisk/install.html
Instructions to install Magisk

https://topjohnwu.github.io/Magisk/
Magisk documentation

https://topjohnwu.github.io/Magisk/guides.html
Magisk developer guide

https://topjohnwu.github.io/Magisk/tools.html
Magisk Tools documentation

https://www.didgeridoohan.com/magisk/Magisk
Magisk TroubleShooting Guide

https://www.didgeridoohan.com/magisk/MagiskModuleIssues Hints for issues with Magisk Modules
https://forum.xda-developers.com/t/magisk-general-support-discussion.3432382/
General Magisk Support forum on XDA

https://forum.xda-developers.com/t/collection-of-magisk-modules-v2.3575758/
An XDA thread with a collection of Magisk Modules

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

https://github.com/Magisk-Modules-Repo/ssh
Magisk module with ssh and rsync

https://github.com/Fox2Code/FoxMagiskModuleManager
Fox Magisk ModuleManager

https://github.com/Magisk-Modules-Alt-Repo/
Another repository with Magisk Modules




https://github.com/Zackptg5/Cross-Compiled-Binaries-Android Source for the Magisk Module with various cross compiled binaries for Android
This zip file contains the binaries for different architectures
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/NewModules/Cross-Compiled-Binaries-Android-master ] $ ls -l bash/*
-rwxr-xr-x. 1 xtrnaw7 xtrnaw7 1541516 Sep 15 23:50 bash/bash-arm
-rwxr-xr-x. 1 xtrnaw7 xtrnaw7 1838864 Sep 15 23:50 bash/bash-arm64
-rwxr-xr-x. 1 xtrnaw7 xtrnaw7 1963936 Sep 15 23:50 bash/bash-x64
-rwxr-xr-x. 1 xtrnaw7 xtrnaw7 1883188 Sep 15 23:50 bash/bash-x86
-rwxr-xr-x. 1 xtrnaw7 xtrnaw7    3072 Sep 15 23:50 bash/etc.zip
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/NewModules/Cross-Compiled-Binaries-Android-master ] $
 [ OmniRomDev - xtrnaw7@t15g /data/develop/android/NewModules/Cross-Compiled-Binaries-Android-master ] $ file bash/*
bash/bash-arm:   ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
bash/bash-arm64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped
bash/bash-x64:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
bash/bash-x86:   ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped
bash/etc.zip:    Zip archive data, at least v2.0 to extract, compression method=store
[ OmniRomDev - xtrnaw7@t15g /data/develop/android/NewModules/Cross-Compiled-Binaries-Android-master ] $


addbin.zip Magisk Module for Android with cross-compiled binaries for devices with arm64 CPU This Magisk Module contains the binaries compiled for arm64 CPUs from the repository

https://github.com/Zackptg5/Cross-Compiled-Binaries-Android

(see above) and some binaries from the TWRP image



Links for the OmniROM


URL
Content
Comment
https://omnirom.org/
OmniROM Homage
OmniROM is Custom ROM for Android phones
https://forum.xda-developers.com/c/omnirom.2601/ OmniROM Forums on XDA
Most of the forums are outdated; it seems that the forums are not used anymore
https://download.lineageos.org/sake
Images and Recoveries for LineageOS 19
LineageOS is a Custom ROM for Android ; the Lineaage Recovery image can be used to install the OmniROM on an Android phone



https://wiki.lineageos.org/devices/sake/install?
Instructions to install the Recovery from the LineageOS




https://github.com/omnirom/android
Github repositories for OmniROM

https://gitlab.com/omnirom
Gitlab repositories for OmniROM

https://github.com/omnirom/android_prebuilts_prebuiltapks List of MicroG packages in the OmniROM image with MicroG




Links for ROM Development


URL
Content
Comment
https://gerrit.googlesource.com/git-repo/
repo script for buidling your own CustomROM

https://source.android.com/setup/develop/repo
documentation for the repo command

https://github.com/omnirom/android
Getting started doc for the OmniROM

https://github.com/omnirom
Repositories for building the OmniROM

https://developer.android.com/studio/command-line/
Documentation for the command line tools from the Android Studio

https://developer.android.com/studio/command-line/apksigner Documentation for the apk signer from the Android studio
https://developer.android.com/studio/command-line/zipalign Documentation for zipalign (zipalign must be used to align an APK file)
zipalign is part of the Android Studio
https://developer.android.com/studio7
Android Studio Download

https://developer.android.com/ndk/downloads NDK Downloads

https://github.com/android/ndk/wiki/Unsupported-Downloads
NDK Downloads (old versions)

https://github.com/omnirom/android_prebuilts_prebuiltapks
Prebuild MicroG packages for the OmniROM

https://github.com/DonkeyCoyote/proprietary_vendor_asus Vendor packages for the ASUS Zenfone 8 (neccessary for building your own CustomROM)
https://github.com/omnirom/android_device_asus_zenfone8/tree/android-12.1 Device tree for the ASUS Zenfone 8 (neccessary for building your own CustomROM)
https://github.com/TheMuppets/
Vendor packages for various devices

https://github.com/DonkeyCoyote/
Vendor packages for various devices
https://github.com/aosp-mirror/platform_build/tree/android12-platform-release/target/product/security
How to create a the ssl keys for a CustomROM image




https://source.android.com/docs/core/architecture/configuration/add-system-properties How to define system properties

https://android.googlesource.com/platform/system/sepolicy/+/master/private/property_contexts
defined SELinux contexts




https://android.googlesource.com/platform/system/core/+/master/init/README.md
Android Init language

https://source.android.com/devices/tech/ota/sign_builds Instructions to create certificates for an OS image







Links for useful Tools


URL
Content
Comment
http://java-decompiler.github.io/#jd-gui-download
Java Decompiler
(jar file with GUI)
https://github.com/pxb1988/dex2jar/blob/2.x/README.md

Tools to work with android .dex and java .class files

  1. dex-reader/writer: Read/write the Dalvik Executable (.dex) file. It has a light weight API similar with ASM.
  2. d2j-dex2jar: Convert .dex file to .class files (zipped as jar)
  3. smali/baksmali: disassemble dex to smali files and assemble dex from smali files. different implementation to smali/baksmali, same syntax, but we support escape in type desc "Lcom/dex2jar\t\u1234;"
  4. other tools: d2j-decrypt-string

https://forum.xda-developers.com/t/tool-imjtool-unpack-and-extract-a-variety-of-ota-images-from-various-vendors.4078159/

http://newandroidbook.com/tools/imjtool.html
mjtool - Unpack and extract a variety of OTA images from various vendors
https://forum.xda-developers.com/t/tool-android-image-kitchen-unpack-repack-kernel-ramdisk-win-android-linux-mac.2073775/ Android Image Kitchen - Unpack/Repack Kernel Ramdisk [Win/Android/Linux/Mac]
https://github.com/hzw1199/xml2axml/releases Convert compiled XML files used in apk files to plain XML files and vice versa
https://forum.xda-developers.com/t/linux-guide-rom-payload-image-dump.4196055/

https://github.com/ssut/payload-dumper-go

ROM payload image dumper - a python script to extract the images for the file payload.bin images for Android 12
https://shatter-box.com/knowledgebase/android-apk-signing-tool-apk-signer/
Android APK Signing Tool (APK Signer)
Note;

Android APK Signing Tool (APK Signer)

Use the apk signer from the Android studio
https://ibotpeaches.github.io/Apktool/ A tool for reverse engineering Android apk files

https://forum.xda-developers.com/t/tool-tickle-my-android-decompile-recompile-with-ease.1633333/
"TMA is a quick, powerful and easy-to-use tool that uses a piece of java called "apktool" to decompile and recompile Android app's. "

http://jsharkey.org/logcat/
python script that reformats the logcat output into a colorful stream that is much easier to visually follow. this script is for Python 2.x




   

Links for the ASUS Zenfone 8


URL
Content
Comment
https://www.asus.com/de/Mobile/Phones/ZenFone/Zenfone-8/HelpDesk_BIOS/
ASUS Zenfone 8 Firmware Download

https://forum.xda-developers.com/t/firmware-download-links.4308547/
XDA Forum for ASUS Zenfone 8 Firmware Download

https://twrp.me/asus/zenfone8.html
official TWRP for the ASUS Zenfone 8

https://www.asus-zenfone.com/2015/10/official-asus-zenfone-2-unlock.html
Official Tool from ASUS to Unlock the ASUS Zenfone 8

https://zentalk.asus.com/en/discussion/27136/unlock-relock-bootloader-unofficially-for-zenfone-3
Instructions to unlock the ASUS Zenfone 8

https://forum.xda-developers.com/f/asus-zenfone-8.12291/ XDA Forums for the ASUS Zenfone 8

https://forum.xda-developers.com/t/recovery-twrp-for-zenfone-8.4329139/
XDA Forum for TWRP for the ASUS Zenfone 8

https://forum.xda-developers.com/t/full-recover-to-stock-if-things-went-really-bad.4337467/
Instructions to install the Android raw image  from ASUS on the ASUS Zenfone 8

https://www.asus.com/Content/Android-13-Beta/
Android 13 Beta and a raw image for Android 12

https://www.asus.com/Content/Android-12-Beta/
Android 12 Beta and a raw image for Android 11
https://dl.omnirom.org/zenfone8/ OmniROM Images for the ASUS Zenfone 8
https://forum.xda-developers.com/t/recovery-twrp-for-zenfone-8.4329139/
XDA Support thread for TWRP for ASUS Zenfone 8




Misc Links



URL
Content
Comment
https://android.googlesource.com/kernel/msm/+/android-7.1.0_r0.2/Documentation/blockdev/zram.txt
ram: Compressed RAM based block devices

Description of the zram device implemented in the Android OS

https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html
Description for zram devices from the Linux Kernel doc




https://android.stackexchange.com/questions/213353/how-to-run-an-executable-on-boot-and-keep-it-running/213623#213623
How o run an executable and keep it running

https://android.stackexchange.com/questions/214839/how-to-run-an-android-init-service-with-superuser-selinux-context
How to run an Android init service with superuser SELinux context?

https://harrisonsand.com/posts/patching-adb-root/
Patching the adb daemon to run as root
only necessary if Magisk or a similar tool allow root access is not usabel




back top top