[WIP] [Redmi 2 ProT] [wt86047] [wt88047] Gnome-mobile - Phosh - Plasma mobile postmarketOS aarch64 - Xiaomi Redmi 2 Guides, News, & Discussion

Link lk2nd, rootfs, twrp, unlock bootloader
https://drive.google.com/drive/folders/1LnHHdc0s-CVMFgR6hCuHxJg4_BUutxxT
default user is user, passwd is 147147
Reference wiki, Redmi 2 full support on community branch:
https://wiki.postmarketos.org/wiki/Xiaomi_Redmi_2_(xiaomi-wt88047)
Xiaomi MI 2/2S (xiaomi-aries) & Xiaomi Redmi 2 (wt86047/wt88047) using factory stock ROM Android 4.2/4.4 to unlock bootloader, no need mi_unlock tool
Redmi 2 variants:
Global wt88047: HM2014811, HM2014812, HM2014817, HM2014818, HM2014819, and HM2014821
China wt86047: HM2014813, HM2014512 and HM2014112
Redmi 2 ProT factory ROM:
https://mifirm.net/model/wt86047.ttt#cn-fastboot-dev
Installing Alpine Linux on Virutalbox:
Go to MI fastboot, press Power + Volume Down
Connect Redmi 2 ProT to PC/Laptop. Using MiFlash to come back stock Android 4.4, boot to Adnroid
Go to MI fastboot, press Power + Volume Down
Using Mi Bootloader Unlock Tool [No Auth ID Required]_BY_flash-firmware.blogspot.com.zip on gdrive to unlock bootloader
Go to MI fastboot, press Power + Volume Down
Instal TWRP on gdrive twrp-3.2.1-wt86047.img
# sudo fastboot flash recovery twrp-3.2.1-wt86047.img
Install firmware LineageOS
https://wiki.lineageos.org/devices/wt88047/install#updating-firmware
Enter TWRP, press hold Power + Volume Up + Volume Down
Go to Advance → ADB Sideload → Slide to install
On PC/Laptop, flash new firmware for Lineage OS (modem, wifi driver, bluetooth, etc.):
# sudo adb sideload wt86047-firmware_20161223.zip
Go to MI fastboot, press Power + Volume Down, to flash lk2nd-msm8916.img
https://github.com/msm8916-mainline/lk2nd#installation
# sudo fastboot flash boot lk2nd-msm8916.img
Build image from pmbootstrap on PC/Laptop:
# sudo apk add pmbootstrap python3 coreutils procps git android-tools
# pmbootstrap init
Channel []: edge
Vendor []: xiaomi
Device codename []: wt88047
Kernel []: mainline-modem
Enable this package? (y/n) [y]: y
Username []: user
User interface [gnome-mobile]: gnome-mobile ← phosh or plasma-mobile
Extra packages []: nano,vim
Choose default locale installation [en_US.UTF-8]: en_US.UTF-8
Device hostname (short form, e.g ‘foo’) []: wt86047-gnome-mobile
Build outdate packages during ‘pmbootstrap install’? (y/n) [y]: y
# pmbootstrap status
Before build image, get packages on chroot local
# pmbootstrap chroot
~/# apk add abuild build-base ccache git devicepkg-dev mkbootimg postmarketos-base ccache-cross-symlinks gcc-aarch64 g++-aarch64 crossdirect ncurses-dev bash bc bison elfutils-dev flex gmp-dev installkernel linux-headers openssl-dev perl sed binutils-aarch64
# pmbootstrap --details-to-stdout install
*** SET LOGIN PASSWORD FOR: 'user' ***
(rootfs_xiaomi-wt88047) % passwd user
New password: 147147
Retype new password: 147147
# pmbootstrap export
Image folder: /home/[username]/.local/var/pmbootstrap/chroot_native/home/pmos/rootfs/xiaomi-wt88047.img
Boot folder: /home/[username]/.local/var/pmbootstrap/chroot_rootfs_xiaomi_wt88047/boot
Install rootfs, go to lk2nd fastboot: Power on the device. After it vibrates, hold Volume Down
On PC/Laptop, go to Redmi 2 ProT image folder
Unzip if you download image from gdrive
# sudo unxz -v xiaomi-wt88047.img.xz
# sudo fastboot flash userdata xiaomi-wt88047.img
# sudo fastboot erase system
# sudo fastboot reboot
Enjoy Phosh pmOS on Xiaomi Redmi 2 ProT!!!
Reference link: https://tinhte.vn/thread/postmarket...-wt86047-wt88047-kernel-5-14-aarch64.3413747/
GNOME Shell on Mobile aka Gnome-mobile
Docker run on redmi2 ProT like RaspberryPi

Related

Arch Linux - Lightweight Custom Environment Setup

