11

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?

5 Answers 5

5

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.

2
  • 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, 2017 at 14:00
  • 3
    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, 2017 at 12:11
4

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'/>
3
  • 2
    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, 2017 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 Oct 4, 2017 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. Dec 8, 2017 at 3:11
4

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

1
  • Note: If the disk is not yet detected as such, you can trigger the change using winsat disk -drive c.
    – DustWolf
    Aug 23, 2023 at 10:04
3

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'/>
</disk>

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:commandline>
    <qemu:arg value='-set'/>
    <qemu:arg value='device.sata0-0-1.rotation_rate=1'/>
  </qemu:commandline>
</domain>

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

domain type='kvm'

to

<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
2

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'>
  …
  <devices>
    <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'/>
    </disk>
    …
  </devices>
  <qemu:commandline>
    …
    <qemu:arg value='-set'/>
    <qemu:arg value='device.sata0-0-0.rotation_rate=1'/>
  </qemu:commandline>
</domain>
1
  • 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). Aug 3, 2018 at 15:13

You must log in to answer this question.

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