익명 07:04

How to keep VirtualBox virtual disks compact without completely rewriting them?

How to keep VirtualBox virtual disks compact without completely rewriting them?

I'd like to keep virtual disk images small even after removing large files inside the guest and I want to avoid complete image rewrites because they reduce my SSD’s lifetime.

I am using:

  • VirtualBox 7.1.12
  • Guests: Linux/ext4 as well as Windows 2000, Windows XP, Windows 7, Windows 10/NTFS.
  • Host: Currently Windows 10, moving to Linux soon.

I currently only see three possibilities to regain freed virtual disk space to the host:

  1. Use a dynamically growing VDI and enable auto-discard (VBoxManage storageattach --discard=on).
  1. Use a dynamically growing VDI and compact it manually (VBoxManage modifymedium --compact).
  2. Use a dynamically growing VHD and compact it manually (diskpart.exe compact vdisk).

How will VBoxManage (or MS diskpart.exe on Windows 10 hosts) handle compacts? It looks like it is always re-writing the complete image, but I cannot find exact documentation. Is it possible to let it punch holes into the image file so it will then be sparse?

In addition, for #2 and #3 I need to zerofill free space first before compacting the disk. So in this case, additional physical writes are also needed prior compacting. Is there a possibility to avoid these zero writes?

Solution #1 looks feasable, but --discard=on is marked as "experimental" in the VBox Manual, and again, I can't find documentation how VirtualBox performs compacting the disk image in this case.

Any ideas to these questions - any recommendations?



Top Answer/Comment:

The Virtualbox manual says for the --compact option that you do need to zero-fill the disk. That will mean more writes to the host SSD, which is not what you want.

The --discard=on option requires the guest OS to issue a "trim" command for its virtual SSD. The manual also does say --discard is experimental.

So none of these options will get you a reliable no-host-writes storage shrink.

The trick then is to keep your virtual disk images small when you make them at the beginning. For disk images that are big now, allow one more host SSD write session and zero-fill/compact the disk, then shrink the big data-filling partition inside the VM's OS. The VM OS won't write to a place on its disk that the VM disk partitions aren't set to take up. (Example: a 100GB virtual disk with a partition inside set to 50GB and the rest not partitioned will only get to 50GB size + the Virtualbox header.)

One note: --compact does alter the VM's disk file. A host PC glitch while this is happening will quite likely destroy the VM's disk. It is best to have backups of the VM (copy the whole VM folder and any VM disk files residing outside the VM folder) before --compacting.

상단 광고의 [X] 버튼을 누르면 내용이 보입니다