Being previously a VirtualBox user, I moved to KVM(QEMU/libvirt or whatever it's called) recently. I know that in VirtualBox, when you add a new storage device to the VM, there is a checkbox labeled Solid-state Drive, if checked, the guest system will see the virtual disk as a solid-state device. This is very useful as it can inform the guest system (i.e. Windows) to disable defragment. I wonder whether this is possible in KVM or not, because I couldn't find anything related on the internet?


You do not need to care about defragmentation in windows, because if belive to some sources defragmentation in Windows don't fully switching off at SSD drives:

If you disable defragmentation completely, you are taking a risk that your filesystem metadata could reach maximum fragmentation and get you potentially in trouble.

What really can be important is enable TRIM support inside VM. You can achive this with virtio-scsi that support TRIM command.

Change your VM configuration to set up controller model to virtio-scsi:

<controller type='scsi' index='0' model='virtio-scsi'>

And change each disk to enable unmap option:

<disk type='file' device='disk'>
 <driver name='qemu' type='qcow2' discard='unmap'/>
| improve this answer | |
  • Thanks for the info. However, I think it is still very important to inform the guest OS of the fact that the storage is backed by a solid-state drive, so that it can optimize for it. Right now my virtual storage is still recognized as "hard disk drive" in the Optimizing Drives tool in Windows, even after applying the changes you suggested (enabling TRIM support). And I feel so uncomfortable that Windows may run defragment as frequently as it does on a rotational storage. – zypA13510 Oct 3 '17 at 13:39
  • most likely it will not possible with KVM or i don't know how. Possibly you care about cell rewrites counts of your ssd drive? If yes, then you don't care about it because it is not a problem with modern SSD which work like traditional HHD in many aspects – Egor Vasilyev Oct 4 '17 at 6:36
  • @zypA13510 Make sure you are using the new virtio-scsi driver, not the old virtio driver. If you've set everything up properly then Windows will see the drive as a Thin provisioned drive and not attempt to do any defragmentation at all; it will only trim (unmap) free space. – Michael Hampton Dec 8 '17 at 3:11

There is no mechanism in QEMU today to mark a SCSI, ATA or VirtioBlk disk as being backed by non-rotational storage. So the guest OS wouldn't do the optimal settings out of the box. As a workaround, for Linux you can override this by unsetting the 'rotational' flag for the block device in sysfs for any disks backed by SSD. I'm talking to people to find out if it is possible to add this feature to QEMU to make it 'just work' in future releases.

| improve this answer | |
  • I wonder why is this feature not implemented in QEMU? (I searched the changelog of VirtualBox, it was implemented since VirtualBox 4.1.0 released 2011-07-19.) Is this a design choice, or due to some technical difficulties? Because copying pieces of code from another open-sourced alternative shouldn't take this long, I think. – zypA13510 Oct 3 '17 at 14:00
  • 2
    I posted patches to report rotational speed, which is an indirect way of indicating a disk is SSD lists.gnu.org/archive/html/qemu-devel/2017-10/msg00698.html – DanielB Oct 4 '17 at 12:11

You don't need to do anything at all with current versions of qemu. If the virtual disk is a QCOW2 image, or is on thin provisioned media such as SAN storage or a sparse LVM or ZFS volume, then it will be presented appropriately as a thin provisioned volume to Windows.

This disk is on a QCOW2 disk image on local SSD storage.

Thin provisioned volume

This will cause Windows to only issue TRIM commands, and not to attempt to defragment the drive.

Clicking Optimize results in:

Optimize trims the disk

| improve this answer | |

To add emulated SSD disk in KVM/libvirt, follow the below steps:

1) Simply add a drive to your guest as you normally would, on the bus you want to use (for example, SCSI or SATA). In my test case, I created SATA disk.

ie: After adding the disk, the xml file would be like this:

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/home/VM-images/nvme-ssd1.qcow2'/>
  <target dev='sdb' bus='scsi'/>
  <address type='drive' controller='0' bus='0' target='0' unit='1'/>

2) Then, start the machine/domain and determine the exact name for your drive by querying the guest with virsh qemu-monitor-command:

# virsh qemu-monitor-command <domain>  --hmp "info qtree" | grep sata
          dev: ide-hd, id "sata0-0-1"  <<===
            drive = "drive-sata0-0-1"
          dev: ide-cd, id "sata0-0-0"
            drive = "drive-sata0-0-0"

3) Then, add the required set command to set rotational speed to make it an SSD (note that you set it to 1 in qemu, which sets it to 0 in Linux).

# virsh edit <domain>


    <qemu:arg value='-set'/>
    <qemu:arg value='device.sata0-0-1.rotation_rate=1'/>

4) Replace the header/below-line in XML-file describing your VM(virsh edit ):

domain type='kvm'


<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>

5) Restart the domain and once the vm booted up, verify the device using the below commands:

# lsblk -d -o name,rota
# smartctl -i /dev/sdXX
| improve this answer | |

Recent versions of QEMU (I tried with 2.12.0) support a rotation_rate parameter. If you set it to 1, the guest should treat the disk as an SSD. Currently, libvirt does not support this parameter in its XML format directly, so you have to pass it in as a qemu:commandline parameter.

Here are the relevant bits of a libvirt configuration that I use for an OSX guest:

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native' discard='unmap'/>
      <source dev='/dev/vg1/osx'/>
      <target dev='sda' bus='sata'/>
      <boot order='2'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    <qemu:arg value='-set'/>
    <qemu:arg value='device.sata0-0-0.rotation_rate=1'/>
| improve this answer | |
  • discard=unmap is necessary if you have to use virtual disk type of SATA instead of virtio-scsi (where it's on by default, ahd which is preferred for performance reasons). – Michael Hampton Aug 3 '18 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.