From fe5cc70ab2f5572343d3f55f949de0e2acc45c46 Mon Sep 17 00:00:00 2001 From: gabriel becker Date: Wed, 9 Nov 2022 13:09:37 +1100 Subject: [PATCH] Initial commit --- Dockerfile | 9 + README.md | 49 + reinstall-magisk-on-lineageos | 179 ++ run_bash_tests.bash | 5 + test/builds_download_page_alioth.fixture.html | 1516 +++++++++++++++++ test/builds_download_page_vayu.fixture.html | 1463 ++++++++++++++++ test/reinstall-magisk-on-lineageos.bats | 53 + watch_bash_tests.bash | 5 + 8 files changed, 3279 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 reinstall-magisk-on-lineageos create mode 100755 run_bash_tests.bash create mode 100644 test/builds_download_page_alioth.fixture.html create mode 100644 test/builds_download_page_vayu.fixture.html create mode 100644 test/reinstall-magisk-on-lineageos.bats create mode 100755 watch_bash_tests.bash diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8b84a7b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM debian:stable-slim + +RUN apt-get update +RUN apt-get install -y \ + android-tools-adb \ + android-tools-fastboot +COPY . /magisk +WORKDIR /magisk +CMD bash reinstall-magisk-on-lineageos diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bd06db --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# Reinstall Magisk on Lineageos + +This little bash script is used to reinstall magisk, which gets uninstalled after each LineageOS update. + +Before I had to patch once more the boot using the Magisk app doing all the steps manually, making some mistakes along the way. Now it takes less than 3 minutes to reinstall magisk (virtually all the time is taken by the download), and without any intervention on my part. + +So now I can update my LineageOS phone every week without any hassle. + +My solution is based on the official [installation guide of Magisk](https://topjohnwu.github.io/Magisk/install.html). + +## Warning + +This script is written in bash, so as any bash script it is run with the same permissions than your current user. +Moreover it needs root privileges on your phone to run. + +So it may screw up both your computer and your phone, so I invite you to check its source code before running it and using it at your own risks! + +## Bash + +Bash is one of the standard shells on Linux and MacOS systems. + +If on Windows, you can use the [Windows Subsystem for Linux](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) to run it. See [this guide](https://docs.microsoft.com/en-us/windows/wsl/install) to install it. + +## Installation + +1. Clone this repo somewhere on your computer (ex: `git clone https://github.com/NicolasWebDev/reinstall-magisk-on-lineageos`) +2. Edit the script to set both variables at the beginning of the program. + +## Usage + +1. Download an update of LineageOS on your phone. +2. Install the update on your phone. The device should reboot automatically. After this step Magisk is no more installed on your phone. +4. Connect your phone to your computer. +5. Run `bash reinstall-magisk-on-lineageos` in the root of this project. + +## How it works + +The script should be pretty readable and self-explanatory, without any bash knowledge. +To sum it up, it downloads the right LineageOS build on your pc (the same one that you have just installed), extracts the `boot.img` image from the archive, transfers it on your phone, uses the Magisk app installed on your phone to patch the `boot.img` image, reboots the phone in fastboot mode, then flashes the patched boot image and restarts the device. + +## Different methods of extracting the `boot.img` file + +There are 3 different methods to extract the `boot.img` file, depending on your particular device. In this [LineageOS wiki article](https://wiki.lineageos.org/extracting_blobs_from_zips) you can find more details on these methods. + +This script currently supports extracting from block-based OTAs and from payload-based OTAs. File-based OTAs are not yet supported. + +## Limitations + +See the current issues in the issue tracker of this repo. diff --git a/reinstall-magisk-on-lineageos b/reinstall-magisk-on-lineageos new file mode 100755 index 0000000..863a052 --- /dev/null +++ b/reinstall-magisk-on-lineageos @@ -0,0 +1,179 @@ +#! /bin/bash + +set -e + +PHONE_ADB_SERIAL_NUMBER=${ADB_DEVICE} # The adb serial number can be found by running `adb devices`. + +print_message() { + printf "$1\n" +} + +check_phone_is_connected() { + if ! adb devices | grep --silent "$PHONE_ADB_SERIAL_NUMBER"; then + print_message "ERROR: phone '$PHONE_ADB_SERIAL_NUMBER' is not connected" + adb devices -l + exit 1 + fi + print_message "Operations will be performed on:" + adb devices -l | grep $PHONE_ADB_SERIAL_NUMBER +} + +check_phone_is_in_fastboot_mode() { + fastboot devices | grep --silent "$PHONE_ADB_SERIAL_NUMBER" +} + +get_lineage_version() { + adb -s "$PHONE_ADB_SERIAL_NUMBER" shell getprop ro.lineage.version | tr -d '\n' +} + +get_device_name() { + adb -s "$PHONE_ADB_SERIAL_NUMBER" shell getprop ro.lineage.device | tr -d '\n' +} + +get_device_downloads_page() { + echo "https://download.lineageos.org/$(get_device_name)" +} + +get_build_url() { + curl --no-progress-meter "$(get_device_downloads_page)" | + grep --only-matching --ignore-case \ + "https://mirrorbits.lineageos.org/[^\"]*"$( + )"$(get_lineage_version)-signed.zip" | + head -1 +} + +download_latest_lineageos_build() { + # The --location lets curl follow the redirection. + curl --location "$(get_build_url)" --output /tmp/lineageos.zip +} + +extract_boot_image() { + # See https://wiki.lineageos.org/extracting_blobs_from_zips to understand the different ways to extract the boot.img file. + if is_ota_block_based; then + extract_boot_image_from_block_based_ota + elif is_ota_payload_based; then + extract_boot_image_from_payload_based_ota + else + extract_boot_image_from_file_based_ota + fi +} + +extract_boot_image_from_file_based_ota() { + echo 'ERROR: the function "extract_boot_image_from_file_based_ota" is not implemented' + exit 1 +} + +install_python_protobuf() { + sudo python3 -m pip install protobuf +} + +extract_payload_from_payload_based_ota() { + unzip -od /tmp /tmp/lineageos.zip payload.bin +} + +generate_random_alnum_string_of_length_6() { + tr -dc A-Za-z0-9 60)); then + echo "Giving up..." + exit 2 + fi + + echo "Phone is not in fastboot mode yet. Waiting..." + sleep 5 + done +} + +flash_patched_boot_image() { + fastboot -s "$PHONE_ADB_SERIAL_NUMBER" flash boot /tmp/patched-boot.img +} + +reboot_phone() { + fastboot -s "$PHONE_ADB_SERIAL_NUMBER" reboot +} + +check_magisk_folder() { + adb -s "$PHONE_ADB_SERIAL_NUMBER" shell ls /data/adb/magisk/ +} + +is_rooted_debugging_enabled() { + adb -s "$PHONE_ADB_SERIAL_NUMBER" root +} + +main() { + print_message "Looking for phone '$PHONE_ADB_SERIAL_NUMBER'" + check_phone_is_connected + print_message "Checking on phone if rooted debugging is enabled" + is_rooted_debugging_enabled + print_message "Checking on phone if Magisk folder is present listing /data/adb/magisk/" + check_magisk_folder + print_message "Downloading build archive from $(get_build_url)" + download_latest_lineageos_build + print_message "Extracting 'boot.img' from build archive in /tmp/boot.img" + extract_boot_image + print_message "Copying from PC to phone the boot image in /sdcard/Download/boot.img" + transfer_unpatched_boot_image_to_phone + print_message "Patching boot image with Magisk script and moving it in /sdcard/Download/patched-boot.img" + patch_boot_image_on_phone + print_message "Copying patched boot image from phone to PC in /tmp/patched-boot.img" + transfer_patched_boot_image_to_pc + print_message "Rebooting phone in fastboot mode" + reboot_to_bootloader + wait_for_phone_to_be_in_fastboot + print_message "Flashing patched boot image on phone" + flash_patched_boot_image + print_message "Rebooting phone" + reboot_phone +} + +# Run the main only when this file is executed as script, to help with testing. +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi diff --git a/run_bash_tests.bash b/run_bash_tests.bash new file mode 100755 index 0000000..aa78c14 --- /dev/null +++ b/run_bash_tests.bash @@ -0,0 +1,5 @@ +#! /bin/bash + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) + +./test/bats/bin/bats "$SCRIPT_DIR"/test/*.bats diff --git a/test/builds_download_page_alioth.fixture.html b/test/builds_download_page_alioth.fixture.html new file mode 100644 index 0000000..b21b1c7 --- /dev/null +++ b/test/builds_download_page_alioth.fixture.html @@ -0,0 +1,1516 @@ + + + +LineageOS Downloads + + + + + + + + + + + + + +
+
+
+
+
+

