[UNLOCKED][ROOTED]Android 11 system-as-root and privacy policy updates permission problem - Samsung Galaxy A51 Themes, Apps, and Mods

Hi,
This is my first post here.
Yesterday I rooted a Samsung A51 SM-A515F/N with android 11 [A515FxxU4EUD7] stock rom with twrp recovery, unlocked bootloader, and magisk.
As far as I can tell zero functionality loss on OS. Got everything to work just right.
Pass SafetyNet Test.
systemized FGL Pro and GPS Joystick as priv-apps.
Both applications recognize system mode but they both crash when you start the location mocking.
I believe I found what is making them crash.
Need confirmation/Advise on how to proceed.
Assume full rooted SM-515F with fully working Magisk 23.0 and Terminal Systemizer module installed
Systemizer command:
a51:/ $ su
a51:/ # systemize
! Busybox not detected
Please install one (@osm0sis' busybox recommended)
df: Unknown option 'm' (see "df --help")
df: Unknown option 'm' (see "df --help")
==============================================
App Systemizer (Terminal Emulator) v17.3.1(70)
by veez21
==============================================
BusyBox not found!
==============================================
Enter logs to upload logs
==============================================
Set SELinux Enabled: false
SELinux Status: Enforcing
==============================================
1 - Systemize Installed Apps (Listed)
2 - Systemize Installed Apps (Enter label)
3 - Systemize given APK
s - Set SELinux
0 - Exit
[CHOICE]: 3
Enter APK Path
Example: /sdcard/launcher.apk
x - Back to Menu
> /sdcard/Download/com.ltp.pro.fakelocation.apk
Checking if file exists - OK
Checking if file is an APK - OK
Checking package name - com.ltp.pro.fakelocation
Where to install?
< 1 - /system/app | 2 - /system/priv-app | 0 - cancel > : 2
df: Unknown option 'm' (see "df --help")
df: Unknown option 'm' (see "df --help")
Checking APK size - 5M
Transfering com.ltp.pro.fakelocation.apk(com.ltp.pro.fakelocation) to '/system/priv-app'...
Granting Permissions
com.ltp.pro.fakelocation - Done
1|a51:/ # mount -o rw,remount /
a51:/ # rm /etc/permissions/privapp-permissions-platform.xml
a51:/ # cp /sdcard/Download/privapp-permissions-platform.xml /etc/permissions/
a51:/ # chown root:root /etc/permissions/privapp-permissions-platform.xml
a51:/ # chmod 644 /etc/permissions/privapp-permissions-platform.xml
a51:/ # mount -o ro,remount /
a51:/ # reboot
Snippet of /etc/permissions/privapp-permissions-platform.xml:
...
<!-- CUSTOM Permissions -->
<privapp-permissions package="com.ltp.pro.fakelocation">
<permission name="android.permission.READ_EXTERNAL_STORAGE" />
<permission name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission name="android.permission.ACCESS_FOREGROUND_LOCATION" />
<permission name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<permission name="android.permission.ACCESS_FINE_LOCATION" />
<permission name="android.permission.ACCESS_COARSE_LOCATION" />
<permission name="android.permission.INSTALL_LOCATION_PROVIDER" />
<permission name="android.permission.ACCESS_MOCK_LOCATION" />
<permission name="android.permission.ACCESS_MEDIA_LOCATION" />
</privapp-permissions>
<!-- END -->>
...
snippet from cmd dumpsys package com.ltp.pro.fakelocation:
Packages:
Package [com.ltp.pro.fakelocation] (1e331cd):
userId=10264
pkg=Package{876da82 com.ltp.pro.fakelocation}
codePath=/system/app/com.ltp.pro.fakelocation
resourcePath=/system/app/com.ltp.pro.fakelocation
legacyNativeLibraryDir=/system/app/com.ltp.pro.fakelocation/lib
primaryCpuAbi=null
secondaryCpuAbi=null
versionCode=67 minSdk=15 targetSdk=28
versionName=3.4.9.3
splits=[base]
apkSigningVersion=2
applicationInfo=ApplicationInfo{876da82 com.ltp.pro.fakelocation}
flags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA LARGE_HEAP ]
privateFlags=[ PRIVATE_FLAG_ACTIVITIES_RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION PRIVATE_FLAG_REQUEST_LEGACY_EXTERNAL_STORAGE PRIVATE_FLAG_ALLOW_NATIVE_HEAP_POINTER_TAGGING ]
forceQueryable=false
queriesPackages=[]
dataDir=/data/user/0/com.ltp.pro.fakelocation
supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
usesLibraries:
android.test.base
android.hidl.manager-V1.0-java
android.hidl.base-V1.0-java
usesOptionalLibraries:
org.apache.http.legacy
usesLibraryFiles:
/system/framework/android.test.base.jar
/system/framework/android.hidl.manager-V1.0-java.jar
/system/framework/android.hidl.base-V1.0-java.jar
/system/framework/org.apache.http.legacy.jar
timeStamp=2021-05-30 19:29:31
firstInstallTime=2021-05-30 19:29:31
lastUpdateTime=2021-05-30 19:29:31
signatures=PackageSignatures{3bad593 version:2, signatures:[721026fa], past signatures:[]}
installPermissionsFixed=true
pkgFlags=[ SYSTEM HAS_CODE ALLOW_CLEAR_USER_DATA LARGE_HEAP ]
declared permissions:
com.ltp.pro.fakelocation.permission.MAPS_RECEIVE: prot=signature, INSTALLED
com.ltp.pro.fakelocation.permission.C2D_MESSAGE: prot=signature, INSTALLED
requested permissions:
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission.SYSTEM_ALERT_WINDOW
com.google.android.providers.gsf.permission.READ_GSERVICES
android.permission.READ_EXTERNAL_STORAGE: restricted=true
android.permission.WRITE_EXTERNAL_STORAGE: restricted=true
android.permission.FOREGROUND_SERVICE
com.android.vending.BILLING
com.android.vending.CHECK_LICENSE
android.permission.WRITE_SETTINGS
android.permission.INSTALL_LOCATION_PROVIDER
android.permission.WRITE_SECURE_SETTINGS
android.permission.ACCESS_MOCK_LOCATION
android.permission.WAKE_LOCK
com.google.android.c2dm.permission.RECEIVE
com.ltp.pro.fakelocation.permission.C2D_MESSAGE
android.permission.ACCESS_BACKGROUND_LOCATION: restricted=true
android.permission.ACCESS_MEDIA_LOCATION
install permissions:
com.ltp.pro.fakelocation.permission.C2D_MESSAGE: granted=true
android.permission.WRITE_SETTINGS: granted=true
com.google.android.c2dm.permission.RECEIVE: granted=true
com.google.android.providers.gsf.permission.READ_GSERVICES: granted=true
android.permission.SYSTEM_ALERT_WINDOW: granted=true
android.permission.FOREGROUND_SERVICE: granted=true
android.permission.INTERNET: granted=true
com.android.vending.CHECK_LICENSE: granted=true
com.android.vending.BILLING: granted=true
android.permission.ACCESS_NETWORK_STATE: granted=true
android.permission.WAKE_LOCK: granted=true
User 0: ceDataInode=46342 installed=true hidden=false suspended=false distractionFlags=0 stopped=false notLaunched=false enabled=0 instant=false virtual=false
overlay paths:
/product/overlay/NavigationBarMode3Button/NavigationBarMode3ButtonOverlay.apk
gids=[3003]
runtime permissions:
android.permission.ACCESS_FINE_LOCATION: granted=true, flags=[ USER_SET]
android.permission.READ_EXTERNAL_STORAGE: granted=false, flags=[ APPLY_RESTRICTION]
android.permission.ACCESS_COARSE_LOCATION: granted=true, flags=[ USER_SET]
android.permission.WRITE_EXTERNAL_STORAGE: granted=false, flags=[ APPLY_RESTRICTION]
android.permission.ACCESS_BACKGROUND_LOCATION: granted=false, flags=[ REVOKE_WHEN_REQUESTED|APPLY_RESTRICTION]
android.permission.ACCESS_MEDIA_LOCATION: granted=true, flags=[ USER_SET|REVOKE_WHEN_REQUESTED]
Please direct your attention to the second last line: "android.permission.ACCESS_BACKGROUND_LOCATION: granted=false, flags=[ REVOKE_WHEN_REQUESTED|APPLY_RESTRICTION]"
This permission is restricted even though permission has been explicitly set with the xml file.
I believe this is due to: https://developer.android.com/training/location/permissions this update of the privacy policy on Android 11.
Now this thread says that the permission for ACCESS_FOREGROUND_LOCATION needs to be requested in the application.manifest file before ACCESS_BACKGROUND_LOCATION is called. This allows the user to select the always allow option that allows background threads to handle location permissions. This is where I believe the app crashes - the thread gets aborted and then crashes the app because it can't get background location access.
I'm thinking the only solution is to get the devs of FGL/GPSJoystick to add these permissions to their minifest files. Am I correct or am I missing something big?
EDIT: interestingly - installing the app as userapp(not system app), gives the "Allow all the time" option under location (Settings->Apps->Permissions->Location). I think I'm missing something, aldo I might be a little sleep deprived, 36 hours straight with this A51
Please let me know if you need any more info from me

