When a new SD card is loaded with a new version of the OS, /boot/cmdline.txt contains a command to execute a script on the very first boot of this SD card. It expands the filesystem to use all space on the SD card, then removes itself from the cmdline file. This automatic method was added to help prevent SD card corruption, I think.
When you run the RPi, it also creates a large swap file space on the SD card that gets saved into the backup image.
For your particular situation, you can change that behaviour by eliminating the expansion statement before you first boot with the card, and turn off swapping to save space on the SD card partition.
Here is a simple, although a little cumbersome method. Depending on your skills, it can be shortened, but this should work. When you use different RPi models, this may not work. Stick to one model for all of this.
Create a new SD card image, like you normally would.
Leave the SD card in the PC after the image has been written. Make a copy of the original cmdline.txt file in the DOS partition of the SD card to make a backup. Edit the original file and remove the expansion command (I can't tell you what it is right now, but it's pretty much self explanatory). Take note of the command(!), and remove it from that single line.
Save the file and then boot the card in the RPi. The best thing is to switch off swapping right away. Execute
Code: Select all
sudo systemctl stop dphys-swapfile.service
Now do your setup (do not expand the filesystem) and load whatever software you need. Make sure it all works. Remove the swap file:
Shutdown the RPi and save that image on your PC. That image should not be much larger than the original one. Keep that image secure!
After you have saved the image on the PC, create new SD card with the saved image of your working system. On that image, you can put the statement back into the cmdline.txt file, or use the backup version, so the partition will be expanded automatically at first boot. Do NOT put that card in the RPi!
Note: When you copy the cmdline.txt file, make sure that Windows does not mess with the file extensions, if in doubt, use the DOS command box.
Save that image on the SD card back on the PC, it will be your master, so keep it safe.
Create a child copy from your master SD image and try that in an RPi before you really start duplicating. The first time you boot, the RPi will boot again after it has expanded the partition on the card. Check everything before you start duplicating.
The added benefit is that you can now also use SD cards with different sizes.