I have just installed Ubuntu 11.10 on the Btrfs filesystem.

Do I really need to defragment files or the whole system?

Defragment # btrfs filesystem defragment /pool1 Defragment

Btrfs defragmentation

  • No, btrs, ext4,3,2, ETC... don't require you to defragment the drives, they do suffer from it as any other filesystem does. – Uri Herrera Dec 1 '11 at 4:20

You don't really need to defrag Btrfs filesystems manually.

Yes, Btrfs is COW (copy-on-write), which would imply it fragments files much more than Ext, but this is addressed in several aspects of the design, including the ability to easily defrag the filesystem while it is online. This excerpt provides more detail (emphasis mine):

Automatic defragmentation

COW (copy-on-write) filesystems have many advantages, but they also have some disadvantages, for example fragmentation. Btrfs lays out the data sequentially when files are written to the disk for first time, but a COW design implies that any subsequent modification to the file must not be written on top of the old data, but be placed in a free block, which will cause fragmentation (RPM databases are a common case of this problem). Additionally, it suffers the fragmentation problems common to all filesystems.

Btrfs already offers alternatives to fight this problem: First, it supports online defragmentation using the command btrfs filesystem defragment. Second, it has a mount option, -o nodatacow, that disables COW for data. Now btrfs adds a third option, the -o autodefrag mount option. This mechanism detects small random writes into files and queues them up for an automatic defrag process, so the filesystem will defragment itself while it's used. It isn't suited to virtualization or big database workloads yet, but works well for smaller files such as rpm, SQLite or bdb databases.

So, as long as you don't plan to run IO-intensive software like a database under significant load, you should be all good, as long as you mount your filesystems with the autodefrag option.

To check the fragmentation of files, you can use the filefrag utility:

$ find /path -type f -exec filefrag {} + >frag.list
# Now you can use your favourite tools to sort the data

On Systemd systems, /var/log/journal/ will probably be the most fragmented. You can also look at ~/.mozilla and other browsers databases.

To defragment, use:

$ sudo btrfs fi defrag -r /path
  • how do i know its defrag.... or not ...command to check defrag status – One Zero Dec 3 '11 at 16:54
  • 3
    This answer is wrong as per the BTRFS wiki: btrfs gotchas. The problem of file fragmentation can easily be reproduced by running systemd-journald on a btrfs volume. Although I have not tested yet inhowfar the autodefrag option solves this problem. – Jonas Schäfer Nov 27 '13 at 16:32
  • I've been using btrfs full-time now for at least 18 months, with systemd, firefox, etc. A quick check using filefrag reveals that journal files are indeed heavily fragmented (averaging 10k extents each), but I have not observed CPU spiking nor performance impact. I do not have autodefrag on, will enable it and report back at next reboot. – Félix Saparelli Nov 28 '13 at 10:38
  • So: file fragmentation does occur, autodefrag does help, performance impact is minor at best on everyday systems. Obviously heavy-load servers might need to measure and tune things up, but then again this is alpha software so you might want to avoid it there anyway. – Félix Saparelli Dec 7 '13 at 12:19
  • find /path -type f -exec filefrag {} + >frag.list avoids all the FIBMAP unsupported errors from directories and other non-regular files, and doesn't break on weird filenames. – Peter Cordes Feb 28 '18 at 23:05

For the the sake of this topic i think is better to clarify that:

Any filesystem != recovery/management utilities

Keep in mind that the filesystem is about how data is organized on a physical hardware, every other activity is done with extra software and utilities that, especially in the GNU/linux, are done by peoples that probably are not strictly related with who had made the filesystem.

This is probably not the case of the Btrfs, but sometimes we are near to that because desktop and enterprise environments can provide different solutions with different grades of realiability.

The answer to your question is "no" but is a "no" related to the normal behaviour of a generic filesystem, and every utility you can find about this is simply a different project.

When you need to defrag your filesystem keep in mind that you are going to use external software that is not really related to the life of the filesystem itself, with all the pros and cons.


The command to defrag a btrfs filesystem is

btrfs filesystem defragment sync -r -v -f [-czlib] / /home

Based on this answer I came up with:

sudo find / -xdev -type f -exec filefrag {} + | sed -En 's/(.+): (\w+) extent.*/\2 \1/p' | sort -nr

This lists the most fragmented files first, with the format:

<num-fragments> <pathname>
  • A little simpler, seems to work find: sudo find / -xdev -type f -exec filefrag {} + | sort -t: -n -k2 – n8henrie Apr 8 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.