Page 1 of 1

Resizing a VDI

Posted: 2. Dec 2009, 01:09
by andygoldberg
I have made a 120GB XP Guest OS dynamic VDI that takes up 2.3GB on my hard drive. I repartition the VDI to 12GB, using GParted, and now the VDI takes up 9.9GB. I tried running sdelete and then using modifyhd --compact, but that caused the VDI to go to 11.6GB instead. When I run the VM it still shows only 3.0GB used out of 12GB. The other thing is that VBox still states that the VDI is 120GB.

Is there a way to tell VBox to resize the VDI to a smaller size? What would be a better way to reduce the size of a VDI, both partition and VDI, from 120GB to 12GB? Is there a way to clone a guest OS from a large VDI to a smaller VDI?

My end goal is to get the VM to be as small as possible on the HDD.

Thanks for the help!

Re: Resizing a VDI

Posted: 2. Dec 2009, 04:17
by MarkCranness
Be aware that the sdelete options are backwards: -C Zeros and -Z securely Cleans. If you used -Z then you have filled your VDI with random bytes which explains its new size.
To get your VDI 100% the smallest it should be will require imaging it and restoring to another VDI, the same as you would image a physical drive to another physical drive.
BUT easier and almost as good would be this:
- Repartition it back to max=120GB,
- Defrag (multiple times if needed) it to get all the files contigious, then either:
- CloneVDI with 'Compact drive while copying' set, OR
- Run sdelete -C, then modifyhd --compact (I would use CloneVDI myself, see the sticky in the Windows hosts forum)
- Repartition back to 12GB.

It is possible that CloneVDI will zero out (not copy) the non-partitioned part of the VDI, in which case the first and last repartition steps would not be needed. mpack?

Likely the growth when you repartitioned was that GParted had to move files down out of the >12GB are into the <12GB area so it could then resize the partition. This left used/non-zero (as far as VirtualBox knows) data out beyond the >12GB area, namely the old file data that was there.

To keep it small: periodically defrag followed by CloneVDI and/or consider these good suggestions by Donut:
How To: minimise the amount of writes in a Windows guest

Re: Resizing a VDI

Posted: 2. Dec 2009, 11:06
by Sasquatch
An easier method is to use the CloneVDI tool from mpack that you can grab from the sticky in Windows Hosts. Please read the same sticky in Linux Hosts too.

Re: Resizing a VDI

Posted: 2. Dec 2009, 12:59
by mpack
MarkCranness wrote:It is possible that CloneVDI will zero out (not copy) the non-partitioned part of the VDI, in which case the first and last repartition steps would not be needed. mpack?
No, CloneVDI can only detect unused blocks from a recognized filesystem. It doesn't have any way to be sure that data outside any recognized partition is unwanted (think GPT).

IMHO the OP is confusing the issues of reducing virtual drive size vs reducing actual VDI file size. I would go with Mark's suggestion of using gparted to expand the partition back to the full size (120GB) - now almost the entire drive falls under the control of a recognized filesystem, in which case CloneVDI (with compact option) will be able to detect the unused blocks.

Re: Resizing a VDI

Posted: 2. Dec 2009, 13:01
by Sasquatch
But, the file used to be just 3 GB, which after the partition shrinking, became 12 GB. That 12 GB is the size of the partition, so your tool will work either way. The used blocks are inside the partition, not outside. That's what I understand anyway.

Re: Resizing a VDI

Posted: 2. Dec 2009, 13:31
by mpack
Sasquatch wrote:But, the file used to be just 3 GB, which after the partition shrinking, became 12 GB. That 12 GB is the size of the partition, so your tool will work either way. The used blocks are inside the partition, not outside. That's what I understand anyway.
From VirtualBox's point of view, a "used block" is any 1MB block which has been written to in the past, and (in the context of shrinking) which contains something other than zeros. The used block does not have to fall inside a partition currently used by the guest.

So if you have a large 120GB VDI, and you attempt to shrink it by (lets say) copying all the data in the second half of the drive into previously unused blocks in the first half, then from VBox's point of view the number of used blocks has increased (the used blocks in the second half of the drive are still used, in addition to which you now also have more used blocks in the first half). Shrinking the partition will not eliminate the used blocks in the second half, neither (after shrinking the partition) will running sdelete/zerofree, neither will running CloneVDI with compact - all of the latter only work inside a recognized filesystem, not on unallocated regions of the drive.

Re: Resizing a VDI

Posted: 2. Dec 2009, 13:40
by Sasquatch
That makes sense.

Re: Resizing a VDI

Posted: 3. Dec 2009, 04:12
by andygoldberg
Thanks for all the suggestions. I found a potential fix. What I have done is create a blank 12GB VDI and linked that VDI and the 120GB VDI to the VM. I then ran Ghost on a boot image and ghosted the 120GB to the 12GB. The VDI went from 12GB to 1.5GB! I have several of these VDI's to do but it will be worth it.

Thanks!

Re: Resizing a VDI

Posted: 3. Dec 2009, 13:33
by mpack
andygoldberg wrote:I found a potential fix. What I have done is create a blank 12GB VDI and linked that VDI and the 120GB VDI to the VM. I then ran Ghost on a boot image and ghosted the 120GB to the 12GB. The VDI went from 12GB to 1.5GB! I have several of these VDI's to do but it will be worth it.
No, it isn't worth it. You have discovered a very longwinded way of doing what CloneVDI (with the compact option enabled) can do in a single step. Shrinking the VDI size was completely unnecessary, the important step was finding a way to detect and discard unused clusters, which I assume Ghost does (certainly Acronis does).

Re: Resizing a VDI

Posted: 3. Dec 2009, 13:39
by Sasquatch
Ghost and Acronis (the latter I'm sure of) have an option to copy the data, or do a sector by sector copy. The prior will result in a faster and smaller clone, the latter might be useful when going to the same drive and you need hidden partitions and all with it.