1

I have seen it with both VBox native VDI format and VMDK format. Even though I delete files from virtual drive, I see it always keeps growing. I have some vague idea that file zero-ing doesn't happen with the virtual drives from this link. When I tried to do that with SDelete (as said in the link) it didn't reduce the VDi disk size. I have applications which once installation grows the disk to 10GB each time. I believe there should be some simple way to zero it than going through the tedious process, is there any? Is there any option in VirtualBox to keep virtual drive small?

  • after zeroing the clusters you have to run vboxmanage --compact to remove those zero clusters out of the vdi file – phuclv Jun 9 '17 at 3:31
  • Does Vbox reuse the zero-ed clusters at any point after consuming it? Should I have to do that manually? Why VBox doesn't do that automatically as windows or mac does on the hdd? – Ajayvignesh Jun 9 '17 at 3:39
  • 2
    Sparce image files—which is what VirtualBox disk images are—start off with only the bare minimum needed for the system and grow as needed and by use. So let’s say a clean VirtualBox setup starts with an 8GB disk, but only 1GB is used, then the file size would be 1GB. Add a 2GB pile of files to that 1GB system and now it’s 3GB. But remove the 2GB and you still will have the file size of the disk image itself be 3GB. – JakeGould Jun 9 '17 at 4:06
  • because harddrives are fixed-sized. If you created a fixed-sized vdi you also don't need to shrink the image, but then it cost you a lot of disk space. Do you want a 1TB file for an empty 1TB drive? Normally vdi and vmdk files are dynamically resized so that if you don't store anything in the disk you don't have to waste space for that. It just stores non-zero clusters – phuclv Jun 9 '17 at 4:33
  • theoretically it's possible to use SSD TRIM commands to automatically compact the removed blocks although it seems none of the current hypervisors implement that – phuclv Jun 9 '17 at 5:33
1

When creating a new virtual machine, you are prompted to create a Virtual Hard Disk. You have two options for the disk, fixed or dynamically allocated. These are the descriptions (from virtual box) of both types of disks.

Fixed: A fixed hard disk file may take longer to create on some systems but is often faster to use

Dynamically allocated: A dynamically allocated hard disk file will only use space on your physical hard disk as it fills up (up to a maximum fixed size), although it will not shrink again automatically when space on it is freed

Chance are, you set it to dynamically allocated so the disk does not shrink again automatically when space on it is freed.

  • Thanks for this info. I have created new VM in fixed .vdi file. However, creating snapshots and working on the snapshots still keeps growing the snapshot size (eventually causing the VM size to keep growing beyond my fixed disk size) – Ajayvignesh Jun 10 '17 at 1:54
1

Here is a batch script I use to compact all the VDI files:

@echo off
for /d %%d in (*.*) do (
    cd "%%d"
    for %%f in (*.vdi) do (
        echo Compacting %%f . . .
        echo Initial size:      %%~zf
        "C:\Program Files\VirtualBox\VBoxManage.exe" modifymedium --compact disk "%%f"
        echo Post compact size: %%~zf
        echo.
    )
    cd ..
)

Just save it in the folder containing the Virtual Machines and run...

  • O wow, this is neat! I didn't know this functionality existed – DeathByTensors Jun 9 '17 at 16:40
  • Should I do SDelete before running this batch file? I see there is no difference between Initial Size and Post Compact Size after running this batch. – Ajayvignesh Jun 10 '17 at 1:51
  • @Ajayvignesh you must run sdelete or any tools like zerofree to zero the free blocks, because virtualbox/vmware work in raw blocks and have no idea about the underlying filesystem. Hence if it sees a non-zero block, it thinks that the block is has some data and cannot remove it from the file – phuclv Jun 10 '17 at 6:12
0

Deleting files from a virtual drive does not reduce it's size for performance reasons.

When a file is deleted it leaves a hole where the files data previously resided. When new files are created or data is written to the disk it will fill in these holes. The file will grow when necessary to accommodate more data but it will never shrink.

Why?

Removing data from a file anywhere but the end is a time consuming operation. Data has to be moved to fill the holes and in the case of a virtual drive many internal references to data locations will need to be updated. Even with an SSD a deletion of even a small file could take minutes. This is clearly not acceptable.

You need to use vboxmanage --compact to remove the holes and shrink the virtual drive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.