Can't get a vhd/vdi file to shrink

Discussions related to using VirtualBox on Windows hosts.
Post Reply
hangerglide
Posts: 183
Joined: 29. Mar 2009, 00:53
Primary OS: MS Windows 7
VBox Version: OSE Debian
Guest OSses: ubuntu 8.10

Can't get a vhd/vdi file to shrink

Post by hangerglide »

I have a vm from a few years ago that was originally created from a physical pc that had a 1 tb hdd. It was a VHD of about 90 gb in size. I realized that inside the vm it was mostly unused disk space so I wanted to see what I could do to reduce the VHD file size.

Inside the VHD, which is windows 2008, I deleted more unneeded files. I removed one partition and resized the remaining partitions, C is now 60 gb and D is 25 gb, both half used.

But the VHD file size grew to 108gb. I then tried cloning the VHD as both vdi and vmdk, dynamic disk size selected, both came out about the same 100gb size. I have tried the --compact on the vdi, it didn't seem to shrink.

Can anyone tell me what I am not doing correctly? Host OS is windows 7 pro, and I always use the current vbox, which just changed to Version 5.1.30 r118389 (Qt5.6.2). Are the VHD or VDI supposed to maintain the needed size automatically, since they were created as dynamically allocated? If so is the size on disk just something I have to live with?

One aspect of how the vm sees itself that might contribute is that in windows 2008's computer management, it still shows the "Disk0" as a 930 GB hdd, with 850 gb unallocated. I am not sure how to change that - the only thing I can think of offhand is to attach a new vdi or other virtual hdd and clone the partitions that are in use to it, then boot from the new virtual disk. If that works and if it's smaller I could delete the vdh. If that is a way forward, what disk cloning tool can I use inside of a vbox machine?
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Can't get a vhd/vdi file to shrink

Post by socratis »

hangerglide wrote:I have tried the --compact on the vdi, it didn't seem to shrink.
Because you didn't "free" or "zero-out" the previously used sectors. When an OS writes to a disk it fills sectors with data. Later on when that data gets deleted/moved, the OS does not zero-out the data in the sector, it just marks the sector as available. The "--compact" option looks at sectors that have zero data. So, you got to tell your OS to zero-out the available sectors, in order for the "--compact" to work. Take a look at ch. 8.23 VBoxManage modifymedium and the "--compact" option paragraph to see how you can do it in your guest.
hangerglide wrote:it still shows the "Disk0" as a 930 GB hdd, with 850 gb unallocated. I am not sure how to change that
Run a defragmentation utility from within your guest. Zero-out the data. Open Disk Manager and shrink your partition. Then run "VBoxManage modifymedium <YourVDI> --compact". The partition that you left empty will not be used again, and your data will live only in the remaining, shrinked partition.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
hangerglide
Posts: 183
Joined: 29. Mar 2009, 00:53
Primary OS: MS Windows 7
VBox Version: OSE Debian
Guest OSses: ubuntu 8.10

Re: Can't get a vhd/vdi file to shrink

Post by hangerglide »

Just to be clear, having deleted the partition, so that most of the supposed 1 tb hdd is 'unallocated', still retains the previously used sectors?
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Can't get a vhd/vdi file to shrink

Post by mpack »

hangerglide wrote:... still retains the previously used sectors?
Yes, naturally so. Just as a physical drive would. Plus it's also impossible to clear out using OS tools after the partition has been reduced.

I wouldn't bother with "zero out" tools, which can't reach unpartitioned space anyway. Use CloneVDI with "compact" enabled and these problems don't arise. Hopefully you've updated your copy of CloneVDI since 2009!
hangerglide
Posts: 183
Joined: 29. Mar 2009, 00:53
Primary OS: MS Windows 7
VBox Version: OSE Debian
Guest OSses: ubuntu 8.10

Re: Can't get a vhd/vdi file to shrink

Post by hangerglide »

Thanks to both of you...you have both been incredibly helpful helping me get a handle on vbox. I used clonevdi, worked well for this purpose.
Post Reply