34

Why don't you have to defrag a Linux system? Because it's using the ext2/ext3 file system, or because it's Linux?

That's relevant, for I have a double boot system (W7 / Ubuntu) and a common partition (NTFS) that can be accessed by both system. If I'm using this partition with Linux, will it get fragmented?

Another issue are the Truecrypt containers. I also access them with Linux and Windows, and they are FAT32. Do they get fragmented by Linux operating on them?

3
  • 2
    The other answers are good but leave out one important bit: All of the filesystems: ext2/3/4 and NTFS will fragment files quite a lot when low on disk space. If you want to prevent fragmentation, don't allow your filesystem use to exceed 90%.
    – Zan Lynx
    Commented May 13, 2013 at 19:57
  • 2
    If you use ext4, then you will be surprised to see how much fragmentation you can have when you run e4defrag -v . in a directory tree, especially if it contains, say, files downloaded with a torrent client (though some do use fallocate/posix_fallocate).
    – rbrito
    Commented May 16, 2013 at 4:12
  • Even though the question is subject to personal opinion, I think it relies more on actual facts. Commented Sep 11, 2016 at 15:09

4 Answers 4

27

Here's an article on How To Geek about how ext2/ext3 allocates files on the disk. And they also have an article asking "Do you really need to defrag?"


On why FAT becomes fragmented:

"When you save a file to a FAT file system, [the file is saved] as close to the start of the disk as possible. When you save a second file, [the file is saved] right after the first file – and so on. When the original files grow in size, they will always become fragmented. There’s no nearby room for them to grow into."
-How To Geek

And wikipedia has more information about FAT fragmentation.

On how EXT2,3,4 allocate files:

"ext2, ext3, and ext4 file systems [...] allocates files in a more intelligent way. 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."
-How To Geek

(And more info on defragmentation on ext3, from wikipedia)

"Modern Linux filesystem(s) keep fragmentation at a minimum by keeping all blocks in a file close together, even if they can't be stored in consecutive sectors. Some filesystems, like ext3, effectively allocate the free block that is nearest to other blocks in a file. Therefore it is not necessary to worry about fragmentation in a Linux system."
-TLDP

8
  • 4
    Great answer! But you should put a little more content. The idea is that any answer on StackExchange should be sufficient by itself. If the links of your questions get broken, your answer won't be of any help, so you should give an sufficient explanation in your post and then give the links for further reading.
    – lgeorget
    Commented May 13, 2013 at 13:36
  • 1
    Thank you @lgeorget -- I fleshed out the answer with some direction quotations so there's always some content to read up on "why". I appreciate your input / constructive crit!!
    – dougBTV
    Commented May 13, 2013 at 13:48
  • "When you save a file to a FAT file system, it saves". Doesn't make sense. What is "it"? FAT is a file system format. It describes where you can save, but it doesn't limit you to a single location. In fact, the OS decides. But there are many different OS'es which can write to FAT, and each makes its own decision.
    – MSalters
    Commented May 13, 2013 at 20:31
  • @MSalters -- agreed. I'll edit the quoted section to omit that vaguery.
    – dougBTV
    Commented May 13, 2013 at 20:35
  • The vaguery is only hidden now. The statement is still rather absolute (where "FAT" saves) while the whole FAT specification says no such thing. In fact, the Wikipedia link you quote directly contradicts it: it specifically says that Windows chooses the location for the second file not directly after the first file. ("selecting large contiguous areas in advance")
    – MSalters
    Commented Apr 10, 2014 at 10:13
24

Fragmentation is always a concern no matter what the filesystem. Ext3/Ext2, though, have minimized the problem to the point where most admins just operate as if it's not a concern. This is minimized by use of a competent I/O scheduler and by the filesystem leaving trailing space after each file to accommodate growing. On top of that, additional fragments are stored closer together so even when there is fragmentation, the effects are severely muted. Ext4 switching to extents was also intended to help mitigate fragmentation even farther.

Fragmentation still exists, ext2 had an offline defragmentation tool, ext4 is getting an online one and btrfs (just because it's related to the question) already has an online defrag. If you're on ext3 your only real choice is either to convert to ext4 (via tune2fs) or try to see how much luck you have with user space tools like shake.

Depending on the type of I/O your filesystem is undergoing, it could still be a a concern even at its muted level, which is why the online tools were deemed useful to begin with.

For the question about FAT32, fragmentation doesn't happen when you just operate on a file, it happens when you write to the file. To answer your question directly, fragmentation is an aspect of the filesystem design, so your FAT32 filesystem has the potential for fragmentation whenever you write something to it.

3
  • "This is minimized by use of competent the I/O scheduler". There is something not quite right with that phrase. Commented Mar 28, 2014 at 22:45
  • Thanks for catching that. I'm kind of bad to revise my language without correcting any problems the revision causes with the overall grammar of the sentence of paragraph.
    – Bratchley
    Commented Mar 29, 2014 at 13:20
  • This is a good answer. I would add something about CoW (copy on write). It has got a close relationship with whether the fs gets fragmented. Commented Sep 11, 2016 at 17:05
4

Answering your questions, one by one:

Why don't you have to defrag a Linux system? Because it's using the ext2/ext3 file system or because it's Linux?

Because ext2/3/4 have a different approach about the files and folders, so they barely get fragmented. Others have already answered about it, and you can read more details here

That's relevant, because a have a double boot system (W7 / Ubuntu) and a common partition (NTFS) that can be accessed by both system. If I'm using this partition with Linux, will it get fragmented?

Yes, it can get fragmented. It's not a matter if Windows or Linux or anything else is accessing it, it's how each filesystem deals with file size, how it stores all those blocks, whether it leaves empty spaces between each file...

Another issue are the Truecrypt containers. I also access them with Linux and Windows, and they are FAT32. Do they get fragmented by Linux operating on them?

Truecrypt containers are a file, for all pratical matters. When you create a container using TrueCrypt, you're creating a file of the size that you choose. For your operating system, it's just like any other file. If you make copies of it, etc, perhaps the copies might end up being saved fragmented.

But, remember, it's never good to have multiple copies of the same container, it might help to reduce the security of it. Always make a new container.

When you save, delete, copy, move, etc, files inside the container, the operating systems doesn't know about it, it's same of opening, editing and saving any other file, without changing it's size.

And if you're worried about truecrypt security about fragmentation, do some research about using truecrypt with SSDs: since the SSD try to lever the wear of all memory inside it, you might have the same effect of copying the container around.

4

In the end, it's down to the file system drivers within the OS.

ext2 and FAT file systems are both methods to record which blocks on disk belong to which file. When not all block of a file are contiguous, the file is called fragmented. But as should be obvious, fragmentation is caused by the block allocation strategy when writing a file, not by how you then record those blocks.

What does make a difference is how far ahead the file system drivers can look. If the driver knows there are 10 more blocks to write, it can pick a contiguous free area and avoid future fragmentation. This depends on the driver architecture, which is OS-specific. But the exact same free space search can be done on FAT and ext2.

In fact, ext2 and FAT aren't really as different as you may think. I've written the ext2 part of a bootloader on top of the existing FAT code, and that required less than 100 extra lines of code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .