88

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.

11
  • Can you mount created img file and see if it is created correctly? Dec 11, 2012 at 14:40
  • @MilanTodorovic - how can I do this?
    – Alex F
    Dec 11, 2012 at 15:15
  • If I remember correctly: mount -o loop sdimage.img /path/to/mount. Then check to see if you can browse img file correctly. Dec 11, 2012 at 15:17
  • @MilanTodorovic: I tried sudo mount -o sdimage.img /dev/mydisk and got: mount: can't find /dev/mydisk in /etc/fstab or /etc/mtab Possibly I need to create some dev entry before. Can you post this as answer with some details? Thanks.
    – Alex F
    Dec 11, 2012 at 15:22
  • 1
    you should not be using dd on mounted devices. unmount all the partitions first, then your command should work.
    – ubfan1
    Dec 11, 2012 at 17:33

5 Answers 5

83

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.

4
  • 17
    when copying big amounts of data you should set a bigger than default blocksize, otherwise you may have to wait very long. sudo dd if=/dev/mmcblk0 of=~/sd-card-copy.img bs=4M
    – xaedes
    Nov 29, 2016 at 14:50
  • Can we copy the resultinf sd-card-copy.img on the same card itself ? Nov 21, 2019 at 23:45
  • 4
    One thing that is really annoying of the dd command is that there is no output by default. You can change that by adding status=progress at the end of your command Therefore it would become: sudo dd if=/dev/mmcblk0 of=/media/data/sd-card-copy.img status=progress
    – desmond13
    Jan 18, 2021 at 12:41
  • 1
    Note: in my case the name of the device was sdc, not sdcX like written in this answer because sdcX was used for the partitions, for example partitions were named sdc1 and sdc2. At least this answer helped me to make the dd command work by using /dev/sdc instead of /dev/sdc1.
    – baptx
    Feb 21, 2022 at 12:45
43

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

2
  • 3
    why is the opposite for me? I've Ubuntu 12.10 and the command has works only with partitions monted... Feb 13, 2014 at 10:12
  • 3
    @Velthune Are you sure about that? If so, I'd suggest making your own question.
    – Vreality
    Jul 17, 2014 at 3:58
17

I am using dd tool to clone usb sticks with multiple partitions, here is my command:

sudo dd if=/dev/sdb of=/dev/sdc bs=4096 conv=notrunc,noerror

notrunc - do not truncate the output file
noerror - continue after read errors

1
  • 2
    @PeterMortensen yes it does, many of us use those 4-in-1 type adapters.
    – Teque5
    Mar 15, 2016 at 21:37
11

dd is fine, but I prefer cat /dev/sdc/ > ~/backup.iso If you want to put it on an SD card again, just run cat ~/backup.iso > /dev/sdc

1
  • Both SD are 8 GB. cat can be used to make SD image, but to make another SD I should use dd, right?
    – Alex F
    Dec 11, 2012 at 15:17
1

Here are the steps which worked for me on Ubuntu to restore the image file (~/raspberrypi2.img in my case) back to a new SD card (inspired heavily by Alon's reply above):

  1. Insert the micro SD card via the card reader.
  2. Open the Disks app.
  3. Quick format the whole card (not a specific partition).
  4. Close Disks.
  5. Open terminal and execute:

    sudo fdisk -l
    

Relevant output (which showed there were no partitions due to the quick format of the whole card):

Disk /dev/sdb: 7.4 GiB, 7948206080 bytes, 15523840 sectors 
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disklabel type: dos 
Disk identifier: 0x6957f2f2

sudo dd if=~/raspberrypi2.img of=/dev/sdb

This step takes a few good minutes (even on USB3). Make sure to not interrupt it by any operation which will invoke mounting (opening the Files or Disks apps).

Thanks for everyone's answers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .