Magic Mount total re-factor

1. Remove stage 4 bind which mount files back, use symlinks instead
2. libs do not support symlink, make a copy to data if needed
3. Proper support for adding new files into /system/vendor on devices with separate vendor partition
This commit is contained in:
topjohnwu 2016-12-07 01:45:09 +08:00
parent 6e3bb48574
commit c07e9ac29d

View File

@ -77,44 +77,44 @@ target_size_check() {
} }
travel() { travel() {
cd "$1/$2" cd "$TRAVEL_ROOT/$1"
if [ -f ".replace" ]; then if [ -f ".replace" ]; then
rm -rf "$MOUNTINFO/$2" rm -rf "$MOUNTINFO/$1"
mktouch "$MOUNTINFO/$2" "$1" mktouch "$MOUNTINFO/$1" "$TRAVEL_ROOT"
else else
for ITEM in * ; do for ITEM in * ; do
if [ ! -e "/$2/$ITEM" ]; then if [ ! -e "/$1/$ITEM" ]; then
# New item found # New item found
if [ "$2" = "system" ]; then if [ "$1" = "system" ]; then
# We cannot add new items to /system root, delete it # We cannot add new items to /system root, delete it
rm -rf "$ITEM" rm -rf "$ITEM"
else else
# If we are in a higher level, delete the lower levels # If we are in a higher level, delete the lower levels
rm -rf "$MOUNTINFO/dummy/$2" rm -rf "$MOUNTINFO/dummy/$1"
# Mount the dummy parent # Mount the dummy parent
mktouch "$MOUNTINFO/dummy/$2" mktouch "$MOUNTINFO/dummy/$1"
if [ -d "$ITEM" ]; then if [ -d "$ITEM" ]; then
# Create new dummy directory and mount it # Create new dummy directory and mount it
mkdir -p "$DUMMDIR/$2/$ITEM" mkdir -p "$DUMMDIR/$1/$ITEM"
mktouch "$MOUNTINFO/$2/$ITEM" "$1" mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
elif [ -L "$ITEM" ]; then elif [ -L "$ITEM" ]; then
# Symlinks are small, copy them # Symlinks are small, copy them
mkdir -p "$DUMMDIR/$2" 2>/dev/null mkdir -p "$DUMMDIR/$1" 2>/dev/null
cp -afc "$ITEM" "$DUMMDIR/$2/$ITEM" cp -afc "$ITEM" "$DUMMDIR/$1/$ITEM"
else else
# Create new dummy file and mount it # Create new dummy file and mount it
mktouch "$DUMMDIR/$2/$ITEM" mktouch "$DUMMDIR/$1/$ITEM"
mktouch "$MOUNTINFO/$2/$ITEM" "$1" mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
fi fi
fi fi
else else
if [ -d "$ITEM" ]; then if [ -d "$ITEM" ]; then
# It's an directory, travel deeper # It's an directory, travel deeper
(travel "$1" "$2/$ITEM") (travel "$1/$ITEM")
elif [ ! -L "$ITEM" ]; then elif [ ! -L "$ITEM" ]; then
# Mount this file # Mount this file
mktouch "$MOUNTINFO/$2/$ITEM" "$1" mktouch "$MOUNTINFO/$1/$ITEM" "$TRAVEL_ROOT"
fi fi
fi fi
done done
@ -122,31 +122,51 @@ travel() {
} }
clone_dummy() { clone_dummy() {
for ITEM in "$1/"* ; do for ITEM in $1/* ; do
if [ ! -e "$DUMMDIR$ITEM" ]; then if [ ! -e "$TRAVEL_ROOT$ITEM" ]; then
if [ ! -d "$MOUNTINFO$ITEM" ]; then if [ ! -d "$MOUNTINFO$ITEM" ]; then
if [ -d "$ITEM" ]; then if [ -L "$ITEM" ]; then
# Create dummy directory # Copy original symlink
mkdir -p "$DUMMDIR$ITEM" cp -afc "$ITEM" "$TRAVEL_ROOT$ITEM"
elif [ -L "$ITEM" ]; then
# Symlinks are small, copy them
cp -afc "$ITEM" "$DUMMDIR$ITEM"
else else
# Create dummy file # Link to mirror item
mktouch "$DUMMDIR$ITEM" ln -s "$MIRRDIR$ITEM" "$TRAVEL_ROOT$ITEM"
fi fi
chcon -f "u:object_r:system_file:s0" "$DUMMDIR$ITEM"
else else
# Need to clone a skeleton # Need to clone a skeleton
(clone_dummy "$ITEM") (clone_dummy "$ITEM")
fi fi
elif [ -d "$DUMMDIR$ITEM" ]; then elif [ -d "$TRAVEL_ROOT$ITEM" ]; then
# Need to clone a skeleton # Need to clone a skeleton
(clone_dummy "$ITEM") (clone_dummy "$ITEM")
fi fi
done done
} }
make_copy_image() {
TARGETSIZE=$2
if [ -z $TARGETSIZE ]; then
TARGETSIZE=`du -s $1 | awk '{print $1}'`
TARGETSIZE=$((($TARGETSIZE / 10240 + 2) * 10240))
fi
TARGETIMG=/data/magisk/${1//\//_}.img
make_ext4fs -l ${TARGETSIZE}K -a $1 $TARGETIMG
loopsetup $TARGETIMG
mkdir -p $MIRRDIR/copy$1
mount -t ext4 -o rw,noatime $LOOPDEVICE $MIRRDIR/copy$1
return $?
}
mount_copy_image() {
TARGETIMG=/data/magisk/${1//\//_}.img
umount $MIRRDIR/copy$1
rm -rf $MIRRDIR/copy
losetup -d $LOOPDEVICE 2>/dev/null
loopsetup $TARGETIMG
mount -t ext4 -o rw,noatime $LOOPDEVICE $1
return $?
}
bind_mount() { bind_mount() {
if [ -e "$1" -a -e "$2" ]; then if [ -e "$1" -a -e "$2" ]; then
mount -o bind $1 $2 mount -o bind $1 $2
@ -200,7 +220,7 @@ merge_image() {
OK=false OK=false
fi fi
if ($OK); then if $OK; then
# Merge (will reserve selinux contexts) # Merge (will reserve selinux contexts)
cd /cache/merge_img cd /cache/merge_img
for MOD in *; do for MOD in *; do
@ -247,7 +267,7 @@ case $1 in
rm -rf /cache/magisk /cache/magisk_merge /cache/magiskhide.log rm -rf /cache/magisk /cache/magisk_merge /cache/magiskhide.log
if [ -d "/cache/magisk_mount" ]; then if [ -d "/cache/magisk_mount" ]; then
log_print "Mounting cache files" log_print "* Mounting cache files"
find /cache/magisk_mount -type f 2>/dev/null | while read ITEM ; do find /cache/magisk_mount -type f 2>/dev/null | while read ITEM ; do
chmod 644 "$ITEM" chmod 644 "$ITEM"
chcon "u:object_r:system_file:s0" "$ITEM" chcon "u:object_r:system_file:s0" "$ITEM"
@ -305,11 +325,11 @@ case $1 in
merge_image /data/magisk_merge.img merge_image /data/magisk_merge.img
# Mount magisk.img # Mount magisk.img
[ ! -d "$MOUNTPOINT" ] && mkdir -p $MOUNTPOINT [ ! -d $MOUNTPOINT ] && mkdir -p $MOUNTPOINT
if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then
loopsetup $IMG loopsetup $IMG
if [ ! -z "$LOOPDEVICE" ]; then if [ ! -z "$LOOPDEVICE" ]; then
mount -t ext4 -o rw,noatime,suid $LOOPDEVICE $MOUNTPOINT mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
fi fi
fi fi
@ -318,11 +338,11 @@ case $1 in
unblock unblock
fi fi
# Remove empty directories, legacy paths and symlinks # Remove empty directories, legacy paths, symlinks, old temporary images
rm -rf $COREDIR/bin $COREDIR/dummy $COREDIR/mirror
find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null find $MOUNTPOINT -type d -depth ! -path "*core*" -exec rmdir {} \; 2>/dev/null
rm -rf $COREDIR/bin $COREDIR/dummy $COREDIR/mirror /data/magisk/*.img
# Remove modules # Remove modules that is labeled to be removed
for MOD in $MOUNTPOINT/* ; do for MOD in $MOUNTPOINT/* ; do
if [ -f "$MOD/remove" ] || [ "$MOD" = "zzsupersu" ]; then if [ -f "$MOD/remove" ] || [ "$MOD" = "zzsupersu" ]; then
log_print "Remove module: $MOD" log_print "Remove module: $MOD"
@ -341,7 +361,7 @@ case $1 in
fi fi
loopsetup $IMG loopsetup $IMG
if [ ! -z "$LOOPDEVICE" ]; then if [ ! -z "$LOOPDEVICE" ]; then
mount -t ext4 -o rw,noatime,suid $LOOPDEVICE $MOUNTPOINT mount -t ext4 -o rw,noatime $LOOPDEVICE $MOUNTPOINT
fi fi
if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then if [ `cat /proc/mounts | grep $MOUNTPOINT >/dev/null 2>&1; echo $?` -ne 0 ]; then
log_print "magisk.img mount failed, nothing to do :(" log_print "magisk.img mount failed, nothing to do :("
@ -349,70 +369,129 @@ case $1 in
fi fi
fi fi
log_print "Preparing modules" log_print "* Preparing modules"
mkdir -p $DUMMDIR mkdir -p $DUMMDIR
mkdir -p $MIRRDIR/system mkdir -p $MIRRDIR/system
# Travel through all mods, all magic happens here # Travel through all mods
for MOD in $MOUNTPOINT/* ; do for MOD in $MOUNTPOINT/* ; do
if [ -f "$MOD/auto_mount" -a -d "$MOD/system" -a ! -f "$MOD/disable" ]; then if [ -f "$MOD/auto_mount" -a -d "$MOD/system" -a ! -f "$MOD/disable" ]; then
(travel $MOD system) TRAVEL_ROOT=$MOD
(travel system)
fi fi
done done
# Proper permissions for generated items # Proper permissions for generated items
find $TMPDIR -exec chcon -h "u:object_r:system_file:s0" {} \; find $TMPDIR -exec chcon -h "u:object_r:system_file:s0" {} \;
# linker(64), t*box required # linker(64), t*box required for bin
if [ -f "$MOUNTINFO/dummy/system/bin" ]; then if [ -f "$MOUNTINFO/dummy/system/bin" ]; then
cd /system/bin cp -afc /system/bin/linker* /system/bin/t*box $DUMMDIR/system/bin/
cp -afc linker* t*box $DUMMDIR/system/bin/
fi fi
# Some libraries are required BACKUPLIBS=false
LIBS="libc++.so libc.so libcutils.so libm.so libstdc++.so libcrypto.so liblog.so libpcre.so libselinux.so libpackagelistparser.so"
# Libraries are full of issues, copy a full clone to data
# lib
if [ -f "$MOUNTINFO/dummy/system/lib" ]; then if [ -f "$MOUNTINFO/dummy/system/lib" ]; then
cd /system/lib BACKUPLIBS=true
cp -afc $LIBS $DUMMDIR/system/lib make_copy_image /system/lib
# Crash prevention!! cp -afc /system/lib/. $MIRRDIR/copy/system/lib
rm -f $COREDIR/magiskhide/enable 2>/dev/null cp -afc $DUMMDIR/system/lib/. $MIRRDIR/copy/system/lib
fi mount_copy_image /system/lib
if [ -f "$MOUNTINFO/dummy/system/lib64" ]; then rm -f $MOUNTINFO/dummy/system/lib
cd /system/lib64
cp -afc $LIBS $DUMMDIR/system/lib64
# Crash prevention!!
rm -f $COREDIR/magiskhide/enable 2>/dev/null
fi fi
# vendor libraries are device dependent, had no choice but copy them all if needed.... # lib64
if [ -f "$MOUNTINFO/dummy/system/lib64" ]; then
BACKUPLIBS=true
make_copy_image /system/lib64
cp -afc /system/lib64/. $MIRRDIR/copy/system/lib64
cp -afc $DUMMDIR/system/lib64/. $MIRRDIR/copy/system/lib64
mount_copy_image /system/lib64
rm -f $MOUNTINFO/dummy/system/lib64
fi
# Whole vendor
if [ -f "$MOUNTINFO/dummy/system/vendor" ]; then if [ -f "$MOUNTINFO/dummy/system/vendor" ]; then
cp -afc /system/vendor/lib/. $DUMMDIR/system/vendor/lib BACKUPLIBS=true
[ -d "/system/vendor/lib64" ] && cp -afc /system/vendor/lib64/. $DUMMDIR/system/vendor/lib64 LIBSIZE=`du -s /vendor/lib | awk '{print $1}'`
if [ -d /vendor/lib64 ]; then
LIB64SIZE=`du -s /vendor/lib64 | awk '{print $1}'`
VENDORLIBSIZE=$(((($LIBSIZE + $LIB64SIZE) / 10240 + 2) * 10240))
else
VENDORLIBSIZE=$((($LIBSIZE / 10240 + 2) * 10240))
fi
make_copy_image /vendor $VENDORLIBSIZE
# Copy lib/lib64
mkdir -p $MIRRDIR/copy/vendor/lib
cp -afc /vendor/lib/. $MIRRDIR/copy/vendor/lib
cp -afc $DUMMDIR/system/vendor/lib/. $MIRRDIR/copy/vendor/lib 2>/dev/null
if [ -d /vendor/lib64 ]; then
mkdir -p $MIRRDIR/copy/vendor/lib64
cp -afc /vendor/lib64/. $MIRRDIR/copy/vendor/lib64
cp -afc $DUMMDIR/system/vendor/lib64/. $MIRRDIR/copy/vendor/lib64 2>/dev/null
fi
cp -afc $DUMMDIR/system/vendor/. $MIRRDIR/copy/vendor
TRAVEL_ROOT=$MIRRDIR/copy
(clone_dummy /vendor)
# Create vendor mirror
if [ `mount | grep -c "on /vendor type"` -ne 0 ]; then
VENDORBLOCK=`mount | grep "on /vendor type" | awk '{print $1}'`
mkdir -p $MIRRDIR/vendor
mount -o ro $VENDORBLOCK $MIRRDIR/vendor
else
ln -s $MIRRDIR/system/vendor $MIRRDIR/vendor
fi
mount_copy_image /vendor
rm -f $MOUNTINFO/dummy/system/vendor
fi fi
# vendor lib
if [ -f "$MOUNTINFO/dummy/system/vendor/lib" ]; then if [ -f "$MOUNTINFO/dummy/system/vendor/lib" ]; then
cp -afc /system/vendor/lib/. $DUMMDIR/system/vendor/lib BACKUPLIBS=true
make_copy_image /system/vendor/lib
cp -afc /system/vendor/lib/. $MIRRDIR/copy/system/vendor/lib
cp -afc $DUMMDIR/system/vendor/lib/. $MIRRDIR/copy/system/vendor/lib
mount_copy_image /system/vendor/lib
rm -f $MOUNTINFO/dummy/system/vendor/lib
fi fi
# vendor lib64
if [ -f "$MOUNTINFO/dummy/system/vendor/lib64" ]; then if [ -f "$MOUNTINFO/dummy/system/vendor/lib64" ]; then
cp -afc /system/vendor/lib64/. $DUMMDIR/system/vendor/lib64 BACKUPLIBS=true
make_copy_image /system/vendor/lib64
cp -afc /system/vendor/lib64/. $MIRRDIR/copy/system/vendor/lib64
cp -afc $DUMMDIR/system/vendor/lib64/. $MIRRDIR/copy/system/vendor/lib64
mount_copy_image /system/vendor/lib64
rm -f $MOUNTINFO/dummy/system/vendor/lib64
fi fi
# Crash prevention!!
$BACKUPLIBS && rm -f $COREDIR/magiskhide/enable 2>/dev/null
# Remove crap folder # Remove crap folder
rm -rf $MOUNTPOINT/lost+found rm -rf $MOUNTPOINT/lost+found
# Start doing tasks # Start doing tasks
# Stage 1 # Stage 1
log_print "Bind mount dummy system" TRAVEL_ROOT=$DUMMDIR
log_print "* Bind mount dummy system"
find $MOUNTINFO/dummy -type f 2>/dev/null | while read ITEM ; do find $MOUNTINFO/dummy -type f 2>/dev/null | while read ITEM ; do
TARGET=${ITEM#$MOUNTINFO/dummy} TARGET=${ITEM#$MOUNTINFO/dummy}
ORIG="$DUMMDIR$TARGET" ORIG="$DUMMDIR$TARGET"
clone_dummy "$TARGET" (clone_dummy "$TARGET")
bind_mount "$ORIG" "$TARGET" bind_mount "$ORIG" "$TARGET"
done done
# Stage 2 # Stage 2
log_print "Bind mount module items" log_print "* Bind mount module items"
find $MOUNTINFO/system -type f 2>/dev/null | while read ITEM ; do find $MOUNTINFO/system -type f 2>/dev/null | while read ITEM ; do
TARGET=${ITEM#$MOUNTINFO} TARGET=${ITEM#$MOUNTINFO}
ORIG=`cat $ITEM`$TARGET ORIG=`cat $ITEM`$TARGET
@ -425,13 +504,13 @@ case $1 in
# Bind hosts for Adblock apps # Bind hosts for Adblock apps
if [ -f "$COREDIR/hosts" ]; then if [ -f "$COREDIR/hosts" ]; then
log_print "Enabling systemless hosts file support" log_print "* Enabling systemless hosts file support"
bind_mount $COREDIR/hosts /system/etc/hosts bind_mount $COREDIR/hosts /system/etc/hosts
fi fi
# Expose busybox # Expose busybox
if [ -f "$COREDIR/busybox/enable" ]; then if [ -f "$COREDIR/busybox/enable" ]; then
log_print "Enabling BusyBox" log_print "* Enabling BusyBox"
cp -afc /data/busybox/. $COREDIR/busybox cp -afc /data/busybox/. $COREDIR/busybox
cp -afc /system/xbin/. $COREDIR/busybox cp -afc /system/xbin/. $COREDIR/busybox
chmod -R 755 $COREDIR/busybox chmod -R 755 $COREDIR/busybox
@ -440,23 +519,11 @@ case $1 in
fi fi
# Stage 3 # Stage 3
log_print "Bind mount system mirror" log_print "* Bind mount system mirror"
bind_mount /system $MIRRDIR/system bind_mount /system $MIRRDIR/system
# Stage 4
log_print "Bind mount mirror items"
# Find all empty directores and dummy files, they should be mounted by original files in /system
TOOLPATH=/data/busybox find $DUMMDIR -type d \
-exec sh -c '[ -z "`ls -A $1`" ] && echo $1' -- {} \; \
-o \( -type f -size 0 -print \) | \
while read ITEM ; do
ORIG=${ITEM/dummy/mirror}
TARGET=${ITEM#$DUMMDIR}
bind_mount $ORIG $TARGET
done
# Restart post-fs-data if necessary (multirom) # Restart post-fs-data if necessary (multirom)
($MULTIROM) && setprop magisk.restart_pfsd 1 $MULTIROM && setprop magisk.restart_pfsd 1
fi fi
unblock unblock
@ -470,21 +537,24 @@ case $1 in
# Magisk Hide # Magisk Hide
if [ -f "$COREDIR/magiskhide/enable" ]; then if [ -f "$COREDIR/magiskhide/enable" ]; then
log_print "** Removing tampered read-only system props" log_print "* Removing tampered read-only system props"
VERIFYBOOT=`getprop ro.boot.verifiedbootstate` VERIFYBOOT=`getprop ro.boot.verifiedbootstate`
FLASHLOCKED=`getprop ro.boot.flash.locked` FLASHLOCKED=`getprop ro.boot.flash.locked`
VERITYMODE=`getprop ro.boot.veritymode` VERITYMODE=`getprop ro.boot.veritymode`
[ ! -z "$VERIFYBOOT" -a "$VERIFYBOOT" != "green" ] && log_print "`$BINPATH/resetprop -v -n ro.boot.verifiedbootstate green`" [ ! -z "$VERIFYBOOT" -a "$VERIFYBOOT" != "green" ] && \
[ ! -z "$FLASHLOCKED" -a "$FLASHLOCKED" != "1" ] && log_print "`$BINPATH/resetprop -v -n ro.boot.flash.locked 1`" log_print "`$BINPATH/resetprop -v -n ro.boot.verifiedbootstate green`"
[ ! -z "$VERITYMODE" -a "$VERITYMODE" != "enforcing" ] && log_print "`$BINPATH/resetprop -v -n ro.boot.veritymode enforcing`" [ ! -z "$FLASHLOCKED" -a "$FLASHLOCKED" != "1" ] && \
log_print "`$BINPATH/resetprop -v -n ro.boot.flash.locked 1`"
[ ! -z "$VERITYMODE" -a "$VERITYMODE" != "enforcing" ] && \
log_print "`$BINPATH/resetprop -v -n ro.boot.veritymode enforcing`"
mktouch $COREDIR/magiskhide/hidelist mktouch $COREDIR/magiskhide/hidelist
chmod -R 755 $COREDIR/magiskhide chmod -R 755 $COREDIR/magiskhide
# Add Safety Net preset # Add Safety Net preset
$COREDIR/magiskhide/add com.google.android.gms.unstable $COREDIR/magiskhide/add com.google.android.gms.unstable
log_print "** Starting Magisk Hide" log_print "* Starting Magisk Hide"
/data/magisk/magiskhide /data/magisk/magiskhide
fi fi
;; ;;