13

What is the best way to defragment a FAT filesystem when running Linux/Unix (on usb stick for instance)?

4
  • 4
    I would go for your first option if space isn't an issue. I would create a clean filesystem in a large file that is loop-mounted. That way you get the opportunity to check the result before overwriting the stick.
    – jippie
    Mar 16, 2013 at 21:24
  • @jippie's suggestion also offers a backup. Else I'd go for a defrag utility, but check carefully first it is reliable, and back up beforehand if I have any attachment to the data.
    – vonbrand
    Mar 16, 2013 at 21:40
  • @jippie : you mean creating a loop file the same size of the initial fat partition, copy all to it, check, then dd to the original device? You will need extra space, and I don't see how it's better from just simply copying the file to the local ext filesystem temporarily. Or I'm missing something. Mar 17, 2013 at 7:47
  • 1
    You can check the result before changing the content of the flash drive.
    – jippie
    Mar 17, 2013 at 7:49

3 Answers 3

8

You can check whether a file is fragmented or not using the filefrag utility.

That way you could filter out the files that aren't fragmented, and do the copy/copy back only for already fragmented files. That should save you some time. However be aware that there is no guarantee the new file layout will be better than the old one.

The best way is to ignore file fragmentation altogether. Especially on a USB stick it hardly matters, only wastes write cycles, and unlike HDDs, there are no movable parts and thus not much of a penalty due to fragmentation.

Defragmentation should only be necessary if something funny happened that caused files to fragment in a very extreme way. For example torrent clients that download file contents in random order without preallocating. But unless FAT started supporting sparse files at some point that's not even an issue there, as preallocating is mandatory on such a FS.

6
  • 4
    Thanks for your answer. In fact I need a way to fully defragment the FAT filesystem because I'm using Easy2boot, and every iso file must not be fragmented. Mar 24, 2013 at 23:13
  • Sounds like an outdated method to me. GRUB2 isoloop is very popular, so lots of distros support loop-mounting an iso from anywhere already. Those that don't, it may be that it works directly from a partition, but it might just as well fail. Entirely depends on the ISO itself. Mar 25, 2013 at 2:32
  • 3
    The drawback with GRUB2 isoloop is that it will directly load a Linux kernel in the ISO. You have to know the path of the kernel/initrd to load, and you will loose the ISOLINUX menu offering custom boot options. Apr 2, 2013 at 11:35
  • Finding the path isn't hard, and the rest depends on how much effort you want to make to convert the isolinux menu items to a grub2 submenu... but yes, all solutions have their drawbacks. I just tested my ISO partition - NONE of the files are unfragmented. And they were copied one by one. Fixing that would be a major pain for me, so I'll rather forgo on ISOs that do not support isoloop mounting. Apr 4, 2013 at 10:29
  • I never used easy2boot myself, but since this solution involves the creating of a partition, couldn't you do that in the first place - one partition per ISO, and dd. No fragmentation possible this way. Or alternatively if that's possible at all, a fragmentation-free filesystem (ISO inside ISO?) Apr 4, 2013 at 10:32
3

There is a utility available called defragfs, which is file-system agnostic. Therefore, it can be used within Linux distributions to defragment FAT32 partitions:

https://sourceforge.net/projects/defragfs/

It is for your exact purpose you intended and will help make contiguous image files for Easy2Boot.

defragfs is recommended and used by the author of Easy2Boot:

https://www.easy2boot.com/make-an-easy2boot-usb-drive/make-using-linux/

0

Let's look at this from a different angle, because you gave an example "on usb stick for instance"

BAD EXAMPLE

A USB stick is FLASH memory, and there are two things to consider: 1) Since FLASH memory does not have moving parts, there is no noticeable performance gain to be seen by doing a defrag. Defrgs are either for cleaning up the file system or re-org of the file blocks to put them closer to each other - to reduce the physical movements of the arm (seek) and the wait time of the disc spinning (latency). Flash does not have this. 2) Writing and writing and writing on flash memory wears it down. Now, for 100K write endurance maybe your USB stick would last several to 10 years, but the number of time you can write is a "finite" number. Constant defrags to the USB stick eat away at that life because of the endurance limitation.

And this is not just your USB stick, SD cards, Compact FLASH and even SSD drives.

My point, don't defrag just for the hell of it, Know what the underlying technology under the file system is and the implications, and even if it is magnetic and not electronic, too much defrag may eventually corrupt your file system and you loose your files.

You must log in to answer this question.

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