As you use a computer, over time your files will tend to get scattered around the disk. This scattering is called fragmentation, and can slow down performance as the disk head has to seek back and forth between fragments (Note: doesn’t apply to solid state media, which doesn’t involve disk heads). Defragmentation (or defragging) is the act of reversing the process, putting order back into your system. With virtual machines, proper defragmentation is a little more complex than it is on a physical machine because of the layers involved.
Before we begin, it’s important to note that defragmentation isn’t a necessary task – your virtual machine will still work just fine even if you never defrag, and the effects of fragmentation are usually not noticeable. Personally, I’ve never feel the need to defrag. However, if for some reason you do feel the need to defrag, here’s how to do it. Note that snapshots get in the way of proper defragmenting.
Consider this hypothetical sparse virtual disk. The guest files (yellow) are fragmented in the guest filesystem (green), which is in turn fragmented in the sparse virtual disk (red), which is finally scattered over the host filesystem (blue). In all, a bit of a mess. The first thing to do is defrag the guest filesystem using your favorite third-party tool – this will of course depend on the guest OS you’re using.
Defragmenting the guest will organize guest files nicely on the virtual disk. In this example, it’s put the four-block file back together and eliminated some holes. However, defragging the guest on a sparse virtual disk will probably also cause the .vmdk file to grow (if previously untouched blocks need to be used for any reason, which they probably will). For this reason, it’s a bad idea to run automatic defragmentation on a sparse virtual disk – it’ll just cause the sparse virtual disk to keep growing, which sort of defeats the point of having a sparse virtual disk. If you really need automatic defragmentation, consider using a preallocated virtual disk.
Guest defragmentation causing the .vmdk to grow is also why you want to defragment the guest before anything else – it makes no sense to clean up lower layers if you’re immediately going to mess them up again by cleaning up the upper layers. Clean up from the inside out, not the outside in.
Follow up by shrinking the virtual disk using VMware Tools (note: requires not having snapshots). This step isn’t really related to defragmentation, but I think it’d be a good idea anyway. This will free up unused space from the .vmdk file.
After doing that, go to the virtual machine’s Settings and check under the Hard Disks pane. Fusion will tell you if it thinks disk cleanup is required – if so, do it, if not, it’s probably fine to skip this step. You can also manually defrag the .vmdk file with vmware-vdiskmanager, but I wouldn’t recommend doing this unless you know what you’re doing and why.
Finally, shut down the virtual machine (if you haven’t already) and defrag the host using your favorite third-party tool. While Apple claims OS X makes defragmenting mostly unnecessary, I believe OS X’s automatic
defragmentation only applies to files smaller than 20 MB. Chances are your virtual machine is larger than that 🙂
Again, I think that for the most part, defragmentation isn’t necessary. Even though a virtual machine has three opportunities to become fragmented (vs. a normal file, which has only one), the actual effect isn’t 3x worse. The major penalty from a fragmented disk is the time it takes for a disk head to seek to the next segment. Even though there are three layers that can get fragmented instead of just one, only the host filesystem involves the disk head (and if you’re using a solid state drive, none of them do).