Condividi tramite


Checking and Correcting Virtual Hard Disk Fragmentation

With both dynamically expanding and differencing virtual hard disks - the following pattern is used when writing data:

  1. Check to see if data has already been written to this area of the disk
  2. If yes - write new data to the same location as the old data
  3. If no - create new blocks on the disk, and record the new block location in the block allocation table for the virtual hard disk

Depending on the write patterns experience by a virtual hard disk - these blocks can end up being poorly aligned, or "fragmented".  You can check the fragmentation level of a virtual machines virtual hard disks by running the following command in PowerShell:

get-vm "VM Name" | Get-VMHardDiskDrive | get-vhd | Select path, FragmentationPercentage

If you believe this is causing a performance problem in your situation - there are a number of ways to defragment a virtual hard disk.  But the easiest way is to perform a storage migration - which will re-layout the blocks in a linear fashion.

Cheers,
Ben

Comments

  • Anonymous
    March 14, 2015
    Can you please explain, what FragmentationPercentage is not ok ? Is thre any another way to defragment a vhdx file ?

  • Anonymous
    April 09, 2015
    Andrey - the less fragmentation percentage is the better. There are another ways of defragmentation but all of them are harder to perform comparing to storage migration and mostly requires vm shutdown

  • Anonymous
    January 07, 2016
    Hi Ben, can you list out the steps to do this kind of de-fragmentation? I have tried to move the VHD to another folder using Hyper-V manager (Windows 10 Build 1511) but the fragmentation percentage did not change after the time consuming move?

  • Anonymous
    September 20, 2017
    Shut down the VM, goto properties, click edit on the disc and migrate