Linux – How to deliberately fragment a file

ext3linux

I am looking for a way to fragment an existing file in order to evaluate the performance of some tools. I found a solution for NTFS file system called MyFragmenter as described in this thread. However I can't find anything for ext2/3/4… I guest I can develop my own file fragmenter but due to time constraint I would like to find a faster solution. I found some tool like HJ-Split which split a file in smaller bits but I doubt this will simulate file fragmentation.

Is their any solution available for my problem?

Best Answer

If you want to ensure fragmentation but not prevent it (so you only have partial control over what happens), and you don't care about the specifics of the fragmentation, here's a quick & dirty way of doing things.

To create a file of n blocks in at least two fragments:

  1. Open the file with synchronous writes, write m < n blocks.
  2. Open another file. Add to it until there are at most n - m blocks free on disk. Don't make it sparse by mistake!
  3. Write the remaining n - m blocks to the first file.
  4. Close and unlink the second file.

You can fragment in more pieces by interlacing more files.

This assumes the filesystem is available for this sort of torture, i.e. not in a multi-user or mission-critical environment. It also assumes the filesystem has no reserved blocks, or the reserved blocks are reserved for your UID, or you're root.

There's no direct way to ensure fragmentation, because Unix systems employ filesystem abstraction, so you never talk to the raw filesystem.

Also, ensuring filesystem-level fragmentation tells you nothing about what happens at lower levels. LVM, software and hardware RAID, hardware-level sector remapping and other abstraction layers can play havoc with your expectations (and measurements).