Significantly better AVD support

This commit is contained in:
topjohnwu
2021-08-26 03:09:56 -07:00
parent 0cd99712fa
commit 4771c2810b
8 changed files with 103 additions and 55 deletions

View File

@ -3,16 +3,19 @@
# AVD Magisk Setup
#####################################################################
#
# This script will setup an environment with minimal Magisk that
# the Magisk app will be happy to run properly within the official
# emulator bundled with Android Studio (AVD).
# Support emulator ABI: x86_64 *only*
# Support API level: 23 - 31 (21 and 22 images do not have SELinux)
#
# ONLY use this script for developing the Magisk app or root apps
# in the emulator. The constructed Magisk environment is not a
# fully functional one as if it is running on an actual device.
# This script will stop zygote, simulate the Magisk start up process
# that would've happened before zygote was started, and finally
# restart zygote. This is useful for setting up the emulator for
# developing Magisk, testing modules, and developing root apps using
# the official Android emulator (AVD) instead of a real device.
#
# The script assumes you are using x64 emulator images.
# Build binaries with `./build.py binary` before running this script.
# This only covers the "core" features of Magisk. Testing magiskinit
# and magiskboot require additional setups that are not covered here.
#
# Build everything by `./build.py all` before running this script.
#
#####################################################################
@ -29,16 +32,13 @@ mount_sbin() {
if [ ! -f /system/build.prop ]; then
# Running on PC
cd "$(dirname "$0")/.."
adb push native/out/x86_64/busybox native/out/x86_64/magiskinit \
native/out/x86_64/magisk scripts/emulator.sh /data/local/tmp
adb push native/out/x86_64/busybox out/app-debug.apk scripts/emulator.sh /data/local/tmp
adb shell sh /data/local/tmp/emulator.sh
exit 0
fi
cd /data/local/tmp
chmod 777 busybox
chmod 777 magiskinit
chmod 777 magisk
chmod 755 busybox
if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
@ -52,20 +52,34 @@ if [ -z "$FIRST_STAGE" ]; then
fi
fi
# Remove previous setup if exist
pgrep magiskd >/dev/null && pkill -9 magiskd
[ -f /sbin/magisk ] && umount -l /sbin
[ -f /system/bin/magisk ] && umount -l /system/bin
if [ -d /dev/magisk ]; then
umount -l /dev/magisk 2>/dev/null
rm -rf /dev/magisk
pm install -r $(pwd)/app-debug.apk
# Extract files from APK
unzip -oj app-debug.apk 'lib/x86_64/*' 'lib/x86/libmagisk32.so' -x 'lib/x86_64/busybox.so'
for file in lib*.so; do
chmod 755 $file
mv "$file" "${file:3:${#file}-6}"
done
# Stop zygote (and previous setup if exists)
magisk --stop 2>/dev/null
stop
if [ -d /dev/avd-magisk ]; then
umount -l /dev/avd-magisk 2>/dev/null
rm -rf /dev/avd-magisk 2>/dev/null
fi
# SELinux stuffs
ln -sf ./magiskinit magiskpolicy
./magiskpolicy --live --magisk
if [ -f /vendor/etc/selinux/precompiled_sepolicy ]; then
./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk
elif [ -f /sepolicy ]; then
./magiskpolicy --load /sepolicy --live --magisk
else
./magiskpolicy --live --magisk
fi
BINDIR=/sbin
MAGISKTMP=/sbin
# Setup bin overlay
if mount | grep -q rootfs; then
@ -81,13 +95,11 @@ if mount | grep -q rootfs; then
elif [ -e /sbin ]; then
# Legacy SAR
mount_sbin
if ! grep -q '/sbin/.magisk/mirror/system_root' /proc/mounts; then
mkdir -p /sbin/.magisk/mirror/system_root
block=$(mount | grep ' / ' | awk '{ print $1 }')
[ $block = "/dev/root" ] && block=/dev/block/dm-0
mount -o ro $block /sbin/.magisk/mirror/system_root
fi
for file in /sbin/.magisk/mirror/system_root/sbin/*; do
mkdir -p /dev/sysroot
block=$(mount | grep ' / ' | awk '{ print $1 }')
[ $block = "/dev/root" ] && block=/dev/block/dm-0
mount -o ro $block /dev/sysroot
for file in /dev/sysroot/sbin/*; do
[ ! -e $file ] && break
if [ -L $file ]; then
cp -af $file /sbin
@ -97,25 +109,43 @@ elif [ -e /sbin ]; then
mount -o bind $file $sfile
fi
done
umount -l /dev/sysroot
rm -rf /dev/sysroot
else
# Android Q+ without sbin, use overlayfs
BINDIR=/dev/magisk/upper
mkdir /dev/magisk
mount -t tmpfs -o 'mode=0755' tmpfs /dev/magisk
chcon u:object_r:system_file:s0 /dev/magisk
mkdir /dev/magisk/upper
mkdir /dev/magisk/work
./magisk --clone-attr /system/bin /dev/magisk/upper
mount -t overlay overlay -o lowerdir=/system/bin,upperdir=/dev/magisk/upper,workdir=/dev/magisk/work /system/bin
# Android Q+ without sbin
MAGISKTMP=/dev/avd-magisk
mkdir /dev/avd-magisk
mount -t tmpfs -o 'mode=0755' tmpfs /dev/avd-magisk
fi
# Magisk stuffs
cp -af ./magisk $BINDIR/magisk
chmod 755 $BINDIR/magisk
ln -s ./magisk $BINDIR/su
ln -s ./magisk $BINDIR/resetprop
ln -s ./magisk $BINDIR/magiskhide
mkdir -p /data/adb/modules 2>/dev/null
mkdir -p /data/adb/magisk 2>/dev/null
unzip -oj app-debug.apk 'assets/*' -x 'assets/chromeos/*' -d /data/adb/magisk
mkdir /data/adb/modules 2>/dev/null
mkdir /data/adb/post-fs-data.d 2>/dev/null
mkdir /data/adb/services.d 2>/dev/null
$BINDIR/magisk --daemon
for file in magisk32 magisk64 magiskinit; do
chmod 755 ./$file
cp -af ./$file $MAGISKTMP/$file
cp -af ./$file /data/adb/magisk/$file
done
cp -af ./magiskboot /data/adb/magisk/magiskboot
cp -af ./busybox /data/adb/magisk/busybox
ln -s ./magisk64 $MAGISKTMP/magisk
ln -s ./magisk $MAGISKTMP/su
ln -s ./magisk $MAGISKTMP/resetprop
ln -s ./magisk $MAGISKTMP/magiskhide
ln -s ./magiskinit $MAGISKTMP/magiskpolicy
mkdir -p $MAGISKTMP/.magisk/mirror
mkdir $MAGISKTMP/.magisk/block
touch $MAGISKTMP/.magisk/config
# Boot up
$MAGISKTMP/magisk --post-fs-data
while [ ! -f /dev/.magisk_unblock ]; do sleep 1; done
rm /dev/.magisk_unblock
start
$MAGISKTMP/magisk --service