32

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?

5
  • 3
    Virtual TRIM used on a virtual machine interfaced to a virtual disk for virtual feelgood... Commented Sep 17, 2013 at 19:12
  • Ramhound: So what is the "Solid-state drive" checkbox option in the storage submenu for? Beside, if there is an option like --discard mentioned in the manual, then it should be detailed. I completely don't get your point saying that "there is a reason why it's not described". If so, why it is in the manual at all? Commented Sep 19, 2013 at 6:32
  • 12
    To answer @Ramhound my blog is one of the post the OP listed. I'm not sure what his reason was, but for me, I had a Virtual Machine that I needed to physically shrink the filesize of the dynamically allocated disk. It was a disk that had held data that was deleted and I was trying to shrink it back down to a smaller size -- passing the TRIM command enabled that to happen...shrinking my virtual disk from 12G to 7G. To the OP, I hope my post helped you. I got here by seeing incoming traffic on my blog. Commented Jan 7, 2014 at 3:03
  • 1
    Just a warning for anybody interested in the topic. The trimming implementation on the VirtualBox disk image emulator is extremely buggy and will likely crash your vm. There's a 2 years old bug opened for it. It's possible to enable it but don't waste time trying it. Commented May 10, 2019 at 9:15
  • 3
    I think this is the bug Dominik is referring to: virtualbox.org/ticket/16450
    – bobpaul
    Commented Jul 9, 2019 at 16:01

3 Answers 3

23

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

1
  • 1
    Also, using some sort of de-duplication thing on btrfs (particularly one that punches out holes for 0 regions) and btrfs balance really helps with creating as many trimmable regions as possible. Commented Jan 11, 2017 at 23:41
14

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.

6
  • Actually I killed some of my VMs using the trim command. Commented Mar 16, 2018 at 21:05
  • @davidbaumann How did that happen?
    – davidtgq
    Commented May 8, 2018 at 16:08
  • 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 Commented May 8, 2018 at 19:34
  • 3
    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
    – RobM
    Commented Jul 9, 2019 at 16:10
  • 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? Commented May 19, 2021 at 23:55
1

There's historically been an issue in VirtualBox where enabling nonrotational="true" discard="true" in AttachedDevice would lead to freezes/lock-ups during the TRIM operation.

The log would contain things like

AssertLogRel /home/vbox/tinderbox/build-trunk/svn/src/VBox/Devices/Storage/DevAHCI.cpp(4466) bool ahciR3CmdPrepare(PPDMDEVINS, PAHCI, PAHCIPORT, PAHCIREQ): ASMAtomicReadU32(&pAhciPort->cTasksActive) <= AHCI_NR_COMMAND_SLOTS
AHCI#0P0: There are more than 32 (+1) requests activeAssertLogRel /home/vbox/tinderbox/build-trunk/svn/src/VBox/Devices/Storage/DevAHCI.cpp(4466) bool ahciR3CmdPrepare(PPDMDEVINS, PAHCI, PAHCIPORT, PAHCIREQ): ASMAtomicReadU32(&pAhciPort->cTasksActive) <= AHCI_NR_COMMAND_SLOTS
VD#0: Request{0x007fdcbe618200}:
    Type=FLUSH State=CANCELED Id=0x8 SubmitTs=458950321 {3269508} Flags=0x2
    Offset=0 Size=0 Left=0 BufSize=0
VD#0: Request{0x007fdcbe61bc80}:
    Type=DISCARD State=CANCELED Id=0x18 SubmitTs=458980326 {3239503} Flags=0x2
    Offset=0 Size=0 Left=0 BufSize=0

With the only way out is to kill the VM: vboxmanage startvm <name> --type emergencystop

This is still an issue today at least in VirtualBox 7.0.6.

As a temporary workaround, enable host I/O caching (useHostIOCache="true" in StorageController)

        <StorageController useHostIOCache="true" ... >
          <AttachedDevice nonrotational="true" discard="true" ... >
1
  • After enabling the useHostIOCache="true", I got much further to trim my VM storage but once the host cache was filled my whole host OS froze! So use this option with caution.
    – darkdragon
    Commented Nov 5, 2023 at 18:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .