locked
Why is my vhdx taking up so much space? RRS feed

  • Question

  • I have built a CentOs 7 guest VM in Hyper-V as Gen1 with two drives, both dynamically expanding drives.

    I have loaded about 170 GB of data into one of the drives, but on disk on the host, it is taking up 410 GB. What is going on?

    This is in the guest:

    $ df -h | grep 'sdb1\|Used'
    Filesystem                     Size  Used Avail Use% Mounted on
    /dev/sdb1                      504G  168G  311G  36% /var/lib/data

    This is from the host:

        Directory: D:\VHDs


    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----       22/01/2019     08:35   420541890560 Lupin-Data.vhdx


    • Edited by MarkAllison Tuesday, January 22, 2019 8:37 AM
    Tuesday, January 22, 2019 8:37 AM

All replies

  • Hello!

    It might be that you have used more disk space at some point, and now you only use about 170GB of disk space, dynamically expanding virtual disks (VHD/VHDX) will grow as needed, but they will not release any space even if you delete data from the disk.

    If you want to reduce the upper size limit of your VHDX you would need to shrink the VHDX disk.
    To shrink a VHDX you can use the Resize-VHD PowerShell cmdlet.

    Here's a good post about shrinking:
    How to Shrink a Hyper-V Virtual Disk (VHD and VHDX)

    Then there's also the possibility to compact the virtual disk, compacting a virtual disk does not alter the size of the disk though, it reduces the physical disk space that is consumed by your VHDX.

    This can be done by using the Optimize-VHD PowerShell cmdlet, example below:

    Example

    Mount-VHD -Path "D:\Hyper-V\Hyper-V Virtual Disks\Yourdisk.vhdx -ReadOnly
    Optimize-VHD -Path "D:\Hyper-V\Hyper-V Virtual Disks\Yourdisk.vhdx -Mode Full
    Dismount-VHD -Path "D:\Hyper-V\Hyper-V Virtual Disks\Yourdisk.vhdx
    


    You can also do it in the Hyper-V Manager (GUI) by right clicking on Edit Disand then choose your VHDX disk and then choose Compact.

    Best regards,
    Leon


    Blog: https://thesystemcenterblog.com LinkedIn:

    Tuesday, January 22, 2019 9:51 AM
  • Thanks, pretty sure this has never got to the larger size. The data is incrementally loaded in and is never deleted so should be just gradually dynamically expanding.

    Because it's a Linux disk, it seems I can't shrink it, is that correct? If I look at the VHD with Get-VHD:

    ComputerName            : FLUORINE
    Path                    : d:\vhds\lupin-data.vhdx
    VhdFormat               : VHDX
    VhdType                 : Dynamic
    FileSize                : 420541890560
    Size                    : 549755813888
    MinimumSize             : 549754782208
    LogicalSectorSize       : 512
    PhysicalSectorSize      : 4096
    BlockSize               : 33554432
    ParentPath              :
    DiskIdentifier          : 05548F47-F47D-4C60-ADB8-B19ACA0F2841
    FragmentationPercentage : 2
    Alignment               : 1
    Attached                : False
    DiskNumber              :
    Number                  :

    The size = minimumsize which means I can't shrink it.

    Tuesday, January 22, 2019 11:12 AM
  • That’s correct, as you mentioned unfortunately this disk cannot be shrunk as the size is the same as the minimumsize. I’m not too familiar with Linux, but maybe adding a new smaller disk and then performing a disk cloning could work.

    Blog: https://thesystemcenterblog.com LinkedIn:

    Tuesday, January 22, 2019 11:40 AM
  • Thanks, pretty sure this has never got to the larger size. The data is incrementally loaded in and is never deleted so should be just gradually dynamically expanding.

    Because it's a Linux disk, it seems I can't shrink it, is that correct? If I look at the VHD with Get-VHD:

    ComputerName            : FLUORINE
    Path                    : d:\vhds\lupin-data.vhdx
    VhdFormat               : VHDX
    VhdType                 : Dynamic
    FileSize                : 420541890560
    Size                    : 549755813888
    MinimumSize             : 549754782208
    LogicalSectorSize       : 512
    PhysicalSectorSize      : 4096
    BlockSize               : 33554432
    ParentPath              :
    DiskIdentifier          : 05548F47-F47D-4C60-ADB8-B19ACA0F2841
    FragmentationPercentage : 2
    Alignment               : 1
    Attached                : False
    DiskNumber              :
    Number                  :

    The size = minimumsize which means I can't shrink it.

    The "MinimumSize" value means that you cannot SHRINK the MAXIMUM capacity of a dynamically-expanding VHDX. It does not mean that you cannot COMPACT it to reclaim space. The MinimumSize value will always reflect the sum of all space allocated by partitions within the contained file systems. If you create a brand new VHDX @ 60GB, attach it to a VM and format the entire 60GB space but put zero files into it, then the MinimumSize will reflect 60 GB.

    You have two things going on:

    First, Linux relies on sparse files in a way that Windows does not, meaning that it will often cause expansion of a great many blocks that are almost completely empty. I see from your readout that you used the default block size of 32MB, which commonly causes the common filesystems used by Linux to bloat out. Not all Linux filesystems behave that way, so it is possible that your disk naturally expanded.

    Second, you can use a compact operation to reclaim disk space. I do not know of any tool that can predict in advance how much space will be reclaimed from a compact. However, the used percentage readout from a df command should give you a rough idea.

    In your case, you can address both problems in a single pass.

    1. Shut the virtual machine off.
    2. If the VM has any checkpoints, delete them.
    3. Detach the VHDX from the VM (not required, but helps to avoid accidents later).
    4. Make sure you have enough total free space on your VHDX storage location to hold a copy of the VHDX.
    5. In PowerShell, run:
    Convert-VHD -Path 'D:\VHDXs\current-vhdx.vhdx' -DestinationPath 'D:\VHDx\converted-vhdx.vhdx' -BlockSizeBytes 1mb

    It will compact the data in the VHDX while transferring it into a new VHDX that uses a 1 megabyte block size instead of 32 megabytes. When it completes, attach the new VHDX to the VM. You can use it immediately.

    Going forward, you can use Optimize-VHD (sparingly, please) to reclaim space. The VM must be off for that to function.


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent contributor, not an Altaro employee. I accept all responsibility for the content of my posts. You accept all responsibility for any actions that you take based on the content of my posts.

    • Proposed as answer by Zoe Tao Tuesday, January 29, 2019 4:19 AM
    Tuesday, January 22, 2019 5:46 PM
  • Hi,

    Once the VHDX grows it will remain that size, or grow larger as the amount of data inside the VM increases.

    If you want to recover some of the disk space on the host that is being consumed by the VHDX you can compact the VHDX.

    Additionally, you could shrink the Windows volume inside the VM and then shrink the VHDX.

    Best Regards,



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, January 23, 2019 5:41 AM
  • Hi,

     

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

     

    Best Regards,



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, January 28, 2019 1:03 AM
  • Why sparingly? Are there any negative consequences to scheduling a compacting process for vhdx?
    Tuesday, August 4, 2020 10:15 PM