Online Defragmentation


Jarret W. Buse

Online Defragmentation

Newer file systems try to keep files from becoming fragmented. Hard disk performance declines with fragmentation. When files are not placed in contiguous blocks, file fragmentation occurs. When a file is spread out over the hard disk, the hard drive "appears" to be slow to the user. The hard drive has to wait for the disk to spin to maneuver the read/write heads over the correct sectors. The lag time causes most users to believe the system is slowing down. The problem is not the computer, but the hard disk.

NOTE: Regular data files can slow down an application which uses the file. If the Linux system files are fragmented, then the whole system will slow because the OS is limited when accessing necessary files. Defragmentation of data files can improve performance by as much as 25% or more.

Most people have seen defrag utilities that places the files in contiguous blocks. While the defragmenter application is running, the system is extremely slow and usually is unusable until the process is finished. For some systems, run a defrag apps every 2-3 months. In extreme cases of file fragmentation, every 4 weeks or less may be required.

Fragmentation can occur (see Intro to Extents) on every system. Some file systems include an online defragmentation process to help reduce fragments in the background.

NOTE: There are three types of fragmentation to consider:

  • File fragmentation – A file is spread out over the disk.
  • Relevant file fragmentation – When multiple files are accessed at once, it is best the files are all close together.
  • Free space fragmentation – When free space is needed for file use, it is best that free spaces are close together in contiguous blocks.
Most file systems need the drive to be unmounted or off-line when the defragmentation application is running. For few file systems, the defrag can occur while the drive is mounted or online.

Let's take, for example, the EXT4 file system (e4defrag). An application can monitor the file system and find the files which are most fragmented. Once it discovers which file is the worst, it finds the file size. The process creates an empty file of the same size with as many contiguous blocks as possible. It checks the fragmentation on the empty file and finds if it is better than the original. If not, it deletes the empty file and the process leaves that file alone.

If the fragmentation is less than the original, the file is copied from the original to the new one. The data is copied one extent at a time until the file is moved. As each extent is copied, starting from the files first extent, the extents "replace" the old ones. Extent Pointers are used to point to the new file up to the last extent written, and then points the rest to the old file. For example, let's assume file A is fragmented. The process creates a new file (file B). The first extent is moved from file A to file B. The extent pointers point to the first extent of file B. The rest of the pointers point to extent 2 and the rest of file A. Then the second extent is copied. Now the pointer points to extent 1 and 2 on file B and extent 3 on file A and so on.

Once all the extents are copied, then the first file is deleted leaving the empty block for other use.

This process can continue through the file system until fragmented files are at a minimum.

To defrag an EXT4 file system, open a Terminal and type the following:

e4defrag /device or file

NOTE: Be sure to update the e4defrag with all available patches. For a single file defrag, specify the file. For a relevant file defrag, specify a directory name.

Here you have a choice to defragment a whole drive, folder or file. If you want, you can check which files need to be defragmented first by using the following command:

e4defrag -c /device or file

Within the output is a fragmentation score for the files tested. A score 0-30 means the fragmentation is very slight. If the score is 31-55, the drive is a little fragmented. Scores 56 and over need defragmentation.

To defrag free space on EXT4, the following can be run:

e4defrag -f

Fragmentation is not a problem on newer file systems until the drive has little free space. What occurs on some file systems is the files are spread out on the drive. Since contiguous space is left between files, the files can grow without becoming fragmented. Once free space is reduced on the drive, the files must start using noncontiguous blocks. One way to combat this is make sure your drive uses less than 90% of the total drive space. With some free space, the file system will eventually move files around to defrag the drive. Swapping the drive with a larger one and replacing the files will also help. Another alternative is to delete unnecessary files, move the files from one drive to another, then move them back to allow the files to be placed in a more orderly fashion leaving more contiguous free space.
$100 Digital Ocean Credit
Get a free VM to test out Linux! Hosting Donations
Consider making a donation

Members online

Latest posts