SD Image Resize
Working on a new crypto related project on the Pi, the blockchain needs to be sync’ed which takes for ever so I set about imaging the SD card after the initial sync has taken place so all it’s doing is playing catch up.
I discover that not all SD Cards are made equal! 16Gb to one manufacture is not 16Gb to another, so I have images that I have taken that can’t be written back to another manufactures cards of the same size, a little frustrating but there has to be a work around.
If you do a normal image backup using something like Win32 Disk Imager, you’ll end up with an image that is 16GB big, and when you try to write it back to another manufactures card you might get an error message stating that there is not enough space. So the answer is to remove the un-used space from the main partition on the SD Card as the image is just that; an image of the partitions that make up the card free space and all.
Using Gparted (or parted), reduce the size of your main partition on the SD card to reduce the amount of free space (leave a little free space to be on the safe side) but do not reduce the size of the other partitions. Then use Gparted to make sure each partition runs consecutively from the start without any unallocated space in between them. Take note of the total size all the partitions are using up and the location of your SD card (dev/sdb for example).
Next up we are going to us dd, you need to be careful at this point as the wrong commands and you could end up erasing your hard drive or any other removable media you have available to the machine at the time.
The command we are after is going to look something similar to this;
dd if=/dev/sdb of=image_name.img bs=1M count=6144
The command is made up of the following sections;
“if=/dev/sdb” (sdb is the name of your SD card, it could be different on your system.)
“of=image_name.img” (The name of the image you want to create)
“bs=1M” (This specifies block size. Here we’ve said copy over blocks of one megabyte (1024×1024 bytes) at a time. Read the manual page for dd if you want to change this value.)
“count=6144” (This is how many blocks to copy over. In this example we’ve said copy over only 6144 blocks and then stop. Because we set the block size as 1 megabyte, we will get an image that is 6144 times 1 megabyte, which is 6 gigabytes.)
The count and bs values need to be adapted to take into consideration the total size of all of your partitions on the SD Card
Because Gparted doesn’t show the exact size of each partition in bytes, you’ll generally want to make the image a little larger than the total size of all the partitions, I normally round things up to the nearest 1/4 GB as for this purpose I’m just trying to get over the manufacture discrepancies in sizing.