Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Just sort of remembered something from OS class about fragmentation and was wondering if anyone knows for sure whether a VM file is liable to become fragmented if you tell it what size the hard drive should be from the outset.

I'm not asking whether the files in the VM can become fragmented, if they're truly a virtual machine then anything that can happen to a physical hard drive can happen to a virtual hard drive.

What I am asking is whether when virtual machines change the state of a particular session, does the big huge file itself get fragmented. I'd imagine that a fragmented virtual machine file can be a big problem because you could be reaching over several platters for data that a regular hard drive would keep right next to each other.

For instance, if I have 30 gigabytes free and I create a 20 gigabyte VM file (fixed size), there may be a good chance that the file is not occupying contiguous space on my hard drive. In a perfect world, defrag might be able to fix that.

But say I've got 150 gig's free, I get add a 20 gig VM (fixed size) then I download all of Star Trek The Next Generation, leaving me with 5 gigabytes of free space. I open and close my virtual machine 900 times. Do I have fragmentation in my VM or does it still occupy the same awesome contiguous hunk?

share|improve this question
If the answer depends on the OS, that's useful info too. – Peter Turner Nov 23 '09 at 17:47
It might help if you clarified whether you're talking about internal or external defragmentation of the virtual disk--internal being the data stored within it, and external meaning whether the virtual disk file itself is fragmented on the host volume. – rob Nov 23 '09 at 19:05
Yeah, I was talking about external fragementation. Internally I'd assume it's managed just like any other hunk of memory. I guess maybe it's unclear but I meant VM file, not files in a VM. – Peter Turner Nov 24 '09 at 14:17
Yeah, it seems as though the 2/3rds of the answers my question is generating are leading me to believe it was confusing. – Peter Turner Nov 24 '09 at 14:18
up vote 4 down vote accepted

The if you pre-allocate the entire space, instead of having a grows-as-needed volume, then there should not be any fragmentation.

I don't know for certain, but I would think it would be wise to pre-allocate the entire space, and then defragment the disk, just to be sure.

share|improve this answer
This is the correct answer. If you're pre-allocated it doesn't become fragmented on the host OS. If you're not pre-allocated it can easily become fragmented. The reason is because the OS doesn't need to allocate new blocks (which is what causes fragmentation) when updating a pre-allocated VM disk. Of course, I'm sure there is some wacky way that a defrag program could mess this up. – Pridkett Dec 2 '09 at 19:30

The host OS treats the *.vmdk file as any other large file -- as long as it is created in a contiguous space and doesn't change size, it will stay that way. (I'm assuming no compression, encryption, velociraptors, or other unusual factors)

As long as the file doesn't change size, the OS has no reason to move sectors around on the disk. The situation is the same with a fixed-size pagefile.

About VMware's "snapshots" feature: Each snapshot is stored in a different file. Your main image might be called blah-flat.vmdk, and then your snapshots would be called something like blah-00000001.vmdk, blah-0000002.vmdk, etc. Your flat file will never change size, but the snapshot files could, and therefore might become fragmented.

Lastly, Windows' defragmenter (IIRC) lists files that it failed to defragment -- that would be the most authoritative source of info.

share|improve this answer

A VM hard drive can be treated in the same way as a normal hard drive, fragmentations included.

Just a few days ago, I use recovery tools to undelete a file from a growing VM hard drive, so even on fixed size, it does not overwrite sectors quickly.

share|improve this answer

There is two layers of fragmentation to consider (if the vm-disk is stored as a file on the host machine), the fragmentation caused by filesystem on the host machine, and the fragmentation in the guests filesystem.

But assuming your preallocated vm-disk is perfectly unfragmented, then the virtual machine got no reason recreating/rearranging the vm-disk and will reuse what exists. So the virtual machine in itself got no reason to cause fragmentation in the host filesystem.

But depending on the host's filesystem there (might) be no guarantee it will forever be unfragmented, if the filesystem decides that it need to arrange some files it might do so (if there are filesystems that do this or not, I got no idea, consider this theory). I'm not a filesystem expert but unless there is a reason (can't see one?) to do so, I doubt it just would randomly fragment files for fun. So in most cases it will remain nicely unfragmented.

But the guest filesystem will of course still get fragmented as always (maybe not a very lot just by open/close 900 times).

And if the host filesystem would fragment the vm-disk file, the internal guest filesystem might seem totally unfragmentet when in reallity it's a total mess, the guest will be totaly unaware of this.

share|improve this answer

Virtual Hard Drives are the same as regular hard drives, it doesn't matter that they are stored in a file container and not a physical drive. If you are running Windows on the VM, it's the same as running windows on your PC. The OS still acts the same and the drive is just as likely to become fragmented.

share|improve this answer

You must log in to answer this question.

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