I like to use dynamically allocated images in VirtualBox.

It is preferred way if you like to distribute you image (remember Vagrant?).

What Linux FS can reclaim unused blocks to VirtualBox when dynamically allocated image is used?

I saw that users run:

sudo dd if=/dev/zero of=/EMPTY bs=1M || : ; rm -f /EMPTY

to shrink VDI images.

Also we all know about TRIM ATA command for SSD drives (discard option for mount)...

  • howtogeek.com/312883/… – ivanivan Oct 14 '18 at 15:00
  • @ivanivan Great reading! Though all recommendations are regarding manual maintenance and there is vboxmanage modifymedium disk disk.vdi --compact which makes my wish for automation useless... (( – gavenkoa Oct 14 '18 at 16:07

Official docs state: https://www.virtualbox.org/manual/ch08.html#vboxmanage-storageattach

VBoxManage storageattach <UUID> --nonrotational:

This switch allows to enable the non-rotational flag for virtual hard disks. Some guests (i.e. Windows 7+) treat such disks like SSDs and don't perform disk fragmentation on such media.

VBoxManage storageattach <UUID> --discard:

This switch enables the auto-discard feature for the virtual hard disks. This specifies that a VDI image will be shrunk in response to the trim command from the guest OS. The following requirements must be met:

The disk format must be VDI.

The size of the cleared area must be at least 1MB.

VirtualBox will only trim whole 1MB blocks. The VDIs themselves are organized into 1MB blocks, so this will only work if the space being TRIM-ed is at least a 1MB contiguous block at a 1MB boundary. On Windows, occasional defrag (with "defrag.exe /D"), or under Linux running "btrfs filesystem defrag" as a background cron job may be beneficial.

Notes: the Guest OS must be configured to issue trim command, and typically this means that the guest OS is made to 'see' the disk as an SSD. Ext4 supports -o discard mount flag; OSX probably requires additional settings. Windows ought to automatically detect and support SSDs - at least in versions 7, 8 and 10. Linux exFAT driver (courtesy of Samsung) supports the trim command.

It is unclear whether Microsoft's implementation of exFAT supports this feature, even though that file system was originally designed for flash.

Alternatively, there are ad hoc methods to issue trim, e.g. Linux fstrim command, part of util-linux package. Earlier solutions required a user to zero out unused areas, e.g. using zerofree, and explicitly compact the disk - only possible when the VM is offline.

So storage defined as:

<AttachedDevice discard="true" nonrotational="true" type="HardDisk">

with FS like Ext4 / Btrfs / JFS / XFS / F2FS / VFAT mounted with -o discard should work...

UPDATE TRIM support in VirtualBox is still unstable: https://www.virtualbox.org/ticket/16795

See also https://superuser.com/questions/646559/virtualbox-and-ssds-trim-command-support

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.