Skip to content

Instantly share code, notes, and snippets.

@tothi
Last active September 14, 2024 21:17
Show Gist options
  • Save tothi/1a206791c8b77d7e42015183c980657e to your computer and use it in GitHub Desktop.
Save tothi/1a206791c8b77d7e42015183c980657e to your computer and use it in GitHub Desktop.
Apply Magisk patches on AVD (Android Emulator) RAMDISK images for running Magisk (root) on Android Emulators
#!/bin/bash
#
# patch ramdisk.img (for installing Magisk on x64 Android emulator)
#
# x86_64 on Android 12 (API Level 32) is supported/tested currently
#
# install AVD:
#
# sudo sdkmanager 'system-images;android-32;google_apis_playstore;x86_64'
# sudo sdkmanager 'platforms;android-32'
# avdmanager create avd -n android12-play -d "Nexus 5X" -k 'system-images;android-32;google_apis_playstore;x86_64'
#
# after 1st boot/poweroff (enabling hw keyboard is highly recommended):
#
# sed -i ~/.android/avd/android12-play.avd/config.ini -e 's/^hw.keyboard = no$/hw.keyboard = yes/'
#
# start emulator (with recommended options):
#
# /opt/android-sdk/emulator/emulator @android12-play -memory 8192 -cores 8 -no-snapstorage -no-snapshot -no-snapshot-save -no-audio -feature -Vulkan
#
# install Magisk (use official github releases only: https://github.com/topjohnwu/Magisk):
#
# adb install Magisk-v26.1.apk
#
APILEVEL=32
APK="Magisk-v26.1"
TMPDIR="/data/local/tmp/.magisk"
RAMDISK="/opt/android-sdk/system-images/android-${APILEVEL}/google_apis_playstore/x86_64/ramdisk.img"
yellow=`tput setaf 3`
green=`tput setaf 2`
red=`tput setaf 1`
reset=`tput sgr0`
echo "${green}[*] Using APK ${APK}.apk"
echo "[*] Emulator temp dir is ${TMPDIR}"
echo "[*] RAMDISK image to patch is ${RAMDISK}${reset}"
rm -fr "${APK}"
echo "${yellow}[+] Extracting Magisk APK...${reset}"
apktool d -r -s "${APK}.apk"
echo "${yellow}[*] Testing ADB access...${reset}"
adb shell uname -a
if [ $? -eq 0 ]; then
echo "${green}[*] ADB access seems to be working. Proceeding..."
else
echo "${red}[!] ADB access error. Aborted.${reset}"
exit 1
fi
echo "${yellow}[+] Pushing required files from APK to device TEMP folder${reset}"
adb shell "rm -fr ${TMPDIR}"
adb shell "mkdir ${TMPDIR}"
adb push "${APK}/lib/x86_64/libmagiskboot.so" "${TMPDIR}/magiskboot"
adb push "${APK}/lib/x86_64/libmagiskinit.so" "${TMPDIR}/magiskinit"
adb push "${APK}/lib/x86_64/libmagisk64.so" "${TMPDIR}/magisk64"
adb push "${APK}/assets/stub.apk" "${TMPDIR}/stub.apk"
adb shell "chmod +x ${TMPDIR}/magiskboot"
adb shell "chmod +x ${TMPDIR}/magisk64"
echo "${yellow}[+] Prepatching and uploading 2-staged RAMDISK (into single staged version)${reset}"
lz4cat "${RAMDISK}" | bbe -b '/070701000493e6000001ed0000000000000000000000010000000000000000000000000000000000000000000000000000000b00000000TRAILER!!!/:/07070100/' -e 'D;A 07070100' | adb shell "cat > ${TMPDIR}/ramdisk.cpio"
adb shell "cd ${TMPDIR}; cp -af ramdisk.cpio ramdisk.cpio.orig"
echo "${yellow}[+] RAMDISK is ready to patch!${reset}"
adb shell "echo KEEPVERITY=true > ${TMPDIR}/config"
adb shell "echo KEEPFORCEENCRYPT=true >> ${TMPDIR}/config"
adb shell "echo PREINITDEVICE=\$(${TMPDIR}/magisk64 --preinit-device) >> ${TMPDIR}/config"
adb shell "cd ${TMPDIR}; ./magiskboot compress=xz magisk64 magisk64.xz"
adb shell "cd ${TMPDIR}; ./magiskboot compress=xz stub.apk stub.xz"
echo "${yellow}[+] Patching MAGISK to RAMDISK${reset}"
adb shell "cd ${TMPDIR}; KEEPVERITY=true KEEPFORCEENCRYPT=true ./magiskboot cpio ramdisk.cpio \"add 0750 init magiskinit\" \"mkdir 0750 overlay.d\" \"mkdir 0750 overlay.d/sbin\" \"add 0644 overlay.d/sbin/magisk64.xz magisk64.xz\" \"add 0644 overlay.d/sbin/stub.xz stub.xz\" \"patch\" \"backup ramdisk.cpio.orig\" \"mkdir 000 .backup\" \"add 000 .backup/.magisk config\""
echo "${yellow}[+] Pulling and compressing patched RAMDISK and cleaning up files on device${reset}"
adb shell "cat ${TMPDIR}/ramdisk.cpio" | gzip -c - > ramdisk-patched.img
adb shell "rm -fr ${TMPDIR}"
echo "${green}[*] MAGISK patched RAMDISK image is available: ramdisk-patched.img"
echo "[*] Run emulator with option '-ramdisk ramdisk-patched.img'${reset}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment