I have a server running a number of Hyper-V guest instances. One recommendation to improve the performance of our guest OSes is to defragment the vhd files. Can I do this while the guest is still running? Is there a way to defragment only a single file?
There is a way to defragment only a single file - Contig from SysInternals:
I don't know that you can do it while the files are in use, I'd guess not but am willing to be surprised.
But I'm a bit skeptical that it will do much good unless they are severely fragmented (contig will tell you how many fragments they are in) - especially if you're using modern hard disks which abstract away where they are writing so they can rewrite to avoid errors, a RAID disk system with a controller that has disk cache, with OS disk cache.
And the files within the virtual machines can be fragmented as well so it wont be accessing contiguous parts of the VHD necessarily. And if Hyper-V is anything like VMWare, it has it's own disk handling which merged multiple reads/writes from the VMs into fewer read/writes for the real disks.
Summary: don't expect miraculous speedups unless you've been adding and removing different size virtual machines over and over for ages.