Back when HDDs were dominant, it's common to use the dd command to migrate system disks. But that creates a problem with SSDs: unused space from the source disk will also be written once, with either zeros or garbage. SSDs don't like that; they need to know which sectors hold really useful data.
Yes, I know I could do a fstrim or Optimize Disk after the migration, but that approach is imperfect: partition gaps, reserved partitions (like MSR), and space allocated but never actually written to (NTFS MFT/btrfs metadata/GlobalReserve/ext4 inode space?) cannot be trimmed. As an enthusiast I'd like to find a way not to mis-allocate one MiB on my new shiny SSD.
Then I came across the conv=sparse option of the dd command. Seems that by doing a dd if=/dev/sda of=/dev/sdb bs=4K conv=sparse, one could skip all unused sectors when moving data from old SSD to new SSD, assuming that both has the Deterministic Read Zeros after TRIM feature. But will that create new problems? For example, some database redo-log files are inherently packed with 0s, then will it create errors if a sector of 0s is allocated in the OS but not in the underlying flash storage?
Any insights are welcome.
will it create errors if a sector of 0s is allocated in the OS but not in the underlying flash storageOf course NOT. Nothing on any level higher than where SSD controller is can tell if a logical block is mapped or unmapped. – Tom Yan Oct 23 '22 at 11:54sparsewon't save a great deal. – harrymc Oct 23 '22 at 11:59ddwould be unable to clone the disk at the black level. – PierU Oct 23 '22 at 12:05sparsecan save from nothing to a lot, it depends on the input data in the first place. It so happened my input data in these tests did not allow to save much; but it could be different. Still I think the link you gave may be helpful here: if the OP uses largeobsthen they will probably save less than they would with smallerobs. – Kamil Maciorowski Oct 23 '22 at 12:07conv=sparsewill have no effect anyway, and no, I wasn't trying to say that at all. What I mean is whether the logical block is mapped behind the scene will not determine whether normal read will succeed or fail. Or maybe you are trying to say "things are still possible even when they could never happen", idk. (By things I mean like drives that are insane by design.) – Tom Yan Oct 23 '22 at 12:20conv=sparseoption here entirely relies on the assumption that any SSD always returns zeros when the OS wants to read a logical sector that has been trimmed. While it seem to be a common behavior, I am not sure this can be garanteed. – PierU Oct 23 '22 at 12:23blkdiscardthe whole SSD prior todd conv=sparse. (2) Unless you're totally sure the source drive is fully healthy, GNUddrescueis better thandd. Unfortunately it doesn't let you use--sparsewhen writing to a block device, whiledd conv=sparsesimply works. I think withddrescueyou could achieve something similar by using--generate-modein a clever way, but this requires reading the source file twice. – Kamil Maciorowski Oct 23 '22 at 12:32fstrim. You don't need toblkdiscarda new drive and you certainly don't want to use it on a drive to clone from... – Tom Yan Oct 25 '22 at 00:08blkdiscardon the new SSD, in general, in case "new" means "not the old one", not necessarily "totally not used before". Ifblkdiscardis not needed for this particular SSD then it will be a harmless no-op. – Kamil Maciorowski Oct 25 '22 at 06:11