Why Linux Doesn’t Need Defragmenting
howtogeek.com
howtogeek.com
To see how fragmented a file is:
filefrag -v filename
To see how fragmented your free space is e2freefrag devicename
http://linux.die.net/man/8/e2freefraghttp://linux.die.net/man/8/filefrag
To learn about the intricacies of how fragmentation can occur in ext3 and some better tools for measuring it, see the fragmentation section of Oliver Diedrich's article 'Tuning the Linux file system Ext3'.
http://www.h-online.com/open/features/Fragmentation-746701.h...
Extents, multiblock allocation, and delayed allocation make ext4 more resistant to fragmentation than ext3, but the developers are still working on a defrag utility.
Take a look at your filesystem properties:
# tune2fs -l /dev/blah
In there you'll see a property called "Reserved Block Count". That is space reserved for kernel use to avoid fragmentation, continue logging, and for any other tasks the kernel/root user may need to do on a full filesystem. Users cannot write to this and your disk utilities will report your disk being full even if this reserved space is still available.I believe it is typically 5% of the partition size. If your partition is hundreds of gigabytes then the reserved portion may be tens of gigabytes (multiply the reserved blocks by the block size to get total byte size).
You can change the percentage of this portion with the tune2fs -m flag. I have filled up a 500 GB backup drive with 0% reserved blocks (%5 reserved would have meant that 25GB was unusable) with no issues. Obviously, you probably don't want it set to 0% if you are constantly changing the contents of that partition.
So... buy a larger hard disk, or get a second disk to copy everything to instead of a defrag utility?
I think I'll take the Windows approach here.
Of course with SSDs becoming more and more prevalent, this is becoming a non-issue anyway.
So, linux scatters the files. My partition is almost full and fragments. Now I copy all the files to a second location and wipe the partition. Now I copy them all back. Won't that just scatter all the files and fragment all over again ?!?
By copying files back en mass, fragmentation / allocation will be more optimized.
If you have insufficient disk overhead and continue the file destruction/creation process, you may end up with higher levels of fragmentation in future. This depends on your particular usage scenario.
If you're not underallocating disk, generally you won't have much of a problem.
This copy method can of course be done on Windows aswell, the only difference is that NTFS's file placement strategy generally results in more fragmentation over time than the strategies employed by Linux filesystems like ext(x). That said all filesystems get fragmented over time, no strategy can avoid this fully.
Also if your partition is very fragmented and you have many large files it can be faster to use this 'copy to another partition' method rather than using a defragmenter which will operate solely on the target partition and thus need to move many parts of files around in the partition to 'fit' in parts of other files. Of course using the copy-method requires you having another partition with enough free space so it might not always be a feasible option.
2. Scattering files all over the partition makes the longest seek times. This is actually the most inefficient solution for linux with its apps and sources (usually) consisting of thousands small files.
3. Windows does not need to be defragmented manually as it defragments itself when PC is not in use.
Yes but it isn't guaranteed that there will be less fragmentation on a windows file system. It depends on when was the last defrag, on the way file are created, and your disk occupation.
Also, when you scatter files on different disks it can be more efficient because you can read several cluster simultaneously. But I guess Windows does this as well.
Say you had a "write/update infrequently, read many times" system profile with 10% disk space avail. It seems like Windows would store these files closer to each other on the drive, making reads more efficient.
Or would that be such a negligible difference that it's not worth mentioning?
However, the locking semantics on NTFS are shitty unless you know what you are doing (i.e. use KTM/Transactional NTFS) which sometimes skews the statistics a lot.
Copy all the files off the partition, erase 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.
does not work for ntfs-3g. Whatever strategy it uses to allocate blocks, it's terrible for streaming reads of large files (last time I checked about 1-2 years ago). So, if you're trying to use the copy-off copy-on method of defragmenting an NTFS partition, don't do it from Linux.
Anecdotally, I run a couple virtual machines on my laptop whose storage is on an external drive that sounds like Geiger counter at Fukushima Reactor 1 when the drive is busy. That is, when I've booted Windows 7. Not when I'm running anything under Windows, Windows just needs to be booted. When I'm running only a Linux VM, it drops to background level.
Can anyone give more details on this?
Oh... And the love in the comments on the original article is quite moving.
It's generally a sign that you're running your disks / filesystems overutilized. Retaining 10-20% free space is generally encouraged.
If you do elect to do the "copy out, wipe, copy in" method, you can do this, in general, while not completely losing the use of your system. There are some tricks (union mounts, remounting filesystems) which will allow you to flip between filesystems, in most cases. Specifics will depend on your partitioning scheme, use case, and the filesystems under consideration (usually you'd be dealing with user files under /home, spool or variable files under /var, source trees, etc.).
As the article states, Linux systems generally experience very minimal fragmentation, and dynamically resolve the issue for you, unlike Windows. You can typically run a Linux system for years, given a 10-20% disk reserve, without running into appreciable fragmentation issues. The gains from any sort of manual defragmentation would be minimal compared to the cost of performing the task.
It's like saying "Our car never breaks down so you don't need to be able to repair it. Even if it does break down you can still walk."
One of the goals of recent ext4 work was to get support for "online defragmentation" but I don't know to what extent it's automatic, what tools exist, et cetera. It sounds like pyfragtools also has attempted to solve this problem.
http://ubuntuforums.org/showthread.php?t=169551
Other filesystems like ZFS and btrfs utilize copy-on-write to virtually eliminate file fragmentation -- files are copied whenever edits are made to the file.
This makes btrfs suitable for virtual machine images.
You could solve this by automatically shredding deleted stuff in the background, but it would probably kill SSDs and other storage devices which you don't want to write to too often.
You really consider being able to shred individual files a critical filesystem feature? Over, say, not having to configure static partition sizes, having snapshots, or being completely immune by design to large classes of data corruption (including all those that require an fsck-like tool to repair)?
And if you're on an ssd its likely doing wear leveling in a similar fashion.
Really the better way to do this than overwriting files in place is to have encrypted files and keys and just reencrypt with a random key you don't retain, or some other method I suppose. But the ability to rewrite to the same spot in the filesystem, for a copy on write filesystem, is rather ludicrous. Hesitate all you want, I prefer cow fs's for most tasks.
xfs_fsr [-v] [-t seconds] [-f leftoff] [-m mtab]
xfs_fsr [-v] [xfsdev | file] ...
xfs_fsr improves the organization of mounted filesystems. The reorganization algorithm operates on one file at a time, compacting or otherwise improving the layout of the file extents (contiguous blocks of file data).http://dd.reddit.com/r/linux/comments/uc1o9/why_linux_doesnt...
So really the title should be "Why Linux Doesn't Need Defragmentation Tools".