Arch Linux - Lightweight Custom Environment Setup Instructions​
NOTICE: I am not responsible if something happens with your device, proceed at your own risk. It is always recommended to follow Arch's own installation guide, even though these instructions below are from it already just more simplified.​
This article is an alternative to the R method from my minimal Arch Linux instructions which you can find here, so don't do anything from here unless you have Arch Linux already installed either from my instructions or from the wiki! This instructions consists of the following packages for creating the custom environment:
i3: dynamic tiling window manager
LightDM: login page
tint2: bottom bar
rofi: application launcher
xfce4-terminal: lightweight terminal
nitrogen: wallpaper tool
thunar: lightweight file manager
autotiling: better autotiling for the window manager
PulseAudio and pavucontrol: audio manager
This has been tested in VMs and on my main computer.
--
Table of Content:
I. Lightweight Custom Environment Installation
II. Optional Notes
--​
I. Lightweight Custom Environment Installation​
R. Installing the Custom Environment
First let's enable NetworkManager, type this (do this if you came here after following my own Arch Linux installation instructions and you want to continue step R from here, or else connect to your internet in any way you want):
sudo systemctl enable NetworkManager
Reboot by typing "reboot", then after the reboot type "nmtui" and connect to your internet.
Next, update your system by typing:
sudo pacman -Syu
Now let's install these packages: xorg-server, lightdm, i3-gaps and tint2. Type:
Note: you can replace "lightdm-gtk-greeter" with another greeter of your choice, check here: https://wiki.archlinux.org/title/LightDM#Greeter
sudo pacman -S xorg-server lightdm lightdm-gtk-greeter i3-gaps tint2
After the packages are installed, enable lightdm by typing:
sudo systemctl enable lightdm
Now let's setup LightDM, type this in order to edit the config:
sudo nano /etc/lightdm/lightdm.conf
Change the [Seat:*] section, which is this:
...
greeter-session=lightdm-yourgreeter-greeter
...
Instead of "yourgreeter" replace it with the greeter you installed, for example in this case it's "gtk".
Type "reboot" to reboot your system, you should see the login page. Login to i3 (not the one with "(with debug log)", click the arrow and change to "i3") and follow the steps to create config file. After creating the config file, you can continue:
Go to TTY by doing the combination (usually ALT+SHIFT+F2) and run this:
sudo cp /etc/i3/config ~/.config/i3/
Now let's setup tint2 by adding this at the bottom of the i3 config file (run this command to be able to edit the config file: sudo nano $HOME/.config/i3/config):
exec --no-startup-id tint2
Now let's install rofi and xfce4-terminal by running this command:
sudo pacman -S rofi xfce4-terminal
Exit TTY by doing the combination (usually ALT+SHIFT+F1), if you're facing issues just reboot pc.
ALT+Enter (after logging in from the login page) is the combination to open terminal.
Now let's finish setting up rofi, do the combination above to open the terminal and run this command in it:
sudo nano ~/.config/i3/config
Go to the "start dmenu (a program launcher)" section, comment the second line, it should be like this:
# bindsym Mod1+d exec --no-startup-id dmenu_run
the # is commenting the line. And uncomment the third line, which is:
bindsym Mod1+d exec "rofi -modi drun,run -show drun"
the # should be gone
Then save by CTRL+S and close by CTRL+X and reboot your system.
Now we will configure i3 further for tint2, remove the following from the i3 configuration file:
# Start i3bar to display a workspace bar (plus the system information i3 status
# finds out, if available)
bar {
status_command i3status
}
This will be at the bottom of the config file ^ it removes i3bar so only i3 would start.
Then reboot your system by typing "reboot" in the terminal.
After rebooting, when doing the combination ALT+D it opens the application menu.
This step is optional, but it is recommended because it will let you install packages from the AUR (user repisotary). We will also install yay by running these separately:
(Let’s install git in order to install yay afterwards with it)
sudo pacman -S git
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
Install Nitrogen and Thunar to have a wallpaper tool and a file manager by running this command:
sudo pacman -S nitrogen thunar
Check here for more information about Nitrogen and how to use it: https://wiki.archlinux.org/title/nitrogen#Usage
And add this at the bottom of the i3 config file:
exec nitrogen --restore &
Now let's install the user folders (Documents, Pictures...) by installing this package:
sudo pacman -S xdg-user-dirs
Then run this in terminal:
xdg-user-dirs-update
Now let's install autotiling https://aur.archlinux.org/packages/autotiling by running this command:
yay -S autotiling
Add "exec_always --no-startup-id autotiling" to the ~/.config/i3/config file.
At last, let's install PulseAudio by running this command:
sudo pacman -S pulseaudio pulseaudio-alsa pavucontrol
Now reboot your PC and enjoy! You should have memorized how to do this already, it's straight forward and simple.
II. Optional Notes​
Type this in the command in the terminal to check the themes available for rofi:
rofi-theme-selector
For controlling the volume, install this package:
yay -S pnmixer
And then add this at the bottom of your i3 config file:
exec_always pnmixer
Restart your PC
In order to fill your wallpaper fullscreen with Nitrogen, you can do it with this command:
nitrogen --set-zoom-fill /path/to/image.png
That's it! Let me know how it goes with you
I use some of these packages now, like Thunar over Dolphin, myself, in most cases, but still end up going back to Dolphin in certain situations. For one, it handles loading thumbnails better and faster, so browsing images is easier.
Everybody loves xfce4-terminal. Even Arch users!
SerjSX said:
Arch Linux - Lightweight Custom Environment Setup Instructions​
NOTICE: I am not responsible if something happens with your device, proceed at your own risk. It is always recommended to follow Arch's own installation guide, even though these instructions below are from it already just more simplified.​
This article is an alternative to the R method from my minimal Arch Linux instructions which you can find here, so don't do anything from here unless you have Arch Linux already installed either from my instructions or from the wiki! This instructions consists of the following packages for creating the custom environment:
i3: dynamic tiling window manager
LightDM: login page
tint2: bottom bar
rofi: application launcher
xfce4-terminal: lightweight terminal
nitrogen: wallpaper tool
thunar: lightweight file manager
autotiling: better autotiling for the window manager
PulseAudio and pavucontrol: audio manager
This has been tested in VMs and on my main computer.
--
Table of Content:
I. Lightweight Custom Environment Installation
II. Optional Notes
--​
I. Lightweight Custom Environment Installation​
R. Installing the Custom Environment
First let's enable NetworkManager, type this (do this if you came here after following my own Arch Linux installation instructions and you want to continue step R from here, or else connect to your internet in any way you want):
sudo systemctl enable NetworkManager
Reboot by typing "reboot", then after the reboot type "nmtui" and connect to your internet.
Next, update your system by typing:
sudo pacman -Syu
Now let's install these packages: xorg-server, lightdm, i3-gaps and tint2. Type:
Note: you can replace "lightdm-gtk-greeter" with another greeter of your choice, check here: https://wiki.archlinux.org/title/LightDM#Greeter
sudo pacman -S xorg-server lightdm lightdm-gtk-greeter i3-gaps tint2
After the packages are installed, enable lightdm by typing:
sudo systemctl enable lightdm
Now let's setup LightDM, type this in order to edit the config:
sudo nano /etc/lightdm/lightdm.conf
Change the [Seat:*] section, which is this:
...
greeter-session=lightdm-yourgreeter-greeter
...
Instead of "yourgreeter" replace it with the greeter you installed, for example in this case it's "gtk".
Type "reboot" to reboot your system, you should see the login page. Login to i3 (not the one with "(with debug log)", click the arrow and change to "i3") and follow the steps to create config file. After creating the config file, you can continue:
Go to TTY by doing the combination (usually ALT+SHIFT+F2) and run this:
sudo cp /etc/i3/config ~/.config/i3/
Now let's setup tint2 by adding this at the bottom of the i3 config file (run this command to be able to edit the config file: sudo nano $HOME/.config/i3/config):
exec --no-startup-id tint2
Now let's install rofi and xfce4-terminal by running this command:
sudo pacman -S rofi xfce4-terminal
Exit TTY by doing the combination (usually ALT+SHIFT+F1), if you're facing issues just reboot pc.
ALT+Enter (after logging in from the login page) is the combination to open terminal.
Now let's finish setting up rofi, do the combination above to open the terminal and run this command in it:
sudo nano ~/.config/i3/config
Go to the "start dmenu (a program launcher)" section, comment the second line, it should be like this:
# bindsym Mod1+d exec --no-startup-id dmenu_run
the # is commenting the line. And uncomment the third line, which is:
bindsym Mod1+d exec "rofi -modi drun,run -show drun"
the # should be gone
Then save by CTRL+S and close by CTRL+X and reboot your system.
Now we will configure i3 further for tint2, remove the following from the i3 configuration file:
# Start i3bar to display a workspace bar (plus the system information i3 status
# finds out, if available)
bar {
status_command i3status
}
This will be at the bottom of the config file ^ it removes i3bar so only i3 would start.
Then reboot your system by typing "reboot" in the terminal.
After rebooting, when doing the combination ALT+D it opens the application menu.
This step is optional, but it is recommended because it will let you install packages from the AUR (user repisotary). We will also install yay by running these separately:
(Let’s install git in order to install yay afterwards with it)
sudo pacman -S git
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
Install Nitrogen and Thunar to have a wallpaper tool and a file manager by running this command:
sudo pacman -S nitrogen thunar
Check here for more information about Nitrogen and how to use it: https://wiki.archlinux.org/title/nitrogen#Usage
And add this at the bottom of the i3 config file:
exec nitrogen --restore &
Now let's install the user folders (Documents, Pictures...) by installing this package:
sudo pacman -S xdg-user-dirs
Then run this in terminal:
xdg-user-dirs-update
Now let's install autotiling https://aur.archlinux.org/packages/autotiling by running this command:
yay -S autotiling
Add "exec_always --no-startup-id autotiling" to the ~/.config/i3/config file.
At last, let's install PulseAudio by running this command:
sudo pacman -S pulseaudio pulseaudio-alsa pavucontrol
Now reboot your PC and enjoy! You should have memorized how to do this already, it's straight forward and simple.
II. Optional Notes​
Type this in the command in the terminal to check the themes available for rofi:
rofi-theme-selector
For controlling the volume, install this package:
yay -S pnmixer
And then add this at the bottom of your i3 config file:
exec_always pnmixer
Restart your PC
In order to fill your wallpaper fullscreen with Nitrogen, you can do it with this command:
nitrogen --set-zoom-fill /path/to/image.png
That's it! Let me know how it goes with you
Click to expand...
Click to collapse
Hi, what are the keyboard shortcuts for the i3 tiling window manager?
$cronos_ said:
Hi, what are the keyboard shortcuts for the i3 tiling window manager?
Click to expand...
Click to collapse
Hello! Check these out:
i3 - ArchWiki
wiki.archlinux.org
i3 - Reference Card
i3 is a dynamic tiling window manager with clean, readable and documented code, featuring extended Xinerama support, usage of libxcb instead of xlib and several improvements over wmii
i3wm.org

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

