I have discovered an easy way to compacting VHDs

Page 1 of 3 123 LastLast

  1. Posts : 15,480
    Windows10
       #1

    I have discovered an easy way to compacting VHDs


    As we all know, vhds grow in physical storage size even if the vhds are much smaller in content inside the vhd.

    For example, I had a PRO 10 1709 VHD of approx. 18GB and upgraded it to 1803. After I did disk cleanup, to remove windows.old etc, the contents of the vhd are back to 18GB but physically the vhd on hard drive was 38GB.

    I ran compact from diskpart (and from Hyper-V on a copy) and it had little effect, shrinking to around 36GB.

    I googled it and found a utility called SDELETE that zeros unused space (compact not work on unzeroed space apparently). I ran this on a 38GB copy, and it had some effect reducing to 27GB.

    However, I know with a bit of effort, shrinking C drive using minitool partition wizard, cloning vhd to another vhd, and re-expanding, the new vhd only occupies 18GB. I have done this many times.

    Then I had a light bulb moment - I thought why does the cloning method work, and I realised the key step is the shrinking step as that is essentially doing a defrag.

    So I thought "what if I defrag the 38GB vhd, and then compact?"

    I did this and, flabber my gast, it worked first time with the existing vhd being reduced to 18GB!

    The defrag of 38GB in a 50GB volume only took few minutes as the VHD was on an SSD (I am not defragging the SSD, only the virtual drive).

    So here is how to do it

    1) Mount vhd as a drive and note letter of OS drive e.g. R:

    2) Open admin command prompt and type

    Code:
    defrag R: /u

    3) Eject mounted vhd from disk management

    4) Compact VHD (substitute correct drive letter, name and path as appropriate)

    Code:
    diskpart
    
    select vdisk file="D:\my path\my vhd.vhdx"
    
    compact vdisk
    
    exit
    Compact from Hyper-V does not work after a defrag - it thinks file is compact.
    Last edited by cereberus; 28 Aug 2018 at 12:39.
      My Computer


  2. Posts : 31,630
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #2

    Good call, worked for me too. I've tried running defrag within the VM before but that seemed to have no effect.

    If you want to see the before and after fragmentation reports, add the /V option ('verbose') to the command line.
      My Computers


  3. Posts : 15,480
    Windows10
    Thread Starter
       #3

    Bree said:
    Good call, worked for me too. I've tried running defrag within the VM before but that seemed to have no effect.

    If you want to see the before and after fragmentation reports, add the /V option ('verbose') to the command line.
    Amended compact stage above.
      My Computer


  4. Posts : 31,630
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #4

    cereberus said:
    Amended compact stage...
    cereberus said:
    Compact from Hyper-V does not work after a defrag - it thinks file is compact.
    That's odd, the first one I tried (as per the original post, before your amendment) did compact in Hyper-V.
    I tried Diskpart's Select Vdisk and Compact Vdisk on that vhd but it didn't compact it any further.
      My Computers


  5. Posts : 17,661
    Windows 10 Pro
       #5

    Nice one Martin! Thanks for sharing.

    Kari
      My Computer


  6. Posts : 15,480
    Windows10
    Thread Starter
       #6

    Bree said:
    That's odd, the first one I tried (as per the original post, before your amendment) did compact in Hyper-V.
    I tried Diskpart's Select Vdisk and Compact Vdisk on that vhd but it didn't compact it any further.
    The Hyper-V interface seems to be a bit temperamental i.e. it does not always offer a compact option but the diskpart always seems to work.

    As you have a command prompt open to do the defrag, it is only a little extra effort to use diskpart.

    Maybe once people have tested the above we can work out its "quirks".
      My Computer


  7. Posts : 17,661
    Windows 10 Pro
       #7

    This really is an excellent tip! Wanted to rep you @Martin, but as I have been a bit lazy in giving rep, I have to "spread the joy" before I can do it.

    I have discovered an easy way to compacting VHDs-image.png
      My Computer


  8. Posts : 15,480
    Windows10
    Thread Starter
       #8

    Kari said:
    This really is an excellent tip! Wanted to rep you @Martin, but as I have been a bit lazy in giving rep, I have to "spread the joy" before I can do it.

    I have discovered an easy way to compacting VHDs-image.png
    I will get pleasure if you confirm it works for you :-D.
      My Computer


  9. Posts : 17,661
    Windows 10 Pro
       #9

    Yes, it works.
      My Computer


  10. Posts : 5,478
    2004
       #10

    I have been doing this for a few years now:
    PHP Code:
    function ShrinkVM 
    # This function calls defgag.exe /x which will either consolidate free space and then run sdelete
    # Should only run this in VM - not on host.

    {
        
    askQuestion "Run Defrag & sdelete?"
        
    Switch ($prompt
        {    

            
    {   $Command="$env:windir\System32\defrag.exe"
                
    $Parms="C: /h /x"
                
    $Prms=$Parms.Split(" ")
                & 
    "$Command$Prms
                
                $Command
    ="$env:windir\System32\defrag.exe"
                
    $Parms="C: /h /o"
                
    $Prms=$Parms.Split(" ")
                & 
    "$Command$Prms
                
                $Command
    =$sdeletePath
                $Parms
    ="C: /z"
                
    $Prms=$Parms.Split(" ")
                & 
    "$Command$Prms
            
    }
            
    1     {Write-Host "Request cancelled - Defrag not run" -f yellow}
        }
        If (-
    not $runAll) {pressAnyKey}

    And then compacting it.

    However, while it works sdelete is very very slow and using macrium (or anything else I guess) to backup and restore an image results in a smaller vhdx and is much faster (that was @Kari's idea some time ago).

    I've not figured a way to automate a backup/restore yet so I stick to defrag (especially the consolidate free space option) followed by sdelete which while much slower is not too much worse as an end result.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 22:06.
Find Us




Windows 10 Forums