Compacting virtual hard disks

Page 1 of 2 12 LastLast

  1. Posts : 15,485
    Windows10
       #1

    Compacting virtual hard disks


    Compressing a vhdx file in Hyper-V

    Well I have had fun today trying to compress some vhdx files used in Hyper-V.

    My VHDXs only had around 15GB of files (10 pro Insider 15061) including hidden files after disk cleanup when in Hyper-V but the vhdx files were around 40 GB/

    Tried following

    1) to compact it using Hyper-V compact - no change

    2) mounted vhdx as drive in host and used vdisk compact - no change

    3) backed up vhdx using Macrium Reflect, deleted partition and restored - no change.

    4) shrunk c drive to minimum size with partition wizard - no change

    5) shrunk c drive to minimum size and expanded to full size with partition wizard - no change

    6) backed up c partition from 5 above (deleting c partition in vhd in Macrium) and restored with Macrium - no change


    So I did this then

    7) deleted c drive partition in vhdx from disk management, and then restored c drive using Macrium (said full copying)- no change!

    8) So I then created a new vhdx and restored the image backup to new vhd (Hoooray - file now only 15 GB)

    9) renamed old vhdx with .bak and renamed new one to old name and ran hyper-v - would not boot.

    Back to trusty Macrium - booted from Rescue Drive and used "fix windows boot problems" - SUCCESS!

    So sequence is

    a) mount vhdx in hyper-v, use disk cleanup etc.

    b) mount vhdx in host disk management

    c) shrink c drive to minimum, the re-expand to full size using minitool partition wizard or similar

    d) image backup vhdx using Macrium Reflect

    e) create new vhdx

    f) restore image backup from Macrium Reflect to new one (renaming old to .bak and new name to old name).

    g) repair boot files using Macrium in hyper-v if needed.

    In hindsight, not sure if step c is needed. More testing needed.
      My Computer


  2. Posts : 633
    Win 10 Pro x64 1607 (Build 14393.953)
       #2

    Compacting virtual hard disks


    Sounds like a fun trip indeed! I'll give that a shot with VMware Fusion VM too. Keep you posted.

    Cheers
      My Computer


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

    M4v3r1ck said:
    Sounds like a fun trip indeed! I'll give that a shot with VMware Fusion VM too. Keep you posted.

    Cheers

    Never had a problem compressing vhds when I used VMWARE. Issue seems to be with vhdx files.
      My Computer


  4. Posts : 5,478
    2004
       #4

    I do this inside the VM before compacting - Defrag to consolidate free space then use sdelete to zero.

    It works much better than just compacting the vhdx.
    Code:
    		{	$Command = "C:\Windows\System32\defrag.exe"
    			$Parms = "C: /h /x"
    			$Prms = $Parms.Split(" ")
    			& "$Command" $Prms
    
    			$Command = "C:\Windows\System32\defrag.exe"
    			$Parms = "C: /h /o"
    			$Prms = $Parms.Split(" ")
    			& "$Command" $Prms
    
    			$Command = "C:\Users\Hali\OneDrive\Programs\SysinternalsSuite\sdelete.exe"
    			$Parms = "C: /z"
    			$Prms = $Parms.Split(" ")
    			& "$Command" $Prms
    		}
    Probably the save restore route with Macrium would be just as good (or better) but it is sometimes easier to run a script I think...
      My Computer


  5. Posts : 15,485
    Windows10
    Thread Starter
       #5

    lx07 said:
    I do this inside the VM before compacting - Defrag to consolidate free space then use sdelete to zero.

    It works much better than just compacting the vhdx.
    Code:
            {    $Command = "C:\Windows\System32\defrag.exe"
                $Parms = "C: /h /x"
                $Prms = $Parms.Split(" ")
                & "$Command" $Prms
    
                $Command = "C:\Windows\System32\defrag.exe"
                $Parms = "C: /h /o"
                $Prms = $Parms.Split(" ")
                & "$Command" $Prms
    
                $Command = "C:\Users\Hali\OneDrive\Programs\SysinternalsSuite\sdelete.exe"
                $Parms = "C: /z"
                $Prms = $Parms.Split(" ")
                & "$Command" $Prms
            }
    Probably the save restore route with Macrium would be just as good (or better) but it is sometimes easier to run a script I think...
    I always avoid defrag as it usually takes so long. Minitool does the same in effect in a fraction of the time.

    What does sdelete do?

    Edit: I have just seen this

    Compacting Client Hyper-V VHDX Files • Helge Klein
      My Computer


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

    cereberus said:
    In hindsight, not sure if step c is needed. More testing needed.
    Please do not take this wrong, I just have to say that your process involves awfully lot unnecessary work!

    To start with, a dynamically expanding VHD can only grow. It never shrinks, that's not what Compact Disk means. Each checkpoint, even when later removed makes VHD bigger, and so on.

    My workflow when I want to free unnecessary space used by my dynamical VHDs:

    • Disk clean-up
    • Create Macrium image of my VM
    • Shut down VM
    • Replace existing VHD with new dynamically expanding one in VM settings
    • Restore Macrium image to new VHD

    Can't understand why you mount VHD in host, can't understand why to save old VHD as .bak when Macrium Image exists.

    Of course I also export all new Hyper-V virtual machines as soon as I have finished installing Windows, when the VHD file size is smallest. If then later I want to start from scratch, from freshly installed Windows, I just delete the VM and import it back.

    Kari
      My Computer


  7. Posts : 5,478
    2004
       #7

    cereberus said:
    I always avoid defrag as it usually takes so long. Minitool does the same in effect in a fraction of the time.

    What does sdelete do?

    Edit: I have just seen this

    Compacting Client Hyper-V VHDX Files • Helge Klein
    sdelete writes zeros to free space. If you do run it note it takes a long time (much longer than defrag) and sits on 100% complete for ages before it finishes. It is tempting to think it isn't working but it is - just need to let it finish.

    I have it as part of a script that compacts the OS, removes apps I don't want, runs disk cleanup and so on. I just let it run after upgrade/clean install. The whole script takes an hour to run I guess but it saves thinking.
      My Computer


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

    op
    Kari said:
    Please do not take this wrong, I just have to say that your process involves awfully lot unnecessary work!

    To start with, a dynamically expanding VHD can only grow. It never shrinks, that's not what Compact Disk means. Each checkpoint, even when later removed makes VHD bigger, and so on.

    My workflow when I want to free unnecessary space used by my dynamical VHDs:

    • Disk clean-up
    • Create Macrium image of my VM
    • Shut down VM
    • Replace existing VHD with new dynamically expanding one in VM settings
    • Restore Macrium image to new VHD

    Can't understand why you mount VHD in host, can't understand why to save old VHD as .bak when Macrium Image exists.

    Of course I also export all new Hyper-V virtual machines as soon as I have finished installing Windows, when the VHD file size is smallest. If then later I want to start from scratch, from freshly installed Windows, I just delete the VM and import it back.

    Kari
    Re. .bak - just cautious - I delete after I verify new vhd works.

    What I found was that exporting vhdx using mrf alone and reimporting to new vhdx did not shrink the vhdx to minimum size possible. The minitool step does that (basically a defrag).

    Sure you can do this inside vm - just found it faster doing in host.
      My Computer


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

    cereberus said:
    What I found was that exporting vhdx using mrf alone and reimporting to new vhdx did not shrink the vhdx to minimum size possible. The minitool step does that (basically a defrag).
    We must have totally different working methods :)

    Let's see what I told to be my workflow, and check how that works in reality. Example W10 x64 VM is one I have used for ESDtoISO and UUPtoISO testing, a clean installed build 15048 with no additional software installed, upgraded several times to builds 15055, 15058, 15060 and 15061, always restored back to clean 15048. This has of course caused the VHD size to grow to multiple times what really is needed.

    Kari said:
    • Disk clean-up
    • Create Macrium image of my VM
    After the above, checking file sizes. Macrium image was made when the original 64 GB dynamically expanding VHD was 34.5 GB:

    Compacting virtual hard disks-image.png

    Macrium image size just under 5 GB:

    Compacting virtual hard disks-image.png

    Kari said:
    • Shut down VM
    • Replace existing VHD with new dynamically expanding one in VM settings
    • Restore Macrium image to new VHD
    After restoring Macrium image to a new dynamically expanding VHD, file size is 9.3 GB. Over two thirds smaller than original one:

    Compacting virtual hard disks-image.png

    The same as seen by File Explorer, yellow highlight is the original VHD, start situation, and green highlight the new VHD where Macrium image from old VHD was restored. Old VHD can now be deleted, net storage save savings over 25 GB in this example case:

    Compacting virtual hard disks-image.png
    (Notice that VHD has already grown over 0.5 GB since the previous screenshot a few minutes earlier. This is because in previous screenshot VM was not started yet after restoring the image.)

    This of course is all logical, happens every time.

    Kari
      My Computer


  10. Posts : 5,478
    2004
       #10

    Just did a similar test. I have a clean install of 10561 with about 300MB of programs installed.

    Before doing anything the VHDX was 10.54GB. I reduced the size of Windows to 6.50GB.
    Ran defrag free space and slab consolidation (took less than 1 minute) and sdelete (took 40 minutes)

    Compact option in Edit disk in Hyper-V reduced the VHDX to 7.38GB. Save and restore using Macrium reduced it to 6.65GB.

    So it seems Macrium is more efficient but IMO more of a faff.

    Code:
    Microsoft Drive Optimizer
    Copyright (c) 2013 Microsoft Corp.
    
    Invoking free space consolidation on (C:)...
    
    
    Pre-Optimization Report:
    
            Volume Information:
                    Volume size                 = 21.99 GB
                    Free space                  = 15.49 GB
                    Total fragmented space      = 35%
                    Largest free space size     = 10.97 GB
    
            Note: File fragments larger than 64MB are not included in the fragmentation statistics.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 21.99 GB
                    Free space                  = 15.49 GB
                    Total fragmented space      = 34%
                    Largest free space size     = 10.98 GB
    
            Note: File fragments larger than 64MB are not included in the fragmentation statistics.
    Microsoft Drive Optimizer
    Copyright (c) 2013 Microsoft Corp.
    
    Invoking slab consolidation on (C:)...
    
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 21.99 GB
                    Free space                  = 15.49 GB
    
            Allocation Units:
                    Slab count                  = 702
                    In-use slabs                = 223
    
            Slab Consolidation:
                    Space efficiency            = 100%
                    Recovered space             = 1.71 GB
    
            Retrim:
                    Total space trimmed         = 4.15 GB
    
    SDelete v2.0 - Secure file delete
    Copyright (C) 1999-2016 Mark Russinovich
    Sysinternals - www.sysinternals.com
    
    SDelete is set for 1 pass.
    Free space cleaned on C:\
    1 drive cleaned.
    
    
    Press Enter to return to menu:
    Before

    Compacting virtual hard disks-before.png

    After running Compact in Edit Disk

    Compacting virtual hard disks-after.png

    After save/restore using Macrium

    Compacting virtual hard disks-macrium-copy.png
      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 04:09.
Find Us




Windows 10 Forums