From 60e93d492590d9e4990a33909b467fa5e53aece6 Mon Sep 17 00:00:00 2001 From: David Scaife Date: Sun, 18 Mar 2018 19:25:01 +1100 Subject: [PATCH] USB controller configuration (#861) * Add USB controller configuration * Update README with USB controller configuration info * Rename USB controller parameter to usb_controller * Code style fixup --- README.md | 21 +++++++++++++++++++ lib/vagrant-libvirt/action/create_domain.rb | 9 ++++++++ lib/vagrant-libvirt/config.rb | 22 ++++++++++++++++++++ lib/vagrant-libvirt/templates/domain.xml.erb | 4 ++++ spec/unit/templates/domain_all_settings.xml | 1 + spec/unit/templates/domain_spec.rb | 1 + 6 files changed, 58 insertions(+) diff --git a/README.md b/README.md index cdd6050..05d8e65 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ can help a lot :-) - [CDROMs](#cdroms) - [Input](#input) - [PCI device passthrough](#pci-device-passthrough) +- [USB Controller Configuration](#usb-controller-configuration) - [USB Redirector Devices](#usb-redirector-devices) - [Random number generator passthrough](#random-number-generator-passthrough) - [Watchdog·Device](#watchdog-device) @@ -833,6 +834,26 @@ Note! Above options affect configuration only at domain creation. It won't chang Don't forget to [set](#domain-specific-options) `kvm_hidden` option to `true` especially if you are passthroughing NVIDIA GPUs. Otherwise GPU is visible from VM but cannot be operated. + +## USB Controller Configuration + +The USB controller can be configured using `libvirt.usb_controller`, with the following options: + +* `model` - The USB controller device model to emulate. (mandatory) +* `ports` - The number of devices that can be connected to the controller. + +See the [libvirt documentation](https://libvirt.org/formatdomain.html#elementsControllers) for a list of valid models. + +```ruby +Vagrant.configure("2") do |config| + config.vm.provider :libvirt do |libvirt| + # Set up a USB3 controller + libvirt.usb_controller :model => "nec-xhci" + end +end +``` + + ## USB Redirector Devices You can specify multiple redirect devices via `libvirt.redirdev`. There are two types, `tcp` and `spicevmc` supported, for forwarding USB-devices to the guest. Available options are listed below. diff --git a/lib/vagrant-libvirt/action/create_domain.rb b/lib/vagrant-libvirt/action/create_domain.rb index 8077b67..003f84c 100644 --- a/lib/vagrant-libvirt/action/create_domain.rb +++ b/lib/vagrant-libvirt/action/create_domain.rb @@ -95,6 +95,9 @@ module VagrantPlugins # Watchdog device @watchdog_dev = config.watchdog_dev + # USB controller + @usbctl_dev = config.usbctl_dev + # USB device passthrough @usbs = config.usbs @@ -260,6 +263,12 @@ module VagrantPlugins env[:ui].info(" -- Watchdog device: model=#{@watchdog_dev[:model]}, action=#{@watchdog_dev[:action]}") end + if not @usbctl_dev.empty? + msg = " -- USB controller: model=#{@usbctl_dev[:model]}" + msg += ", ports=#{@usbctl_dev[:ports]}" if @usbctl_dev[:ports] + env[:ui].info(msg) + end + @usbs.each do |usb| usb_dev = [] usb_dev.push("bus=#{usb[:bus]}") if usb[:bus] diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index 73d6e5d..37f6fcf 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -132,6 +132,9 @@ module VagrantPlugins # Watchdog device attr_accessor :watchdog_dev + # USB controller + attr_accessor :usbctl_dev + # USB device passthrough attr_accessor :usbs @@ -245,6 +248,9 @@ module VagrantPlugins # Watchdog device @watchdog_dev = UNSET_VALUE + # USB controller + @usbctl_dev = UNSET_VALUE + # USB device passthrough @usbs = UNSET_VALUE @@ -434,6 +440,19 @@ module VagrantPlugins end + def usb_controller(options = {}) + if options[:model].nil? + raise 'USB controller model must be specified.' + end + + if @usbctl_dev == UNSET_VALUE + @usbctl_dev = {} + end + + @usbctl_dev[:model] = options[:model] + @usbctl_dev[:ports] = options[:ports] + end + def usb(options = {}) if (options[:bus].nil? || options[:device].nil?) && options[:vendor].nil? && options[:product].nil? raise 'Bus and device and/or vendor and/or product must be specified. Check `lsusb` for these.' @@ -709,6 +728,9 @@ module VagrantPlugins # Watchdog device @watchdog_dev = {} if @watchdog_dev == UNSET_VALUE + # USB controller + @usbctl_dev = {} if @usbctl_dev == UNSET_VALUE + # USB device passthrough @usbs = [] if @usbs == UNSET_VALUE diff --git a/lib/vagrant-libvirt/templates/domain.xml.erb b/lib/vagrant-libvirt/templates/domain.xml.erb index 044560e..a5cb1fb 100644 --- a/lib/vagrant-libvirt/templates/domain.xml.erb +++ b/lib/vagrant-libvirt/templates/domain.xml.erb @@ -243,6 +243,10 @@ <% end -%> + <% if not @usbctl_dev.empty? %> + <%# USB Controller -%> + /> + <% end %> <% unless @qargs.empty? %> diff --git a/spec/unit/templates/domain_all_settings.xml b/spec/unit/templates/domain_all_settings.xml index 16b9df4..d87fa56 100644 --- a/spec/unit/templates/domain_all_settings.xml +++ b/spec/unit/templates/domain_all_settings.xml @@ -128,6 +128,7 @@ + diff --git a/spec/unit/templates/domain_spec.rb b/spec/unit/templates/domain_spec.rb index 5b700af..561e686 100644 --- a/spec/unit/templates/domain_spec.rb +++ b/spec/unit/templates/domain_spec.rb @@ -64,6 +64,7 @@ describe 'templates/domain' do domain.random(model: 'random') domain.pci(bus: '0x06', slot: '0x12', function: '0x5') domain.pci(bus: '0x03', slot: '0x00', function: '0x0') + domain.usb_controller(model: 'nec-xhci', ports: '4') domain.usb(bus: '1', device: '2', vendor: '0x1234', product: '0xabcd') domain.redirdev(type: 'tcp', host: 'localhost', port: '4000') domain.redirfilter(class: '0x0b', vendor: '0x08e6',