Virtual machines and TRIM
Solution 1:
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:
--discard
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 runningbtrfs filesystem defrag
as a background cron job may be beneficial.
More information can be found in
- VirtualBox and SSD's TRIM command support
- Compacting a VirtualBox VDI
Hyper-V can also shrink VHD and VHDX images
Use cases
- ...
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
See also
- 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
VMware Workstation 10.0 Release Notes
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.
vSphere 6.0 Storage Features Part 8: VAAI UNMAP changes
See also
- 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.
Shrink Qcow2 Disk Files
- Recover Space From VM Disk Images By Using Discard/FSTRIM
- Automatically shrink your vm disk images when you delete files
- Enable Trim on QEMU disk in MacOS/OSX
- Reclaim unused space on a virtual machine on a sparse vm
Solution 2:
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.