2

If I do

btrfs fi defrag -rv /home

then I get a long list of files which needs to be defragmented.

It seams as if it doesn't actually do anything.

Is it possible to see how far in the defrag progess it is?

Jasmine Lognnes
  • 2,520
  • 8
  • 33
  • 51

1 Answers1

4

You can use -f to flush, otherwise it works in batches and may stall. If it's printed the file then it's done, subject to caching / locking (hopefully not any filesystem errors).

-f flush data for each file before going to the next file.

-r defragment files recursively in given directories.

Note: Directory arguments without -r do not defragment files recursively but will defragment certain internal trees (extent tree and the subvolume tree). This has been confusing and could be removed in the future.

-v be verbose.


$ btrfs filesystem defrag -v -r dir/

Recursively defragment files under dir/, print files as they are processed. The file names will be printed in batches, similarly the amount of data triggered by defragmentation will be proportional to last N printed files. The system dirty memory throttling will slow down the defragmentation but there can still be a lot of IO load and the system may stall for a moment.

$ btrfs filesystem defrag -v -r -f dir/

Recursively defragment files under dir/, be verbose and wait until all blocks are flushed before processing next file. You can note slower progress of the output and lower IO load (proportional to currently defragmented file).

Rob
  • 320
  • 1
  • 3
  • 9
  • I don't think it does it. Can it be because it is a SSD? I mount it with `UUID=5e2ef3a3-afbc-43d1-9f16-4af70cea68ff /home btrfs defaults,discard 0 2`. – Jasmine Lognnes Feb 09 '18 at 17:40
  • I always get the same number of files with `btrfs fi defrag -r -v -f /home|wc -l` – Jasmine Lognnes Feb 09 '18 at 18:53
  • 4
    Don't defrag an SSD. Sources: https://www.easeus.com/partition-master/defrag-an-ssd.html and https://www.pcgamer.com/should-i-defrag-my-ssd/ . – Rob Feb 09 '18 at 21:07
  • 4
    Don't defrag an SSD, *unless* you want to use `btrfs filesystem defrag` to force compression using the `-c` flag on a previously uncompressed device. – MiKy Mar 28 '19 at 09:31
  • 5
    The rule for never defragging an SSD does not strictly apply to BTRFS - thanks to CoW it is possible for a file to become so extremely fragmented that it affects performance, for example with databases. The overhead in this case may come from the CPU as well as the disk. A better rule with BTRFS on SSDs is to only defrag files that have reached insane levels of fragmentation. – Dark Jul 02 '19 at 19:54