Builds for alioth

+Recent changes +• +Device info +• +Installation instructions +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TypeVersionFileSizeRecovery imageRecovery size Date
nightly19.1 +lineage-19.1-20220604-nightly-alioth-signed.zip
sha256 +
1011.77 MB +lineage-19.1-20220604-recovery-alioth.img
+sha256 +
192.0 MB 2022-06-04
nightly19.1 +lineage-19.1-20220528-nightly-alioth-signed.zip
sha256 +
1011.35 MB +lineage-19.1-20220528-recovery-alioth.img
+sha256 +
192.0 MB 2022-05-28
nightly19.1 +lineage-19.1-20220521-nightly-alioth-signed.zip
sha256 +
1011.33 MB +lineage-19.1-20220521-recovery-alioth.img
+sha256 +
192.0 MB 2022-05-21
nightly19.1 +lineage-19.1-20220514-nightly-alioth-signed.zip
sha256 +
1011.22 MB +lineage-19.1-20220514-recovery-alioth.img
+sha256 +
192.0 MB 2022-05-14
nightly18.1 +lineage-18.1-20220507-nightly-alioth-signed.zip
sha256 +
976.69 MB +lineage-18.1-20220507-recovery-alioth.img
+sha256 +
192.0 MB 2022-05-07
nightly18.1 +lineage-18.1-20220430-nightly-alioth-signed.zip
sha256 +
976.69 MB +lineage-18.1-20220430-recovery-alioth.img
+sha256 +
192.0 MB 2022-04-30
nightly18.1 +lineage-18.1-20220423-nightly-alioth-signed.zip
sha256 +
976.7 MB +lineage-18.1-20220423-recovery-alioth.img
+sha256 +
192.0 MB 2022-04-23
nightly18.1 +lineage-18.1-20220416-nightly-alioth-signed.zip
sha256 +
976.72 MB +lineage-18.1-20220416-recovery-alioth.img
+sha256 +
192.0 MB 2022-04-16
+
+
+
+
+
+
+ + + + + + + diff --git a/test/builds_download_page_vayu.fixture.html b/test/builds_download_page_vayu.fixture.html new file mode 100644 index 0000000..ccf0ca6 --- /dev/null +++ b/test/builds_download_page_vayu.fixture.html @@ -0,0 +1,1463 @@ + + + +LineageOS Downloads + + + + + + + + + + + + + +
+
+
+
+
+

