Reference: here
Make a script
sudo nano autosizer.sh
Insert the followin into the file:
#!/bin/bash # Automatic Image file resizer # Written by SirLagz strImgFile=$1 export PATH=$PATH:/sbin if [[ ! $(whoami) =~ “root” ]]; then echo “” echo “**********************************” echo “*** This should be run as root ***” echo “**********************************” echo “” exit fi if [[ -z $1 ]]; then echo “Usage: ./autosizer.sh ” exit fi if [[ ! -e $1 || ! $(file $1) =~ “x86” ]]; then echo “Error : Not an image file, or file doesn’t exist” exit fi partinfo=`parted -ms $1 unit B p` existRootPart=`echo “$partinfo” | grep -o ‘B:ext4:\|B:ext3:’` numberOfMatchPart=`echo “$existRootPart” | wc -l` if [[ $existRootPart == “” || $numberOfMatchPart -eq 0 || $numberOfMatchPart -gt 1 ]] ; then echo “Error : Your partition layout is not currently supported by this tool.” exit fi fileSystemType=`echo “$existRootPart” | cut -d: -f2` partnumber=`echo “$partinfo” | grep $fileSystemType | awk -F: ‘ { print $1 } ‘` partstart=`echo “$partinfo” | grep $fileSystemType | awk -F: ‘ { print substr($2,0,length($2)) } ‘` loopback=`losetup -f –show -o $partstart $1` e2fsck -f $loopback minsize=`resize2fs -P $loopback | awk -F’: ‘ ‘ { print $2 } ‘` minsize=`echo $minsize+1000 | bc` resize2fs -p $loopback $minsize sleep 1 losetup -d $loopback partnewsize=`echo “$minsize * 4096” | bc` newpartend=`echo “$partstart + $partnewsize” | bc` parted $1 rm $partnumber parted $1 unit B mkpart primary $partstart $newpartend endresult=`parted -m $1 unit B print free | tail -1 | awk -F: ‘ { print substr($2,0,length($2)) } ‘` truncate -s $endresult $1
Save and exit editor