VirtualBox and SSD's TRIM command support

--discard options specifies that vdi image will be shrunk in response to trim command from guest OS. Following requirements must be met:

  • disk format must be VDI
  • cleared area must be at least 1MB (size)
  • [probably] cleared area must be cover one or more 1MB blocks (alignment)

Obviously guest OS must be configured to issue trim command, typically that means guest OS is made to think the disk is an SSD. Ext4 supports -o discard mount flag; OSX probably requires additional settings as by default only Apple-supplied SSD's are issued this command. Windows ought to automatically detect and support SSD's at least in versions 7 and 8, I am not clear if detection happens at install or run time. Linux exFAT driver (courtesy of Samsung) supports discard command. It is not clear if Microsoft implementation of exFAT supports same, even though the file system was designed for flash to begin with.

Alternatively there are ad hoc methods to issue trim, e.g. Linux fstrim command, part of util-linux package.

Earlier solutions required user to zero out unused areas, e.g. using zerofree and compact the disk explicitly (I'm assuming that's only possible when vm is offline).


Since this is the top result on Google, let me clarify other answers a bit, even though this is an old post. It is in fact possible to get TRIM working in the sense that unused virtual blocks on the guest filesystem can have the corresponding physical blocks of flash marked as unused for better utilization of the flash. The pieces are even already present in the other answers and comments.

First, the host must be set up so that free space is TRIM'ed. You can either mount the filesystem with -o discard, or you can run fstrim on the filesystem regularly through cron. I prefer the latter, as first option can lead to the system locking up when deleting many files at one time.

The disk format used must be VDI dynamic size as qarma writes.

Make sure that nonrotational="true" discard="true" are set in the .vbox file as described under OP.

Then enable TRIM in the guest OS as normal. In Linux, I again recommend a cron job running fstrim. This is probably even more important here, since the cost of doing TRIM on the virtual disk image is much higher than on a physical SSD, since data is moved around in order to make the image smaller.

Now, since the disk image is regularly compacted, it will only take up the actual space used, plus some 1MB block size overhead as qarma writes. This again means that the free space will be TRIM'ed on the host SSD.