[ASUS ZenFone 8] bnsmb:
For automatic installation and configuration of a phone running the Android OS the access via adb is required. "Unfortunately" the access via adb is disabled in most Android distributions (in reality it is of course good that the access via adb is disabled in the default setting! See also the notes regarding the LineageOS at the end of this post))

There are (at least) two ways to enable access via adb:

Manually via the GUI of Android on the phone or via the Android command settings executed by the user root in a shell ,e.g.:

settings put global development_settings_enabled 1
settings put global adb_enabled 1

The latter can also be done in a script in an adb shell -- but of course we need a working adb connection ....

One way around this problem is to use Magisk:

In principle this could be done using an init script for Magisk:

Magisk allows to run scripts while booting the phone (see How to run a script at every boot using Magisk); this feature could be used to enable adb via script.

Unfortunately, this method no longer works in the latest Android versions (Androd 12 and Android 13; Android 11 not tested):

Android now uses encryption for most of the files in the subdirectories in /data and to initiate the encryption keys for the files in these directories, the phone must boot once from the installed OS.

To use a Magisk init script to enable the access via adb, we need to create files in the directories /data/adb and /data/misc/adb. If you create these files while booting into TWRP recovery without a configured Android operating system, these files will be created unencrypted, making it impossible to boot the installed Android operating system:

If the Android operating system finds unencrpyted files in these directories at the first reboot, it boots into the installed recovery with the famous error message "Cannot load Android system. Your data may be corrupt" (see How to fix the error "Cannot load Android system. Your data may be corrupt" for details)

Fortunately, the Android operating system does not encrypt files in the directory /data/recovery (and therefore continues booting if there are already files in that directory) and the part of Magisk in the boot partition does not need the Magisk files /data/adb.

So to enable the access via adb using Magisk, we can create a script in the directory /data/recovery and a new Init .rc file for the Android operating system to execute that script while the Android OS is booting - for details about how to do that see How to trigger an action when a property is changed.

The working method to enable adb via Magisk is as follows:

- Boot the phone from a recovery (like TWRP)

- Install the new OS image but do not reboot the phone (see How to install an OS image using the TWRP binary twrp for how to install the OS via the cli commands from TWRP)

- Install Magisk into the boot partition of the phone and delete all files in /data/adb afterwards (see How to install Magisk into the boot partition using a script for how to install Magisk via script)

- copy a script to the directory /data/recovery on the phone that will enable access via adb using the Android command settings and copy the public ssl keys for the access via adb to the directory /data/misc/adb on the phone

- install a new init .rc file via Magisk which executes the script in the direcory /data/recovery to enable the access via adb - this script will be executed while doing the 1st reboot of the new installed Android OS.

(see How to trigger an action when a property is changed for how to add a new init .rc file using Magisk)

- reboot the phone

I wrote two scripts to enable adb using this method:

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:

#
# copy the script enable_adb_using_magisk.sh to the directory /tmp on the phone
#
adb push enable_adb_using_magisk.sh /tmp/enable_adb_using_magisk.sh

# copy the public ssl key for access via adb to the directory /tmp on the phone
#
adb push $HOME/.android/adbkey.pub /tmp/adbkey.pub

# execute the script /tmp/enable_adb_using_magisk.sh on the phone
#
adb shell sh /tmp/enable_adb_using_magisk.sh

# reboot the phone into the new installed OS
#
adb reboot

Notes:

The script enable_adb_using_magisk.sh installs the init *rc file into the boot partition from the slot for the next reboot: In this scenario this is the slot with the new installed OS.

To specify the boot partition to use add the boot partition as parameter, e.g.

adb shell sh /tmp/enable_adb_using_magisk.sh _a

The init *rc service used is:

Spoiler content hidden.

The script enable_adb_using_magisk.sh creates the files adb_keys and adb_temp_keys.xml in the directory /data/misc/adb if the public ssl key /tmp/adbkey.pub exists on the phone.

Note that the file /data/misc/adb/db_temp_keys.xml is an Android Binary XML fIle (see How to change files in Android XML Binary format for details about Android Binary XML files)

To avoid the manual steps listed above I wrote another script:

enable_access_via_adb.sh

The script enable_access_via_adb.sh does all the steps listed above; the usage for enable_access_via_adb.sh is

[ OmniRom 13 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -h
enable_access_via_adb.sh version - v1.0.0.0 - shell script to enable adb access via an init .rc file configured via Magisk

enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]

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

Spoiler content hidden.

So installing a new OS and enabling access via adb in a script without user intervention can be done using these commands:

#
# do a factory reset and install the OS image using the TRWP binary twrp
#
REBOOT=no ./install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/omnirom_local/omni-13/omni-13-20221211-zenfone8-MICROG.zip

