SD card cloning using the dd command

I am trying to clone an SD card which may contain a number of partitions, some of which Ubuntu cannot recognize. Generally, I want to clone the whole volume, not only some partition. So, I mount the SD card and see something like this in the Log viewer:

kernel: [  262.025221]  sdc: sdc1 sdc2

alex@u120432:~$ ls /dev/sdc*
/dev/sdc  /dev/sdc1  /dev/sdc2

Since I want to copy the whole disk, I execute:

dd if=/dev/sdc of=sdimage.img bs=4M

File sdimage.img, 7.9 GB (7,944,011,776 bytes) is created (SD card is 8 GB). Now I mount another SD card and execute:

dd if=sdimage.img of=/dev/sdc bs=4M

The problem is that the second dd command hangs on some stage, and never succeeds. After this, I cannot reboot or shut down computer, and I need just to switch power off.

Is this the correct approach? Maybe there is another way to clone an SD card?

OS: Ubuntu 12.04 (Precise Pangolin), 32 bit.


Solution 1:

Insert the original SD card and check the name of the device (usually mmcblkX or sdcX):

sudo fdisk -l

You might see:

Device         Boot   Start      End  Sectors  Size Id Type
/dev/mmcblk0p1 *       2048  2099199  2097152    1G  c W95 FAT32 (LBA)
/dev/mmcblk0p2      2099200 31116287 29017088 13.9G 83 Linux

In my case the SD card is /dev/mmcblk0 (the *p1 and *p2 are the partitions).

Now you have to unmount the device:

sudo umount /dev/mmcblk0

Now to create an image of the device:

sudo dd if=/dev/mmcblk0 of=~/sd-card-copy.img bs=1M status=progress

This will take a while.

Once it's finished, insert the empty SD card. If the device is different (USB or other type of SD card reader) verify its name and be sure to unmount it:

sudo fdisk -l
sudo umount /dev/mmcblk0

Write the image to the device:

sudo dd if=~/sd-card-copy.img of=/dev/mmcblk0 bs=1M status=progress

The write operation is much slower than before.

Solution 2:

You should not be using dd on mounted devices. unmount all the partitions first, then your command should work.