12

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 (dcfldd, in fact):

# dcfldd 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 dcfldd

What may I do? Can I use dd or dcfldd ? 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 dcfldd 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

3 Answers3

17

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
flg
  • 286
  • 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 00: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
  • 2
    @SopalajodeArrierez Beware, count=61315071 omits the last sector! you need sectors 0 through 61315071, that's 61315072 sectors. – Gilles 'SO- stop being evil' Oct 22 '16 at 23:23
  • This caused kernel panic in my situation. Do not recommend. I'm no expert (which is why I'm here reading answers) but I think you have to ensure there's nothing on the sectors you're leaving out. You can't just truncate and expect it to work. – fivedogit Apr 10 '20 at 15:31
  • @fivedogit I imagine you've figured this out by now, but you have to make sure the numbers relate to what fdisk -l returns. If a partition has been resized to the whole disk, it will have to be reduced first before copying to a smaller disk; that way you're not truncating anything. – DigitalMan Feb 01 '21 at 22:48
3

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.

Ole Tange
  • 35,514
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.