This article describes the correct procedure for defragmenting Cluster Shared Volumes on a Hyper-V R2 cluster using PowerShell.
A Cluster Shared Volume (CSV) contains the configuration, virtual hard disk and snapshot files of multiple Hyper-V guests. Notably fragmentation of the large VHD files deserve your attention.
Fragmentation of these files can become a problem because the disk head needs to use an increasing number of seeks, lowering the throughput and thus the perceived performance of the guest as a whole.
On the other hand, NTFS has become more and more efficient in recent OS versions and fragmentation need not always have a severe impact on performance.
CSV is a distributed orchestration layer on top of NTFS (implemented as a file system filter driver) and for fragmentation it takes advantage of all the NTFS techniques. The advantage of this design is that all disk management tools which have been written for NTFS continue to work, including a variety of defrag tools.
Before writing a file, NTFS searches for enough free space to write the entire file to disk. On a newly formatted disk the VHDs will be written contiguously while all the blocks are written and "zero out" which can be fairly time consuming. Of course this mainly applies when a fixed VHD format is used for which 100% of the space of the virtual hard disk is allocated and reserved on the disk. With dynamic VHD’s you are much more susceptible to fragmentation because initially only the header and a few control blocks are written to the physical disk. The VHD file will grow as more data is written to the file that the virtual hard disk represents. Suppose you have 10 VMs on the same CSV and each VM has access to one or more dynamic VHDs. The chances of fragmentation have grown tenfold.