Defragmentation with Hyper-V
On a physical system that uses hard disk drives (HDDs), it is common to defragment
the filesystem. This is required because files can become fragmented: instead of a file
being stored contiguously on the disk, it is broken into many pieces, or fragments,
over the disk. This can impair performance because when data needs to be read, many
seek operations occur (moving the disk head to the data location on disk). This slows
operations compared to all of the data being stored contiguously on disk, in which
case the data can be read efficiently. Performing a disk defragmentation optimizes the
disk by moving all the pieces of files so that they are contiguous on the disk and
therefore can be accessed efficiently.
Files become fragmented for many reasons. Typically, when a file grows, if there is not
empty space at the end of the file’s current position, additional space must be
allocated elsewhere on the disk. This would be common with a dynamic VHDX/VHD
file that grows as data is written. Note that the problem with fragmentation is the
seeking of the data. If you are using solid-state drives (SSDs), there is no seek time
and therefore fragmentation does not incur a performance penalty. Moving data can
actually decrease the life span of an SSD, so defragmentation is typically not
performed or recommended. Windows 2012 and above automatically disables
defragmentation on a SSD.
Consider local HDDs in a system. If you are using a fixed-size VHDX, it is likely not
highly fragmented because it does not grow. Therefore, defragmentation should not
often be required. If you use dynamic VHDX, the disk will most likely fragment over
time, and so performing a defragmentation will improve performance. The only caveat
is to remember that a VHDX file contains a filesystem that itself contains files, so
even if the VHDX file is not fragmented, when the VHDX file is used by a virtual
machine, the content will be randomly accessed throughout the VHDX because the OS
accesses various files, but defragmenting will still make it as contiguous as possible
and certainly won’t do harm.
If your storage is not local HDDs but a SAN, your virtual hard disks will be split over
multiple disks anyway, there will be other optimizations in place on the SAN, and
typically defragmenting is not recommended. Similarly, defragmenting a tiered
volume that uses different types of storage is not recommended, because this would
touch different blocks of the disk and potentially interfere with the automatic tiering
that optimizes performance.
The SAN recommendation of not performing defragmentation also applies when using
Storage Spaces and Storage Spaces Direct. In both cases, the virtual disk created is
hosted on multiple physical disks that likely again use tiering. In the case of Storage
Spaces Direct, the disks are also distributed over multiple nodes. Performing
defragmentation would not result in the blocks being contiguous and would taint the
tiering mechanisms.