Restore DMG to SD Card - MAC OS X

22K1011

Intro: 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)

8 Comments

Add status = progress to the dd command line:
example:

$ sudo dd bs=2048 if=raspberry3.img.cdr of=/dev/disk5 status=progress
2778802176 bytes (2779 MB, 2650 MiB) transferred 2369.969s, 1173 kB/s

Sorry - Internet doesn't like my key combinations:
If you press [CTRL]+t whilst the tool is running you get a status report.
To see how far you have left to go press [CMD]+[SPACE], you can type the number of bytes transferred (e.g. 10000000 bytes) and you'll be able to see how many gigabytes have been written...

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 ?

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

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/

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

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.

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.