#!/sbin/sh OUTFD="/proc/self/fd/$2" ZIP=$3 set_con() { chcon -h u:object_r:"$1":s0 $2 chcon u:object_r:"$1":s0 $2 } set_perm() { chmod $1 $2 } set_owner() { chown $1:$2 $3 } ui_print() { echo "ui_print $1" > "$OUTFD"; echo "ui_print" > "$OUTFD"; } getprop2() { grep -m 1 "^$2=" $1 | cut -d= -f2 } nice_arch() { case $1 in aarch64*|armv8*) echo "arm64" ;; arm*) echo "arm" ;; *) echo $1 ;; esac } cleanup() { ui_print "Cleaning up files" rm -rf $TMP/system rm -rf $TMP/bin rm $TMP/toybox ui_print "Unmounting partitions" umount -l "$SYSTEM_MNT" umount -l /product || true } error() { ui_print "$1" cleanup exit 1 } error_no_space() { error "Not enough space for GApps! Aborting" } error_mounting() { error "Could not mount $1! Aborting" } get_block_for_mount_point() { grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1 } find_block() { local name="$1" local fstab_entry=$(get_block_for_mount_point "/$name") # P-SAR hacks [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/") [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root") local dev if [ "$DYNAMIC_PARTITIONS" = "true" ]; then if [ -n "$fstab_entry" ]; then dev="${BLK_PATH}/${fstab_entry}${SLOT_SUFFIX}" else dev="${BLK_PATH}/${name}${SLOT_SUFFIX}" fi else if [ -n "$fstab_entry" ]; then dev="${fstab_entry}${SLOT_SUFFIX}" else dev="${BLK_PATH}/${name}${SLOT_SUFFIX}" fi fi if [ -b "$dev" ]; then echo "$dev" fi } compute_apps_size() { NEEDED_STORAGE_SYSTEM=$(expr $(du -cs `find -maxdepth 1 -mindepth 1` | tail -n1 | cut -f1) + $STORAGE_BUFFER) RECLAIMABLE_STORAGE_SYSTEM=$(find . -type f | sed "s|^./|$SYSTEM_OUT/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1) NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM - $RECLAIMABLE_STORAGE_SYSTEM) } remove_big_optional_apps() { ui_print "Low resource device detected, removing large extras" rm -rf app/MarkupGoogle rm -rf priv-app/AndroidMigratePrebuilt rm -rf priv-app/SetupWizardPrebuilt rm -rf priv-app/Velvet } ui_print "**********************" ui_print "MindTheGapps installer" ui_print "**********************" ui_print "Extracting files" TMP=/tmp cd "$TMP" unzip -o "$ZIP" rm -rf META-INF # Check for arch. We need to do this before extracting our toybox, since that might be # compiled for a different architecture. Just hope that all environments have at least # a proper `grep` and `uname`. GAPPS_ARCH=$(getprop2 $TMP/build.prop arch) CPU_ARCH=$(uname -m) if [ $GAPPS_ARCH != $CPU_ARCH ]; then error "This package is built for $(nice_arch $GAPPS_ARCH) but your device is $(nice_arch $CPU_ARCH)! Aborting" fi ui_print "Setting up environment" TOYBOX="${TMP}/toybox" chmod +x "$TOYBOX" TOYS_PATH="${TMP}/bin" rm -rf "$TOYS_PATH"; $TOYBOX install -d "$TOYS_PATH" for toy in $($TOYBOX); do $TOYBOX ln -sf "$TOYBOX" "${TOYS_PATH}/${toy}" done PATH="${TOYS_PATH}:${PATH}" ui_print "Mounting partitions" # Ensure system is unmounted so mounting succeeds umount /system || umount /mnt/system || true umount /product || true # Find partitions DYNAMIC_PARTITIONS=`getprop ro.boot.dynamic_partitions` if [ "$DYNAMIC_PARTITIONS" = "true" ]; then BLK_PATH="/dev/block/mapper" else BLK_PATH=/dev/block/bootdevice/by-name fi CURRENTSLOT=`getprop ro.boot.slot_suffix` if [ ! -z "$CURRENTSLOT" ]; then if [ "$CURRENTSLOT" == "_a" ]; then SLOT_SUFFIX="_a" else SLOT_SUFFIX="_b" fi fi SYSTEM_BLOCK=$(find_block "system") PRODUCT_BLOCK=$(find_block "product") # Disable rw protection on dynamic partitions if [ "$DYNAMIC_PARTITIONS" = "true" ]; then blockdev --setrw "$SYSTEM_BLOCK" fi # Mount and define SYSTEM_OUT SYSTEM_MNT=/mnt/system mkdir -p "$SYSTEM_MNT" || true if mount -o rw "$SYSTEM_BLOCK" "$SYSTEM_MNT"; then ui_print "$SYSTEM_MNT mounted" else error_mounting "$SYSTEM_MNT" fi SYSTEM_OUT="${SYSTEM_MNT}/system" # Compare sdk version GAPPS_VERSION=$(getprop2 $TMP/build.prop version) ANDROID_VERSION=$(getprop2 $SYSTEM_OUT/build.prop ro.build.version.sdk) if [ "$GAPPS_VERSION" != "$ANDROID_VERSION" ]; then gapps_version_nice=$(getprop2 $TMP/build.prop version_nice) android_version_nice=$(getprop2 $SYSTEM_OUT/build.prop ro.build.version.release) error "This package is for Android $gapps_version_nice (SDK $GAPPS_VERSION) but your system is Android $android_version_nice (SDK $ANDROID_VERSION)! Aborting" fi # Ignore product block device in case it's a symlink # This is common on devices where maintainers have chosen not to use # real partitions because of their size being too small to be useful if [ -L "${SYSTEM_MNT}/product" ]; then PRODUCT_BLOCK="" fi if [ -n "$PRODUCT_BLOCK" ]; then mkdir /product || true if mount -o rw "$PRODUCT_BLOCK" /product; then ui_print "/product mounted" else error_mounting "/product" fi fi # Compute storage requirements SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4` STORAGE_BUFFER=10240 cd system compute_apps_size if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then remove_big_optional_apps compute_apps_size if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then error_no_space fi fi ui_print "Generating addon.d file" cat addon.d/addond_head > addon.d/30-gapps.sh for f in `find . ! -path "./addon.d/*" -type f`; do line=$(echo "$f" | sed 's/\.\///') echo "$line" >> addon.d/30-gapps.sh done cat addon.d/addond_tail >> addon.d/30-gapps.sh rm addon.d/addond_head addon.d/addond_tail ui_print "Preparing files for copying" for d in `find . -mindepth 1 -type d -type d`; do set_perm 0755 $d set_owner root root $d done for f in `find . -type f`; do type=$(echo "$f" | sed 's/.*\.//') if [ "$type" == "sh" ] || [ "$type" == "$f" ]; then set_perm 0755 $f else set_perm 0644 $f fi set_owner root root $f set_con system_file $f done ui_print "Copying files" cp --preserve=a -r ./* "${SYSTEM_OUT}/" if [ -e priv-app/SetupWizardPrebuilt ] ; then rm -rf "${SYSTEM_OUT}/product/priv-app/Provision" fi cleanup ui_print "Done!" exit 0