I'm in the process of experimenting with backing up a Raspberry Pi (Raspbian) sd card. I'm using Win32DiskImager to read the image (from SD card reader) onto my HDD, successfully - but now the size issue. The image is just bigger than the original SD card and can only be written back to a bigger SD card, which, if you back that up has the same problem.

After creating an .img file, how can I remove all the unused space again? (is this because I used raspi-config to allocate all available space to the root?)

I tried to get an idea of what the partition(s) look like with Cygwin, but can't manage to get fdisk working (-bash: fdisk: command not found) - (after reading Image of a 16Gb card containing unpartitioned space at the end: Truncating possible?)

up vote 12 down vote accepted

So the best way I have found is to use GParted (you can either use a Linux-based OS, or boot from a GParted Live USB). It is simple enough to find detailed instructions on how to do each of these steps in detail, but here is the general method I have found to work:

  1. Run GParted, find your SD card in the device list and shrink the main partition to as small as it will go.
  2. Take the value of the last sector of this partition and multiply this by the sector size (512 for me; I presume this is standard, although you can check this in GParted settings for your device): this will give you the total size in bytes to copy.
  3. Divide the total number of bytes by 4096 (round up if you need to - better to take a bit more space than not enough), and this will give you the 'count' to then enter into the following command at the terminal in a Linux environment (You can probably do this from the GParted Live USB operating system terminal or in Cygwin without sudo so long as you are running it as an administrator):

sudo dd if=/dev/mmcblk0 of=/path/to/your/file.img bs=4k count=<count>

Change the input device location if you need to. This should give you a final image file that is the smallest that it can be, omitting empty space.

  • I can verify that this works. I've just tried it a few minutes ago to go from an 8GB card to a 4GB card. This should work provided that your data is smaller than your destination card. – pandalion98 Jun 9 '16 at 17:05
  • Sort off forgot about this post since I moved on. Will try it out in the future. Seems like this is a common problem, so thanks for your input. – Renier Delport Jul 4 '16 at 16:48
  • This should be marked as the correct answer! Thanks! – rodripf Nov 19 '16 at 15:43
  • any way on Windows? – Flash Thunder Dec 19 '16 at 17:26
  • 1
    You can boot from a GParted Live USB. – Mike Roberts Dec 20 '16 at 11:36

The exists a nice tool called pishrink which reduces the size of a dd image as much as possible by shrinking the root partition. If you start up the restored image the first time the partition is expanded to it's maximum size again. If you run Windows just use your Linux on your Raspberry to shrink the image.

Newest version of Win32DiskImager has option Read Only Allocated Partitions. It writes only used space, not empty.

  1. Shrink partition on sd card with GParted.
  2. Create image with Win32DiskImager from bigger card.
  3. Write image with Win32DiskImager on smaller card using option mentionted higher.

The simple answer is you can't. If you want to shrink the image use compression, which is what most do (on 'NIX gzip works well). You will find many discussions on this on this site.

The alternative is to backup only files. My approach is described in the following. https://raspberrypi.stackexchange.com/a/28087/8697

  • 1
    you can (if the data content is smaller than the new SD card) – Wilf Nov 21 '15 at 16:39
  • 2
    Compression won't help. It's not a matter of disk space where the IMG file is located. It's a matter of space on the SD card where the IMG file shall be written to – Thomas Weller Aug 31 '16 at 12:39
  • @Thomas Compressing the IMG file will make it smaller so it (hopefully) fits onto the SD card you want to write it to.... am I missing something? – immibis Oct 11 '16 at 2:49
  • @immibis: You can gzip the IMG file so it uses less space on your PC's hard disk. If you write the gzipped data to the SD cards, it will not boot. The data needs to be in correct format in order to boot and it's not gzip format. – Thomas Weller Oct 11 '16 at 6:13
  • @Thomas Oh, I thought the OP wanted to store their backups on another SD card for some reason, as opposed to restoring them. – immibis Oct 11 '16 at 7:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.