Files
vendor_gapps/build/meta/com/google/android/update-binary
Alessandro Astone 2b244a1ead Adapt free space checks to new partitions
And don't count as needed space that will be freed from the replaced
files. This will allow to flash the zip over an existing installation
when space is tight.

Also:
awk      -->  tr | cut  because recovery might not provide awk
grep -v  -->  tail -1   in du because it might spit out 3 lines
2021-02-10 23:50:12 +01:00

252 lines
6.7 KiB
Bash

#!/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";
}
cleanup() {
ui_print "Cleaning up files"
cd ../
rm -rf system
ui_print "Unmounting partitions"
umount -l "$SYSTEM_MNT"
umount -l /product || true
umount -l /system_ext || true
}
error_no_space() {
ui_print "Not enough space for GApps! Aborting"
cleanup
exit 1
}
get_block_for_mount_point() {
grep -v "^#" /etc/recovery.fstab | grep " $1 " | tail -n1 | tr -s ' ' | 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 ! -name product ! -name system_ext` | tail -n1 | cut -f1) + $STORAGE_BUFFER)
NEEDED_STORAGE_PRODUCT=$(expr $(du -s ./product | cut -f1) + $STORAGE_BUFFER)
NEEDED_STORAGE_SYSTEM_EXT=$(expr $(du -s ./system_ext | cut -f1) + $STORAGE_BUFFER)
RECLAIMABLE_STORAGE_SYSTEM=$(find . ! -path "./product/*" ! -path "./system_ext/*" -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)
RECLAIMABLE_STORAGE_PRODUCT=$(find ./product -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_PRODUCT=$(expr $NEEDED_STORAGE_PRODUCT - $RECLAIMABLE_STORAGE_PRODUCT)
RECLAIMABLE_STORAGE_SYSTEM_EXT=$(find ./system_ext -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_EXT=$(expr $NEEDED_STORAGE_SYSTEM_EXT - $RECLAIMABLE_STORAGE_SYSTEM_EXT)
if [ -z "$PRODUCT_BLOCK" ]; then
NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM + $NEEDED_STORAGE_PRODUCT - $STORAGE_BUFFER)
fi
if [ -z "$SYSTEM_EXT_BLOCK" ]; then
NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM + $NEEDED_STORAGE_SYSTEM_EXT - $STORAGE_BUFFER)
fi
}
remove_big_optional_apps() {
ui_print "Low resource device detected, removing large extras"
rm -rf product/app/MarkupGoogle
rm -rf product/priv-app/AndroidMigratePrebuilt
rm -rf product/priv-app/SetupWizardPrebuilt
rm -rf product/priv-app/Velvet
}
ui_print "**********************"
ui_print "MindTheGapps installer"
ui_print "**********************"
ui_print "Mounting partitions"
# Ensure system is unmounted so mounting succeeds
umount /system || umount /mnt/system || true
umount /product || true
umount /system_ext || 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")
SYSTEM_EXT_BLOCK=$(find_block "system_ext")
# Disable rw protection on dynamic partitions
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
blockdev --setrw "$SYSTEM_BLOCK"
if [ -n "$PRODUCT_BLOCK" ]; then
blockdev --setrw "$PRODUCT_BLOCK"
fi
if [ -n "$SYSTEM_EXT_BLOCK" ]; then
blockdev --setrw "$SYSTEM_EXT_BLOCK"
fi
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
ui_print "Could not mount $SYSTEM_MNT! Aborting"
exit 1
fi
SYSTEM_OUT="${SYSTEM_MNT}/system"
if [ -n "$PRODUCT_BLOCK" ]; then
mkdir /product || true
if mount -o rw "$PRODUCT_BLOCK" /product; then
ui_print "/product mounted"
else
ui_print "Could not mount /product"
exit 1
fi
fi
if [ -n "$SYSTEM_EXT_BLOCK" ]; then
mkdir /system_ext || true
if mount -o rw "$SYSTEM_EXT_BLOCK" /system_ext; then
ui_print "/system_ext mounted"
else
ui_print "Could not mount /system_ext"
exit 1
fi
fi
# Compute storage requirements
SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4`
if [ -n "$PRODUCT_BLOCK" ]; then
PRODUCT_STORAGE=`df /product | tail -1 | tr -s ' ' | cut -d ' ' -f4`
fi
if [ -n "$SYSTEM_EXT_BLOCK" ]; then
SYSTEM_EXT_STORAGE=`df /system_ext | tail -1 | tr -s ' ' | cut -d ' ' -f4`
fi
STORAGE_BUFFER=10240
ui_print "Extracting files"
cd /tmp
unzip -o "$ZIP"
rm -rf META-INF
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
if [ -n "$PRODUCT_BLOCK" ] && [ "$PRODUCT_STORAGE" -lt "$NEEDED_STORAGE_PRODUCT" ]; then
remove_big_optional_apps
compute_apps_size
if [ "$PRODUCT_STORAGE" -lt "$NEEDED_STORAGE_PRODUCT" ]; then
error_no_space
fi
fi
if [ -n "$SYSTEM_EXT_BLOCK" ] && [ "$SYSTEM_EXT_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM_EXT" ]; then
error_no_space
fi
ui_print "Generating addon.d file"
cat addon.d/addond_head > addon.d/30-gapps.sh
for f in `find . -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 [ -n "$PRODUCT_BLOCK" ]; then
cp --preserve=a -r ./product/* /product
fi
if [ -n "$SYSTEM_EXT_BLOCK" ]; then
cp --preserve=a -r ./system_ext/* /system_ext
fi
if [ -e product/priv-app/SetupWizardPrebuilt ] ; then
rm -rf /system/system_ext/priv-app/Provision
fi
cleanup
ui_print "Done!"
exit 0