2

I had a 32GB SD Card with this structure (or very close):

luis@Fresoncio:~$ sudo fdisk -l
Disk /dev/mmcblk0: 29.2 GiB, 31393316864 bytes, 61315072 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: 0xec4e4f57

Device         Boot    Start      End  Sectors  Size Id Type
/dev/mmcblk0p1             1   125000   125000   61M  c W95 FAT32 (LBA)
/dev/mmcblk0p2        125001 33292287 33167287 15.8G 83 Linux
/dev/mmcblk0p3      33292288 61315071 28022784 13.4G 83 Linux

And I transferred (from another computer, so the devices where sda and sdb) it to a (I choose the wrong one) 64GB SD Card via dd (ddcfld, in fact):

# ddcfld if=/dev/sda of=/dev/sdb bs=1M

So now, my new 64GB SD Card is:

luis@Fresoncio:~$ sudo fdisk -l
Disk /dev/mmcblk0: 59.5 GiB, 63864569856 bytes, 124735488 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: 0xec4e4f57

Device         Boot    Start      End  Sectors  Size Id Type
/dev/mmcblk0p1             1   125000   125000   61M  c W95 FAT32 (LBA)
/dev/mmcblk0p2        125001 33292287 33167287 15.8G 83 Linux
/dev/mmcblk0p3      33292288 61315071 28022784 13.4G 83 Linux

Well, no problem for now, but now I don't have the source 32 GB SD Card anymore, only the 64GB SD Card remains, and I would like to transfer it to some empty 32 GB SD Card again.
In this case, I assume I can not use dd or ddcfld

What may I do? Can I use dd or ddcfld ? What could happen when the transfer arrives to the 32 GB boundary on the destination SD Card (data integrity problems)?

Further notes:

  • Any other method to clone the SD cards would be OK, but I have a problem: this case scenario is some SD card boot drive for a Raspberry Pi 2, and cloning via partimage or gparted did not work (the Raspberry does not boot). Only dd seems to do the cloning without flaws.

  • Similar question, but I think not the same.

  • The ddcfld tool has the same syntax and behavior as dd. It just gives more info (progress... etc). Here is the man page.

  • I do not know ddcfld, but plain dd has a count flag where you can do thing like dd if=inputfile of=outputfilename bs=1M count=x which transfers x times 1M blocks. – Hennes Oct 20 '16 at 22:58
5

Assuming sda is your 64GB source SD card and sdb is your 32GB destination SD card. You can limit dd to only copy the number of required sectors with:

dd if=/dev/sda of=/dev/sdb bs=512 count=61315072
  • 1
    I understand sectors of 512 bytes (as fdisk yields) and count=61315071 due to last partition end sector, right? – Sopalajo de Arrierez Oct 21 '16 at 0:19
  • 1
    That's correct. The meaning of dd's parameters is: if is the input file, of is the output file (in UNIX, almost everything is represented as a file, including disks and partitions), bs is the "block" size and count is the number of "blocks" you want to write from the input to the output. – flg Oct 21 '16 at 19:45
  • @SopalajodeArrierez Beware, count=61315071 omits the last sector! you need sectors 0 through 61315071, that's 61315072 sectors. – Gilles Oct 22 '16 at 23:23
1

I have done this with harddisks many times: You simply dd the big one onto the small one. It will fail when it hits the 32 GB, but that is fine.

If you still have doubts, I will suggest you try doing it on the old USB-stick/harddisk you have lying around.

  • Tested working without flaws. Don't know if there will be any problem later, but for now there are no issues. – Sopalajo de Arrierez Oct 21 '16 at 20:58
1

You can use any tool to make the copy, such as cat (dd has no advantage over cat, only downsides). It'll stop when it reaches the end of the target disk. The downside of this simple approach is that it's hard to tell whether the copy of the part you were interested in was successful.

To ensure that the copy command returns a success status if it successfully copies what you wanted to copy, and errors out otherwise, use a command that copies the right amount of data. To copy the first N bytes of a file, use head -c. The number of bytes to copy is the number of sectors multiplied by the sector size (512 bytes in your fdisk listing). Don't forget that sectors start at 0 (sector 0 contains the partition table) so the total number of sectors to copy is the largest “end” value plus one.

head -c $((61315071*512+512)) /dev/sdSOURCE >/dev/sdDESTINTATION

Replace sdSOURCE and sdDESTINATION by the appropriate device names. Run fdisk -l /dev/sdDESTINATION and grep sdDESTINATION /proc/partitions and file -s /dev/sdDESTINATION and any other command you consider useful to make sure that the device you're overwriting is the device you intended to overwrite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.