#! /bin/sh
writable_part="$(findfs LABEL=writable)"
writable_disk="$(echo $writable_part|sed 's/[0-9]*$//')"
partnum=$(echo $writable_part|sed 's/.*[a-z][^0-9]*//g')
# sanitize for mmcblk0p vs mmcblk0
case $(basename $writable_part) in
mmcblk*)
DISKNAME="$(echo $writable_disk|sed 's/p$//')"
;;
*)
DISKNAME="$writable_disk"
;;
esac
# obtain partition sizes
disk_size="$(grep -w $(basename $DISKNAME) /proc/partitions|tr -s ' '|cut -d' ' -f4)"
part_size="$(grep -w $(basename $writable_part) /proc/partitions|tr -s ' '|cut -d' ' -f4)"
all_parts="$(($(grep $(basename $writable_disk)[0-9].*$ /proc/partitions|tr -s ' '|cut -d' ' -f4|tr '\n' '+'|sed 's/+$//')))"
# if the sum of all parts is leaving more than 10% unused, grow writable
# partition to full disk size
if [ "$(($disk_size/10))" -lt "$(($disk_size-$all_parts))" ]; then
# grow our selected partition to max space available
echo ", +"|LANG=C /sbin/sfdisk -N $partnum $DISKNAME
# check the filesystem before attempting re-size
LANG=C /sbin/e2fsck -fy $writable_part
# resize the filesystem to full size of the partition
LANG=C /sbin/resize2fs -p $writable_part
fi