How to shrink a dynamically expanding VHD after remove content from vhd?

Hello!

    I have a VM with SO Ubuntu, this VM have one 200GB dynamically expanding VHD.  At a given time, I copied a backup files to this dynamically expanding VHD and reached maximum size of the dynamically expanding VHD (200GB). Now, I delete the backup files, the content size from VHD is 20GB, but the size of VHD in the parent partition continues with the maximum size (200 GB).

Can I shrink the size from VHD ?

I am sorry for my bad English. :D

Thank you!

March 16th, 2012 4:20pm

Hi, 

Yes you can shrink it. Make sure VM in off condition, then go to Settings of the VM. Point to Hard Drive > Edit > and there's a Compact option.

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2012 4:40pm

You can compact the VHD file to a smaller size.

http://social.technet.microsoft.com/wiki/contents/articles/hyper-v-how-to-shrink-a-vhd-file.aspx

http://linhost.info/2011/01/windows-how-to-compact-a-dynamic-vhd/

March 16th, 2012 4:42pm

When you compact a VHD, the process actually removes references to "sectors" that are zeroed.  A dynamic VHD contains a "Block Allocation Table" and a series of "Data Blocks".  In this context, a block means an extent withing the VHD file; the size of which in 2008 RTM was 512K and in SP2/R2 is 2M.  The block allocation table tells you how many "blocks" make up the dynamic VHD and where they are.  Now the term "Data Block" can be a bit misleading.  A Data Block starts with a 512 byte bitmap followed by the actual extent.  The purpose of the bitmap is to track the "sectors" inside the extent.  A "1" means the sector contains valid data, and a "0" means the sector contains all zeros and has never been written to.

Compact simply reads the Block allocation table to find the data blocks, then for each data block reads the bitmap to determine which "sectors" have and which have not been used.  In your case, you wrote a bunch of data to the Dynamic VHD.  This caused the bits in the bitmap to flip from zero to 1.  Even though you deleted those files from your linux file system in the partition inside the VHD, the VHD driver has no way to know that.  All it knows it that a prticular sector at one time held data.  This is true independed of what the file system is inside the VHD. 

If you read http://social.technet.microsoft.com/wiki/contents/articles/hyper-v-how-to-shrink-a-vhd-file.aspx  you'll notice a lot of extra hoops to go through before compacting a dynamic VHD; things like, in the ntfs case running defrag, or in a more general case running a utility that zeros out unused sectors (as recommended in http://technet.microsoft.com/en-us/library/cc708394(v=WS.10).aspx ).  Within your Ubuntu guest, I would imagine you could you the secure delete utilities.. something like sfill -z to fill all the free space on the volume with zeros before you take it offline and compact the VHD...

 
Free Windows Admin Tool Kit Click here and download it now
March 17th, 2012 4:30am

For Ubuntu in hyper V, there couple of options you will need to take.

1.First fill all the empty space with zeros. Check this blog http://blog.osmicro.org/compact-vhd-size-linux-vm-microsoft-hyper-v/ . Continuously run df -h to check the size. Note, when this command Is done, it will display there is no more space.

2.Second you need to run the optimize command after step #1 is done. use the full mode.

https://technet.microsoft.com/en-us/library/hh848458.aspx

Cheers!

  • Proposed as answer by mykirii 3 hours 57 minutes ago
March 6th, 2015 11:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics