+6 votes
3.6k views
in Home by (6.6k points)

How to compact VirtualBox VDI files to use less space

1 Answer

+7 votes
by (201k points)
Those who work with VirtualBox we are accustomed to the benefits of virtualizing an operating system inside another, either to learn to use a new system to try "things" we do not want to try in our system if you go wrong, to open files that could contain viruses, or even to test software so old that we need to "simulate" a Windows 2000 virtual to use it. 
 
But something that will teach any novice, and many struggle to find a detailed compact a virtual disk file after much use VDI method. 
 
First we need to know how they work. With VirtualBox you can use two disc formats: static or dynamic. 
 
 
Static assigns all disk capacity from the start, so if we create a static disk 25 GB, we will occupy 25 GB disk real host. The downside is that even if we use less than 1 GB of those 25, the host disk will be 25 GB reserved but not use it. But it is noteworthy that the stator plates have better performance, precisely because the empty sectors are already assigned since its inception. 
 
But if we create a dynamic disk 25GB, start taking a few KB and will be extending as you go filling of files. It is interesting when you do not really know how much space will be consumed and not real and have enough space on the host disk. But the downside is that as these dynamic disks "emulate" the location of the files on a disc can be a little slower as they try to take the minimum necessary space. And the big downside is that when working with virtualized systems that have many moves files (copy, create, and most importantly, eliminate) the virtual vdi disk will increasing its size so that increasingly approaches the actual size to be assigned. Although we delete files, they will never shrink our own, but grow to the size designated. And that situation often undesirable (because otherwise we would use static discs!) Is what we will try to work with this tutorial. 
 
We will follow a short series of steps to thin the VDI disk file and occupying the indispensable according to their content. Let's see: 
 
1) First we need to defragment the virtual disk VDI.
You can use the defragmenter that comes with Windows, but I prefer to use the free Defraggler from Piriform tool, which has an excellent user interface that allows us to know with certainty how the process and even defragment individual files. In the first menu option we will display the option that says "Defragment free space" to gather all the files to the beginning of the virtual hard disk and gives the greatest possible amount of blank space at the end. 
 
We have to use that option or yes because compacting a virtual hard disk (or even if we did a physical disk partition) can only take place "cutting" the void at the end of a disc space. 
 
But to make sure that the vacuum at the end of the disc space is really empty, we go to the second step: 
 
2) nullify the empty space. 
 
As you know, you can recover deleted files from a computer because data are always saved to disk. Even when empty the Windows Recycle Bin, just those files are no longer visible and available to the user, but the data are recorded on the disc. Deleting a file will simply say the disc if you want you can use the space occupied by the file to save others, but Windows tries to avoid stepping deleted files for a while, and whenever there is enough free space, so that we have chances to recover some we have deleted by mistake. 
 
The only way to really clean up that "empty space" on the disc, nullifying that space. This is done with programs that drain the contents of each field marked as free on the drive, eliminating all traces of deleted files. 
 
You can use various programs for this. Piriform CCleaner also has its famous, having an Erase function units, where you select the option "erase free space" and the number of passes (with one reaches us in this case). 
 
But I feel more comfortable with the dear old SDelete, Secure Delete, Mark Russinovich part of the SysInternals suite that sold to Microsoft a few years ago. From there go down a zip full of interesting tools, but what interests us here is the sdelete.exe:
 
sdelete
 
This little program DOS has a designed to nullify free space on virtual disks command. So we just have to execute sdelete z from the same unit that we want to clean, and let it do its work. It may take some if it is a large disk. 
 
If the album had many moves files (copied many, many were removed), may have been not well defragmented in a single pass and therefore have not been all files to the beginning of the disc. One trick that we can use in these cases is to configure Windows not to use virtual page file and restarting the virtual and open the defragmenter will be available at the space used before the pagefile.sys file, that unless we had put a fixed size from the first minute we created the virtual space, it is normal to be highly fragmented, and sometimes quite spread out (especially if the virtual contains versions of Windows other than XP or earlier, who did a very good management paging file). After the free space defrag page file, we give another pass with SDELETE.EXE z, and at the end, continue with the next step of the tutorial. EYE: This paragraph should use it only in cases of small virtual disks, where the free space is very valuable, for example in cases where despite 25-50 GB disk, of which 50% is occupied and the paging file use 2 -3 those GB. In disks with more GB you do not gain much really, unless the host disk is starting to stay guy, or that we are fortunate that the pagefile.sys is happily situated at the end of the disc (typical Windows 98). Note that we're still not back to do create the pagefile.sys file ... 
 
3) Compact the VDI file with Virtualbox. 
 
Virtualbox has in its installation folder an executable that allows us to do a number of things with virtual systems. Now that we have the virtual disk defragmented and the really empty space, we will compact it removing all the empty space at the end. 
 
For this we will open a DOS console in VirtualBox installation folder and we will run the following command: 
 
VBoxManage.exe modifyhd "C: \ Virtual \ the virtual.vdi album" -compact 
 
"C: \ Virtual \ the virtual.vdi disk" must be replaced by the full path to the disk file, and quotation marks if the name contains spaces. 
 
This task can take a while on large disks, so I run it before I go to eat :) 
When finished, we will notice how the VDI file now weighs less than before. You may even notice that the virtual system faster loading now. 
 
Do not forget that if you did step 2 remove the virtual file pagefile.sys, now we have to recreate it for the virtual system loads faster.
 
 

 

Ask a Question
Welcome to WikiTechSolutions where you can ask questions and receive answers from other members of the community.

You can ask a question without registration.

Categories

...