Home > Computers, MythTV > Defragging a Linux Partition

Defragging a Linux Partition

October 15th, 2008 Leave a comment Go to comments

So, further to my previous ramblings about when it is a good idea to defrag a Linux partition, the advice that I saw repeated time and time again, on how to defrag a hard disk, was quite simple to understand.

The basic theory is, is that you do not need to do anything more spectacular than move some suspected fragmented files from one partition onto another, and then back to the original location. Now, this is something that is quite easy to do, and the theory behind it is quite simple as well.

Because doing a file move users the userland commands (like “mv” and “cp”), we will be copying the contents of the file, and not the disk blocks used by the file, and it will then be deleted. Therefore, the file will be copied from partition A to partition B in an unfragmented way – i.e. from the start of the file to the end. Now, remember that the file system will choose a contiguous block of disk space, plus a little extra. This means that as soon as the file is on partition B, it is automatically defragmented for us. Brilliant!

Now, the only problem with this is, that you file is now on a different partition, and therefore in a different directory to before. This therefore means that you have to move the file back to the original partition in order for everything to work. This also has a slight plus, as if there is not much free space on partition B to hold the file in a contiguous block, the filesystem would therefore fragment it (not the desired effect), however, seeing as the file came from partition A in the first place, there should be enough free space on there to hold it in a contiguous block (unless of course partition A is already very full, in which case it could become fragmented again, but in this situation, there is nothing really that you could do in order to defragment anyway).

If you have enough free space, you do not necessarily need two partitions in order to make this work. You could simple copy the files from one directory to another, delete the originals and copy them back again. Note, however, that there is a problem with this procedure if you are not careful. You cannot simply move the files from the original directory to the new directory – it explicitly has to be a copy. As Linux’s ext2 and 3 filesystems are based on a linked list approach, moving a file involves nothing more than moving around a few address pointers on disk (hence why it is a very quick procedure to move files on disk in Linux). The files themselves are not physically moved, and therefore they would not be defragmented. The second “copy” procedure, can be a “move”, as the files would have already been defragmented in the original “copy”.

I should also point out that this procedure also works on a Windows NTFS partition as well. If I could find the link I had, I could point you towards a third-party disk defragmentor for Windows, that does the same thing (and if I remember correctly, I’ve got a feeling I saw Microsoft’s Raymond Chen using it as well.)

So, how do you actually achieve all this, and how did I get on when I did it?. It’s actually quite simple, and I’ll post that later.

Categories: Computers, MythTV Tags:
  1. No comments yet.
  1. No trackbacks yet.