Builds for vayu

+Recent changes +• +Device info +• +Installation instructions +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TypeVersionFileSizeRecovery imageRecovery size Date
nightly18.1 +lineage-18.1-20220603-nightly-vayu-signed.zip
sha256 +
1046.18 MB +lineage-18.1-20220603-recovery-vayu.img
+sha256 +
128.0 MB 2022-06-03
nightly18.1 +lineage-18.1-20220527-nightly-vayu-signed.zip
sha256 +
1046.17 MB +lineage-18.1-20220527-recovery-vayu.img
+sha256 +
128.0 MB 2022-05-27
nightly18.1 +lineage-18.1-20220506-nightly-vayu-signed.zip
sha256 +
1053.69 MB +lineage-18.1-20220506-recovery-vayu.img
+sha256 +
128.0 MB 2022-05-06
nightly18.1 +lineage-18.1-20220429-nightly-vayu-signed.zip
sha256 +
1053.69 MB +lineage-18.1-20220429-recovery-vayu.img
+sha256 +
128.0 MB 2022-04-29
+
+
+
+
+
+
+ + + + + + + diff --git a/test/reinstall-magisk-on-lineageos.bats b/test/reinstall-magisk-on-lineageos.bats new file mode 100644 index 0000000..cf76cd3 --- /dev/null +++ b/test/reinstall-magisk-on-lineageos.bats @@ -0,0 +1,53 @@ +#! /bin/bash + +setup() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" +} + +test_get_build_url_vayu() { #@test + load ../reinstall-magisk-on-lineageos + function get_lineage_version() { echo 18.1-20220527-NIGHTLY-vayu; } + export -f get_lineage_version + function get_device_name() { echo vayu; } + export -f get_device_name + function curl() { cat "$DIR/builds_download_page_vayu.fixture.html"; } + export -f curl + + run get_build_url + + assert_output https://mirrorbits.lineageos.org/full/vayu/20220527/lineage-18.1-20220527-nightly-vayu-signed.zip +} + +test_get_device_downloads_page() { #@test + load ../reinstall-magisk-on-lineageos + function get_device_name() { echo vayu; } + export -f get_device_name + + run get_device_downloads_page + + assert_output https://download.lineageos.org/vayu +} + +test_get_build_url_alioth() { #@test + load ../reinstall-magisk-on-lineageos + function get_lineage_version() { echo 19.1-20220604-NIGHTLY-alioth; } + export -f get_lineage_version + function get_device_name() { echo alioth; } + export -f get_device_name + function curl() { cat "$DIR/builds_download_page_alioth.fixture.html"; } + export -f curl + + run get_build_url + + assert_output https://mirrorbits.lineageos.org/full/alioth/20220604/lineage-19.1-20220604-nightly-alioth-signed.zip +} + +test_generate_random_alnum_string_of_length_6() { #@test + load ../reinstall-magisk-on-lineageos + + run generate_random_alnum_string_of_length_6 + + assert_output --regexp '^[0-9a-zA-Z]{6}$' +} diff --git a/watch_bash_tests.bash b/watch_bash_tests.bash new file mode 100755 index 0000000..da0013b --- /dev/null +++ b/watch_bash_tests.bash @@ -0,0 +1,5 @@ +#! /bin/bash + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) + +fd . | entr "$SCRIPT_DIR/run_bash_tests.bash"