Magisk/scripts/emulator.sh

148 lines
3.9 KiB
Bash
Raw Normal View History

2019-09-03 17:06:14 -04:00
#!/usr/bin/env bash
#####################################################################
2019-09-03 17:06:14 -04:00
# AVD Magisk Setup
#####################################################################
2019-09-03 17:06:14 -04:00
#
# Support emulator ABI: x86_64 and arm64
2021-08-26 03:09:56 -07:00
# Support API level: 23 - 31 (21 and 22 images do not have SELinux)
2019-09-03 17:06:14 -04:00
#
2021-08-26 03:09:56 -07:00
# 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.
2019-09-03 17:06:14 -04:00
#
2021-08-26 03:09:56 -07:00
# This only covers the "core" features of Magisk. Testing magiskinit
# and magiskboot require additional setups that are not covered here.
#
#####################################################################
2019-09-03 17:06:14 -04:00
mount_sbin() {
mount -t tmpfs -o 'mode=0755' tmpfs /sbin
2021-08-18 03:44:32 -07:00
chcon u:object_r:rootfs:s0 /sbin
}
2019-09-03 17:06:14 -04:00
if [ ! -f /system/build.prop ]; then
# Running on PC
echo 'Please run `./build.py emulator` instead of directly executing the script!'
exit 1
2019-09-03 17:06:14 -04:00
fi
cd /data/local/tmp
2021-08-26 03:09:56 -07:00
chmod 755 busybox
2019-09-03 17:06:14 -04:00
2020-07-27 03:32:54 -07:00
if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
export ASH_STANDALONE=1
if [ $(./busybox id -u) -ne 0 ]; then
2020-07-27 03:32:54 -07:00
# Re-exec script with root
exec /system/xbin/su 0 ./busybox sh $0
else
# Re-exec script with busybox
exec ./busybox sh $0
fi
fi
2019-09-03 17:06:14 -04:00
2021-08-26 03:09:56 -07:00
pm install -r $(pwd)/app-debug.apk
# Extract files from APK
unzip -oj app-debug.apk 'assets/util_functions.sh'
. ./util_functions.sh
api_level_arch_detect
unzip -oj app-debug.apk "lib/$ABI/*" "lib/$ABI32/libmagisk32.so" -x "lib/$ABI/busybox.so"
2021-08-26 03:09:56 -07:00
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
2021-08-18 03:44:32 -07:00
fi
# SELinux stuffs
2021-08-18 03:44:32 -07:00
ln -sf ./magiskinit magiskpolicy
2021-08-26 03:09:56 -07:00
if [ -f /vendor/etc/selinux/precompiled_sepolicy ]; then
./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk 2>&1
2021-08-26 03:09:56 -07:00
elif [ -f /sepolicy ]; then
./magiskpolicy --load /sepolicy --live --magisk 2>&1
2021-08-26 03:09:56 -07:00
else
./magiskpolicy --live --magisk 2>&1
2021-08-26 03:09:56 -07:00
fi
2019-09-03 17:06:14 -04:00
2021-08-26 03:09:56 -07:00
MAGISKTMP=/sbin
2020-07-27 03:32:54 -07:00
# Setup bin overlay
if mount | grep -q rootfs; then
2020-07-27 03:32:54 -07:00
# Legacy rootfs
mount -o rw,remount /
rm -rf /root
mkdir /root
chmod 750 /root
ln /sbin/* /root
mount -o ro,remount /
mount_sbin
ln -s /root/* /sbin
2020-07-27 03:32:54 -07:00
elif [ -e /sbin ]; then
# Legacy SAR
mount_sbin
2021-08-26 03:09:56 -07:00
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
else
2021-04-09 21:29:42 -07:00
sfile=/sbin/$(basename $file)
touch $sfile
mount -o bind $file $sfile
fi
done
2021-08-26 03:09:56 -07:00
umount -l /dev/sysroot
rm -rf /dev/sysroot
2020-07-27 03:32:54 -07:00
else
2021-08-26 03:09:56 -07:00
# 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
2021-08-26 03:09:56 -07:00
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
2019-09-03 17:06:14 -04:00
mkdir /data/adb/post-fs-data.d 2>/dev/null
mkdir /data/adb/services.d 2>/dev/null
2021-08-26 03:09:56 -07:00
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