Introduction: Restore DMG to SD Card - MAC OS X

After making a backup of a SD Card, and trying to restore it using 'Disk Utility' you may have some issues during the process like:

Error: Could not validate source - Invalid argument

To solve this, there is this 3 step process that you can perform in the terminal.

If you do not want to run this steps you can just run a script I made:

https://github.com/magamig/dmgtosd

Step 1: Step 1: Converting the DMG

First you need to convert the .dmg image to a raw image:

hdiutil convert foo.dmg -format UDTO -o bar.img

(Note: change directories to the one with the dmg file (if in Desktop do first: 'cd Desktop'), and change 'foo' and 'bar' with the filename)

Step 2: Step 2: .CDR (raw Image) to .IMG

In the last step we created a file with .cdr extension but it is a raw image. So you can change the file extension to .img, by easily renaming the file name:

mv bar.img.cdr bar.img

Step 3: Step 3: Copy IMG to SD Card

Before this step unmount all the partitions of the SD Card, or you will get this error:

dd: /dev/diskX: Resource busy

Finally you can copy the image to your SD card using dd:

sudo dd bs=2048 if=bar.img  of=/dev/diskX

(Substitute X with the disk num, you can find this under: Disk Utility -> Select the SD Card on the right -> Info (top button), and in the window you can see the disk number)

And patiently wait for it to end, if it takes more than 2 hours, just check if it is complete by removing the SD Card and reinserting it, if not try this command in the terminal again. But you really need to wait... like a lot... not kiddin (depends on the SD Card size)

Comments

author
jarts1 (author)2016-06-20

Because of the stupid way apple does this, I can not do this since the *.dmg I made is ~60gb and there is not enough hard disk space to do this. Does anyone has an idea for a workaround ?

author
rlenoil (author)jarts12017-01-16

Issue the following command to create a virtual disk in the system from the .dmg file, where path is the path to the file:

sudo hdiutil attach path -readonly -nomount -noverify

hdiutil will output the path of the virtual disk, something like:

/dev/disk4 Apple_partition_scheme

Then you can use the dd command as shown by other posters to perform the copy. When finished, remove the virtual disk with the following command, substituting the disk number returned by hdiutil attach for diskn:

sudo hdiutil detach diskn

author
blackjack75 (author)jarts12016-12-19

I have no solution if you have already done the backup using disk utility as hdiutil does not seem to appear to allow outputting to stdout, but if you use dd to both backup and restore you can use gzip in the process on both sizes and save a lot on disk space and possibly time.

To backup (assuming your device is in /dev/disk3 )

sudo dd if=/dev/rdisk3 bs=1m | gzip > mybackup.gz

To restore (after unmounting destination partitions but not ejecting device):

gzip -dc mybackup.gz | sudo dd of=/dev/rdisk3 bs=1m

Detailed explanation here:

https://smittytone.wordpress.com/2013/09/06/back-up-a-raspberry-pi-sd-card-using-a-mac/

author
blackjack75 (author)2016-12-19

Tips to speed up:

a) the ready-made script in the intro text does NOT pass the bs parameter. So it takes ages. I use bs=1M for high performance.

b) on OSX instead of /dev/disk3 you can use /dev/rdisk3 to get faster performance. Think of it when you you enter the destination device.

With both options (compared to dd without bs parameter to write a 64gb Lexus card) I changed the dd writing time from 16 hours ....to 20 minutes

author
jmeyer15 (author)2016-12-01

You deserve a beer for this walkthrough, sir! I can't believe Disk Utility makes this so easy to image off an SD card, but not the other way.

author
Plugh (author)2016-02-15

Silly Apple. They make it easy to create a DMG from an SD, but have to go through hoops to go the other direction. Thanks for this how-to.