For installing and configuring an Android phone via script it is often required to boot the phone from an TWRP recovery image
Because of this I wrote a Linux shell script to reboot an Android phone from an TWRP recovery image:
boot_phone_from_twrp.sh
The script boot_phone_from_trwp.sh will detect the current status of the attached phone and use the approbiate commands (fastboot and/or adb) to reboot the phone from the TWRP recovery image.
The usage for the script is:
Code:
boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image
boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Use the parameter -H to get a detailed usage help:
Spoiler: Detailed Usage
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./boot_phone_from_twrp.sh -H
boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image
Usage
boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]
All parameter are optional
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
Use the parameter "serial=#sn" to define the serialnumber of the phone to process. This parameter is only necessary if there
are more then one phones connected via USB and the environment variable SERIAL_NUMBER is not set.
The value for the parameter "wait" is the maximum number of seconds to wait after booting from the TWRP image until
the adb deamon is ready to use. The default value is 10 seconds.
Use the parameter "force" to reboot the phone from the TWRP image even if it's booted from a TWRP installed in
the boot or recovery partition.
Use the parameter "noforce" to disable rebooting the phone from the TWRP image if it's booted from a TWRP installed in
the boot or recovery partition. This is the default setting.
Use the parameter "reboot" to reboot the phone from the TWRP image even if it's already booted from an TWRP image.
Use the parameter "noreboot" to disable rebooting the phone from the TWRP image if it's already booted from an TWRP image
This is the default setting.
Use the parameter "checkonly" to only retrieve the current boot mode of the phone; the script will also return
the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.
Use the parameter "status" to only print the current boot mode of the phone; the script will return
the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.
The parameter "twrp_image" can be used to define another TWRP image to use. The parameter is optional - the
default for "twrp_image" is the value of the environment variable TWRP_IMAGE; the default for the variable
TWRP_IMAGE is hardcoded in the script in the variable DEFAULT_TWRP_IMAGE (= the TWRP image for the ASUS Zenfone 8)
Parameter overwrite the values defined via environment variables.
Returncodes:
0 - the phone was successfully booted from the TWRP image
1 - the phone is already booted from the TWRP image
2 - the phone is booted from TWRP installed in the boot or recovery partition
3 - the phone is booted into the Android OS
4 - the phone is booted into bootloader
5 - the phone is booted into the fastbootd
6 - the phone is booted into the safe mode of the Android OS
7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition
9 - too many phones connected
10 - usage help printed and exited
100 - invalid parameter found
101 - TWRP image not found
102 - booting the phone into the bootloader failed
103 - booting the phone into the TWRP failed
252 - access to the phone failed
253 - requirement check failed (e.g. one or more required executables not found, etc)
254 - unknown error
The phone to boot must be attached via USB.
The phone can be either in fastboot mode, in normal mode with enabled adb support, in the boot loader, or already booted from an installed TWRP or an TWRP image
To change some of the values used by the script you can set environment variables before starting the script:
Set the environment variable TWRP_IMAGE to the name of the TWRP image file that should be used (the parameter of the script will overwrite the variable)
Set the environment variable SERIAL_NUMBER to the serial number of the phone to patch if there is more then one phone connected via USB
Set the environment variable ADB_OPTIONS to the options to be used with the adb command
Set the environment variable FASTBOOT_OPTIONS to the options to be used with the fastboot command
Set the environment variable FAST_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the bootloader (default are 60 seconds)
Set the environment variable ADB_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the TWRP image (default are 60 seconds)
Set the environment variable ADB to the adb executable that should be used; default: search adb in the PATH
Set the environment variable FASTBOOT to the fastboot executable that should be used; default: search fastboot in the PATH
Set the environment variable TIMEOUT to the timeout executable that should be used; default: search timeout in the PATH
The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
When more than one phone is connected via USB use the parameter serial=#sn or the environment variable SERIAL_NUMBER to define the serial number of the phone to use.
To only retrieve the current status of the attached phone execute the script with the parameter checkonly or status.
The script will then only check the status of the phone and return the current status in the return code:
1 - the phone is booted from the TWRP image
2 - the phone is booted from TWRP installed in the boot or recovery partition
3 - the phone is booted into the Android OS
4 - the phone is booted into bootloader
5 - the phone is booted into the fastbootd
6 - the phone is booted into the safe mode of the Android OS
7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition
If the parameter status is used the script will also print a message with the current status of the phone,e.g:
Spoiler: example output
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh checkonly ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
RC is 6
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh status ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
The phone is currently booted into the safe-mode of the Android OS
RC is 6
[[email protected] /data/develop/android/scripts_on_linux]$
The script boot_phone_from_twrp.sh can also be used as include file using
. ./boot_phone_from_twrp.sh
Using this method these functions are available in the script that includes boot_phone_from_twrp.sh:
retrieve_phone_status
print_phone_status
boot_phone_from_the_TWRP_image
reboot_phone
wait_for_phone_to_be_in_the_bootloader
wait_for_phone_with_a_working_adb_connection
wait_for_the_adb_daemon
Please check the comments at the start of the script for details about using the script in this way; the command
Code:
grep "#S#" boot_phone_from_twrp.sh | cut -c4-
prins the hints for using the script as include file
Spoiler: Details for using the script as include
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ grep "#S#"
boot_phone_from_twrp.sh | cut -c4-
---------------------------------------------------------------------
To use the script as include file add the statement
. boot_phone_from_twrp.sh
to your file
If used as include file the script will
- define these constants if not already defined:
__TRUE
__FALSE
- define these functions if not already defined:
LogMsg
LogError
isNumber
wait_some_seconds
- define these functions (overwriting existing functions with the same name)
check_prereqs_for_boot_phone_from_twrp
init_global_vars_for_boot_phone_from_twrp
set_serial_number
retrieve_phone_status
print_phone_status
boot_phone_from_the_TWRP_image
reboot_phone
wait_for_phone_to_be_in_the_bootloader
wait_for_phone_with_a_working_adb_connection
wait_for_the_adb_daemon
- the variable PHONE_BOOT_ERROR contains the error code (102 or 103) if rebooting the phone fails
The script including this script can optional define these variables before
including this script (see the source code below for the meaning of these variables):
SUDO_PREFIX
CUR_USER
FORCE_BOOT_INTO_TWRP_IMAGE
FORCE_REBOOT_INTO_TWRP_IMAGE
TWRP_IMAGE
SERIAL_NUMBER
ADB_DAEMON_WAIT_TIME
FAST_BOOT_WAIT_TIME
ADB_BOOT_WAIT_TIME
FASTBOOT_OPTIONS
ADB_OPTIONS
ADB
FASTBOOT
TIMEOUT
CHECKONLY
The global variables listed above will be defined after including this script
[[email protected] /data/develop/android/scripts_on_linux]$
Note:
After including the script you should use the command
${ADB} ${ADB_OPTIONS}
to run the adb binary and the command
${FASTBOOT} ${FASTBOOT_OPTIONS}
to run the fastboot binary.
Examples:
To boot the phone from the TWRP image use this code:
Code:
. ./boot_phone_from_twrp.sh
retrieve_phone_status
boot_phone_from_the_TWRP_image
To boot the phone into the Android OS use this code:
Code:
. ./boot_phone_from_twrp.sh
retrieve_phone_status
reboot_phone
Spoiler: Simple test script
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ cat ./test.sh
echo "*** Loading the functions to boot the phone ..."
. ./boot_phone_from_twrp.sh
echo ""
echo "*** Checking the status of the phone"
retrieve_phone_status
print_phone_status
echo ""
echo "*** Booting the phone into the TWRP image ..!"
boot_phone_from_the_TWRP_image
echo ""
echo "*** Press return to boot the phone into the Android OS ..."
read USER_INPUT
reboot_phone
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$ ./test.sh
*** Loading the functions to boot the phone ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
*** Checking the status of the phone
The phone is currently booted from the Android OS
*** Booting the phone into the TWRP image ..!
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.761s]
Booting OKAY [ 10.361s]
Finished. Total time: 13.167s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
*** Press return to boot the phone into the Android OS ...
Booting the Android OS using the command "adb reboot " now ...
Waiting up to 60 seconds for the boot of the phone .....
Waiting up to 10 seconds for the adb daemon to get ready ..........
[[email protected] /data/develop/android/scripts_on_linux]$
The script boot_phone_from_twrp.sh is used as include script in the script init_magisk_db.sh - see this post How to enable root access using Magisk in a script. The script is also used in the script install_os_via_twrp.sh see this post How to install an OS image using the twrp command in a script .
The script boot_phone_from_twrp.sh is also available on my homepage:
http://bnsmb.de/files/public/Android/boot_phone_from_twrp.sh
Spoiler: Changelog for the script boot_phone_from_twrp.sh
Bash:
# History
# 05.11.2022 v1.0.0.0 /bs #VERSION
# inital release
#
# 01.12.2022 v2.0.0.0 /bs #VERSION
# script rewritten using functions
# added the parameter --serial=#sn
#
# 07.12.2022 v2.1.0.0 /bs #VERSION
# added a check for the Android OS safe mode
# missing shift command in the code to process the parameter for the serial number -- fixed
#
# 17.12.2022 v2.2.0.0 /bs #VERSION
# added support for the LineageOS recovery
Spoiler: History
17.12.2022 /bs
Updated the post with the infos for the script version 2.2.0.0
18.12.2022 /bs
Added some details for using the script as include file
Added a link to another script using this script

