68

Do ext4 filesystems need to be defragmented? If so, how do I defragment them?

If not, could you post a simple explanation of why they do not need to be defragmented?

Breakthrough
  • 34,227
  • 10
  • 105
  • 149
BenjiWiebe
  • 8,854
  • 11
  • 42
  • 65
  • 4
    If you are as interested as you sound, perhaps this paper would be of interest regarding this: [Fedora Project](http://ols.fedoraproject.org/OLS/Reprints-2007/sato-Reprint.pdf), specifically starting at page 179 (which is really page 3) – nerdwaller Jan 15 '13 at 21:39
  • @nerdwaller Interesting, though a little to in-depth for me to fully comprehend. – BenjiWiebe Jan 15 '13 at 21:42
  • 2
    Related question on Ask Ubuntu: [Why is defragmentation unnecessary?](http://askubuntu.com/questions/1090/why-is-defragmentation-unnecessary), where the current accepted answer is: `The underlying filesystems used by Ubuntu, like ext2 and ext3, simply don't need defragmenting because they don't fragment files in the same way as NTFS. There are more details at`[`http://en.wikipedia.org/wiki/Ext3#Defragmentation`](http://en.wikipedia.org/wiki/Ext3#Defragmentation)`.` – Breakthrough Jun 14 '13 at 17:28
  • If anyone is interested in Takashi Sato paper "ext4 online defragmentation" mentioned in the comment from @nerdwaller above, it looks like Fedora Project is no longer hosting that paper -- here's a link to it in the [Internet Archive](https://web.archive.org/web/20131228103929/http://ols.fedoraproject.org/OLS/Reprints-2007/sato-Reprint.pdf) – user2070305 Oct 31 '21 at 21:46

3 Answers3

44

Do ext4 filesystems need to be defragmented?

Yes (but very rarely).

If so, how do I defragment them?

Copy all the files off the partition, erase the files from the partition, then copy the files back onto the partition. The file system will intelligently allocate the files as you copy them back onto the disk.

If not, could you post a simple explanation of why they do not need to be defragmented?

ext4 acts in a more intelligent way than merely adding new files into the next available space. Instead of placing multiple files near each other on the hard disk, Linux file systems scatter different files all over the disk, leaving a large amount of free space between them. When a file is edited and needs to grow, there’s usually plenty of free space for the file to grow into. If fragmentation does occur, the file system will attempt to move the files around to reduce fragmentation in normal use, without the need for a defragmentation utility.

Thanks to a Comment by @Green Reaper my attention has been drawn to e4defrag.

pnuts
  • 6,072
  • 3
  • 28
  • 41
  • 1
    So there is no on-demand defragmenter software? – BenjiWiebe Jan 15 '13 at 21:42
  • 19
    A link to the source you copy/pasted from never hurts... ;) http://www.howtogeek.com/115229/ – Christophe L Nov 17 '13 at 02:10
  • 5
    The tool known as e4defrag can be used to defragment ext4. It is provided by e2fsprogs on most distros. [ext4 also tries to avoid fragmentation](https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Block_and_Inode_Allocation_Policy), but it is not always successful. – GreenReaper Dec 15 '16 at 23:01
  • Is there a way to view the percentage of fragmented files? – Aaron Franke Oct 25 '17 at 18:26
  • Be aware that e4defrag only defragments files, not the free space. If your free space is fragmented, then e4defrag won't work well, because there's no contiguous space to move any files to. – jlh Mar 17 '19 at 09:39
  • @ChristopheL From what I see, the source you claimed to be has the timestamp "SEPTEMBER 28, 2016, 11:00AM EDT", while this answer is written at "Jan 15 '13 at 21:35". What amazed me is how you were able to foretell the URL of a post 3 years in advance? – zypA13510 Jun 27 '19 at 08:43
  • Wouldn't scattering the files increase random access times? – Tooniis Dec 25 '19 at 05:05
  • @zypA13510 - first [Wayback capture](https://web.archive.org/web/20131023151415/https://www.howtogeek.com/115229/) shows publication date of 30-May-2012 – Tom Goodfellow Feb 25 '20 at 08:17
  • @Tom. Thank you. I agree far too much (almost all) the wording is identical and How-to Geek has precedence. I'd like this A removed. – pnuts Feb 25 '20 at 17:51
27

I have been using ext4 partition for over a year. I often ran out of space, moved a lots of files in and out etc etc... these things are bad for fragmentation and yesterday I checked for fragmentation for the first time and only 2 files (not %, just two files) were fragmented.

On the Windows partition that was used for about a year longer with much more free space available, I have 95% fragmentation.

So no, you really don't need to defragment ext4 and if you want to be sure, leave the default free space for ext4 (default is 5%, can be changed by ex2tunefs -m X).

Glorfindel
  • 4,089
  • 8
  • 24
  • 37
zahjin
  • 283
  • 3
  • 6
  • 4
    How do you check for fragmentation? – Sparhawk Apr 26 '15 at 10:06
  • 16
    If you have an ext4 file system created with the `extent` option (it's the default in most recent distros), you can check fragmentation on it with `e4defrag -c /path/to/check`, and defragment it without umounting with `e4defrag /path/to/check`. But if you have enough free space you won't need to do so. – gerlos May 08 '15 at 08:46
4

Here's an update from 2022

Fragmentation occurs due to multiple reasons:

  • When an OS cannot manage file intelligently
  • There's not enough continuous space to write the file
  • When multiple applications write different files to the the disk at the same time

Reasons to defragment

Why was it needed to defragment files at all? On a HDD this is required to make access to files faster or move them to the area of the disk which provides higher speeds (the outer side of the platter). However there's another important thing many people are unaware of: when files are fragmented it becomes impossible to restore them when you intentionally or accidentally delete them since with so many fragments it's impossible to understand in which order they need to be reassembled. The normal sector size for ext4 is 4K so a 1GB file can theoretically have up to 262144 fragments. And before people say this can never happen I've had servers where our programmers wrote to two MySQL files simultaneously at approximately the same speed, so the resulting files were heavily fragmented and their read speed was well below 1MB/sec vs around 200MB/sec for "normal" files.

As for SSD there are almost no reasons to defragment but there are reasons not to do that since SSDs have a limited number of erase/write cycles. But that still requires an enormous amount of data to be written since modern SSDs feature quite large spare areas to alleviate the issue.

In terms of data recovery there's an issue. If you enabled the discard mount option deleted files are gone for good, so whether or not they were defragmented earlier it doesn't matter, you cannot physically recover them.

If you choose not to use this option and instead rely on a weekly job which does that (fstrim), then defragmenting makes sense.

Finding out the amount of fragmentation

I really doubt the previous comments were honest about the level of fragmentation that ext4 has. It can and it will heavily fragment files under certain circumstance, some applications like systemd-journald do that intentionally.

You can run e2fsck to check how heavily your partition is fragmented. On mounted partitions this will spew a ton of errors, please disregard them:

sudo e2fsck -n -v -f /dev/partition

Actually defragmenting

For the purpose of defragmenting ext4 I've written a script which needs to be run under root/sudo - you can get it here. It will defragment everything except the binaries of running applications - you may want to exit them beforehand. It needs a single argument to run - a path, e.g.

sudo defrag /

The script only defragments files on a given partition. If you have mounted partitions, specify them directly, i.e. sudo defrag /mnt/archive. It will probably won't defragment your swap file either, so running sudo swapoff -a prior to it is a necessity.

This will not defragment directories which can become fragmented as well. For that you'll need to run e2fsck on an unmounted partition:

sudo e2fsck -D -f -v -C 0 -n /dev/partition

Sometimes unmounting partitions becomes very difficult, please use any live Linux distro for that, e.g. System Rescue.

Free space defragmentation issue

The biggest issue with ext4 is that it's only capable of defragmenting individual files, thus you cannot defragment or make continuous free space (for this you'll want to use a different filesystem, e.g. XFS). It's perfectly possible to have a ton of free space yet being unable to defrag relatively small files.

E.g. my root FS for Fedora has 20GB of space, less than 7GB are occupied yet I cannot defrag a few binaries and libraries (chrome, libxul.so) which weigh in at less than 200MB. The reason? The average Linux distro contains tons of files (over 30K for my installation which is not even Gnome/KDE) and when you spread out these many files across the partition, there are not that many "holes" with a lot of space available.

Conclusion/TLDR

  • If you have an HDD you probably want to defragment it
  • If you have an SSD there's generally no need to defragment it unless you want to increase your chances of successful recovery of deleted files
  • If you want to fully defragment your filesystem, you'll need a different filesystem, e.g. XFS.
  • I checked the e2fsck command you mentioned, and on my system it does not say how fragmented the partition is. It lists the total number of files and the total number of blocks. It does not say what percentage is contiguous. – BenjiWiebe Jun 16 '22 at 22:42
  • @BenjiWiebe this doesn't look right, here's my output: https://pastebin.com/raw/7rwJxySk Either you're not using it right, or it's an old version, or there's a bug. – Artem S. Tashkinov Jun 16 '22 at 22:53
  • Yes I am running a slightly older version. Apparently that was added fairly recently. – BenjiWiebe Jun 17 '22 at 17:48
  • It showed fragmentation status as early as 10 years ago. Are you using Debian 9? RHEL 7? – Artem S. Tashkinov Jun 17 '22 at 20:18
  • Fedora 33. `e2fsck 1.45.6 (20-Mar-2020)`. EDIT I see what's going on. Your e2fsck command was ran against a partition that wasn't cleanly unmounted. You need to add a `-f` to force checking of a clean partition. – BenjiWiebe Jun 17 '22 at 22:46
  • The `-n` flag should be enough, maybe it's relatively new. – Artem S. Tashkinov Jun 18 '22 at 07:14
  • @JimmyCarter I'm not sure what you've aliased. You may want to ask a question on https://unix.stackexchange.com/questions – Artem S. Tashkinov Nov 06 '22 at 18:33
  • 1
    I needed to save the script you made to `/usr/local/bin/defrag` (no `.sh`). See https://askubuntu.com/questions/222361/how-to-add-shell-script-to-my-linux-system – Subatomic Tripod Nov 07 '22 at 17:23
  • Do you have any idea how to defragment free space? Also, yes the defrag script should be in path ./defrag will not work. – Валерий Заподовников Jun 08 '23 at 10:07
  • @ВалерийЗаподовников Directly for ext4? You cannot. Indirectly: 1) Backup/format (that's necessary)/restore 2) Shrink as much as you can, expand back. Then defrag again. If you're concerned about defragging free space, you really could use XFS instead or even NTFS which must work near perfectly with Linux using the ntfs3 driver. – Artem S. Tashkinov Jun 08 '23 at 10:21
  • Algorithm: use RAM to offload some files from hard drive in a smart way to defrag biggest files. I mean... How hard can it be? – Валерий Заподовников Jun 09 '23 at 06:21
  • @ВалерийЗаподовников Nah, not going to work. OK, you've moved large files off to RAM. How will you make the kernel relocate smaller files which are already fully defragmented? And those small files are dispersed evenly all over the disk. You may as well ... use option 1 that I offered earlier. Someone could ask kernel devs to implement the API to force the kernel to move files close to each other. It shouldn't be too difficult. But without knowing the underlying layout you'll never do that efficiently - it will be a wild guess and it more likely won't have a good effect. – Artem S. Tashkinov Jun 09 '23 at 10:21
  • I deleted /var/log 15 GB log files and it allowed me to defragment most of the files. And you know the underlying layout... You can read segments and inodes directly... – Валерий Заподовников Jun 09 '23 at 10:27
  • You can read all you want, the question is how you will make the kernel put the files where you want them to be. Here's my issue: `/dev/root 20G 6.0G 13G 32% /` - 13GB free (!), yet five files around ~150MB (`libxul.so x2`, the `chrome` binary and some NVIDIA libraries) cannot be defragmented as free space is fragmented as hell. I've just given up. It's XFS/NTFS or bust :-) – Artem S. Tashkinov Jun 09 '23 at 11:49
  • How big are biggest files in your /var/log? You can clean them with "> /var/log/messages", also ext4 always has extra free space because of this genius reserved design. You can make it 0 using: tune2fs -r 0 /dev/sda1 – Валерий Заподовников Jun 10 '23 at 10:18
  • `60,508,368 B in 42 files` - nothing to write home about. `tune2fs -r 0 /dev/sda1` - this is inconsequential. – Artem S. Tashkinov Jun 10 '23 at 10:27
  • Only one user (usually root) can write into the reserved blocks... -u option controls it: https://unix.stackexchange.com/a/8042/360526 What can also work is writing zeroes into all free space and then deleting it. It will force the ext4 to defragment free space and files, by force as in by design. – Валерий Заподовников Jun 10 '23 at 11:35
  • I know, systemd-journald log files are owned by root. In fact in my root partition I have barely any files which are not owned by root. I've run this command, it didn't change anything: https://pastebin.com/raw/rEPXr9MB – Artem S. Tashkinov Jun 10 '23 at 11:57