# install Magisk into the boot sector of the partition with the new OS and delete all files in /data/adb afterwards
#
# The parameter delete_adb_dir for the script "install_magisk_via_twrp.sh" is only available in version 2.2.0.0 or newer of the script.
#
REBOOT=no ./install_magisk_via_twrp.sh next copy_apk delete_adb_dir

# enable adb and copy the public keys for adb via new init *rc file using Magisk in the boot partition
#
REBOOT=no ./enable_access_via_adb.sh

# reboot the phone
#
adb reboot

# wait until the new OS is booted and adb is enabled
#
adb wait-for-device

# install the Magisk App
#
./install_apk.sh /data/backup/Android/EssentialApps/Magisk-v25.2.apk

# install the Magisk directories and binaries in /data/adb
#
REBOOT=no ./install_magisk_via_twrp.sh adb_only

# (optional) enable root access for the adb shell
#
REBOOT=yes ./init_magisk_db.sh

Notes:

The script enable_access_via_adb.sh needs the helper script boot_phone_from_twrp.sh.sh and the script enable_adb_using_magisk.sh .

The scripts can be downloaded from my homepage:

enable_access_via_adb.sh

enable_adb_using_magisk.sh

boot_phone_from_twrp.sh

The other scripts mentioned in ths post can also be downloaded from my home page:

install_os_via_twrp.sh (see How to install an OS image using the TWRP binary twrp for a descripton of this script)

install_magisk_via_twrp.sh (see How to install Magisk into the boot partition using a script for a description for this script)

install_apk.sh (see How to install packages (apk files) for Android via script for a description for this script)

init_magisk_db.sh (see How to enable root access using Magisk in a script for a description for this script)

Disabling the service to enable access via adb

To only run once, the script enable_adb_using_magisk.sh creates the file /data/recovery/adb_initialized exists on the phone. If that file already exists when enable_adb_using_magisk.sh starts, the script will do nothing and end immediately.

Therefor, it's not necessary to delete the new init *rc service.

It might even be useful to leave the service installed, since it can be used for other purposes later by modifying the script

However, if necessary, you can either delete the script used by the init *rc service enable_adb_using_magisk.sh or reinstall the original boot partition to get rid of the new service completely:

#
# while the phone is booted into the Android OS with enabled root access or while the phone is booted into TWRP
#

# use the parameter "yes" for the script to run without requesting user input
#
adb shell su - -c /data/recovery/work/restore_boot_partition.sh

Directories and files used

Name

Type

Content

Comment

/tmp/adbkey.pub	file	ssl public key that should be configured	/tmp is on a ramdisk so this file is lost after booting the phone

/data/recovery/work	Directory	temporary files used for enabling adb
/data/recovery/work/enable_adb_via_service.rc	file	init *rc file added to the boot partition via Magiks
/data/recovery/work/boot_a.img	file	image of the original boot partition
/data/recovery/work/original_boot_partition.img	SymLink	link to the image with the original boot partition
/data/recovery/work/restore_boot_partition.sh	file	Script to restore the boot partition using the image of the original boot partition
/data/recovery/work/header

/data/recovery/work/kernel

/data/recovery/work/ramdisk.cpio	files	contents of the boot partition

/data/recovery/adbkey.pub	file	public ssl key to be configured for access via adb
/data/recovery/adb_temp_keys.xml.human	file	XML file with the configuration for the access via adb

(will be converted to an XML file in Android binary XML format in /data/misc/adb)
/data/recovery/enable_adb_via_service.sh	file	script to enable the access via adb and installl the public ssl key for the access via adb

The script is configured in the init*rc file /data/recovery/work/enable_adb_via_service.rc
/data/recovery/enable_adb_via_service.log	file	log file created by the script /data/recovery/enable_adb_via_service.sh
/data/recovery/adb_initialized	file	semphor file for the script /data/recovery/enable_adb_via_service.sh - if this file exists the script will do nothing
/data/recovery/semfile	file	test file created by the new init *rc service

/data/recovery/log.gz

/data/recovery/last_log.gz

/data/recovery/recovery.fstab

/data/recovery/storage.fstab	file	Files created by TWRP	only for information

Enabling access via adb in the LineageOS (Update 11.01.2023)

In the LineageOS 19 for the ASUS Zenfone 8 access via adb is enabled in the default configuration.

Therefor these instructions are not necessary. The only configuration that must be done on a phone running the LineageOS is to copy the public ssl keys to /data/misc/adb to enable the access via adb.

Unfortunately the method used for this described in this post seems not to work in the LineageOS.

In addition, root access via adb can be enabled in the developer settings in the LineageOS. Until now I did not find out how to enable the plain root access from the LineageOS from within a script.

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:

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.

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 content hidden.

If something does not work execute the script manually in an adb session to check :

Spoiler content hidden.