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:

ls /dev

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.

sudo dd if=/dev/zero of=/dev/X

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).

2 thoughts on “Configure an SD Card Image for Better Compression

  1. fra on April 11, 2020 at 3:29 pm Reply

    Hello,

    I like the method because I too would like with the smallest possible img file of my sd card for my raspberry.
    I know how to use win32diskimager but to use the instructions on this page you need a Linux OS … The only one I have is on my raspberry pi.
    How to physically install the SD card to prepare? On a USB socket with a usb-sd adapter?

    1. ShawnHymel on April 12, 2020 at 2:13 pm Reply

      Yes, a USB to SD adapter should work.

Leave a Comment

Your email address will not be published. Marked fields are required.