27 Apr

How to migrate a Raspberry Pi SD card to a bigger SD Card

Snazzy title, James.

I recently bought a 200GB SD card from Amazon for what I consider to be a completely crazy price (around $80USD). My Raspberry Pi 3 is running OwnCloud (howto post coming soon) and the 32GB card currently serving as the OS’ home is a little on the small side. It’s also quite an old card, so I’m worried it will one day up and die.

Here’s a quick step-by-step on how to move your SD card to a bigger one, using a Mac with OSX El Capitan (or basically any Unix based computer with an SD card reader).

Step 0: Backup anything important. Things can go wrong any time you mess with this stuff, so caution.

Step 1: Shutdown your Pi (pull the plug out)

Step 2: Take the SD card you want to copy out of the pi, and stick it in your Mac (with an adaptor). It will mount automatically, so run Disk Utility and unmount the ‘boot’ partition, which is what Raspbian OS calls it by default. Once the partitions are greyed out, they’re not mounted anymore.

Screen Shot 2016-04-26 at 2.28.51 pm

Step 3: Check you have space. You will need as much free hard-drive space on your Mac as equals the size of the SD card, even if it’s not full. Mine is 32GB, so I need 32GB hard drive space free. Sadly, I don’t have that much free on my tiny SSD, so I will be using an external USB 3 drive with ample space.

Step 4: Find the BSD name of your SD card reader/card. You do this by opening a terminal and entering:

Yeah which will spit out a list of your connected media. We’re looking for something that matches the 32GB size of the SD Card (yours might be 8GB, 16GB… whatever)

Here we see ‘disk2’ under Identifier, which is what we want.

Step 5: Find out where the file copy of the SD card should go. I want to stick it on my external HDD called SUGAR ROSS (for some reason), so I need to find where it’s mounted.

There I show you that the 1TB external drive I have connected is located at /Volumes/Sugar\ Ross/ . The extra \ I have in there is an escape character, which helps Unix based Operating Systems handle things like spaces in folder names, which it didn’t historically like.

Step 6: Copy that floppy. We’re now going to use one of the oldest Unix commands there is – dd. Once again, in the terminal, tell the computer to make a direct byte-for-byte copy of the SD card and stick it in a file called sdcard.img (or whatever you fancy).

This will take a long while. It’s pretty oldschool and takes its job very seriously. You can tell what’s going on behind the scenes by pressing control-t every now and then. So far, mine has written 23GB in 1226 seconds, and still running. Won’t be much longer now, have a cup of tea. (I will not be doing this for my 200GB card.. Hopefully).

Step 7: Check your image. Ok, 27 minutes elapsed time later, and dd tells me it has finished. As I never believe anything my computer tells me, I want to check the output file.

Using ‘ls -la’ shows me all the files on SUGAR ROSS, and I can see my nice .img file there at just shy of 32GB.

Step 8: Format wars. The SD card I am replacing my Pi’s with is 200GB, and I’m told that gargantuan SD cards have a funny filesystem that’s not compatible with the Pi. As I am writing a physical copy of the existing card onto the new one, I don’t think I’ll have a problem. If you do, I suggest formatting it to FAT32 and going from there. The dd process (this time in reverse) we just ran will take care of everything, as we didn’t copy a partition of the SD card, we copied the whole thing.

Step 9: Prepare the new card. Stick the new (bigger) SD Card into the Mac’s card reader. Use diskutil list again to find the BSD name of the disk, and then unmount any partitions that automatically mounted.

Step 10: Write to disk. This time, we use dd in reverse (and wait a lot longer).

Hint: make sure you unmount, not eject the SD card partition on the card you want to write out to. Disk Utility is good for this.

Step 11: Fire it up. Now that we’ve spent 11,000 seconds writing the 32GB image back to the new card, unmount it (if you succeed with dd in step 10, it should auto mount on OSX). Place the new card into the Pi and power it up. With any luck, it will fire up as it did before. Note, you’ll still only see the original filesize, for now..

(See how it still says 29G? That’s a good indication my old SD card (and all the partitions on it) were copied over to the 200G card.

Step 12: Expand the filesystem. Run the following command to stretch out the filesystem to the full 200G.

You can then select ‘Expand filesystem’ from the menu and reboot..

Step 13: Revel in your success.

Here we see 181GiB of usable space (roughly 200GB). Hooray!

4 thoughts on “How to migrate a Raspberry Pi SD card to a bigger SD Card

  1. Hi James,

    thanks a lot for your help – Am in the middle of attempting it myself.
    One question though… When creating the image of your old card using dd – Where did the “r” in your input file name come from? (if=/dev/rdisk2) – Typo? Your disk was called disk2 not rdisk2.

    Greetings from Munich

    • Hallo Stephan,

      I think that’s a curiosity of MacOS (I was doing this on my Mac), it’s not a typo. You can use Tab auto-completion in most shells, go with what that presents you.

      All the best

      James

    • The ‘r’ in ‘rdisk’ if for ‘raw disk’. It’s a different access point to the disk that is direct block-writes instead of going through the buffer cache of ‘/dev/disk’. The write goes much faster against the ‘raw’ disk.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.