Enabling Guest Cursor Movement with libvirt/KVM Virtual Machine GPU Passthrough
When using a Windows virtual machine with libvirt/KVM and GPU passthrough, you may encounter an issue where the mouse pointer can't move on connected monitors, and is stuck within the virtual monitor.
GPU Passthrough Configuration
To configure GPU passthrough, you need to create a XML configuration file for the virtual machine, specifying the GPU to be passed through to the guest. This can be achieved using the following command:
<domain type='kvm'>
<name>windows</name>
<memory unit='KiB'>16777216</memory>
<currentMemory unit='KiB'>16777216</currentMemory>
<vcpu placement='static'>4</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-bios'/>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic>
<driver/>
</apic>
</features>
<cpu mode='host-model' check='partial'>
<model fallback='forbid'>Haswell</model>
<topology sockets='1' cores='4' threads='1'/>
</cpu>
<clock offset='localtime'>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
<timer name='hypervclock' present='yes'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/windows.qcow2'/>
<backingStore/>
<target dev='vda' bus='virtio'>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</target>
</disk>
<interface type='bridge'>
<mac address='52:54:00:12:34:56'/>
<source bridge='virbr0'/>
<model type='virtio'/>
</interface>
<interface type='direct'>
<source>
<host dev='/dev/vg0/win10gpu'/>
<mode>subsystem</mode>
</source>
<target dev='vfio/0' bus='pci' slot='0x10' function='0x0'/>
<alias name='vfio-pci-0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x10' function='0x0'/>
</interface>
<graphics type='vnc' port='-1' autoport='yes'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<sound model='ich6'/>
<video>
<model type='qxl' vram='65536'>
<alias name='video0'/>
</model>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
</devices>>
</domain>
```
In the above code block, the relevant section for GPU passthrough is the <interface type='direct'> element. This section defines the GPU device to be passed through to the guest.
Enabling Guest Cursor Movement
By default, the mouse pointer is confined within the virtual monitor due to the virtual graphics card's limitations. To enable guest cursor movement, you need to modify the virtual machine's XML configuration file, adding the following kernel option:
<qemu:commandline>
<qemu:arg value='-object'/>
<qemu:arg value='input-linux,id=mouse0,evdev=/dev/input/by-path/platform-i8042-serio-0-event-mouse'/>
<qemu:arg value='-object'/>
<qemu:arg value='input-linux,id=kbd0,evdev=/dev/input/by-path/platform-i8042-serio-0-event-kbd'/>
<qemu:arg value='-device'/>
<qemu:arg value='virtio-serial-gpio,id=mouse-gpio-port,gpio_port=0x04,bus=virtio-serial-0.auto_created,addr=0'/>
<qemu:arg value='-device'/>
<qemu:arg value='virtio-serial-gpio,id=kbd-gpio-port,gpio_port=0x03,bus=virtio-serial-0.auto_created,addr=1'/>
<qemu:arg value='-device'/>
<qemu:arg value='virtio-tablet-linux,id=tablet0,bus=virtio-serial-0.auto_created,addr=2'/>
<qemu:arg value='-chardev'/>
<qemu:arg value='spicevmc,id=spicechannel0,name=vdagent'/>
<qemu:arg value='-device'/>
<qemu:arg value='virtserialport,bus=virtio-serial-0.auto_created,nr=1,chardev=spicechannel0,id=channel0,name=com.redhat.spice.0'/>
</qemu:commandline>
The above kernel option defines the input devices to be used by the guest. In this case, three input devices are defined—a mouse, a keyboard, and a tablet. By assigning the input devices to specific GPIO ports, the mouse pointer can now move freely across connected monitors.
- GPU passthrough in libvirt/KVM requires the creation of a XML configuration file for the virtual machine.
- Enabling guest cursor movement requires the modification of the virtual machine's XML configuration file, adding a kernel option to define the input devices.
- By assigning the input devices to specific GPIO ports, the mouse pointer can move freely across connected monitors.
References
-
Larabel, M. (2020). QEMU/Libvirt 4.2 Officially Released With New Features.
-
Red Hat, Inc. (2020). Configuring Passthrough of Graphics Devices.
-
Kubuntu Community Help Wiki. (2020). Virtio.