2019-09-03 23:06:14 +02:00
|
|
|
#!/usr/bin/env bash
|
2020-03-20 11:44:08 +01:00
|
|
|
#####################################################################
|
2019-09-03 23:06:14 +02:00
|
|
|
# AVD Magisk Setup
|
2020-03-20 11:44:08 +01:00
|
|
|
#####################################################################
|
2019-09-03 23:06:14 +02:00
|
|
|
#
|
|
|
|
# This script will setup an environment with minimal Magisk that
|
|
|
|
# Magisk Manager will be happy to run properly within the official
|
|
|
|
# emulator bundled with Android Studio (AVD).
|
|
|
|
#
|
2019-09-04 17:12:09 +02:00
|
|
|
# ONLY use this script for developing Magisk Manager 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.
|
2019-09-03 23:06:14 +02:00
|
|
|
#
|
2019-09-04 17:12:09 +02:00
|
|
|
# The script assumes you are using x86/x64 emulator images.
|
2019-09-03 23:06:14 +02:00
|
|
|
# Build binaries with `./build.py binary` before running this script.
|
|
|
|
#
|
2020-03-20 11:44:08 +01:00
|
|
|
#####################################################################
|
2019-09-03 23:06:14 +02:00
|
|
|
|
|
|
|
abort() {
|
|
|
|
echo "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:12:09 +02:00
|
|
|
mount_sbin() {
|
|
|
|
mount -t tmpfs -o 'mode=0755' tmpfs /sbin
|
|
|
|
$SELINUX && chcon u:object_r:rootfs:s0 /sbin
|
|
|
|
}
|
|
|
|
|
2019-09-03 23:06:14 +02:00
|
|
|
if [ ! -f /system/build.prop ]; then
|
|
|
|
# Running on PC
|
|
|
|
cd "`dirname "$0"`/.."
|
2019-09-04 17:12:09 +02:00
|
|
|
adb push native/out/x86/busybox scripts/emulator.sh /data/local/tmp
|
|
|
|
emu_arch=`adb shell uname -m`
|
2019-10-11 07:45:06 +02:00
|
|
|
if [ "$emu_arch" = "x86_64" ]; then
|
2019-09-04 17:12:09 +02:00
|
|
|
adb push native/out/x86/magiskinit64 /data/local/tmp/magiskinit
|
|
|
|
else
|
|
|
|
adb push native/out/x86/magiskinit /data/local/tmp
|
|
|
|
fi
|
2019-09-03 23:06:14 +02:00
|
|
|
adb shell sh /data/local/tmp/emulator.sh
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd /data/local/tmp
|
|
|
|
chmod 777 busybox
|
2019-09-04 17:12:09 +02:00
|
|
|
chmod 777 magiskinit
|
2019-09-03 23:06:14 +02:00
|
|
|
|
2019-10-21 19:58:57 +02:00
|
|
|
if [ `./busybox id -u` -ne 0 ]; then
|
2020-03-09 07:27:06 +01:00
|
|
|
# Re-exec script with root
|
|
|
|
exec /system/xbin/su 0 ./busybox sh -o standalone $0
|
2019-10-21 19:58:57 +02:00
|
|
|
fi
|
2019-09-03 23:06:14 +02:00
|
|
|
|
2019-10-21 19:58:57 +02:00
|
|
|
# Remove previous setup if exist
|
|
|
|
pgrep magiskd >/dev/null && pkill -9 magiskd
|
|
|
|
[ -f /sbin/magisk ] && umount -l /sbin
|
|
|
|
|
2019-09-04 17:12:09 +02:00
|
|
|
# SELinux stuffs
|
|
|
|
[ -e /sys/fs/selinux ] && SELINUX=true || SELINUX=false
|
|
|
|
if $SELINUX; then
|
|
|
|
ln -sf ./magiskinit magiskpolicy
|
|
|
|
./magiskpolicy --live --magisk 'allow magisk * * *'
|
|
|
|
fi
|
2019-09-03 23:06:14 +02:00
|
|
|
|
|
|
|
# Setup sbin overlay
|
2019-09-04 17:12:09 +02:00
|
|
|
if mount | grep -q rootfs; then
|
|
|
|
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
|
|
|
|
else
|
|
|
|
mount_sbin
|
|
|
|
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
|
|
|
|
for file in /sbin/.magisk/mirror/system_root/sbin/*; do
|
|
|
|
[ ! -e $file ] && break
|
|
|
|
if [ -L $file ]; then
|
|
|
|
cp -af $file /sbin
|
|
|
|
else
|
|
|
|
sfile=/sbin/`basename $file`
|
|
|
|
touch $sfile
|
|
|
|
mount -o bind $file $sfile
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Magisk stuffs
|
2020-01-10 20:20:59 +01:00
|
|
|
./magiskinit -x magisk /sbin/magisk
|
2019-09-03 23:06:14 +02:00
|
|
|
chmod 755 /sbin/magisk
|
|
|
|
ln -s ./magisk /sbin/su
|
2019-09-04 17:12:09 +02:00
|
|
|
ln -s ./magisk /sbin/resetprop
|
|
|
|
ln -s ./magisk /sbin/magiskhide
|
2019-09-03 23:06:14 +02:00
|
|
|
mkdir -p /sbin/.magisk/busybox
|
|
|
|
cp -af ./busybox /sbin/.magisk/busybox/busybox
|
|
|
|
/sbin/.magisk/busybox/busybox --install -s /sbin/.magisk/busybox
|
|
|
|
mkdir -p /data/adb/modules 2>/dev/null
|
|
|
|
mkdir /data/adb/post-fs-data.d 2>/dev/null
|
|
|
|
mkdir /data/adb/services.d 2>/dev/null
|
2019-09-04 17:12:09 +02:00
|
|
|
/sbin/magisk --daemon
|