2019-09-03 21:06:14 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
# AVD Magisk Setup
|
2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
#
|
|
|
|
# This script will setup an environment with minimal Magisk that
|
2021-03-21 22:25:56 +00:00
|
|
|
# the Magisk app will be happy to run properly within the official
|
2019-09-03 21:06:14 +00:00
|
|
|
# emulator bundled with Android Studio (AVD).
|
|
|
|
#
|
2021-03-21 22:25:56 +00:00
|
|
|
# ONLY use this script for developing the Magisk app or root apps
|
2019-09-04 15:12:09 +00:00
|
|
|
# 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 21:06:14 +00:00
|
|
|
#
|
2021-04-10 04:29:42 +00:00
|
|
|
# The script assumes you are using x64 emulator images.
|
2019-09-03 21:06:14 +00:00
|
|
|
# Build binaries with `./build.py binary` before running this script.
|
|
|
|
#
|
2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
|
|
|
|
abort() {
|
|
|
|
echo "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:12:09 +00:00
|
|
|
mount_sbin() {
|
|
|
|
mount -t tmpfs -o 'mode=0755' tmpfs /sbin
|
|
|
|
$SELINUX && chcon u:object_r:rootfs:s0 /sbin
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:06:14 +00:00
|
|
|
if [ ! -f /system/build.prop ]; then
|
|
|
|
# Running on PC
|
2021-01-18 12:25:26 +00:00
|
|
|
cd "$(dirname "$0")/.."
|
2021-05-13 11:45:13 +00:00
|
|
|
adb push native/out/x86_64/busybox native/out/x86_64/magiskinit \
|
2021-04-10 04:29:42 +00:00
|
|
|
native/out/x86_64/magisk scripts/emulator.sh /data/local/tmp
|
2019-09-03 21:06:14 +00:00
|
|
|
adb shell sh /data/local/tmp/emulator.sh
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd /data/local/tmp
|
|
|
|
chmod 777 busybox
|
2019-09-04 15:12:09 +00:00
|
|
|
chmod 777 magiskinit
|
2021-01-18 12:25:26 +00:00
|
|
|
chmod 777 magisk
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2020-07-27 10:32:54 +00:00
|
|
|
if [ -z "$FIRST_STAGE" ]; then
|
|
|
|
export FIRST_STAGE=1
|
|
|
|
export ASH_STANDALONE=1
|
|
|
|
if [ `./busybox id -u` -ne 0 ]; then
|
|
|
|
# Re-exec script with root
|
|
|
|
exec /system/xbin/su 0 ./busybox sh $0
|
|
|
|
else
|
|
|
|
# Re-exec script with busybox
|
|
|
|
exec ./busybox sh $0
|
|
|
|
fi
|
2019-10-21 17:58:57 +00:00
|
|
|
fi
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2019-10-21 17:58:57 +00:00
|
|
|
# Remove previous setup if exist
|
|
|
|
pgrep magiskd >/dev/null && pkill -9 magiskd
|
|
|
|
[ -f /sbin/magisk ] && umount -l /sbin
|
2020-07-27 10:32:54 +00:00
|
|
|
[ -f /system/bin/magisk ] && umount -l /system/bin
|
2019-10-21 17:58:57 +00:00
|
|
|
|
2019-09-04 15:12:09 +00:00
|
|
|
# SELinux stuffs
|
2020-11-30 00:35:34 +00:00
|
|
|
SELINUX=false
|
|
|
|
[ -e /sys/fs/selinux ] && SELINUX=true
|
2019-09-04 15:12:09 +00:00
|
|
|
if $SELINUX; then
|
|
|
|
ln -sf ./magiskinit magiskpolicy
|
2020-07-06 08:13:50 +00:00
|
|
|
./magiskpolicy --live --magisk
|
2019-09-04 15:12:09 +00:00
|
|
|
fi
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2020-07-27 10:32:54 +00:00
|
|
|
BINDIR=/sbin
|
|
|
|
|
|
|
|
# Setup bin overlay
|
2019-09-04 15:12:09 +00:00
|
|
|
if mount | grep -q rootfs; then
|
2020-07-27 10:32:54 +00:00
|
|
|
# Legacy rootfs
|
2019-09-04 15:12:09 +00:00
|
|
|
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 10:32:54 +00:00
|
|
|
elif [ -e /sbin ]; then
|
|
|
|
# Legacy SAR
|
2019-09-04 15:12:09 +00:00
|
|
|
mount_sbin
|
2020-07-27 10:32:54 +00:00
|
|
|
if ! grep -q '/sbin/.magisk/mirror/system_root' /proc/mounts; then
|
|
|
|
mkdir -p /sbin/.magisk/mirror/system_root
|
2021-04-10 04:29:42 +00:00
|
|
|
block=$(mount | grep ' / ' | awk '{ print $1 }')
|
2020-07-27 10:32:54 +00:00
|
|
|
[ $block = "/dev/root" ] && block=/dev/block/dm-0
|
|
|
|
mount -o ro $block /sbin/.magisk/mirror/system_root
|
|
|
|
fi
|
2019-09-04 15:12:09 +00:00
|
|
|
for file in /sbin/.magisk/mirror/system_root/sbin/*; do
|
|
|
|
[ ! -e $file ] && break
|
|
|
|
if [ -L $file ]; then
|
|
|
|
cp -af $file /sbin
|
|
|
|
else
|
2021-04-10 04:29:42 +00:00
|
|
|
sfile=/sbin/$(basename $file)
|
2019-09-04 15:12:09 +00:00
|
|
|
touch $sfile
|
|
|
|
mount -o bind $file $sfile
|
|
|
|
fi
|
|
|
|
done
|
2020-07-27 10:32:54 +00:00
|
|
|
else
|
|
|
|
# Android Q+ without sbin, use overlayfs
|
|
|
|
BINDIR=/system/bin
|
|
|
|
rm -rf /dev/magisk
|
|
|
|
mkdir -p /dev/magisk/upper
|
|
|
|
mkdir /dev/magisk/work
|
|
|
|
./magisk --clone-attr /system/bin /dev/magisk/upper
|
|
|
|
mount -t overlay overlay -olowerdir=/system/bin,upperdir=/dev/magisk/upper,workdir=/dev/magisk/work /system/bin
|
2019-09-04 15:12:09 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Magisk stuffs
|
2021-01-18 12:25:26 +00:00
|
|
|
cp -af ./magisk $BINDIR/magisk
|
2020-07-27 10:32:54 +00:00
|
|
|
chmod 755 $BINDIR/magisk
|
|
|
|
ln -s ./magisk $BINDIR/su
|
|
|
|
ln -s ./magisk $BINDIR/resetprop
|
|
|
|
ln -s ./magisk $BINDIR/magiskhide
|
2019-09-03 21:06:14 +00:00
|
|
|
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
|
2020-07-27 10:32:54 +00:00
|
|
|
$BINDIR/magisk --daemon
|