Score:3

How is TRIM handled when cloning a disk with dd?

in flag

The TRIM command allows an OS to inform an SSD which sectors contain data, and which are free. This allows the disk to re-use free sectors internally and improves wear leveling.

Now, if an SSD is cloned to anoher via dd, each single sector is copied to the target disk. However, dd does not know if the content of the sectors is actually data, or unused. That is, it can not send a TRIM command to the target disk. To my understanding, the target disk has to consider EACH written sector as used.

The only way around would be if the used/free information is somehow stored in the sectors themself, and so is copied by dd to the new disk.

So, how is it done? Is there a problem?

Tom Yan avatar
in flag
No problem. There's no *real* way to tell whether a block is mapped to actual / wanted data or not. You can at best mount all the filesystems on the clone and `fstrim` them all after cloning. (Although, `sparse` *might* help you avoid writing all-zero blocks...? That's technically irrelevant with whether the source block was TRIM'd before cloning though.)
in flag
Yes there is a problem in that you have written to a disk and caused ware on it. Don't use dd with a SSD as target.
Score:0
nc flag

The TRIM standard states that reading a trimmed block gives undefined results. Some versions of trim will simulate returning a zeroed block when you read it, others may return the previous data, or some random data.

Michael Hampton avatar
cz flag
ACS-3 says the drive will report whether it does deterministic read after TRIM and whether such reads return zeroes. Most modern SSDs will do both.
sweber avatar
in flag
That is not what I'm asking. My question is: dd blindly copies blocks from source to target. It does not know if a block is trimmed, and so does not tell the target disk. Finally, when cloning an empty disk with all trimmed blocks, you will end up with an empty target disk where each block is marked as used...
user10489 avatar
nc flag
@sweber: that's not a question, that's the answer. And if you don't like that result, don't use dd. For instance, you could create a new fs on the new disk and copy with a tar pipe or something. Or use a fs clone utility that knows about filesystem free space and doesn't copy it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.