How can I shrink an image of an SD card to fit a smaller card?

4.5k views Asked by At

I've been slowly building a home automation framework (Python and SQL) on a Raspberry Pi. I'm really happy with it and am trying to deploy it on a few other Pi's around the house.

I've used win32diskimager to create a .img archive of the SD card and have (on a few occasions when I stuffed things up) been able to restore to the same card.

The problem is that (illustrative numbers only) my current "8GB" SD card is 7.88GB, but the new SD cards I've purchased are 7.86GB. The image is 7.88, so it refuses to write to the 7.86GB card (even though about half of that image is just blank space).

Can I resize my image? Tried using "truncate" in nix but (for some reason) vital data is stored at the end, or there's a checksum, because lopping off a few hundred mb was enough to corrupt the file and prevent mounting / writing.

Happy to take alternate solutions for SD card cloning too. I have access to OSX / Ubuntu / W8.1, but Windows is preferred because that's where I've got the img at the moment.

Appreciate any tips!

Sam

1

There are 1 answers

0
WARdd On BEST ANSWER

A bit late but I found a setup that works:

Run the SD card inside a raspberry and use

sudo apt-get install -y gparted
sudo gparted

To edit its partitions. Look for the main partition, unmount it and resize it to the desired size. I haven't confirmed whether it matters but I made sure the relevant data is "on the left" and the unallocated data on the right.

There's a more detailed guide on how to do this here: https://learn.adafruit.com/resizing-raspberry-pi-boot-partition/edit-partitions

If you can't do this for whatever reason, an alternative is setting up a fresh SD card with a new installation of say Raspbian lite and removing the option to expand into remaining space of the SD card from cmdline.txt (in my case this was the last option in the list) BEFORE running the new installation in a Raspberry pi. Then you can set up the Pi the way you like inside a smaller partition.

Once you have shrunk the needed partitions you can easily make a small image: In Windows, you can use Win32DiskImager to make the image, while making sure "Read Only Allocated Partitions" is checked, giving you an image that - surprise surprise - omitted the unallocated data.