How to Copy Your Raspberry Pi to a new SD Card

...even if it doesn't fit on the new card.

Troubles in Raspberry land

I started noticing problems with the Raspberry Pi I have running Domoticz a couple of months ago. After being away from the house, a fuse had gone out and after restoring power the automation didn't quite work. Looking at Domoticz, it seemed to have reverted to an earlier configuration. There was a gap in the temperature logs as well.

I didn't think too much of it, I just put the config back to what it was before the power out. However, the next time I restarted the Pi the config reverted again. I Googled around for tips and found several testimonies to SD cards that would stop writing files. This seemed to have happened to mine, although in a strange way that allowed changes to the files but where they'd revert at reboot (maybe they were updated in a cache in memory or something.

Cloning to a New SD Card

So I got a new SD card, the same size as the old one, and looked up the easiest way to clone the old card to the new one.

The short version of the recipe I found most efficient (in OSX):

  1. Insert the source SD card
  2. Open the application Disk Utility
  3. Click on the SD card (the top level of the card, not just one of the partitions like boot)
  4. In the menu, select File > New image > Image from "SD Card" (or "Untitled" or whatever your SD card is named according to OSX).
  5. Select format CD/DVD master
  6. When it's done, rename the image with the extension ".iso" instead of ".cdr".
  7. Eject the SD card.
  8. Insert the new SD card.
  9. Use Etcher to write the image to the new card.

But... it doesn't fit?

If you're lucky, it worked just fine writing the image to the new card. If you're like me Etcher (and any other image writing application) will give you an error message saying the image is too big for the new card. The thing is, 16 Gb is not necessarily the exact same amount of bytes on two different cards. When you run the Raspberry command to expand the file system to the whole card, it really fills the card which might mean slightly different number of bytes on different cards.

So how do you make it fit? It's extremely complicated, although not impossible, to manipulate the partitions in an image. We will choose a somewhat easier way which is to shrink a partition before making the image. MacOS can't manipulate partitions with the ext4 file format, so we need a Linux installation to make this happen. Luckily, a live boot from a USB drive will work just fine.

The application to use in Linux is GParted. Be careful, the changes you make to the paritions on the card with GParted are live and cannot be rolled back. So you might want a backup of the card (for instance in the form of an image - see above). I had yet another problem though. Since my card had gone into read-only mode (presumably from a hardware failure) I couldn't make the changes on the card. Luckily, I had a card with larger storage space lying around. It was the wrong kind for the Raspberry, but worked well to do this:

  1. Write the image I created in the section above on the larger card.
  2. Boot my Mac into Kali Linux
  3. Start GParted, find the extended partition on the card and reduce it by 1 GB.
  4. Boot back into OSX, and repeat the steps 1-9 in the list in the section

To make things really confusing, Disk utility in MacOS won't make an image if there is not as much free disk space on your Mac as the total size of the card, even though the partitions on the card are smaller (and the resulting image will be smaller).

comments powered by Disqus