Configure an SD Card Image for Better Compression

If you’re working on a Linux image for your single board computer (e.g. Raspberry Pi, OlinuXino, BeagleBone Black, etc.) and you notice that the compression of gzip sucks for the backup image, you need to re-format the SD card. For example, I created a Debian image on a card, which was partitioned for the whole 4GB. Now, we know that only the first few megabytes will be used, and the rest will be empty. However, if you’ve previously used the card (or you might see this with a factory-fresh card), the rest of the card will not be zeros. This will result in a poor compression ratio. My 4GB compressed to 3.1GB. Not great.

I recommend performing these steps before starting to build Linux, as it will erase everything on the card.

Figure out where the SD card is located:

Mine is /dev/mmcblk0. Yours might be /dev/sdb. I’ll use /dev/X from now on to denote the SD card.

WARNING: Let me repeat myself. THIS WILL ERASE EVERYTHING ON THE CARD. Use with caution.

It’s pretty simple. You’ll just have to wait a while for the writing to finish. After that, you can load your bootloader, Linux kernel, and rootfs to the SD card, and any unused space should be all zeros.

I tried backing up the card again, and this time, I saw much better compression: my 4GB card became a 203MB zipped image file (granted, this was a bare-bones Debian image).

Leave a Reply