trim – How to keep VirtualBox virtual disks compact without complete 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?

Read more here: Source link