Although TRIM support was introduced to make SSD work better, it should also help virtual machines optimizing virtual disks (for example to know which parts of virtual disks needs no storage on physical partitions). Does any VM machine use TRIM this way?
Virtualbox supports TRIM since version 4.2
- Storage: implemented support for discarding unused image blocks through TRIM for SATA and IDE and UNMAP for SCSI when using VDI images
You'll need to use the
--nonrotational on and
--discard on options:
Enables the auto-discard feature for a 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 1 MB.
- Oracle VM VirtualBox will only trim whole 1 MB blocks. The VDIs themselves are organized into 1 MB blocks, so this will only work if the space being trimmed is at least a 1 MB contiguous block at a 1 MB boundary. On Windows, occasional defragmentation with
defrag.exe /D, or on Linux running
btrfs filesystem defragas a background cron job may be beneficial.
More information can be found in
Hyper-V can also shrink VHD and VHDX images
Consumer VHD mounted on a thinly provisioned SAN:The VHD is initially mounted on one minimum slab of a thinly provisioned environment. As files are stored in the VHD, the storage footprint of the VHD grows in multiples of slabs. When files are removed in the VHD, the Hyper-V calls File_TRIM to the underlying thinly provisioned SAN. If the TRIMs are bigger than the SLAB granularity, the SAN can now remove a SLAB and hence reduce the footprint of the VHD on that SAN.
If the VHD is resident on a Windows 8 based server, the Storage Optimizer will also send TRIMs to reduce the slab footprint of the VHD from within the mounted VHD.
New API allows apps to send "TRIM and Unmap" hints to storage media
- Thin Provisioning - Storage Space Reclamation Using the UNMAP Command
- Windows 8 and server 2012 support TRIM for the new VHDX image format
- Windows Server 2012 VHDX Thin Provisioning Benefits Explored
- TRIM/UNMAP Support in Windows Server 2012 & Hyper-V/VHDX
- Monitor the UNMAP/TRIM effect on a thin provisioned SAN
VMware Workstation 10 claims to support SSD passthrough
- SSD Pass-through Windows 8 is capable of detecting when it is being run from a solid state drive (SSD) and optimizes itself for this hardware. In Workstation 10, the guest operating system will be able to detect when the virtual machine Disk file is being stored on an SSD drive and the operating system can make the same optimizations when it is running in a virtual machine
That's also mentioned in VMWare Workstation Pro Documentation Center's Disk Drive Requirements for Host Systems - Solid-State Drives:
If your host machine has a physical solid-state drive (SSD), the host informs guest operating systems they are running on an SSD.
This allows the guest operating systems to optimize behavior. How the virtual machines recognize SSD and use this information depends on the guest operating system and the disk type of the virtual disk (SCSI, SATA, or IDE).
But from the text it looks like the TRIM command is only passed to the underlying physical SSD instead of shrinking the VMDK image
Other VMware products like vSphere and ESXi also support TRIM, but it's still unclear whether they utilize it to shrink the image or not
Q. Does VMware support UNMAPs directly from the Guest OS in vSphere 6.0.
Yes. The issuing UNMAPs directly from the Guest OS to reclaim space is supported in vSphere 6.0.
Q. What are the prerequisites required for UNMAP to run?
There are a number of prerequisites which must be met. These are:
- VMDK must be thin provisioned
- Virtual Machine Hardware version must be 11 (ESXi 6.0)
- The advanced setting EnableBlockDelete must be set to 1
- The Guest OS must be able to identify the disk as thin (Windows 2012 [updated 30-Oct-2015] uses the B2 mode page to achieve this)
Q. Does reclaiming only work for UNMAP? What about TRIM?
This is an “it depends” answer. TRIM is only used when the underlying device is an SSD – Solid State Disk. From what I understand, the TRIM gets converted to UNMAP in the I/O stack. However, there are some issues with TRIM getting converted into UNMAP. UNMAP work at certain block boundaries on VMFS, whereas TRIM do not have such restrictions. So, certain TRIMs converted into UNMAPs may fail due to block alignment issues.
- Using SSDs with ESXi
- Direct Guest OS UNMAP in vSphere 6.0
- Does ESXi support TRIM on SSDs and does it function?
QEMU and KVM also support the
discard=unmap option to reclaim disk image space
In order to shrink the *.qcow2 files you've two options, enable TRIM support or zero out all free space of the partitions contained within the guest and then reconvert the image with qemu-img.
TRIM support requires intimate knowledge of the underlying hardware. Since Virtual machines (VM Guests) by definition run on virtual hardware, they are not aware of the details of the hard drive or SSD on which they reside.
So, if the VM Host operating system understands TRIM, that should be sufficient for all VM Guests running on top of it.
Some VM Hosts can "pass through" a physical drive and mount it directly to the operating system on a VM Guest. Depending on the exact implementation, it is theoretically possible that the Guest could then support the TRIM feature of an SSD. As with most things, your mileage may vary, and if it's critical for your environment you should test it thoroughly before you commit.
1The only thing I would add is if the vm hard drive file is dynamic, you could shrink it and the host would then, if appropriate, tell the SSD to trim the newly freed sectors. This may speed vm hard drive file writes because when the file grows your not modifying existing host sectors, new/trimmed sectors could be used for the guest's write. Normally I fix size on my vm hard drive files, but that is to prevent fragmentation on HDs. With SSDs I would probably go dynamic and periodic shrink. Oct 1, 2010 at 5:32
3-1. You're missing the fact that TRIM/unmap is used to shrink the dynamically sized VM image file. And as a result, the host OS may issue TRIM/unmap on the underlying hardware. It's an important part of the thin provisioning concept.– rustyxMar 7, 2017 at 21:21
TRIM was invented because writes to an SSD must be done in 512K blocks and detmining how much of a block is actually used by a file or files is impossible for the SSD drive itself and must be communicated by the OS and the filesystem.
The wiki link says it better than I could:
The TRIM command does not work with disks which are stored in disk image files. This is caused by the fact that computer files can only be deleted completely or truncated at the end. This problem affects applications like virtual disk services, virtual machines, etc.
7VM could keep information about which blocks were trimmed to be used when user request to repack and shrink virtual disks. VMWare currently has a similar operation, but it works by overwriting all free space inside VM firstly.– lioriSep 29, 2010 at 9:05
7The question is not about translating a TRIM operation on the virtual disk to a TRIM operation on the host disk. As you point out, it does not work so directly. The question is clearly about translating a TRIM call on the virtual disk to a shortening of the image file on the host disk (which, incidentally, can in turn trigger a TRIM operation on the host disk, but the question is about the first step) Feb 3, 2015 at 10:41
2There are spares files, which should be ideal to be used as a trim-able disk image for a vm. Nov 27, 2015 at 15:36