RESERVED

Were you able to solve this problem facing same with my custom system app

Hi,
I emailed the devs of the afore mentioned apps, asking for them to add the permission in their manifest files but never got any replies.
After that I just switched over to patching the framework so the OS can not detect when a location mock app is selected in developer settings (using Smali patcher).
I have run into this permission and location settings issue a couple times since then on other apps, some not written by me and others written by me. The only fix I have been able to find is to add the permissions in the app manifest file. Ever since Android 10 google made it much harder to enable the "Always Allow Location Access" in settings.
Hope this helps

Related

[Script] App/Data Backup

Hello all,
I have updated the script further to allow for arguments to be accepted as well as having the script placed in /system/bin. This works pretty much like Titanium Backup except that it's a script and uses a CLI. No pretty GUI . You can backup, restore, and fix app and data permissions from this script. 'fix_permissions' is embedded within this script and it will be install in the /system/bin folder if the startup script does not already find it. The script does not really work like an Apps2SD other than it backs up apps from /data/app and their appropriate data folders from /data/data.
This script can be very useful when switching between ROMs as your app and data can be reinstalled with the script's restore feature.
The script currently looks for APKs to backup from /data/app, /data/app-private, and /mnt/asec.
My first creation of this script was based upon a modified version of Firerat's Apps2SD script so naturally many thanks goes to him. Also the fix_permissions script comes from Koush and the Cyanogen Team.
No EXT partition required! Just download the file and run on an Android device.
STEPS:
1. Put the attached file on your SD card and run. It will install app_backup to /system/bin. From there you can type the following to start the program:
Code:
app_backup
Here's an example of backing up a couple apps with a data backup:
Code:
C:\Users\zlippard\Documents>adb shell
# app_backup
app_backup
Welcome to AppBackup 1.1
OPTIONS
--------------------------------
'b' to backup
'r' to restore
'f' to fix all app permissions
'd' to delete app_backup folder
'x' to reboot
'q' to quit: b
b
Creating app list to /dev/applist.txt...
Done!
[1] sd com.DefiantDev.RocketBunnies
[2] sd com.rovio.angrybirds
[3] sd com.rovio.angrybirdsseasons
[4] sd sts.pl
[5] app android.tether
[6] app com.alfray.timeriffic
[7] app com.androidemu.gbalite
[8] app com.aurorasoftworks.quadrant.ui.standard
[9] app com.batterypoweredgames.lightracer3dbasic
[10] app com.bfs.papertoss
[11] app com.chase.sig.android
[12] app com.chung.compasslevel
[13] app com.citrix.Receiver
[14] app com.facebook.katana
[15] app com.faziklogic.scripter
[16] app com.fede.launcher
[17] app com.forthegamer.android.scheduler
[18] app com.google.android.apps.chrometophone
[19] app com.google.android.apps.googlevoice
[20] app com.google.android.apps.maps
[21] app com.google.android.apps.unveil
[22] app com.google.android.carhome
[23] app com.google.android.gm
[24] app com.google.android.stardroid
[25] app com.google.android.street
[26] app com.google.android.voicesearch
[27] app com.google.android.youtube
[28] app com.google.zxing.client.android
[29] app com.gravitymobile.app.hornbill
[30] app com.icenta.sudoku.ui
[31] app com.intuit.instantreturn
[32] app com.intuit.instantreturn.filing.key
[33] app com.joelapenna.foursquared
[34] app com.koushikdutta.rommanager.license
[35] app com.metago.astro
[36] app com.mhuang.overclocking
[37] app com.mobitv.client.nfl2010
[38] app com.pandora.android
[39] app com.paypal.android.p2pmobile
[40] app com.shazam.android
[41] app com.sirma.mobile.bible.android
[42] app com.southwestairlines.mobile
[43] app com.sporadicsoftware.NetQFree
[44] app com.twitter.android
[45] app com.verizon.ams.companion
[46] app com.verizon.android.wifios
[47] app com.vzw.hss.myverizon
[48] app com.zebdor.android.poolCare
[49] app net.flixster.android
[50] app net.rbgrn.lightracer
[51] app org.connectbot
[52] app org.coolcode.emuroms
[53] app ru.orangesoftware.areminder
Enter app #'s to backup or 'a' for all and press [ENTER].
Or 0 to exit (ex: 1 5 15): 1 3 4
1 3 4
Copy data? ('y' or 'n') y
y
1 of 3] - Backing up com.DefiantDev.RocketBunnies...Done!
2 of 3] - Backing up com.rovio.angrybirdsseasons...Done!
3 of 3] - Backing up sts.pl...Done!
OPTIONS
--------------------------------
'b' to backup
'r' to restore
'f' to fix all app permissions
'd' to delete app_backup folder
'x' to reboot
'q' to quit: q
q
Goodbye!
#
When starting the script, you will be brought to the main menu screen where you can choose to either backup, restore, fix permissions, delete /sdcard/app_backup (this is the storage folder), reboot or quit. The rest is pretty self explanatory...
So far I have tested this script on the DROID and DROID Incredible. The DROID was running Bugless Beast 2.2 and the Inc I've tried ran CM6 and CM7.
UPDATES:
1.4
Added "busybox " before each command call within the app_backup script to make sure busybox commands are being called.
1.3
Fixed issue with backup apps not showing up. Thanks to welcomb for notifying me.
Fixed mounting issue for ro/rw in /system
1.2
Fixed backup feature for phones that use Apps2SD. Thanks to welcomb.
Fixed deletion feature of apps and data.
1.1
Arguments: use -b to auto-run a full backup. -r will auto-run a full restore of all apps in android_app_backup
Apps and data are now stored in /sdcard/android_app_backup.
app_backup script is now automatically installed to /system/bin/app_backup when running the new script. The new script is more of an installer.
Let me know of any bugs!
Much of the credit goes to Firerat for creating the initial apps2sdext script. This is what gave me the idea to continue on with an app backup script. Here's Firerat's latest version of apps2sdext in case you want to see it or don't have an incredible: http://forum.cyanogenmod.com/topic/...mount-issues-i-hope/page__p__24386#entry24386
As the usual goes, I'm not responsible for any mishaps that may occur while using this script or bricked phones, data loss, blah blah blah... So, USE AT YOUR OWN RISK.
The script is attached below. Have fun! Please post any findings, bugs, questions, comments, or updates you would like to see here. Feel free to make any modifications to this script.
NOTE TO SAMSUNG RELAY 4G USERS:
A friend of mine has this phone and was getting the following error when running the .sh script:
Code:
can't create temporary file /sqlite_stmt_journals/mksh.<random>
To remedy this, I remounted the root directory / as read-write,created the folder /sqlite_stmt_journals, ran the .sh script, and afterwards remounted to read-only.
anyone tried this? lots of success of nexus one
For future use...
zebdor44 said:
Hello all,
I have modified my script to be more of an app & data backup utility. This works pretty much like Titanium Backup except that it's a script and uses a CLI. No pretty GUI . You can backup, restore, and fix app and data permissions from this script. 'fix_permissions' is embedded within this script and it will be install in the /system/bin folder if the app_backup.sh script does not already find it. Since this is a complete revision, I have set the version back to 1.0. The script does not really work like an Apps2SD other than it backs up apps from /data/app and their appropriate data folders from /data/data.
This script can be very useful when switching between ROMs as your app and data can be reinstalled with the script's restore feature.
The script currently looks for APKs to backup from /data/app, /data/app-private, and /mnt/asec.
<...>
Click to expand...
Click to collapse
This is SO much easier than Titanium, and I am trying to figure out how to automate backup / restore. My end goal:
Run a Nandroid
Run this script (in Backup mode)
Wipe userdata
Install Rom
Install extras (kernel, Gapps, etc)
Restore apps.
Anyone want this?
If people are interested, I may take a look into this.
Woooow, nice work man =]
I was gonna write something really simple, but you beat me to it! Thanks!
EDIT: ooppps, didn't mean that your script was simple! I meant that you beat me to an app backup script, and yours would blow mine out of the water xD
I'm a bit busy right now (haha, but not too busy to post... ), but does anyone know if this supports restoring apps only? I feel like it probably does, but can anyone confirm?
Again, this looks awesome!
Thanks guys for the comments!! Good to hear you like it
alanthemanofchicago said:
This is SO much easier than Titanium, and I am trying to figure out how to automate backup / restore. My end goal...
Click to expand...
Click to collapse
Let me try making an update.zip that'll run the script. Then you could run the update.zip in recovery mode like CWM or AmonRa.
gmichaelow said:
Woooow, nice work man =]...but does anyone know if this supports restoring apps only? I feel like it probably does, but can anyone confirm?
Click to expand...
Click to collapse
The script will restore apps and their data folders. Basically what it does is it reinstalls the app that you selected to restore and then overwrites the appropriate /data/data folder for that app with the backup.
Automating backups and restores
So it seems it may not be that easy to create an update.zip that will work properly. The script will need to mount /system, /data and /datadata to access the apks and data folders while in recovery. That's not difficult to do but apps on the sdcard under /mnt/asec may prove to be a little more difficult. I made a working update.zip that would run 'app_backup -b' to automate the process but there is no output of script so I have not posted that. The output would be kinda nice to see just so you know how far along the backup process is at.
I may need to dig deeper on the update.zip but for now check out the updates!
Thanks!
After finally getting the script into /system and running it, it doesn't work. I get the output:
Code:
[: not found
[: not found
[: not found
Ok nevermind!
Goodbye!
Using a rooted Samsung Galaxy 3. I'm still very new to custom ROMs, etc.
Hi
I get the following error when trying to backup on my Samsung Galaxy S :
data/system/packages.xml no such file or directory
Nothing to backup
What can i do?
Skele Drew said:
After finally getting the script into /system and running it, it doesn't work. I get the output:
Code:
[: not found
[: not found
[: not found
Ok nevermind!
Goodbye!
Using a rooted Samsung Galaxy 3. I'm still very new to custom ROMs, etc.
Click to expand...
Click to collapse
FadeFx said:
Hi
I get the following error when trying to backup on my Samsung Galaxy S :
data/system/packages.xml no such file or directory
Nothing to backup
What can i do?
Click to expand...
Click to collapse
Hmm... If you have Astro lookup /data/system and see if the packages.xml file exists. Better yet, if you have a terminal emulator or ADB do a
Code:
ls /data/system | grep packages.xml
and see if anything comes up. The Galaxy series should have the same file system structure as any other Android phone... What Android OS are you running?
EDIT: It looks like the Galaxy series is using 2.2... Also if one of you could do a
Code:
su
cd /
find -name packages.xml
find -name packages.list
within a terminal emulator or ADB this should show where the packages files are located. It may take a bit of time to do the search since its looking for it from /.
./dbdata/system/packages.xml
find -name packages.list does search forever it seams...
Ok I might have to update the script to look in that directory for the packages.xml file. Stay tuned! Sorry for the delay in getting back. Been pretty busy.
EDIT: Download Busybox Installer from the market (the free one) and try installing the latest busybox for your samsung phone and then try running the script again. Does the script seem to act any better? I'm thinking the :not found] issue has to do with a bad busybox and the script not finding the right commands it needs to use. Let me know how that turns out.
Remember to nandroid backup before installing the busybox just in case
Busybox version is 1.19.0 but i didn't try the script yet. Will when i come home...
FadeFx said:
Busybox version is 1.19.0 but i didn't try the script yet. Will when i come home...
Click to expand...
Click to collapse
Just curious, any luck with the busybox upgrade?
I've updated the script to version 1.3. Welcomb noticed that there was an issue with bringing up the apps when requesting a backup although I'm not sure if that will fix the issue you had Fade...
Also mounting /system to copy the script over should be fixed .
I've added this to the OP already but for those with a Samsung Relay 4G phone:
A friend of mine has this phone and was getting the following error when running the .sh script:
Code:
can't create temporary file /sqlite_stmt_journals/mksh.<random>
To remedy this, I remounted the root directory / as read-write,created the folder /sqlite_stmt_journals, ran the .sh script, and afterwards remounted to read-only.
Is this still going?
I'm getting "There is nothing to backup" in CM11. I'm on the appropriae line fo the script but I can't figure out what's wrong.
So, this is the only script I found to backup and restore app data.
Needs some changes for newer android OS
Needs a check, as out of the box toybox is used, not busybox
Mount point needs something:
As
Code:
# busybox grep /system /proc/mounts | busybox awk '{print $1}'
/dev/block/bootdevice/by-name/system
/dev/block/bootdevice/by-name/system
It is doubled.
Removing $DEVICE seems to work. I removed $DEVICE from all mount commands:
From:
Code:
$MOUNT -o remount,rw $DEVICE /system
To:
Code:
$MOUNT -o remount,rw /system
backup needs a fix for app list
$EGREP "^< seems to cause issues. I removed ^ and it worked, meaning applist success.
I was able to backup data, but the apks were missing. Maybe because it used to be app.apk now it is base.apk?
restore does not work at the moment
As applist returns empty. Checked /dev/applist.txt and it was empty for some reason.
Also, fix_permissions file not in some devices, /dev/fix_permissions not found.
Some issues:
Code:
{system("busybox cat /data/system/packages.list | busybox grep $2")}
causes errors
Some errors when creating applist, but apps are displayed afterwards. So, might be same issue as above
Code:
Creating app list to /dev/applist.txt...
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
sh: /data/system/packages.list: can't execute: Permission denied
Done!
[1] app a...
[2] app a...
[3] app c...
...
[35] app o...
[36] app o...
[37] app o...
[38] app o...
[39] app o...
[40] app x...
Last thing is that the commands used to generate applist
Code:
$ECHO "Creating app list to /dev/applist.txt..."
$CAT /data/system/packages.xml | $EGREP "<package.*serId" | $GREP -v framework-res.apk | \
$GREP -v com.htc.resources.apk | $SED 's/<package name=\"//' | $SED 's/\" codePath="/ /' | \
$SED 's/\"//g' | $SED -e 's/\/data\/app-private\//private /' | $SED 's/\/data\/app\//app /' | \
$SED 's/\/system\/app\//system /' | $SED 's/\/mnt\/asec\//sd /' | \
$AWK '{if($2 !~ /system/) printf "%10s %s %s",$2,$1,$3"\n"}' | sort | \
$AWK '(++count) {printf "%5s %7s %s %s\n","["count"]",$1,$2,$3} \
{system("$CAT /data/system/packages.list | $GREP $2")}' | $SED 's/\/data\/data\///' | \
$AWK '{printf "%5s %7s %s %s %s\n",$1,$2,$3,$4,$8}' > $applist
causes some errors, even though apps are displayed at the end:
Code:
BusyBox v1.22.1 bionic (2017-04-22 22:10 +0300) multi-call binary.
Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...
Search for PATTERN in FILEs (or stdin)
-H Add 'filename:' prefix
-h Do not add 'filename:' prefix
-n Add 'line_no:' prefix
-l Show only names of files that match
-L Show only names of files that don't match
-c Show only count of matching lines
-o Show only the matching part of line
-q Quiet. Return 0 if PATTERN is found, 1 otherwise
-v Select non-matching lines
-s Suppress open and read errors
-r Recurse
-i Ignore case
-w Match whole words only
-x Match whole lines only
-F PATTERN is a literal (not regexp)
-E PATTERN is an extended regexp
-m N Match up to N times per file
-A N Print N lines of trailing context
-B N Print N lines of leading context
-C N Same as '-A N -B N'
-e PTRN Pattern to match
-f FILE Read pattern from file
cat: write error: Broken pipe
BusyBox v1.22.1 bionic (2017-04-22 22:10 +0300) multi-call binary.
Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...
Search for PATTERN in FILEs (or stdin)
-H Add 'filename:' prefix
-h Do not add 'filename:' prefix
-n Add 'line_no:' prefix
-l Show only names of files that match
-L Show only names of files that don't match
-c Show only count of matching lines
-o Show only the matching part of line
-q Quiet. Return 0 if PATTERN is found, 1 otherwise
-v Select non-matching lines
-s Suppress open and read errors
-r Recurse
-i Ignore case
-w Match whole words only
-x Match whole lines only
-F PATTERN is a literal (not regexp)
-E PATTERN is an extended regexp
-m N Match up to N times per file
-A N Print N lines of trailing context
-B N Print N lines of leading context
-C N Same as '-A N -B N'
-e PTRN Pattern to match
-f FILE Read pattern from file

How To Guide 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
Code:
./build/make/target/product/security
in your build tree for the CustomROM, e.g.
Code:
[[email protected] /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
[[email protected] /data/develop/android/OmniROM]$
Now re-sign the apk file using these commands:
Code:
# 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 it:
To test the signed apk do:
Copy the signed apk to the phone running your self compiled CustomROM and issue
Code:
ls -l /sdcard/Download/SMMI_TEST1.apk
# 25733396 in the next command is the size of the file /sdcard/Download/SMMI_TEST1.apk in byte
#
cat /sdcard/Download/SMMI_TEST1.apk | pm install -S 25733396
If you get an error like this
Code:
130|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:
Code:
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
Code:
adb reboot
After the reboot the ASUS Hardware Test App should be visible in the directory /system/app, e.g.
Code:
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
Code:
# open a shell on the ASUS Zenfone 8 (either local or via "adb shell"), become root user
su -
and execute
Code:
# 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 ...)
Code:
# switch SELinux to permissive mode (I currenlty do not know the neccessary SELinux policies to avoid this step ...)
#
setenforce 1
Sample Script to start the ASUS Hardware Test App
Code:
# 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:
Code:
# 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 :
Code:
# 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.
Update 26.06.2022
The adb commands to create the "simulated" Magisk Module must be executed by the user root - fixed.
Added a sample script to start the ASUS Hardware Test Tool
Update 04.08.2022
Added instructions to to test if signing the apk was successfull
Thank you so much for your guides! This is very detailled documentation with good explanations!

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

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 command on Android to do that is
Code:
pm install [apk_file]
But unfortunately that does not work, neither as user shell (due to missing file access rights) nor as user root:
Code:
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...ling-new-limitations-of-data-local-tmp-folder
pm install also supports the installation of a package piped to stdin, e.g
Code:
cat [package_file] | pm install -S [package_size]
Example:
Code:
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, from the PC:
Code:
[[email protected] /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
[[email protected] /data/backup/Android]$
[[email protected] /data/backup/Android]$ cat EssentialApps/F-Droid.apk | adb shell pm install -S $( ls -l EssentialApps/F-Droid.apk | awk '{ print $5 }' )
Success
[[email protected] /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:
Code:
#
# 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 tjhe phone:
Code:
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:
Code:
[[email protected] /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
[[email protected] /data/backup/Android/myapps]$
Notes:
Another method to install packages via script from an PC is
Code:
adb install [package]
This command copies the file to a temporary location on the phone and installs it then via "pm install".
e.g
Code:
[[email protected] /data/backup/Android/myapps]$ adb install F-Droid.apk
Performing Streamed Install
Success
[[email protected] /data/backup/Android/myapps]$
Another ugly and insecure method on the phone to install an apk file using pm install as root is
Code:
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
Code:
adb shell pm help
or on the phone
Code:
pm help
or see here: https://developer.android.com/studio/command-line/adb#pm (Google Account neccessary) or here: http://adbcommand.com/adbshell/pm
Update 06.07.2022
I attached the script to this entry but I can not see the attachment.
Therefor: The script is also available on my web site:
http://bnsmb.de/files/public/Android/install_apk.sh
Update 09.11.2022
The script is now attached to this post in a ZIP file

How To Guide 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.
Please note that I mirror all my HowTos for Android on my web page:
http://bnsmb.de/My_HowTos_for_Android.html
I will update this post when 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 version used for this tutorial is 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 How to install Magisk into the boot partition using a script
Update 18.04.2023/bs
see How to install Magisk v26.0 or newer via script for how to install Magisk v26 via script
Note that the Magisk files for the boot partition must be reinstalled after an OS upgrade but this can be done from within the Magisk App.
Uninstalling Magisk
Use the Button "Uninstall Magisk" in the Magisk App to uninstall Magisk.
The Magisk App can also remove the Magisk files from the boot partition.
To uninstall the Magisk App only via script do
Bash:
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:
Bash:
# 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:
Code:
[[email protected] ~]$ 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)
[[email protected] ~]$ 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!
[[email protected] ~]$ adb reboot
[[email protected] ~]$
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 via shell command to remove the module after the next reboot create the file
remove
in the module directory.
If uninstalling the Magisk Module via Magisk 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.
Bash:
# 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
Bash:
adb shell ls -d /data/adb/modules/* | cut -f5 -d "/"
e.g.
Code:
[[email protected] ~]$ adb shell ls -d /data/adb/modules/* | cut -f5 -d "/"
PlayStore_for_MicroG
ccbins
dummy_module
initshell
nano-ndk
terminalmods
[[email protected] ~]$
# or
[[email protected] /]$ 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
[[email protected] /]$
or on the phone:
Code:
[email protected]_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
[email protected]_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.:
Bash:
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
Bash:
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.:
Bash:
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.
Code:
# contents of my Dummy Magisk Module
#
[email protected]_I006D:/data/adb/modules/dummy_module/system # pwd
/data/adb/modules/dummy_module/system
[email protected]_I006D:/data/adb/modules/dummy_module/system # find .
.
./bin
./bin/my_new_binary
./etc
./etc/my_new_file_for_systme
[email protected]_I006D:/data/adb/modules/dummy_module/system #
After rebooting the phone the files are visible in /system:
Code:
[email protected]_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
[email protected]_I006D:/ # cat /system/etc/my_new_file_for_systme
This is my new file
[email protected]_I006D:/ #
[email protected]_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
[email protected]_I006D:/ # my_new_binary
Hello world from my dummy binary
[email protected]_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
Code:
[email protected]_I006D:/ # cat /system/etc/my_new_file_for_systme
This is my new file
[email protected]_I006D:/ #
[email protected]_I006D:/ # echo "Test Test" >> /system/etc/my_new_file_for_systme
[email protected]_I006D:/ # cat /system/etc/my_new_file_for_systme
This is my new file
Test Test
[email protected]_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.
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.
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)
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.
Code:
[email protected]_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
[email protected]
description=Dummy Module ..
[email protected]_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 FakeStore installed in the OmniROM with MicroG by default.
That's done by creating an empty file in the Magisk Module for the apk file for the FakeStore, the result looks like this:
Code:
[email protected]_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
[email protected]_I006D:/ #
[email protected]_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
[email protected]_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.
Click to expand...
Click to collapse
How to access the files replaced by Magisk
Use another bind mount to access the files replaced by Magisk, e.g:
Code:
[email protected]_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
[email protected]_I006D:/ #
[email protected]_I006D:/ #
[email protected]_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
[email protected]_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
[email protected]_I006D:/ #
[email protected]_I006D:/ # mkdir -p /data/test
[email protected]_I006D:/ #
[email protected]_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:
#
[email protected]_I006D:/ #
[email protected]_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
[email protected]_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.
Click to expand...
Click to collapse
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:
Code:
[email protected]_I006D:/ # cd /data/adb/modules/dummy_module/system
#
# files in the Magisk Module
#
[email protected]_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
[email protected]_I006D:/data/adb/modules/dummy_module/system #
#
# the new files for /product, /vendor, and /system_ext in the Magisk Module do not exist:
#
[email protected]_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
#
[email protected]_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
[email protected]_I006D:/data/adb/modules/dummy_module/system #
[email protected]_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
[email protected]_I006D:/data/adb/modules/dummy_module/system #
[email protected]_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
[email protected]_I006D:/data/adb/modules/dummy_module/system #
[email protected]_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
[email protected]_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]/
and create the file .replace in that directory and add the new file(s) to that directory.
Example:
Code:
[email protected]_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
[email protected]_I006D:/ #
[email protected]_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
[email protected]_I006D:/ #
[email protected]_I006D:/ # pwd
/
[email protected]_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
[email protected]_I006D:/ #
See How to change any file or directory using Magisk for another approach to change files on read-only mounted filesystems.
Executing scripts while booting the phone
Magisk also supports executing additional scripts while booting the phone.
The scripts to be executed must be copied to one of these directories
/data/adb/post-fs-data.d/
/data/adb/service.d/
see the description in the original Magisk documentation: https://topjohnwu.github.io/Magisk/details.html
Working examples for this technique are described in these posts:
How to run a script atevery 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 :
Bash:
# 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:
Bash:
#!/system/bin/sh
mkdir -p /cache/logs
rm -rf /cache/logs/*
/system/bin/logcat -r 102400 -n 9 -v threadTime -f /cache/logs/log >/cache/logs/info.log 2>/cache/logs/err.log &
To execute additional scripts earlier in the boot process you must add them to the boot image -- see the section Using Magisk to unpack and repack the boot image below for details.
Creating Magisk Modules
The official documentation for creating a Magisk Module is here: https://topjohnwu.github.io/Magisk/guides.html
Creating Magisk Modules that only use the standard features isn't that difficult and the official documentation should be sufficient.
I suggest to download an existing simple Magisk Module; unzip the ZIP file and study the contents.
Using Magisk to unpack and repack the boot image
The binary magiskboot that is part of the Magisk package can be used to unpack and repack the boot partition for every phone supported by Magisk.
For details see here: How to change files in the boot image using Magisk
Magisk also supports changing files in the root directory via Root Directory Overlays (see https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md) :
This feature can be used to create additional start or stop services for the Android OS:
Android uses init *rc files to define the services to start when booting and also to define the services to run when doing the shutdown (like the systemd or initd in other Linux implementations).
These files are read early in the boot process and therefore reside only in the ramdisk on the boot partition. To add new init* rc files the boot image must be modified. This can be done with Magisk:
Just add the new init*rc files and optimal other scripts or files to the boot image using Magisk . Magisk will then take care of processing the new init*rc files by the Android operating system when booting the phone.
See these posts:
How to run a script at shutdown
How to trigger an action when a property is changed
How to enable root access using Magisk in a script
for examples for using that feature.
There are also magiskboot binaries for x86 in the Magisk apk file that can be used on a PC running the Linux OS:
see How to process Android boot image files on a PC running the Linux OS for more details about this feature.
Using Magisk to change the active slot
The Magisk App can also be used to change the active slot on phones with A/B slots -- for details see here :
How to manually 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)
Code:
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 the magisk binary, e.g:
Code:
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 .
How to create the Magisk database manually
To create the Magisk database manually (e.g. after installing Magisk manually via script) these commands can be used:
Bash:
# start the Magisk daemon if necessary
#
MAGISK_DAEMON_IS_RUNNING=true
/data/adb/magisk/magisk64 -v || /data/adb/magisk/magisk64 --daemon && MAGISK_DAEMON_IS_RUNNING=false
#
# force Magisk to create the Magisk database if it not yet exists
#
/data/adb/magisk/magisk64 --sqlite "PRAGMA user_version"
# stop the Magisk daemon again
#
[ ${MAGISK_DAEMON_IS_RUNNING} = false ] && /data/adb/magisk/magisk64 --stop
To create the Magisk database using sqlite3 use these commands:
SQL:
.open ${CUR_MAGISK_DATABASE_FILE}
CREATE TABLE IF NOT EXISTS policies (uid INT, policy INT, until INT, logging INT,notification INT, PRIMARY KEY(uid));
CREATE TABLE IF NOT EXISTS settings (key TEXT, value INT, PRIMARY KEY(key));
CREATE TABLE IF NOT EXISTS strings (key TEXT, value TEXT, PRIMARY KEY(key));
CREATE TABLE IF NOT EXISTS denylist (package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));
PRAGMA user_version=6;
.exit
Note:
The Magisk database will be upgraded to the latest version automatically by the Magisk daemon after the next restart
Magisk root access configuration details
Magisk uses the table policies in the sqlite database /data/adb/magisk.db to store the list of root enabled apps.
See this post https://forum.xda-developers.com/t/how-to-enable-root-access-using-magisk-in-a-script.4527035/ for details about this feature and how to enable root access via Magisk using a script.
Magisk App installation directory
Use
Bash:
pm list packages -f | grep magisk
to get the Magisk App installation directory.
Credits: https://forum.xda-developers.com/t/magisk-general-support-discussion.3432382/page-2689#post-87694851
The Magisk App Configuration files
The Magisk App stores the configuration (e.g. the app settings) in the default directory for App Settings, on my phone this is
/data/user_de/0/com.topjohnwu.magisk/shared_prefs
The file in that directory used to store the settings is
com.topjohnwu.magisk_preferences.xml
For more details about the file see the post Where does the Magisk App store the settings?.
Temporary Magisk config directory
While running Magisk uses a temporary directory for the configuration.
The path to the config directory can be retrieved via magisk binary:
Code:
ASUS_I006D:/dev/hP3B # magisk --path
/dev/hP3B
ASUS_I006D:/dev/hP3B#
The contents of that directory look like :
Code:
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 #
Backup of the boot partitions
Magisk creates backups of the boot partitions in sub directories in /data/adb with filenames starting with magisk_backup_, e.g.:
Code:
[email protected]_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
[email protected]_I006D:/data/adb #
The uniq string after the second underscore in the name of the directory with the backup is the SHA-1 from the boot image that was patched to install Magisk:
Code:
[[email protected] /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
....
[[email protected] /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:
Code:
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:
Code:
ASUS_I006D:/ # sha1sum /sdcard/Download/boot_b.1086412.img
1a05ccb9844d3ad4f6d1873dfbf76ebf83a5bdeb /sdcard/Download/boot_b.1086412.img
ASUS_I006D:/ #
or
Code:
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.:
Code:
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.:
Code:
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:
Code:
# 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:
Bash:
grep "^SHA1=" $( magisk --path )/.magisk/config | cut -f2 -d "="
e.g.:
Code:
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)
Code:
adb shell tar --exclude data/adb/magisk/ -czf /sdcard/Download/magisk_config_$( date +%Y-%m-%d).$$.tar /data/adb/
To restore the backup unpack the tar file on the phone and reboot the phone.
To also create a backup the settings from the Magisk App create a backup of the directory with the Magisk App Settings (see above for details).
Start/Stop the Magisk App
To start the Magisk App via CLI command use
Bash:
am start -n com.topjohnwu.magisk/.ui.MainActivity
To stop the Magisk App via CLI command use:
Bash:
am force-stop com.topjohnwu.magisk
Start/Stop the Magisk Daemon
To stop the Magisk Daemon use
Bash:
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
Bash:
/data/adb/magisk/magisk64 --daemon
To check if the Magisk Daemon is running use
Bash:
/data/adb/magisk/magisk64 -v
Example output:
Code:
# 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:
Code:
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 Magisk
Using the magisk binary while the phone is booted into TWRP
If the used TWRP can mount the volume for /data you can also use the binary magisk while in TWRP. The magisk binary is not in the path while booted into TWRP - therefor you must use the fully qualified filename:
This is
/data/adb/magisk/magisk64
for 64 Bit CPUs and
/data/adb/magisk/magisk32
for 32 Bit CPUs.
Some functions of Magisk are only usable if the Magisk daemon is running. To start the Magisk daemon the Magisk binary can also be used - example:
Code:
# read the policies table from the Magisk squlite database
#
ASUS_I006D:/ # /data/adb/magisk/magisk64 --sqlite "select * from policies ;"
No daemon is currently running!
#
# -> the Magisk daemon is not running -> start it
#
1|ASUS_I006D:/ # /data/adb/magisk/magisk64 --daemon
ASUS_I006D:/ #
ASUS_I006D:/ # /data/adb/magisk/magisk64 --sqlite "select * from policies ;"
logging=1|notification=1|policy=2|uid=2000|until=0
logging=1|notification=1|policy=2|uid=10135|until=0
logging=1|notification=1|policy=2|uid=10143|until=0
logging=1|notification=1|policy=2|uid=10055|until=0
logging=1|notification=1|policy=2|uid=10142|until=0
ASUS_I006D:/ #
Use
Bash:
/data/adb/magisk/magisk64 -V
to check if the Magisk daemon is running
Use
Bash:
/data/adb/magisk/magisk64 --stop
to stop the Magisk Daemon, e.g.:
Code:
ASUS_I006D:/ # /data/adb/magisk/magisk64 -V
25200
# -> The Magisk Daemon is running
ASUS_I006D:/ # /data/adb/magisk/magisk64 --stop
ASUS_I006D:/ #
ASUS_I006D:/ # /data/adb/magisk/magisk64 -V
No daemon is currently running!
1|ASUS_I006D:/ # 2D
Trouble Shooting
If something went wrong and booting the phone does not work anymore after installing a Magisk Module 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
An error like this
Code:
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
Bash:
chmod o+r /system/app/AsusFMRadio/AsusFMRadio.apk
to fix it.
To catch errors from a script executed by Magisk you might use this technique:
Bash:
# 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:
Code:
magisk --remove-modules
to remove all modules (but not the new init scripts!) and reboot the phone
According to the FAQ Magisk will not start if the phone is booted into safe mode (see
https://topjohnwu.github.io/Magisk/faq.html)
Be aware that after rebooting the phone again in normal mode all Magisk Modules are disabled and must be enabled again using either the Magisk App or a CLI command:
To reenable all Magisk Modules via shell command do
Bash:
adb shell rm /data/adb/modules/*/disable
Spoiler: History
HIstory
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
add the link to the Howto about making a file in /system writable
corrected some formatting errors
06.11.2022/bs
added more details about the boot partition backups created by Magisk
added missing "su - -c" to some adb shell commands
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 section about how to get the Magisk App installation directory
25.11.2022/bs
added infos about how to build Magisk using a local copy of the repository
02.12.2022 /bs
added the infos about sing the magisk binary while the phone is booted into TWRP
06.12.2022 /bs
added the section Magisk root access configuration details
added infos about the files used to store the settings for the Magisk App
30.12.2022 /bs
added more infos about how to add new start / stop services in Android
07.05.2023 /bs
add infos about how to create the Magisk database manuallay
Nice.
Thank you very much for all the details and explanations.
possible to change "Automatic Response" setting via adb command?
LEENO said:
possible to change "Automatic Response" setting via adb command?
Click to expand...
Click to collapse
What do you mean with "Automatic Response"?
bnsmb said:
What do you mean with "Automatic Response"?
Click to expand...
Click to collapse
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Most probably yes ... I will check that
LEENO said:
View attachment 5778489
Click to expand...
Click to collapse
Where does the Magisk App store the settings?

How To Guide How to enable access via adb on a new installed OS

For automatic installation and configuration of a phone running the Android OS the access via adb is required. "Unfortunately" the access via adb is disabled in most Android distributions (in reality it is of course good that the access via adb is disabled in the default setting! See also the notes regarding the LineageOS at the end of this post))
There are (at least) two ways to enable access via adb:
Manually via the GUI of Android on the phone or via the Android command settings executed by the user root in a shell ,e.g.:
Bash:
settings put global development_settings_enabled 1
settings put global adb_enabled 1
The latter can also be done in a script in an adb shell -- but of course we need a working adb connection ....
One way around this problem is to use Magisk:
In principle this could be done using an init script for Magisk:
Magisk allows to run scripts while booting the phone (see How to run a script at every boot using Magisk); this feature could be used to enable adb via script.
Unfortunately, this method no longer works in the latest Android versions (Androd 12 and Android 13; Android 11 not tested):
Android now uses encryption for most of the files in the subdirectories in /data and to initiate the encryption keys for the files in these directories, the phone must boot once from the installed OS.
To use a Magisk init script to enable the access via adb, we need to create files in the directories /data/adb and /data/misc/adb. If you create these files while booting into TWRP recovery without a configured Android operating system, these files will be created unencrypted, making it impossible to boot the installed Android operating system:
If the Android operating system finds unencrpyted files in these directories at the first reboot, it boots into the installed recovery with the famous error message "Cannot load Android system. Your data may be corrupt" (see How to fix the error "Cannot load Android system. Your data may be corrupt" for details)
Fortunately, the Android operating system does not encrypt files in the directory /data/recovery (and therefore continues booting if there are already files in that directory) and the part of Magisk in the boot partition does not need the Magisk files /data/adb.
So to enable the access via adb using Magisk, we can create a script in the directory /data/recovery and a new Init .rc file for the Android operating system to execute that script while the Android OS is booting - for details about how to do that see How to trigger an action when a property is changed.
The working method to enable adb via Magisk is as follows:
- Boot the phone from a recovery (like TWRP)
- Install the new OS image but do not reboot the phone (see How to install an OS image using the TWRP binary twrp for how to install the OS via the cli commands from TWRP)
- Install Magisk into the boot partition of the phone and delete all files in /data/adb afterwards (see How to install Magisk into the boot partition using a script for how to install Magisk via script)
- copy a script to the directory /data/recovery on the phone that will enable access via adb using the Android command settings and copy the public ssl keys for the access via adb to the directory /data/misc/adb on the phone
- install a new init .rc file via Magisk which executes the script in the direcory /data/recovery to enable the access via adb - this script will be executed while doing the 1st reboot of the new installed Android OS.
(see How to trigger an action when a property is changed for how to add a new init .rc file using Magisk)
- reboot the phone
I wrote two scripts to enable adb using this method:
enable_adb_using_magisk.sh
The script enable_adb_using_magisk.sh must run in an adb session in TWRP after the new OS was installed and after Magisk has been installed into the boot partition but before the first reboot of the new installed operating system.
The script enable_adb_using_magisk.sh
- creates the script /data/recover/enable_adb_via_service.sh on the phone to enable adb and install the public ssl key for access via adb
- creates a new init *rc file using Magisk that will run the script /data/recover/enable_adb_via_service.sh on the phone while doing the 1st reboot (after the file encryption for the files /data is initalized by the Android OS)
To use the script enable_adb_using_magisk.sh manually after installing the new OS and Magisk do while the phone is booted into TWRP:
Bash:
#
# copy the script enable_adb_using_magisk.sh to the directory /tmp on the phone
#
adb push enable_adb_using_magisk.sh /tmp/enable_adb_using_magisk.sh
# copy the public ssl key for access via adb to the directory /tmp on the phone
#
adb push $HOME/.android/adbkey.pub /tmp/adbkey.pub
# execute the script /tmp/enable_adb_using_magisk.sh on the phone
#
adb shell sh /tmp/enable_adb_using_magisk.sh
# reboot the phone into the new installed OS
#
adb reboot
Notes:
The script enable_adb_using_magisk.sh installs the init *rc file into the boot partition from the slot for the next reboot: In this scenario this is the slot with the new installed OS.
To specify the boot partition to use add the boot partition as parameter, e.g.
Bash:
adb shell sh /tmp/enable_adb_using_magisk.sh _a
The init *rc service used is:
Spoiler: init *rc service to enable adb
Code:
#
# additional RC service to enable the access via adb after the 1st boot into the new installed Android OS
#
service bnsmb_enable_adb /system/bin/sh /data/recovery/enable_adb_via_service.sh
user root
group root
seclabel u:r:magisk:s0
disabled
oneshot
on zygote-start
setprop sys.bnsmb_enable_adb_done 0
start bnsmb_enable_adb
#
# Note: the following entries are for testing only!
#
on zygote-start
write /data/recovery/semfile Here_I_am
setprop sys.bnsmb.test.okay 0
The script enable_adb_using_magisk.sh creates the files adb_keys and adb_temp_keys.xml in the directory /data/misc/adb if the public ssl key /tmp/adbkey.pub exists on the phone.
Note that the file /data/misc/adb/db_temp_keys.xml is an Android Binary XML fIle (see How to change files in Android XML Binary format for details about Android Binary XML files)
To avoid the manual steps listed above I wrote another script:
enable_access_via_adb.sh
The script enable_access_via_adb.sh does all the steps listed above; the usage for enable_access_via_adb.sh is
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -h
enable_access_via_adb.sh version - v1.0.0.0 - shell script to enable adb access via an init .rc file configured via Magisk
enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Spoiler: Detailed Usage
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -H
enable_access_via_adb.sh version - v1.0.0.0 - shell script to enable adb access via an init .rc file configured via Magisk
Usage
enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]
All parameter are optional. The parameter can be used in any order.
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
If the parameter "--reboot" is used the script will reboot the phone after installing Init .rc file; to disable the automatic reboot use the parameter "--noreboot".
Default is to ask the user for confirmation to reboot the phone.
Use the parameter "--nopubkey" to disable configuring the public key of the current user on the PC for the access via adb; use the parameter "--pubkey" to configure
the public key; default is to configure the public key.
The default public ssl key used is the key in the file "${HOME}/.android/adbkey.pub".
To change some of the values used by the script these environment variables can be set before starting the script:
Set the environment variable PUBLIC_KEY_ON_PC to the file with the public ssl key to use for the access via adb if another public key should be used
Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access
See also the source code of the script
boot_phone_from_twrp.sh
for environment variables supported by this script
Prerequisites
- the phone must be connected via USB
- Magisk must be already installed in the boot partition of the phone
- the phone must be either booted into the fastbootd or bootloader with a working fastboot connection
or already booted into a recovery image with working adb connnection
The scripts boot_phone_from_twrp.sh and enable_adb_using_magisk.sh are required by this script -- see the source code of the script
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
So installing a new OS and enabling access via adb in a script without user intervention can be done using these commands:
Bash:
#
# do a factory reset and install the OS image using the TRWP binary twrp
#
REBOOT=no ./install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/omnirom_local/omni-13/omni-13-20221211-zenfone8-MICROG.zip
# install Magisk into the boot sector of the partition with the new OS and delete all files in /data/adb afterwards
#
# The parameter delete_adb_dir for the script "install_magisk_via_twrp.sh" is only available in version 2.2.0.0 or newer of the script.
#
REBOOT=no ./install_magisk_via_twrp.sh next copy_apk delete_adb_dir
# enable adb and copy the public keys for adb via new init *rc file using Magisk in the boot partition
#
REBOOT=no ./enable_access_via_adb.sh
# reboot the phone
#
adb reboot
# wait until the new OS is booted and adb is enabled
#
adb wait-for-device
# install the Magisk App
#
./install_apk.sh /data/backup/Android/EssentialApps/Magisk-v25.2.apk
# install the Magisk directories and binaries in /data/adb
#
REBOOT=no ./install_magisk_via_twrp.sh adb_only
# (optional) enable root access for the adb shell
#
REBOOT=yes ./init_magisk_db.sh
Notes:
The script enable_access_via_adb.sh needs the helper script boot_phone_from_twrp.sh.sh and the script enable_adb_using_magisk.sh .
The scripts can be downloaded from my homepage:
enable_access_via_adb.sh
enable_adb_using_magisk.sh
boot_phone_from_twrp.sh
The other scripts mentioned in ths post can also be downloaded from my home page:
install_os_via_twrp.sh (see How to install an OS image using the TWRP binary twrp for a descripton of this script)
install_magisk_via_twrp.sh (see How to install Magisk into the boot partition using a script for a description for this script)
install_apk.sh (see How to install packages (apk files) for Android via script for a description for this script)
init_magisk_db.sh (see How to enable root access using Magisk in a script for a description for this script)
Disabling the service to enable access via adb
To only run once, the script enable_adb_using_magisk.sh creates the file /data/recovery/adb_initialized exists on the phone. If that file already exists when enable_adb_using_magisk.sh starts, the script will do nothing and end immediately.
Therefor, it's not necessary to delete the new init *rc service.
It might even be useful to leave the service installed, since it can be used for other purposes later by modifying the script
However, if necessary, you can either delete the script used by the init *rc service enable_adb_using_magisk.sh or reinstall the original boot partition to get rid of the new service completely:
Bash:
#
# while the phone is booted into the Android OS with enabled root access or while the phone is booted into TWRP
#
# use the parameter "yes" for the script to run without requesting user input
#
adb shell su - -c /data/recovery/work/restore_boot_partition.sh
Directories and files used
Name
Type
Content
Comment
/tmp/adbkey.pubfilessl public key that should be configured/tmp is on a ramdisk so this file is lost after booting the phone/data/recovery/workDirectorytemporary files used for enabling adb/data/recovery/work/enable_adb_via_service.rcfileinit *rc file added to the boot partition via Magiks/data/recovery/work/boot_a.imgfileimage of the original boot partition/data/recovery/work/original_boot_partition.imgSymLinklink to the image with the original boot partition/data/recovery/work/restore_boot_partition.shfileScript to restore the boot partition using the image of the original boot partition/data/recovery/work/header
/data/recovery/work/kernel
/data/recovery/work/ramdisk.cpiofilescontents of the boot partition/data/recovery/adbkey.pubfilepublic ssl key to be configured for access via adb/data/recovery/adb_temp_keys.xml.humanfileXML file with the configuration for the access via adb
(will be converted to an XML file in Android binary XML format in /data/misc/adb)/data/recovery/enable_adb_via_service.shfilescript to enable the access via adb and installl the public ssl key for the access via adb
The script is configured in the init*rc file /data/recovery/work/enable_adb_via_service.rc/data/recovery/enable_adb_via_service.logfilelog file created by the script /data/recovery/enable_adb_via_service.sh/data/recovery/adb_initializedfilesemphor file for the script /data/recovery/enable_adb_via_service.sh - if this file exists the script will do nothing/data/recovery/semfilefiletest file created by the new init *rc service/data/recovery/log.gz
/data/recovery/last_log.gz
/data/recovery/recovery.fstab
/data/recovery/storage.fstabfileFiles created by TWRPonly for information
Enabling access via adb in the LineageOS (Update 11.01.2023)
In the LineageOS 19 for the ASUS Zenfone 8 access via adb is enabled in the default configuration.
Therefor these instructions are not necessary. The only configuration that must be done on a phone running the LineageOS is to copy the public ssl keys to /data/misc/adb to enable the access via adb.
Unfortunately the method used for this described in this post seems not to work in the LineageOS.
In addition, root access via adb can be enabled in the developer settings in the LineageOS. Until now I did not find out how to enable the plain root access from the LineageOS from within a script.
userdebug builds
In userdebug builds of Android the access via adb (without authentification) is enabled in the default.
Trouble Shooting
The script enable_adb_using_magisk.sh uses the log file /data/recovery/enable_adb_via_service.log if executed via init *rc service.
To check if the init *rc file was configured successfully check the properties
sys.bnsmb_enable_adb_done
sys.bnsmb.test.okay
in the running Android OS. Both properties should be defined with the value 0:
Code:
ASUS_I006D:/ # getprop sys.bnsmb_enable_adb_done
0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop sys.bnsmb.test.okay
0
ASUS_I006D:/ #
In addition, if everything worked there should exist the file /data/recovery/semfile.
Code:
130|ASUS_I006D:/ # ls -l /data/recovery/semfile
-rw------- 1 root root 9 2022-12-29 11:44 /data/recovery/semfile
ASUS_I006D:/ #
Use the Android command start to check if the service exists and can be started, e.g.
Spoiler: start bnsmb_enable_adb
Code:
ASUS_I006D:/ # start bnsmb_enable_adb ; dmesg | tail -10
[ 3069.861365] [ 3069.861365] (CPU:2-pid:1:init) [12:35:31.711593934] init: Service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) exited with status 0 waiting took 0.024000 seconds
[ 3069.861402] [ 3069.861402] (CPU:2-pid:1:init) [12:35:31.711630236] init: Sending signal 9 to service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) process group...
[ 3069.861618] [ 3069.861618] (CPU:2-pid:1:init) [12:35:31.711846069] libprocessgroup: Successfully killed process cgroup uid 1000 pid 5734 in 0ms
[ 3071.202534] [ 3071.202486] (CPU:2-pid:5077:wk:0xffffffe40) [12:35:33.052716433] [BAT][CHG]asus_jeita_cc_worker set BATTMAN_OEM_WORK_EVENT : WORK_JEITA_CC
[ 3071.220807] [ 3071.220807] (CPU:0-pid:350:wk:0xffffffe40) [12:35:33.071038100] [BAT][CHG]handle_message set property:16 successfully
[ 3071.570753] [ 3071.570753] (CPU:2-pid:1:init) [12:35:33.420982631] init: starting service 'bnsmb_enable_adb'...
[ 3071.575971] [ 3071.575971] (CPU:3-pid:1:init) [12:35:33.426199819] init: Control message: Processed ctl.start for 'bnsmb_enable_adb' from pid: 5735 (start bnsmb_enable_adb)
[ 3071.614570] [ 3071.614570] (CPU:3-pid:1:init) [12:35:33.464799298] init: Service 'bnsmb_enable_adb' (pid 5736) exited with status 0 oneshot service took 0.040000 seconds in background
[ 3071.614609] [ 3071.614609] (CPU:3-pid:1:init) [12:35:33.464837267] init: Sending signal 9 to service 'bnsmb_enable_adb' (pid 5736) process group...
[ 3071.614788] [ 3071.614788] (CPU:3-pid:1:init) [12:35:33.465016277] libprocessgroup: Successfully killed process cgroup uid 0 pid 5736 in 0ms
ASUS_I006D:/ #
If something does not work execute the script manually in an adb session to check :
Spoiler: /data/recovery/enable_adb_via_service.sh
Code:
ASUS_I006D:/ # rm -f /data/recovery/adb_initialized
ASUS_I006D:/ #
ASUS_I006D:/ #
ASUS_I006D:/ # sh -x /data/recovery/enable_adb_via_service.sh
+ tty -s
+ '[' 0 -ne 0 ']'
+ PATH=/system/bin:/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin
+ export PATH
+ '[' ! -r /data/recovery/adb_initialized ']'
+ echo 'Sleeping 30 seconds now ...'
Sleeping 30 seconds now ...
+ sleep 30
+ touch /data/recovery/adb_initialized
+ settings put global development_settings_enabled 1
+ settings put global adb_enabled 1
+ '[' ! -d /data/misc/adb ']'
+ '[' ! -r /data/misc/adb/adb_keys ']'
+ '[' -r /data/recovery/adbkey.pub ']'
+ >>/data/misc/adb/adb_keys
+ cat /data/recovery/adbkey.pub
+ '[' -r /data/recovery/adb_temp_keys.xml.human ']'
+ xml2abx /data/recovery/adb_temp_keys.xml.human /data/misc/adb/adb_temp_keys.xml
+ chmod 0600 /data/misc/adb/adb_temp_keys.xml
+ chown system:shell /data/misc/adb/adb_temp_keys.xml
+ chcon -v u:object_r:adb_keys_file:s0 /data/misc/adb/adb_temp_keys.xml
chcon '/data/misc/adb/adb_temp_keys.xml' to u:object_r:adb_keys_file:s0
ASUS_I006D:/ #
Thank you for your explanation, I leave my footprint, I need this sometimes.

Categories

Resources