4

I'd like to understand the interaction between fstrim and a file system driver (such as ext4). More precisely I'd like to understand whether dd interfere with this.

I've queried elsewhere about how the file system informs an SSD when a block is not used. This is important because it can help with wear levelling. This was my earlier question

When a partition is copied with dd or equivalent, every byte of every block gets copied regardless of whether it's used by the file system. Besides being slower on mostly empty file systems, this also tells the disk to store data in unused areas of the file system.

Will fstrim recover from this or is it incremental, only discarding after a file is deleted?

(Meta question) Is it now recommended that users call fstrim or similar after using dd to copy a disk?

Philip Couling
  • 17,591
  • 5
  • 42
  • 82
  • Interesting question :-) I have no answer, but a comment: [**Clonezilla**](https://clonezilla.org) is smart enough to only copy used blocks of file systems. It skips free blocks (of file systems), unallocated drive space and swap partitions. In addition it backs up the partition table and boot sectors if you clone (or make an image of) a whole drive. Finally, it is safer than `dd` because there are checkpoints, where you can double-check that you are telling Clonezilla to do what you want to do (and avoid mistakes that might overwrite valuable data). – sudodus Jul 08 '20 at 14:06
  • 1
    if the target device is fully trimmed (zero) you can also use `dd` with `conv=sparse` to preserve free (trimmed) blocks in the copy. no special filesystem-aware tools needed. will lead to data corruption if the target device had non-zero data, though. – frostschutz Jul 08 '20 at 18:17

1 Answers1

8

Will fstrim recover from this or is it incremental, only discarding after a file is deleted?

It will recover from this, it discards all unused blocks. It doesn’t care about when the blocks became unused.

Is it now recommended that users call fstrim or similar after using dd to copy a disk?

Doing so will only add one trimming “instance” to the drive’s history, so it shouldn’t have any adverse consequences. Put another way, if it’s OK to run fstrim once a week, running it twice in one week after copying a volume isn’t going to make much difference over the drive’s lifetime. I don’t know whether it’s recommended.

I would recommend using a file system-aware tool instead, for example e2image for ext4 file systems; it won’t copy unused blocks when creating the image, and will even avoid writing blocks if their contents are identical to what’s already on the target.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164