[Sony Xperia M4 Aqua] HRSE:
i have followed the guide made by OP and successfully resized both /system and /data partition (new /system is 1.14GB and new /data is 5.24GB - see attached photos)

some notes:

OP's script will auto calculate the new size for /system and /data, but only resize /system. user have to manually resize /data the script only outputs the commands needed to be executed, user has to copy these output lines and paste to adb shell prompt to actually make changes to the partition

Example output when runing the script to resize /system:

[SIZE="2"][B]sh ./calculate_sectors.sh[/B]
mount-grep RC=1
/system not mounted. mounting now
mount RC=0
Your system partion has 531 mbyte free ( kbyte = 544012 ; sectors = 1088024)
Data partition first sector: 1449984
Data partition last sector: 10043391
System partition first sector: 10043392
System partition last sector: 15269375
Data partion NEW last sector: 11115023
System partion NEW first sector: 11115024

echo Shrik system partition
echo unmount /system partition
echo delete partition
echo create system partition with new sector boundaries
echo change name
echo create ext4 partion

umount /system
sgdisk /dev/block/mmcblk0 --delete 30
sgdisk /dev/block/mmcblk0 --new 30:11115024:15269375
sgdisk /dev/block/mmcblk0 --change-name 30:system
[COLOR="Red"]make_ext4fs -b 4096 -L system /dev/block/mmcblk0p30[/COLOR]

# TODO increase data paratition[/SIZE]

Somehow the last command to format the partition make_ext4fs -b 4096 -L system /dev/block/mmcblk0p30 made the /system partition unable to be modified by TWRP or Flashtool, and the phone entered bootloop (Sony logo then restart). OP also encountered the same issue (post #8).

I don't know how OP solved it, but for my case, neither Flashtool 0.9.24.4 nor 0.9.18.6 could flash /system in flashmode, and fastboot mode also failed. My solution is to format /system again using TWRP (somewhere inside Wipe>AdvancedWipe/FormatData). After that TWRP can restore normally. I also do the resize using TWRP, but I'm not sure if it's compulsory

When restore TWRP: MUST tick Use rm -rf instead of formatting, otherwise the new partition size is not updated properly.

Some good read for anyone wants to know the details: https://unix.stackexchange.com/questions/10776/why-are-partition-size-and-df-output-different and https://access.redhat.com/articles/1196353

Summary

: 1) remove the line make_ext4fs -b 4096... in OP's script, 2) untick TWRP setting rm -rf and manually format in TWRP AdvancedWipe/FormatData, 3) tick TWRP setting rm -rf then restore maybe the correct sequence is format, and

resize

before restore?

some useful commands:

cat /proc/partitions to check the partition size

df to verify if the partition is resized properly

To resize /userdata (partition 29), the sequence is almost the same: 1) note down the "Data partition first sector" and "Data partion NEW last sector" that shown after running the script, then 2) umnount/delete/create/change name of the partition. For my case:

[SIZE="2"]umount /data
sgdisk /dev/block/mmcblk0 --delete 29
sgdisk /dev/block/mmcblk0 --new 29:1449984:11115023 [COLOR="Gray"] (29 is /userdata partition, middle number is first sector, last numer is NEW last sector)[/COLOR]
sgdisk /dev/block/mmcblk0 --change-name 29:userdata[/SIZE]

MANUALLY BACKUP /DATA/MEDIA BEFORE TOUCHING /DATA PARTITION !

TWRP doesn't modify (wipe/backup/restore) /data/media, you need to do it yourself. (I learned it the hard way)

More reading here https://www.xda-developers.com/what-is-a-data-media-device/

After resizing partition : need to turn off phone and reboot to TWRP again so that the partition sizes are updated

I have to repeat the resize process twice because I do not have enough free space in /data to move all apps out of /system in one go.