How To Guide How to install an OS image using the TWRP binary twrp

The most important step to automate the installation and configuration of the Android OS is the installation of an OS image via script without user intervention. One method to do that is using the binary twrp from the recovery TWRP .
The twrp command to install an OS image with twrp is
Code:
/system/bin/twrp install /path/to/update.zip
The usage for the binary twrp is (see also here)
Spoiler: twrp usage help
Code:
I006D:/ # /system/bin/twrp --help
TWRP openrecoveryscript command line tool, TWRP version 3.7.0_12-0
Allows command line usage of TWRP via openrecoveryscript commands.
Some common commands include:
install /path/to/update.zip
backup <SDCRBAEM> [backupname]
restore <SDCRBAEM> [backupname]
wipe <partition name>
format data
sideload
set <variable> [value]
decrypt <password> [USER ID]
remountrw
fixperms
mount <path>
unmount <path>
listmounts
print <value>
mkdir <directory>
reboot [recovery|poweroff|bootloader|download|edl]
See more documentation at https://twrp.me/faq/openrecoveryscript.html
I006D:/ #
The binary twrp can also be used to wipe the data from the phone using the parameter wipe <partition_name>.
The command twrp install will not change the active boot slot for the next reboot so this must be done manually after installing the OS image. This can be done using the binary bootctl which is also part of TWRP.
The usage for the command bootctl is:
Spoiler: bootctl usage help
Code:
ASUS_I006D:/ # /system/bin/bootctl -h
/system/bin/bootctl - command-line wrapper for the boot HAL.
Usage:
/system/bin/bootctl COMMAND
Commands:
hal-info - Show info about boot_control HAL used.
get-number-slots - Prints number of slots.
get-current-slot - Prints currently running SLOT.
mark-boot-successful - Mark current slot as GOOD.
get-active-boot-slot - Prints the SLOT to load on next boot.
set-active-boot-slot SLOT - On next boot, load and execute SLOT.
set-slot-as-unbootable SLOT - Mark SLOT as invalid.
is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.
is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
get-suffix SLOT - Prints suffix for SLOT.
set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
partition is in progress. Valid STAT values
are: none, unknown, snapshotted, merging,
or cancelled.
get-snapshot-merge-status - Prints the current snapshot-merge status.
SLOT parameter is the zero-based slot-number.
64|ASUS_I006D:/ #
Afterwards the phone can be rebooted to activate the new installed OS
So the commands for installing an OS image via twrp are
- boot the phone from the TWRP image
- (optional) use twrp to wipe the data
- copy the OS image file via adb push to the phone
- install the OS image file via the twrp binary
- change the next active boot slot via the binary bootctl.
- reboot the phone
I wrote a simple script to just do that
install_os_via_twrp.sh
The usage of the script is :
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh -h
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Use the parameter --help or -H to view the detailed usage help
Spoiler: Detailed usage help for install_os_via_twrp.sh
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh -H
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Usage
install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]
All parameter are optional, except the parameter for the OS image to install "os_image_file".
The parameter can be used in any order.
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
If the parameter "--reboot" is used the script will reboot the phone after successfully installing the OS image; to disable the automatic reboot use the parameter "--noreboot".
Default is to ask the user for confirmation to reboot the phone.
Use the parameter "wipe" or "wipeall" to wipe /data, /cache, and Dalvik before installing the OS image.
Use one or more of the parameter "wipe_cache", "wipe_data", or "wipe_dalvik" to only wipe some of the partitions used.
Use the parameter "format_metadata" to format the meta data partition; this wipe is NOT included in "wipe_all"
Use the parameter "format_data" to format the data partition; this wipe is NOT included in "wipe_all"
Use the parameter "factory_reset" to do a factory reset before installing the OS image; a factory reset is done by formatting the data and the metadata partitions.
Use the parameter "force" to ignore errors while wiping or formatting the data; without this parameter the script will abort if one of the wipe or format commands fails
To change some of the values used by the script these environment variables can be set before starting the script:
Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access
Set the environment variable UPLOAD_DIR_ON_THE_PHONE to set the upload directory for the OS image file on the phone (default dir is /tmp; /tmp is mounted on a ramdiks)
See also the source code of the script
boot_phone_from_twrp.sh
for environment variables supported by this script
Prerequisites
- the phone must be connected via USB
- there must be a working connection to the phone using fastboot or adb
- a working TWRP image for the phone must exist
The script boot_phone_from_twrp.sh is required by this script -- see the source code of the script
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Note:
The script needs the helper script boot_phone_from_twrp.sh.sh (see How to boot a phone from a twrp recovery image)
Spoiler: Sample output of the script installing a new version of the same OS
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ REBOOT=yes /data/develop/android/scripts_on_linux/install_os_via_twrp.sh /data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.833s]
Booting OKAY [ 10.359s]
Finished. Total time: 13.243s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready
... the adb daemon is ready after second(s)
Coyping the OS image file "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip" to "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" on the phone ...
adb: error: failed to read copy response8-MICROG.zip
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 201.8 MB/s (1261116637 bytes in 5.959s)
adb: error: failed to read copy response
adb: device 'M6AIB760D0939LX' not found
Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1261116637 bytes in 37.056s)
Installing the OS image "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" into the slot "_b" ...
Installing zip file '/tmp/omni-13-202212180505-zenfone8-MICROG.zip'
Flashing A/B zip to inactive slot: B
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" successfully installed.
Now changing the next active slot to _b ...
... successfully changed the active slot for the next reboot
Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Spoiler: Sample output of the script installing another OS in the inactive slot
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ /data/develop/android/scripts_on_linux/install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.840s]
Booting OKAY [ 10.406s]
Finished. Total time: 13.286s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready
... the adb daemon is ready after second(s)
Wiping all data including media ...
Done.
You may need to reboot recovery to be able to use /data again.
TWRP will not recreate /data/media on an FBE device. Please reboot into your rom to create /data/media
Formatting Metadata using mke2fs...
Done.
Done processing script file
Wiping the metadata ...
++ /usr/bin/adb -s M6AIB760D0939LX shell umount /dev/block/by-name/metadata
umount: /dev/block/sda22: Invalid argument
++ /usr/bin/adb -s M6AIB760D0939LX shell mke2fs -F -t ext4 /dev/block/by-name/metadata
mke2fs 1.45.4 (23-Sep-2019)
Discarding device blocks: done
Creating filesystem with 4096 4k blocks and 4096 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
Coyping the OS image file "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip" to "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" on the phone ...
adb: error: failed to read copy responsey-sake-signed.zip
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 313.7 MB/s (1114300349 bytes in 3.387s)
adb: error: failed to read copy response
adb: device 'M6AIB760D0939LX' not found
Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1114300349 bytes in 32.719s)
Installing the OS image "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" into the slot "_a" ...
Installing zip file '/tmp/lineage-19.1-20221216-nightly-sake-signed.zip'
Flashing A/B zip to inactive slot: A
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" successfully installed.
Now changing the next active slot to _a ...
... successfully changed the active slot for the next reboot
*** Press return to reboot the phone now
Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Testenvironment
I tested the script using this hardware and software:
Hardware:
ASUS Zenfone 8
TWRP
TWRP 3.7.0.12
OS Images installed via script
OmniROM 12
OmniROM 13
LineageOS 19
The script does not work for the images with the original Android from ASUS; the OS installation works but the phone does not boot from the new installation
The script install_os_via_twrp.sh is also available on my home page:
install_os_via_twrp.sh
Trouble Shooting
To update the installed OS to a new version in most cases neither a wipe nor a factory reset is necessary.
To install a different distribution on the phone in most cases a factory reset is required. (e.g installing LIneageOS if the current OS is OmniROM)

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.

