From 185796a26d2eea159e7cd6fe8de71efcaf33ced7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 11 Jul 2017 01:51:39 +0800 Subject: [PATCH] Magisk Module Template v3 -> v4 --- META-INF/com/google/android/update-binary | 300 ++++++---------------- README.md | 9 +- common/file_contexts_image | 1 - config.sh | 4 +- module.prop | 2 +- 5 files changed, 87 insertions(+), 229 deletions(-) mode change 100644 => 100755 META-INF/com/google/android/update-binary delete mode 100644 common/file_contexts_image diff --git a/META-INF/com/google/android/update-binary b/META-INF/com/google/android/update-binary old mode 100644 new mode 100755 index 861d0b29f..0d0a75e07 --- a/META-INF/com/google/android/update-binary +++ b/META-INF/com/google/android/update-binary @@ -1,15 +1,36 @@ #!/sbin/sh +########################################################################################## +# +# Magisk Module Template Install Script +# by topjohnwu +# +########################################################################################## # Detect whether in boot mode ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false +$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true + +# This path should work in any cases +TMPDIR=/dev/tmp +MOUNTPATH=/magisk +IMG=/data/magisk.img +if $BOOTMODE; then + MOUNTPATH=/dev/magisk_merge + IMG=/data/magisk_merge.img +fi +INSTALLER=$TMPDIR/install +MAGISKBIN=/data/magisk # Default permissions umask 022 ########################################################################################## -# Functions +# Flashable update-binary preparation ########################################################################################## +OUTFD=$2 +ZIP=$3 + ui_print() { if $BOOTMODE; then echo "$1" @@ -19,167 +40,39 @@ ui_print() { fi } -grep_prop() { - REGEX="s/^$1=//p" - shift - FILES=$@ - if [ -z "$FILES" ]; then - FILES='/system/build.prop' - fi - cat $FILES 2>/dev/null | sed -n "$REGEX" | head -n 1 -} - -is_mounted() { - if [ ! -z "$2" ]; then - cat /proc/mounts | grep $1 | grep $2, >/dev/null - else - cat /proc/mounts | grep $1 >/dev/null - fi - return $? -} - -mount_image() { - if [ ! -d "$2" ]; then - mount -o rw,remount rootfs / - mkdir -p $2 2>/dev/null - ($BOOTMODE) && mount -o ro,remount rootfs / - [ ! -d "$2" ] && return 1 - fi - if (! is_mounted $2); then - LOOPDEVICE= - for LOOP in 0 1 2 3 4 5 6 7; do - if (! is_mounted $2); then - LOOPDEVICE=/dev/block/loop$LOOP - if [ ! -f "$LOOPDEVICE" ]; then - mknod $LOOPDEVICE b 7 $LOOP 2>/dev/null - fi - losetup $LOOPDEVICE $1 - if [ "$?" -eq "0" ]; then - mount -t ext4 -o loop $LOOPDEVICE $2 - if (! is_mounted $2); then - /system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE $2 - fi - if (! is_mounted $2); then - /system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE $2 - fi - fi - if (is_mounted $2); then - ui_print "- Mounting $1 to $2" - break; - fi - fi - done - fi -} - -set_perm() { - chown $2:$3 $1 || exit 1 - chmod $4 $1 || exit 1 - if [ ! -z "$5" ]; then - chcon $5 $1 2>/dev/null - else - chcon 'u:object_r:system_file:s0' $1 2>/dev/null - fi +require_new_magisk() { + ui_print "***********************************" + ui_print "! $MAGISKBIN isn't setup properly!" + ui_print "! Please install Magisk v13.1+!" + ui_print "***********************************" + exit 1 } -set_perm_recursive() { - find $1 -type d 2>/dev/null | while read dir; do - set_perm $dir $2 $3 $4 $6 - done - find $1 -type f 2>/dev/null | while read file; do - set_perm $file $2 $3 $5 $6 - done -} +# Mount /data to access MAGISKBIN +mount /data 2>/dev/null -mktouch() { - mkdir -p ${1%/*} - if [ -z "$2" ]; then - touch $1 - else - echo $2 > $1 - fi - chmod 644 $1 -} +# MAGISKBIN must exist, binaries and utility functions are placed there +[ -d $MAGISKBIN -a -f $MAGISKBIN/magisk -a -f $MAGISKBIN/util_functions.sh ] || require_new_magisk -request_size_check() { - reqSizeM=`unzip -l "$1" 2>/dev/null | tail -n 1 | awk '{ print $1 }'` - reqSizeM=$((reqSizeM / 1048576 + 1)) -} +# Load utility fuctions +. $MAGISKBIN/util_functions.sh +[ ! -z $SCRIPT_VERSION -a $SCRIPT_VERSION -ge 1310 ] || require_new_magisk +get_outfd -image_size_check() { - e2fsck -yf $1 - curBlocks=`e2fsck -n $1 2>/dev/null | grep $1 | cut -d, -f3 | cut -d\ -f2`; - curUsedM=`echo "$curBlocks" | cut -d/ -f1` - curSizeM=`echo "$curBlocks" | cut -d/ -f1` - curFreeM=$(((curSizeM - curUsedM) * 4 / 1024)) - curUsedM=$((curUsedM * 4 / 1024 + 1)) - curSizeM=$((curSizeM * 4 / 1024)) -} +rm -rf $TMPDIR 2>/dev/null +mkdir -p $INSTALLER +unzip -o "$ZIP" config.sh -d $INSTALLER 2>/dev/null ########################################################################################## -# Flashable update-binary preparation +# Prepare ########################################################################################## -OUTFD=$2 -ZIP=$3 - -readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null -if [ "$?" -eq "0" ]; then - OUTFD=0 - - for FD in `ls /proc/$$/fd`; do - readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null - if [ "$?" -eq "0" ]; then - ps | grep " 3 $FD " | grep -v grep >/dev/null - if [ "$?" -eq "0" ]; then - OUTFD=$FD - break - fi - fi - done -fi +[ ! -f $INSTALLER/config.sh ] && abort "! Unable to extract zip file!" -if $BOOTMODE && ! is_mounted /magisk; then - ui_print "! Magisk is not activated!... abort" - exit 1 -fi +. $INSTALLER/config.sh -# Fix SuperSU..... -$BOOTMODE && $BINDIR/sepolicy-inject --live "allow fsck * * *" - -# This path should work in any cases -TMPDIR=/dev/tmp - -MOUNTPATH=/magisk -IMGNAME=magisk.img - -if $BOOTMODE; then - MOUNTPATH=/dev/magisk_merge - IMGNAME=magisk_merge.img -fi - -mkdir -p $TMPDIR 2>/dev/null -cd $TMPDIR -unzip -o "$ZIP" config.sh - -if [ ! -f "config.sh" ]; then - ui_print "! Failed: Unable to extract zip file!" - exit 1 -fi - -source config.sh - -INSTALLER=$TMPDIR/$MODID MODPATH=$MOUNTPATH/$MODID -mkdir -p $INSTALLER -cd $INSTALLER -unzip -o "$ZIP" "common/*" module.prop - -########################################################################################## -# Main -########################################################################################## - # Print mod name print_modname @@ -188,73 +81,50 @@ ui_print "******************************" ui_print "Powered by Magisk (@topjohnwu)" ui_print "******************************" -ui_print "- Mounting /system(ro), /vendor(ro), /data, /cache" +ui_print "- Mounting /system, /vendor, /data, /cache" mount -o ro /system 2>/dev/null mount -o ro /vendor 2>/dev/null mount /data 2>/dev/null mount /cache 2>/dev/null -if [ ! -f '/system/build.prop' ]; then - ui_print "! Failed: /system could not be mounted!" - exit 1 -fi - -API=`grep_prop ro.build.version.sdk` -ABI=`grep_prop ro.product.cpu.abi | cut -c-3` -ABI2=`grep_prop ro.product.cpu.abi2 | cut -c-3` -ABILONG=`grep_prop ro.product.cpu.abi` +[ ! -f /data/magisk.img ] && abort "! Magisk is not installed" +$BOOTMODE && ! is_mounted /magisk && abort "! Magisk is not activated!" +[ ! -f /system/build.prop ] && abort "! /system could not be mounted!" -ARCH=arm -IS64BIT=false -if [ "$ABI" = "x86" ]; then ARCH=x86; fi; -if [ "$ABI2" = "x86" ]; then ARCH=x86; fi; -if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; IS64BIT=true; fi; -if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; IS64BIT=true; fi; +# Detect version and architecture +api_level_arch_detect # You can get the Android API version from $API, the CPU architecture from $ARCH # Useful if you are creating Android version / platform dependent mods -if is_mounted /data; then - IMG=/data/$IMGNAME - if [ ! -f "/data/magisk.img" ]; then - ui_print "! Magisk is not installed!" - ui_print "! Magisk is required for this mod!" - exit 1 - fi -else - IMG=/cache/magisk.img - ui_print " " - ui_print "***********************************" - ui_print "* !! Data unavailible !! *" - ui_print "* Magisk detection is impossible *" - ui_print "* Installation will still proceed *" - ui_print "* But please make sure you have *" - ui_print "* Magisk installed!! *" - ui_print "***********************************" - ui_print " " -fi +########################################################################################## +# Install +########################################################################################## -request_size_check "$ZIP" +ui_print "- Extracting module files" +unzip -o "$ZIP" -d $INSTALLER 2>/dev/null +request_size_check $INSTALLER + +# We're going to use magisk binary now, require some recovery fixes +$BOOTMODE || recovery_actions if [ -f "$IMG" ]; then ui_print "- $IMG detected!" image_size_check $IMG if [ "$reqSizeM" -gt "$curFreeM" ]; then - SIZE=$(((reqSizeM + curUsedM) / 32 * 32 + 64)) - ui_print "- Resizing $IMG to ${SIZE}M..." - resize2fs $IMG ${SIZE}M + newSizeM=$(((reqSizeM + curUsedM) / 32 * 32 + 64)) + ui_print "- Resizing $IMG to ${newSizeM}M" + $MAGISKBIN/magisk --resizeimg $IMG $newSizeM fi else - SIZE=$((reqSizeM / 32 * 32 + 64)); - ui_print "- Creating $IMG with size ${SIZE}M" - make_ext4fs -l ${SIZE}M -a /magisk -S $INSTALLER/common/file_contexts_image $IMG + newSizeM=$((reqSizeM / 32 * 32 + 64)); + ui_print "- Creating $IMG with size ${newSizeM}M" + $MAGISKBIN/magisk --createimg $IMG $newSizeM fi -mount_image $IMG $MOUNTPATH -if ! is_mounted $MOUNTPATH; then - ui_print "! $IMG mount failed... abort" - exit 1 -fi +ui_print "- Mounting $IMG to $MOUNTPATH" +MAGISKLOOP=`$MAGISKBIN/magisk --mountimg $IMG $MOUNTPATH` +is_mounted $MOUNTPATH || abort"! $IMG mount failed..." # Create mod paths rm -rf $MODPATH 2>/dev/null @@ -262,7 +132,7 @@ mkdir -p $MODPATH # Copy files ui_print "- Copying files" -unzip -o "$ZIP" "system/*" -d $MODPATH +mv $INSTALLER/system $MODPATH/system # Handle replace folders for TARGET in $REPLACE; do @@ -270,14 +140,10 @@ for TARGET in $REPLACE; do done # Auto Mount -if $AUTOMOUNT; then - mktouch $MODPATH/auto_mount -fi +$AUTOMOUNT && touch $MODPATH/auto_mount # prop files -if $PROPFILE; then - cp -af $INSTALLER/common/system.prop $MODPATH/system.prop -fi +$PROPFILE && cp -af $INSTALLER/common/system.prop $MODPATH/system.prop # Module info cp -af $INSTALLER/module.prop $MODPATH/module.prop @@ -288,36 +154,30 @@ if $BOOTMODE; then fi # post-fs-data mode scripts -if $POSTFSDATA; then - cp -af $INSTALLER/common/post-fs-data.sh $MODPATH/post-fs-data.sh -fi +$POSTFSDATA && cp -af $INSTALLER/common/post-fs-data.sh $MODPATH/post-fs-data.sh # service mode scripts -if $LATESTARTSERVICE; then - cp -af $INSTALLER/common/service.sh $MODPATH/service.sh -fi +$LATESTARTSERVICE && cp -af $INSTALLER/common/service.sh $MODPATH/service.sh ui_print "- Setting permissions" set_permissions -ui_print "- Unmounting partitions" +########################################################################################## +# Finalizing +########################################################################################## -umount $MOUNTPATH -losetup -d $LOOPDEVICE +$MAGISKBIN/magisk --umountimg $MOUNTPATH $MAGISKLOOP rmdir $MOUNTPATH # Shrink the image if possible image_size_check $IMG -NEWDATASIZE=$((curUsedM / 32 * 32 + 32)) -if [ "$curSizeM" -gt "$NEWDATASIZE" ]; then - ui_print "- Shrinking $IMG to ${NEWDATASIZE}M..." - resize2fs $IMG ${NEWDATASIZE}M +newSizeM=$((curUsedM / 32 * 32 + 64)) +if [ $curSizeM -gt $newSizeM ]; then + ui_print "- Shrinking $IMG to ${newSizeM}M" + $MAGISKBIN/magisk --resizeimg $IMG $newSizeM fi -if ! $BOOTMODE; then - umount /system - umount /vendor 2>/dev/null -fi +$BOOTMODE || recovery_cleanup ui_print "- Done" exit 0 diff --git a/README.md b/README.md index f1bc1d4a0..099fc3a9d 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,11 @@ Check the [Markdown Cheat Sheet](https://github.com/adam-p/markdown-here/wiki/Ma 6. Your repo should be cloned into [Magisk-Modules-Repo](https://github.com/Magisk-Modules-Repo), and you should receive an email to become the collaborator of that repo so you can edit it in the future. ## Notes -1. (Windows aware!!) This git repo is configured to force Unix endlines on all necessary files. The line endings on these files should remain the Unix format. Please use advanced text editors like Sublime, Atom, Notepad++ etc. to edit the text files -2. In `module.prop`, `version` is any string you like, so any fancy version name (e.g. ultra-beta-v0.0.0.1) is allowed. However, `versionCode` **MUST** be an integer. The value is used for version comparison. -2. Make sure your module ID **doesn't contain any spaces**. -3. (For repo developers) Magisk Manager monitors all repo's `master` branch. So any changes to the branch `master` will be reflected to all users immediately. If you are working on an update for a module, please work on another branch, make sure it works, and then merge the changes back to `master`. +- Module Template v4 is **NOT** backwards compatibile with any Magisk version lower than v13.1! +- (Windows aware!!) This git repo is configured to force Unix endlines on all necessary files. The line endings on these files should remain the Unix format. Please use advanced text editors like Sublime, Atom, Notepad++ etc. to edit the text files +- In `module.prop`, `version` is any string you like, so any fancy version name (e.g. ultra-beta-v0.0.0.1) is allowed. However, `versionCode` **MUST** be an integer. The value is used for version comparison. +- Make sure your module ID **doesn't contain any spaces**. +- (For repo developers) Magisk Manager monitors all repo's `master` branch. So any changes to the branch `master` will be reflected to all users immediately. If you are working on an update for a module, please work on another branch, make sure it works, and then merge the changes back to `master`. ## Best Practice for Updating a Repo 1. Open a new branch, and start update your files on the new branch diff --git a/common/file_contexts_image b/common/file_contexts_image deleted file mode 100644 index ee54edc68..000000000 --- a/common/file_contexts_image +++ /dev/null @@ -1 +0,0 @@ -/magisk(/.*)? u:object_r:system_file:s0 diff --git a/config.sh b/config.sh index 5d0d56131..af7e65cbf 100644 --- a/config.sh +++ b/config.sh @@ -1,10 +1,8 @@ ########################################################################################## # -# Magisk +# Magisk Module Template Config Script # by topjohnwu # -# This is a template zip for developers -# ########################################################################################## ########################################################################################## # diff --git a/module.prop b/module.prop index b38d97bcb..4aca12e67 100644 --- a/module.prop +++ b/module.prop @@ -4,4 +4,4 @@ version=v1 versionCode=1 author=topjohnwu description=A short description -template=3 +template=4