You will find many ways to clone an SD-card there are softwares like win32diskimager (A great tool for all practical purposes related to flashing cloning). You have the famous Linux
The problem arise when you are working on a bigger sd-card say
64 Gb. You have done something really cool and you want this to be in production or just share this with your friend so they will not spend 4-5 hours following your script to recreate the same image. You can just clone your card and zip it that will make the footprint small because anyway most of the
64 Gb part was empty to zip has done it’s trick but what if your friend has a
8 Gb card. You shouldn’t have started with a big card? Anyway cloning the whole card look very ugly to me (time factor is also there).
How to cleanly clone a SD card?
This is very simple I will explain it first then we will walk through a sample use case with all the commands. All the exercise is on Linux machine.
- Insert the SD-card on Linux machine and get the device name e.g.
- Start Gparted and select your device from top.
sudo apt-get install gpartedIf you do not have this. I like using the GUI interface but if you are the bearded Unix man you can just use the
- Shrink your partition with the graphical interface. You need to unmount the partitions if it is auto mounted. You can do this from the gparted UI itself by right clicking on the partition and selecting unmount. After the device is unmounted(all its partitions) you can select a partition and see the options of deleting the partition shrinking/resizing it.
- gparted will automatically recognize the unused space with some margin and give you option to shrink it to that much space. shrink your partition and click save to apply it. This will take some time.
- Now you have just sufficient amount of space for each partition unused area is mark unallocated now. We can now clone the SD-card as usual.
- Just check the memory used via your favorite software/command then copy using
sudo dd if=/dev/sdb of=~/shrinked.img bs=1M count=2470
The way to it
Now let us walk through an example with all the commands:
:~$ sudo gparted
:~$ sudo dd if=/dev/sdb of=~/myRaspberryPi.img bs=1M count=2470
:~$ sudo fdisk -l
Partition table entries are not in disk order Disk /dev/sdb: 64.0 GB, 64021856256 bytes 64 heads, 32 sectors/track, 61056 cylinders, total 125042688 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 Disk identifier: 0x00042380 Device Boot Start End Blocks Id System /dev/sdb1 49152 131071 40960 83 Linux /dev/sdb2 131072 5160959 2514944 83 Linux
You need this information to know the size you have to copy with
:~$ sudo dd if=/dev/sdb of=~/eyerisDvpCam.img bs=1M count=2470
2470+0 records in 2470+0 records out 2589982720 bytes (2.6 GB) copied, 82.9483 s, 31.2 MB/s
Do you know SD stands for Secure Digital?
There are some open points like:
- What if your partitions are not continuous?
- What if there is a big unallocated space in the beginning of the sd-card?
You can use
skip with dd to reject the initial space I tried once didn’t worked for me got image was corrupted. May be I have done it incorrectly. Then you can also copy your partitions one by one and then create an image separately by joining them.
:~$ sudo dd if=/dev/sdb of=~/myCoolTvSkip.img bs=1M skip=23552 count=2444
2444+0 records in 2444+0 records out 2562719744 bytes (2.6 GB) copied, 107.389 s, 23.9 MB/s