Can I use dd to clone a larger SD card to a smaller SD card if the actual partitions will fit?
I have a 16GB SD card that has a few partitions on it (it's actually the card for a Raspberry Pi). I have several other 4GB cards that I want to clone the primary card to. The 3 partitions on the primary card are:
- Rasp boot partition, FAT, 60MB
- Linux partition, ext2, 1GB
- Additional "storage" partition, FAT, 1GB
If I use dd
to create an image of the card via:
dd if=/dev/sdb of=~/sd-card.bin
then the resulting .bin is 16GB in size. Is there a way to use dd
to copy just what's actually being used, i.e. < 4GB so that I can then dd
this onto a new 4GB card? Or is there a better solution that I should be using?
I assume you are using a PC linux or mac computer to perform the copy, not the raspberry pi itself. You will probably need to add a block size.
I have seen one and four megs used for Raspberry pi disks by specifying bs=1M or bs=4M. I think block size is more important when writing the disk as large transfers are quicker than smaller ones. This does not set the block size for the disk, it just effects the size of transfers dd uses. One reason for setting a large block size is the need to erase the flash before writing it. This is done automatically but faster for transfers that are larger than the minimal erase size.
You can limit the total amount of data copied by dd using "count". "count" is in units of blocks. If the end of the last partition on the source disk is before the size of the destination you can do what you want.
Something like dd if=/dev/sdb of=~/sd-card.bin bs=1M count=4000
will create an image that is 4000MBs in size.
See http://en.wikipedia.org/wiki/Dd_(Unix) and http://elinux.org/RPi_Easy_SD_Card_Setup for more information. Not sure how to find the end of the last partition or the cards total size. However if you have formatted the disks you will probably know how to do this.
Expanding upon William's answer, one could calculate the end of the last partition using fdisk
and a calculator:
$ fdisk -l /dev/mmcblk0
Disk /dev/mmcblk0: 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: 0x00057540
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 2048 186367 184320 90M c W95 FAT32 (LBA)
/dev/mmcblk0p2 186368 3667967 3481600 1.7G 5 Extended
/dev/mmcblk0p5 188416 3667967 3479552 1.7G 83 Linux
Total used space in bytes = end sector of last partition X sector size (here that's 3667967 x 512).
Total used space in GB = total used space in bytes / 10243 (here that's 1.749023 GB).
Usually, it is not vital to create an image that is pared right down to the last useful bit of data so in the above example I would create an image of 2 GB using the method described by William in that same earlier answer:
dd if=/dev/mmcblk0 of=/path/to/pi_updated.img bs=1M count=2048
Included at the end of the image will be a small portion of the useless guff after your last useful data but that is no different in principle than the useless guff that will be overwritten when you write the image back out to your media.
This method has been working for me on a dozen or so clones. If there are any fatal flaws in this method, they haven't surfaced yet.