Clone Micro SD Card [Raspberry Pi, Linux, dd]


Clone Micro SD Card [Raspberry Pi, Linux, dd]

Introduction

We had a dying RaspPi Micro SD card on our hands and only option was to copy or clone Micro SD Card. Although this cloning approach can be used for any type of drive (HDD, SDD, SD Cards), here we’re going to try and use it to clone Micro SD Card.

Clone Micro SD Card with DD

Insert the SD card and check the name of the device (usually sdX):

$ sudo fdisk -l

or

$ sudo lsblk -l

You’ll see something like (64 GB SDCard in this case):

 Disk /dev/sdc: 59,7 GiB, 64045973504 bytes, 125089792 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: 0x2b8bd59a

or (in case lsblk):

 sdc     8:32   1  59,7G  0 disk 
sdc1 8:33 1 41,5M 0 part
sdc2 8:34 1 59,6G 0 part

Here, SD Card is /dev/sdc, sdc1 & sdc2 are the partitions. Just in case it got mounted automatically, unmount the device:

$ sudo unmount /dev/sdc

To create an image of the device:

$ sudo dd if=/dev/sdc of=~/sdcard-copy.img

This can take a while. In our case, for 64 GB card (Metoo HC), it took around an hour (USB3):

125089792+0 records in
125089792+0 records out
64045973504 bytes (64 GB, 60 GiB) copied, 3714,37 s, 17,2 MB/s

real 61m54,458s

Once copying is finished, insert an empty SD Card. Do the previous steps (validate the name, device file) and be certain it’s unmounted. We used the same sized card:

 sdc     8:32   1  59,5G  0 disk 
sdc1 8:33 1 59,5G 0 part

Write the previously generated copy (image) to the device:

$ sudo dd if=~/sdcard-copy.img of=/dev/sdc

This is even worse, much slower ( ~4 hours):

 124735489+0 records in
124735488+0 records out
63864569856 bytes (64 GB, 59 GiB) copied, 16049,8 s, 4,0 MB/s

real 267m29,899s

As you can see, it’s not a same amount of bytes, and it’s due to “lack of space”:

dd: writing to '/dev/sdc': No space left on device

If we look at the lsblk:

 sdc     8:32   1  59,5G  0 disk 
sdc1 8:33 1 41,5M 0 part
sdc2 8:34 1 59,4G 0 part

We can see that the size of the new card is 59.5 G, 200 MB less compared to old one (59.7 G). Damn, this didn’t work out.

Note: If you try an put that card into RaspPi, you’ll understandably end up with:
Kernel panic - not syncin : VFS: Unable to mount root fs on unknown-block

In general, that might be strange since we used a better SD Card of the “same” size for the destination (SanDisk Extreme, class 10), but the fact is that each SD Card (even of the same brand sometime) can have a different number of blocks and a slightly different capacity (enough to mess things up).

Cloning a larger Micro SD Card to a smaller one (PiShrink)

Trying to shrink the image with PiShrink did work. PiShrink reduced the size from 59 G to 14 G in only 6 minutes. Writing now took 70 minutes.

$ ./pishrink.sh sdcard-copy.img

pishrink.sh v0.1
Gatherin data…
Checking filesystem…
rootfs: 256226/3838896 files (0.6% non-contiguous), 3159572/15624448 blocks
resize2fs 1.44.1 (24-Mar-2018)
Shrinking filesystem…
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/loop0 to 3511782 (4k) blocks.
Begin pass 2 (max = 280792)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 477)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 23255)
Updating inode references         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop0 is now 3511782 (4k) blocks long.

Shrinking image…
Shrunk sd-card-copy.img from 60G to 14G…

real    6m12,905s

Writing:

$ time sudo dd if=./sdcard-copy.img of=/dev/sdc 
     
28188465+0 records in
28188465+0 records out
14432494080 bytes (14 GB, 13 GiB) copied, 4146,94 s, 3,5 MB/s

real    69m6,953s

One thing you’ll notice is the partition size. Check df -h :

Filesystem      Size  Used Avail Use% Mounted on
/dev/root 14G 12G 1.5G 89% /

PiShrink made those adjustments and now we need to expand it. Type:

$ raspi-config

and follow the instructions below:

Clone Micro SD Card: Raspi-config -> Advances Options
Raspi-config -> Advances Options
Clone Micro SD Card: Raspi-Config -> Expand Filesystem
Raspi-Config -> Expand Filesystem
Clone Micro SD Card: Raspi-config - Root partition resized
Raspi-config – Root partition resized

Format with DD

Just in case you need it:

$ time sudo dd if=/dev/zero of=/dev/sdc bs=4k && sync

dd: error writing '/dev/sdc': No space left on device
15591937+0 records in
15591936+0 records out
63864569856 bytes (64 GB, 59 GiB) copied, 4207,45 s, 15,2 MB/s

real 70m7,474s

Direct Micro SD Card cloning

Same thing as before, but in this case data from the source card is directly written to destination card:

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

Conclusion

This is somewhat a mixed post, covering SD Card or drive scaling, RaspPi adjustments, few tools & commands. It’s not what we had in mind, basics, but it might be useful as a reminder. If you forget it, you’ll know where to find it.