brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: filefrag shows how fragmented a file is on Linux filesystems

Filesystems
~/tips/tip-filesystems-filefrag-extents

filefrag /var/lib/libvirt/images/vm1.qcow2

Count the pieces a large file is split into on disk.

A disk image or database file that reads slowly on a spinning disk might be in thousands of pieces. You can check a single file in a second.

filefrag /var/lib/libvirt/images/vm1.qcow2

The answer is a count of extents, where an extent is one continuous run of blocks. One or a few is perfect. For the details, add -v:

sudo filefrag -v /var/lib/libvirt/images/vm1.qcow2

That lists each extent with its position and length. To find the worst files in a folder:

sudo find /srv/images -type f -size +100M -exec filefrag {} + | sort -t: -k2 -rn | head

I would look at this before spending time on defragmenting anything, since on ext4 the answer is often that there is nothing to fix.

A high number is not automatically bad. On an SSD it hardly matters, and a very large file needs many extents because each one has a maximum length. Copy on write filesystems like btrfs fragment files that are rewritten in place, such as VM images, and compressed files there report many small extents that look worse than they are. The tool needs support from the filesystem, so on some network or FUSE mounts it just returns an error.

Further reading: filefrag man page

More Filesystems tips sudo fsfreeze -f /srv/data sudo btrfs filesystem usage /mnt/pool sudo tune2fs -l /dev/sdX1 | grep -iE 'mount count|check' findmnt --df lsblk -f All Filesystems tips (6) All topics

Link