Hi there, this very short tutorial is a solution when you need to backup/clone/save as small *.img file as possible of your whole fully bootable system (e.g. you have 8GB card but you want to make smaller system image for 2GB card). And another reason why I created this tutorial - I need to burn the same image to many microsd cards.
I am using Windows (yup, hate me know) and Debian in these steps:
Put your microsd card to Windows machine and make image of card with Win32diskimager. If your card is e.g. 8GB, you get *.img file with the same size (my name backup.img).
Now in linux maxine (in my case Debian virtual machine) we are going to work with backup.img file.
Run these commands in terminal (if you are not root use sudo at the start of each line)
losetup /dev/loop0 backup.img
Run gparted with this command and move slider of main partition to the left to make partition smaller (leave some space, e.g. I left 400MB free space)
Click on Apply once you are happy with it.
We can unload loopback device now
losetup -d /dev/loop0
With fdisk find out end of last block
fdisk -l backup.img
In my case last block is 3571711 so I run command
truncate --size=$[(3571711+1)*512] backup.img
Done! Size of your backup.img file should be now about 1.5GB.
You can burn this new backup.img file to another (or the same) card/s now. In my case I dramatically reduced time needed for burning data to many many cards (it saves hours/days when you need to burn a lot of microsd cards). Enjoy!
P.S. this can be done also with just Debian, so no need for Windows, in this case you just need to make first step with backup.img with linux command (e.g. dd). And of course you can set up a script on your newly burned card to expand system partition to the whole microsd size during first boot, if needed.