On Ubuntu 12.04 I have VirtualBox 4.1.12 and a Windows 7 guest. I have been working a couple of days on shrinking the VM. The VDI is 55GB, the VM has more than 50% free. I tried several recipes that can be found on the internet. No recipe works however.
The drives are defragmented. I ran the sdelete utility. I used clonehd. I used modifyhd with compact switch. Nothing works. There is no error message. The utility does do something, takes a long time to complete, shows percentages complete along the way...and fails to shrink the VM with a single bit.
Shrinking does not seem to be part of VirtualBox 4.1.12. It used to part of VirtualBox. For example I can find the following in the documentation (which has become obsolete with newer/recent version of VirtualBox it seems).
"Compacting virtual disk images is limited to VDI files. The VBoxManage modifyhd --compact command is currently only implemented for VDI files. At the moment the only way to optimize the size of a virtual disk images in other formats (VMDK, VHD) is to clone the image and then use the cloned image in the VM configuration."
Is there a way to shrink? Documented? Proven?
Shrink VM - can it be done?
-
Perryg
- Site Moderator
- Posts: 34369
- Joined: 6. Sep 2008, 22:55
- Primary OS: Linux other
- VBox Version: OSE self-compiled
- Guest OSses: *NIX
Re: Shrink VM - can it be done?
Compact and shrink are two different things. Compact will if done properly compact the files and cause the content to look smaller because you are freeing up unused parts of the drive that had garbage. shrink is not nor has ever been a part of VirtualBox. You can use gparted to do this but be warned that you are likely going to trash the guest.
Re: Shrink VM - can it be done?
Ok, that is clear. There are many recipes on the internet that describe how to decrease (shrink, compact, I dont mind) the size of the VM disk. I can confirm here that they dont work!
The modifyhd compact is a mysterious function, its purpose unknown. Similar to sdelete utility downloadable from the Microsoft site, or defrag on Windows 7, defrag is always 0% under MS Windows 7.
Anyway I tried a single last time - too much time invested in this already - and got interesting results. I ran defrag in Windows 7 guest, I ran sdelete with c and z switch. I ran modifyhd with compact - no idea what I am doing, you are right.
The VM grew! the vdi was around 55GB with around 22GB in use - I was trying to shrink to 50%! On this attempt the vdi grew to 168GB!!!!!!
So the conclusion must be: if you have a large vdi with lots of free space, you CANNOT DECREASE VDI size. You can however more than TRIPLE the size using modifyhd --compact if that is what you aim to do. Makes sense? I understand now I have been trying to go where no one has gone before. Well, at least with VirtualBox.
The modifyhd compact is a mysterious function, its purpose unknown. Similar to sdelete utility downloadable from the Microsoft site, or defrag on Windows 7, defrag is always 0% under MS Windows 7.
Anyway I tried a single last time - too much time invested in this already - and got interesting results. I ran defrag in Windows 7 guest, I ran sdelete with c and z switch. I ran modifyhd with compact - no idea what I am doing, you are right.
The VM grew! the vdi was around 55GB with around 22GB in use - I was trying to shrink to 50%! On this attempt the vdi grew to 168GB!!!!!!
So the conclusion must be: if you have a large vdi with lots of free space, you CANNOT DECREASE VDI size. You can however more than TRIPLE the size using modifyhd --compact if that is what you aim to do. Makes sense? I understand now I have been trying to go where no one has gone before. Well, at least with VirtualBox.
-
mpack
- Site Moderator
- Posts: 39134
- Joined: 4. Sep 2008, 17:09
- Primary OS: MS Windows 10
- VBox Version: VirtualBox+Oracle ExtPack
- Guest OSses: Mostly XP
Re: Shrink VM - can it be done?
If I was you I would desist until you have learned the rather important difference between shrink and compact, because Perry's comments do not seem to have sunk in at all.
If sdelete followed by compact did not reduce the VDI size then the most likely possibility that occurs to me is that the partition you are "sdeleting" does not fill the drive, perhaps because you already previously shrank the main partition using gparted (itself an exceedingly bad idea, because you now probably have congested disk), not knowing that shrinking a partition will not affect the file size one iota, but will defeat your purpose by putting large chunks of the drive - along with the data which happens to be stored there - beyond the areas managed by the guest filesystem.
Stop implying that there is some issue with VirtualBox, go off and do some research: even a cursory search of this site should have turned at least dozens of previous discussions of this subject, and the best approaches to dealing with the problem.
If sdelete followed by compact did not reduce the VDI size then the most likely possibility that occurs to me is that the partition you are "sdeleting" does not fill the drive, perhaps because you already previously shrank the main partition using gparted (itself an exceedingly bad idea, because you now probably have congested disk), not knowing that shrinking a partition will not affect the file size one iota, but will defeat your purpose by putting large chunks of the drive - along with the data which happens to be stored there - beyond the areas managed by the guest filesystem.
Stop implying that there is some issue with VirtualBox, go off and do some research: even a cursory search of this site should have turned at least dozens of previous discussions of this subject, and the best approaches to dealing with the problem.
Re: Shrink VM - can it be done?
You are right in pointing out that I am not a virtualization scientist. I am more like a dumbfounded end user. Personally I think it would useful if it was at all possible to decrease size of VDI and my cursory ´research´ indicates I am not the only one. Decrease recipes abound, all on personal web logs, none of them work. This is an example https://blogs.oracle.com/virtualbox/ent ... ur_virtual.
Excuse me for implying something wrong with VirtualBox. Maybe VirtualBox can be improved in this particular area?
Maybe you can help me interpret the VirtualBox manual. English is not my native language.
Excuse me for implying something wrong with VirtualBox. Maybe VirtualBox can be improved in this particular area?
Maybe you can help me interpret the VirtualBox manual. English is not my native language.
From reading that, I have to admit, I did not expect that my VDI would triple in size. Is this the result I should expect? Can it be explained? Okay, I have to do even more research to become as enlightened as you are on the compact-growing VDI phenomena to prevent me from asking stupid questions or implying things are wrong with anything/anyone else than me.In addition, the modifyhd --compact command can be used to compact disk images, i.e. remove blocks that only contains zeroes. For this operation to be effective,it is required to zero out free space in the guest system using a suitable software tool.Microsoft provides the sdelete tool for Windows guests. Execute sdelete -c in the guest to zero the free disk space before compressing the virtual disk image. Compaction works both for base images and for diff images created as part of a snapshot.
-
mpack
- Site Moderator
- Posts: 39134
- Joined: 4. Sep 2008, 17:09
- Primary OS: MS Windows 10
- VBox Version: VirtualBox+Oracle ExtPack
- Guest OSses: Mostly XP
Re: Shrink VM - can it be done?
A dynamic disk grows when a sector is written to for the first time. sdelete fills unused spaces with zeros, which will almost certainly result in new sectors being written to and the disk increasing in size: this follows from an understanding of what sdelete does and what a dynamic disk is.
Compaction - at least as done by VBoxManage - discards 1MB VDI blocks which are filled only with zero bytes. Note however that "modifyhd --compact" will probably do little or nothing on its own - the blocks have to be zeroed by sdelete first.
Shrinking - using gparted - is an adjustment of guest filesystem data structures. It has no effect on the disk capacity or the VDI file size. It will however change what parts of the disk can be affected by sdelete.
If you find the sdelete followed by "VBoxManage modifyhd" procedure hard to follow then I would suggest you download CloneVDI from the sticky in "Windows Hosts" (as a Windows app you'll need to install Wine to run it on an Ubuntu host). Use CloneVDI to clone the vdi, making sure you have the "Compact" option selected.
Compaction - at least as done by VBoxManage - discards 1MB VDI blocks which are filled only with zero bytes. Note however that "modifyhd --compact" will probably do little or nothing on its own - the blocks have to be zeroed by sdelete first.
Shrinking - using gparted - is an adjustment of guest filesystem data structures. It has no effect on the disk capacity or the VDI file size. It will however change what parts of the disk can be affected by sdelete.
If you find the sdelete followed by "VBoxManage modifyhd" procedure hard to follow then I would suggest you download CloneVDI from the sticky in "Windows Hosts" (as a Windows app you'll need to install Wine to run it on an Ubuntu host). Use CloneVDI to clone the vdi, making sure you have the "Compact" option selected.