the usual
inconsistent at best
Duplicate an SD Card in OS X

I made these notes as a result of my experimentation with a Raspberry Pi. I kept messing up the software on the SD card and needed to start over, but it took a long time to go through all of the RPi setup steps. By copying the partitions, I was able to restore relatively quickly.

Get the info of each partition you want to copy on your SD card using diskutil:

$ diskutil info /dev/disk1s1
   Device Identifier:        disk1s1
   Device Node:              /dev/disk1s1
   Part Of Whole:            disk1
   Device / Media Name:      Untitled 1

   Volume Name:
   Escaped with Unicode:

   Mounted:                  Yes
   Mount Point:              /Volumes/Untitled
   Escaped with Unicode:     /Volumes/Untitled

   File System:              MS-DOS FAT16
   Type:                     msdos
   Name:                     MS-DOS (FAT16)

   Partition Type:           Windows_FAT_32
   ...

$ diskutil info /dev/disk1s2
   Device Identifier:        disk1s2
   Device Node:              /dev/disk1s2
   Part Of Whole:            disk1
   Device / Media Name:      Untitled 2

   Volume Name:
   Escaped with Unicode:

   Mounted:                  No

   File System:              fuse-ext2
   Type:                     fuse-ext2
   Name:                     fuse-ext2

   Partition Type:           Linux
   ...

This disk (/dev/disk1) has two partitions: an MS-DOS partition and a fuse-ext2 partition. We can back up each of these partitions using dd:

$ dd bs=1m if=/dev/rdisk1s1 of=backup-boot.img
$ dd bs=1m if=/dev/rdisk1s2 of=backup-root.img

You now have two partition images backup-boot.img and backup-root.img.

Restoring from backup

We’ve broken our SD card somehow and want to restore from backup.

Be sure to unmount (but not detach/eject) any existing filesystems on the SD card after inserting it. Now, you can partition the SD card using diskutil:

$ diskutil partitionDisk disk1 2 MBR MS-DOS "" 114688S fuse-ext2 "" 3665920S
Started partitioning on disk1
Unmounting disk
Creating partition map
Waiting for disks to reappear
Formatting disk1s1 as MS-DOS (FAT) with name
Formatting disk1s2 as fuse-ext2 with name
Finished partitioning on disk1
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.9 GB    disk1
   1:                 DOS_FAT_32                         58.7 MB    disk1s1
   2:                      Linux                         15.8 GB    disk1s2

I’ve decided to create a 3rd “data” partition:

$ diskutil partitionDisk disk1 3 MBR MS-DOS boot 114688S fuse-ext2 root 3665920S fuse-ext2 data R
Started partitioning on disk1
Unmounting disk
Creating partition map
Waiting for disks to reappear
Formatting disk1s1 as MS-DOS (FAT) with name boot
Formatting disk1s2 as fuse-ext2 with name root
Formatting disk1s3 as fuse-ext2 with name data
[ 0%..10%..20%..30%..40%..50%..60%..70%..80%........... ]

Now we can restore our images to the SD card (be sure to unmount the partitions again after diskutil):

$ dd bs=1m if=backup-boot.img of=/dev/disk1s1
56+0 records in
56+0 records out
58720256 bytes transferred in 157.174893 secs (373598 bytes/sec)

When you write to the disk, use rdisk1sX instead of disk1sX. It copies data about 13x faster. I think this is because it is unbuffered (r for raw?)

Note: for the boot partition, don’t use the rdisk1s1—it doesn’t boot well.

Additional notes

Sat May 4 16:33:40 MDT 2013

As of today, here is the partition scheme I’m using:

$ diskutil partitionDisk disk1 5 MBR MS-DOS boot 114688S fuse-ext2 root 3665920S fuse-ext2 usr 6G fuse-ext2 home 8G fuse-ext2 backup R
Started partitioning on disk1
Unmounting disk
Creating partition map
Waiting for disks to reappear
Formatting disk1s1 as MS-DOS (FAT) with name
Formatting disk1s2 as fuse-ext2 with name root
Formatting disk1s3 as fuse-ext2 with name usr
Formatting disk1s5 as fuse-ext2 with name home
Formatting disk1s6 as fuse-ext2 with name backup
Finished partitioning on disk1
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *32.5 GB    disk1
   1:                 DOS_FAT_32                         58.7 MB    disk1s1
   2:                      Linux root                    1.9 GB     disk1s2
   3:                      Linux usr                     6.0 GB     disk1s3
   4:                      Linux home                    8.0 GB     disk1s5
   5:                      Linux backup                  16.5 GB    disk1s6

Last modified on 2013-04-28