Mac – VirtualBox and SSD’s TRIM command support

ssdtrimvirtual machinevirtualbox

I am aware of the huge number of posts on the internet saying that this would not work and why and I really spent days looking for the solutions months ago but I've found yesterday some tips how to "enable TRIM command support" for guest machines. I've tried it and "it looks" like working. What I would like to know is where's the catch or is this really working as it should.

Sources:
https://forums.virtualbox.org/viewtopic.php?f=7&t=51768
http://jaysonrowe.blogspot.com/2013/08/compacting-virtualbox-vdi.html

My exact command attaching the disk file:

VBoxManage storageattach "GuestOsMachineName" –storagectl "SATA"
–port 1 –device 0 –nonrotational on –discard on –medium "C:\path\to\file.vdi" –type hdd

Which genereted this entry in the machine's *.vbox file:

<AttachedDevice nonrotational="true" discard="true" type="HardDisk" port="1" device="0">
    <Image uuid="{3836a042-a83e-4000-9a59-e95ad65162ce}"/>
</AttachedDevice>

To be sure I would not lose any data this drive was the second one attached to the machine. I've made simple test like copying some file to the drive, leaving it, restarting the machine, shutting down the machine, checking if it's there after booting back, looking at the disk file usage in the host OS. Results are:

  • disk file attached without options –nonrotational and –discard keeps its (dynamic) size even after deleting files in the guest OS
  • disk file attached with both options mentioned above releases the space after the data was deleted

Now here are my questions:
– what does exactly –discard option do? it's not described in the VirtualBox manual
(http://www.virtualbox.org/manual/ch08.html#vboxmanage-storageattach)
– is it really passing TRIM down to the host OS or does it just look like?

Best Answer

--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).

Related Question