Can ‘dd’ be used to clone to a smaller HDD, knowing that partitions will need editing

cloningddhard-diskpartition

I've used dd to clone disks like this:

 dd if=/dev/sdb of=/dev/sda bs=4096 conv=notrunc,noerror,sync

And it's always worked fine. Any and all docs on 'dd' take pains to remind you that the target disk must be the same size or bigger than the source. Does that absolutely have to be true?

Now, I quite understand that if I clone to a smaller disk I can't expect any partitions that are even partially 'out of bounds' on the target to be intact.

However, knowing full well that I'd need to edit my partitions on the target later, deleting the 'out of bounds' ones, could I still use 'dd' to make a brute force copy of the source up to the limits of the physical size of the target? Or would 'dd' reduce the target to a smoking pile of wreckage when it reached the limit of its size 😉

BTW, researching this, I've seen recommended values for bs= of everything from bs=1024 up to bs=32M, what really is best?

Best Answer

As others have mentioned here using just dd won't work due to the copy of the GPT table placed at the end of the disk.

I have managed to perform a migration to a smaller drive using the following method:

First - boot into liveCD distro of your choice.

Resize the source drive partitions to indeed fit within the smaller drive's constraints (using gparted for example). Then, assuming sda is the source disk, using sgdisk, first create a backup of GPT table from the source drive to be on the safe side: `

    sgdisk -b=gpt.bak.bin /dev/sda

Assuming sdb is the target, replicate the table from the source drive to the target:

    sgdisk -R=/dev/sdb /dev/sda

sgdisk will now complain that it tried placing the header copy out of the bounds of the target disk, but then will fallback and place the header correctly at the upper bound of the target disk.

Verify that a correct clone of the partition table has been created on the target drive using the tool of your choice (gparted for example).

Using dd, copy each partition from the source drive to the target:

dd if=/dev/sda1 of=/dev/sdb1 bs=1M
dd if=/dev/sda2 of=/dev/sdb2 bs=1M
dd if=/dev/sda3 of=/dev/sdb3 bs=1M
etc...

Obviously, if you mix up the names of the drives when replicating the GPT partition table without a backup or when dding the content you can kiss your content goodbye :)

Related Question