How To Guide How to Install TWRP into the boot partition again after the installation of an OS update

After installing an operating system update on a phone with the Android operating system and A/B slots, Magisk can be installed in the boot partition of the inactive slot with the new operating system version using the "Install to inactive slot" installation option in the Magisk App.
Unfortunately this option is not available for TWRP.
Therefor I wrote a litlle script to install TWRP into the boot partition of the inactive slot with the new operating system version after an OS upgrade.
The method works on my ASUS Zenfone 8 and should in principle also work on other phones with the same architecture (Qualcomm CPU, A/B slots, and boot partitions with ram disks)
Looking at the source code for TWRP (https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp) it can be seen that the installation of TWRP is in principle only the installation of a new ramdisk:
Spoiler: TWRP source code excerpt
C++:
bool twrpRepacker::Flash_Current_Twrp() {
if (!TWFunc::Path_Exists("/ramdisk-files.txt")) {
LOGERR("can not find ramdisk-files.txt");
return false;
}
PartitionManager.Unlock_Block_Partitions();
Repack_Options_struct Repack_Options;
Repack_Options.Disable_Verity = false;
Repack_Options.Disable_Force_Encrypt = false;
Repack_Options.Type = REPLACE_RAMDISK_UNPACKED;
Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
std::string verifyfiles = "cd / && sha256sum --status -c ramdisk-files.sha256sum";
if (TWFunc::Exec_Cmd(verifyfiles) != 0) {
gui_msg(Msg(msg::kError, "modified_ramdisk_error=ramdisk files have been modified, unable to create ramdisk to flash, fastboot boot twrp and try this option again or use the Install Recovery Ramdisk option."));
return false;
}
std::string command = "cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/currentramdisk.cpio && /system/bin/gzip -f /tmp/currentramdisk.cpio";
if (TWFunc::Exec_Cmd(command) != 0) {
gui_msg(Msg(msg::kError, "create_ramdisk_error=failed to create ramdisk to flash."));
return false;
}
if (!Repack_Image_And_Flash("/tmp/currentramdisk.cpio.gz", Repack_Options))
return false;
else
return true;
}
Therefor to install TWRP via script after an OS version update two steps are neccessary:
Step one
First we must create the ramdisk with TWRP; this step must only be done once for each TWRP version.
Boot the phone from the TWRP image (NOT from a TWRP installed in the recovery!), e.g:
Bash:
adb rebboot bootloader
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img
Then open an adb session to the running TWRP on the phone and execute:
Bash:
# create the RAM disk
#
cd / && sha256sum --status -c ramdisk-files.sha256sum
if [ $? -eq 0 ] ; then
cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/ramdisk_with_twrp.cpio && /system/bin/gzip -f /tmp/ramdisk_with_twrp.cpio
fi
# copy the RAM disk and magiskboot to a directory on the internal storage that supports the execution permission bit (not /sdcard/<something>)
#
mkdir -p /data/develop/twrp
cp /tmp/ramdisk_with_twrp.cpio.gz /data/develop/twrp/ramdisk_with_twrp.cpio
cp /bin/magiskboot /data/develop/twrp/
Notes:
It is recommended to store the files ramdisk_with_twrp.cpio and magiskboot also on the PC.
The executable magiskboot is only required if Magisk is not installed on the phone.
Now copy the script install_twrp_ramdisk.sh.sh to the phone; this script can be copied to any filesystem with write access on the phone.
That's it .
Step two
The next time an OS update should be done using these steps:
1. install the new OS version either manually or via OTA but do NOT reboot afterwards.
2. execute the script install_twrp_ramdisk.sh.sh to install TWRP into the boot partition for the inactive slot either in an shell on the phone or in an adb shell, e.g. (sh is only necessary if the script is on a filesystem that does not support the execution permission)
Bash:
sh /sdcard/Download/install_twrp_ramdisk.sh
Note that the script needs root access to read and write the boot partition
3. optional: Install Magisk into the new boot partition using the install option "Install to inactive slot" in the Magisk App
4. reboot the phone to activate the new OS version
Test Environment
ASUS Zenfone 8
OmniROM 13
Magisk v25.2
TWRP 3.7.0
Notes
Set the environment variable WORK_DIR before executing the script to use another directory with the image
Set the environment variable TWRP_RAMDISK_NAME before executing the script to use another filename for the ram disk image.
see How to add additional files to an TWRP image for how to add additional files to the TWRP image
see How to install TWRP via script for how to install TWRP with a script running on the PC
Usage for the script install_twrp_ramdisk.sh:
Spoiler: ./install_twrp_ramdisk.sh -H
Code:
./install_twrp_ramdisk.sh -H
Usage:
install_twrp_ramdisk.sh [-h|-H]
This script must run in a shell on the phone (or in an adb shell) after an OS update was installed (either manual or via OTA) but before rebooting the phone.
The script will then install TWRP into the inactive slot.
The script must be executed by the user "root"; if it's executed by a non-root user it will restart itself using "su - -c scriptname"
Supported environment variables
WORK_DIR
directory with the ram disk image and probably the executable magiskboot
TWRP_RAMDISK_NAME
name of the file with the ram disk image (without path)
Prerequisites
The script requires these files:
/data/develop/twrp/ramdisk_with_twrp.cpio
- the ram disk with TWRP
/data/develop/twrp/magiskboot or /data/adb/magisk/magiskboot
- the magiskboot executable
To create the ram disk with TWRP do:
Boot the phone from the TWRP image (NOT from a TWRP installed in the recovery!),
open an adb session to the running TWRP on the phone and execute:
cd / && sha256sum --status -c ramdisk-files.sha256sum
if [ $? -eq 0 ] ; then
cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/ramdisk_with_twrp.cpio && /system/bin/gzip -f /tmp/ramdisk_with_twrp.cpio
fi
mkdir -p /data/develop/twrp
cp /tmp/ramdisk_with_twrp.cpio.gz /data/develop/twrp/ramdisk_with_twrp.cpio
cp /bin/magiskboot /data/develop/twrp/
Note:
It is recommended to store the files ramdisk_with_twrp.cpio and magiskboot also on the PC.
The file magiskboot is only required if Magisk is not installed on the phone.
The directory used for the files can be any directory on the phone on a filesystem supporting the execute permission bit.
But do not forget to change the directory used in the script below (variable WORK_DIR) also.
Update 04.06.2023:
see https://github.com/Magisk-Modules-Repo/twrp-keep for a Magisk module with a script to reinstall TWRP into the inactive boot partition after an OS Udate
Update 11.06.2023:
I successfully tested the Magisk Module twrp-keep on an ASUS Zenfone 8 with OmniROM 13, TRWP 3.7.0_12, and Magisk v26.2.
ToDo:
Create an App with GUI to execute the script

Categories

Resources