Working with SD Cards

When deploying and working with embedded linux systems on SD cards we regularly need to take snapshots of the SD card and re-image the cards, or partition and format them.

This page is essentially a quick SD Carding imaging cheat sheet to perform those tasks. I’m working under Ubuntu 12.10 during these steps. If you are working on Windows and sighing because everything you read is in Linux, why not get Oracle VirtualBox and run this on an Ubuntu Virtual Machine. That way, this guide works under Windows too! 😉

If you’re running in a virtual machine under virtual box, don’t forget to hook the USB card reader across so that the Linux client has control of the card reader! Otherwise you’ll not get anything done!

Which Device

Knowing which device relates to your card is essential. Your main harddrive is normally /dev/sda, but an SD card under Ubuntu usually enumerates to /dev/sdb or …c, or ….d depending on how many drives you have, etc.

Insert your SD card that you want to image into the card reader and do the following on the console:

dmesg

The last few lines is all we’re interested in, it’ll tell us which card device we’ve just inserted. In my case from the output below my SD Card is in /dev/sdb. I’m going to assign this to the variable ${CARD} and use it below. Everytime you see this variable, replace it with your SD card device instead.

DD

We’re going to make use of the dd utility under Linux – it’s best to have a look at the man page for this tool and get used to what it can do. It’s very powerful, and it can easily wipe out complete hard drives, so be careful. I recommend only running dd in a VM, especially if you’re just starting to get used to using it!

The dd man page can be found here: http://www.freebsd.org/cgi/man.cgi?query=dd&sektion=1 as well as typing “man dd” of course from the console!

Snapshotting an SD Card Image
Imaging an SD card is useful so that we can come back to exactly the same place as we are now at some point in the future (usually when we’ve broke something). We use dd to image the drive: (Takes a long time!)

sudo dd if=${CARD} of=~/card.img

Restoring a card

As they say in the Haynes manuals, is the reverse of snapshotting! Again, takes ages!

sudo dd if=~/card.img of=${CARD}

Embedded Linux SD Cards

Nearly all Embedded linux installs have two partitions on the SD card. Under Ubuntu we mount the filesystems like so:

Make sure you have two folders under the /mnt directory where you can mount the sd card partitions:

sudo mkdir /mnt/sd-boot
sudo mkdir /mnt/sd-fs

One partition is known as the boot partition and is nearly always FAT formatted (it has the bootloader and kernel), the second is the linux filesystem and is generally an ext3 filesystem (but doesn’t have to be!)

Once you’ve inserted the card, the first partition will be the boot partition and the second the filesystem partition. Mount them in the following way:

sudo mount -t vfat /dev/sdb1 /mnt/sd-boot
sudo mount /dev/sdb2 /mnt/sd-fs

When you’re finished with the card, simply unmount both paritions

sudo umount /mnt/sd-boot
sudo umount /mnt/sd-fs

Resizing the SD Card to original size (Windows)

On Windows, it can be a pain to regain the original size of the card as Windows only exposes the initial boot FAT partition and so you end up with an SD Card with about 70Mb on it, even when you format it. You’ll need to get something like the official SD Card Formatter in order to reformat the SD card to it’s original size. The SD Card Formatter sorts the partitioning out to make full use of all the space on the card.

One thought on “Working with SD Cards

Leave a Reply