VirtualBox and SSD's TRIM command support

20,422

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.

Share:
20,422
Krzysztof Szynter
Author by

Krzysztof Szynter

Updated on September 18, 2022

Comments

  • Krzysztof Szynter
    Krzysztof Szynter over 1 year

    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?

  • davidbaumann
    davidbaumann about 6 years
    Actually I killed some of my VMs using the trim command.
  • davidtgq
    davidtgq about 6 years
    @davidbaumann How did that happen?
  • davidbaumann
    davidbaumann about 6 years
    Actually, after enabling it, it started to trim about 20GB. Exactly at this moment, the Laptop crashed (had some problems with my GPU these days). forums.virtualbox.org/viewtopic.php?f=2&t=75308
  • RobM
    RobM almost 5 years
    Command to do this to the existing first disk (assuming SATA): VBoxManage storageattach $VM --storagectl "SATA Controller" --port 0 --device 0 --nonrotational on --discard on
  • xpusostomos
    xpusostomos almost 3 years
    The Virtual Box virtual disk is "regularly compacted"? Are you saying it will dynamically move stuff around and shrink the file when there is free space?
  • hwertz
    hwertz over 2 years
    Re: xpuostomos's question, this is both the upside and downside of the discard option. I tried it -- it works as advertised, you turn on discard option and if you delete some files and either run fstrim or have filesystem mounted with discard flag, you end up with a smaller VDI, roughly the same amount as if you zero'ed out the free space then compacted the VDI. But that's literally what it does, so it's extremely slow; I either had I/O timeouts or (if I worked around that) 30+ second delays when I deleted files or ran fstrim.