Browse Source

Initial commit

pull/1/head
gabriel becker 2 years ago
commit
fe5cc70ab2
  1. 9
      Dockerfile
  2. 49
      README.md
  3. 179
      reinstall-magisk-on-lineageos
  4. 5
      run_bash_tests.bash
  5. 1516
      test/builds_download_page_alioth.fixture.html
  6. 1463
      test/builds_download_page_vayu.fixture.html
  7. 53
      test/reinstall-magisk-on-lineageos.bats
  8. 5
      watch_bash_tests.bash

9
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

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

179
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 </dev/urandom | head -c 6
}
extract_boot_image_from_payload_file() {
install_python_protobuf
# For cases in which the repo has already been cloned, I prefer to create a random directory name to clone into, instead of deleting any file or directory on the user's machine.
scripts_directory_name="scripts_$(generate_random_alnum_string_of_length_6)"
git clone https://github.com/LineageOS/scripts /tmp/$scripts_directory_name
python /tmp/$scripts_directory_name/update-payload-extractor/extract.py --partitions boot --output_dir /tmp/ /tmp/payload.bin
}
extract_boot_image_from_payload_based_ota() {
extract_payload_from_payload_based_ota
extract_boot_image_from_payload_file
}
is_ota_payload_based() {
unzip -l /tmp/lineageos.zip | grep -wq payload.bin
}
is_ota_block_based() {
unzip -l /tmp/lineageos.zip | grep -wq boot.img
}
extract_boot_image_from_block_based_ota() {
unzip -od /tmp /tmp/lineageos.zip boot.img
}
transfer_unpatched_boot_image_to_phone() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" push /tmp/boot.img /sdcard/Download/boot.img
}
patch_boot_image_on_phone() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell /data/adb/magisk/boot_patch.sh /sdcard/Download/boot.img
adb -s "$PHONE_ADB_SERIAL_NUMBER" shell mv /data/adb/magisk/new-boot.img /sdcard/Download/patched-boot.img
}
transfer_patched_boot_image_to_pc() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" pull /sdcard/Download/patched-boot.img /tmp
}
reboot_to_bootloader() {
adb -s "$PHONE_ADB_SERIAL_NUMBER" reboot bootloader
}
wait_for_phone_to_be_in_fastboot() {
SECONDS=0
until check_phone_is_in_fastboot_mode; do
if ((SECONDS > 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

5
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

1516
test/builds_download_page_alioth.fixture.html

File diff suppressed because it is too large Load Diff

1463
test/builds_download_page_vayu.fixture.html

File diff suppressed because it is too large Load Diff

53
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}$'
}

5
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"
Loading…
Cancel
Save