From dcbfea2f49e4ccd5e7c152786fcefea1cfec245c Mon Sep 17 00:00:00 2001 From: Aleksandr Mezin Date: Tue, 26 Oct 2021 15:11:30 +0600 Subject: [PATCH] Add options for 3d acceleration (#1386) When enabling video_accel3d, as graphics_gl is typically required, will by default set it to true unless explicitly set to false. Enabling these should result in a significant performance improvement for any VM where the desktop is being used. Fixes: #893 Fixes: #1009 --- README.md | 4 +++ lib/vagrant-libvirt/action/create_domain.rb | 3 ++ lib/vagrant-libvirt/action/start_domain.rb | 36 ++++++++++++++++++++ lib/vagrant-libvirt/config.rb | 6 ++++ lib/vagrant-libvirt/templates/domain.xml.erb | 8 +++-- spec/unit/templates/domain_all_settings.xml | 8 +++-- spec/unit/templates/domain_spec.rb | 2 ++ 7 files changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a9a3ce7..0f20b1a 100644 --- a/README.md +++ b/README.md @@ -598,6 +598,8 @@ end * `graphics_autoport` - Sets autoport for graphics, Libvirt in this case ignores graphics_port value, Defaults to 'yes'. Possible value are "yes" and "no" +* `graphics_gl` - Set to `true` to enable OpenGL. Defaults to `true` if +`video_accel3d` is `true`. * `keymap` - Set keymap for vm. default: en-us * `kvm_hidden` - [Hide the hypervisor from the guest](https://libvirt.org/formatdomain.html#elementsFeatures). Useful for @@ -608,6 +610,8 @@ end "cirrus", "vmvga", "xen", "vbox", or "qxl". * `video_vram` - Used by some graphics card types to vary the amount of RAM dedicated to video. Defaults to 9216. +* `video_accel3d` - Set to `true` to enable 3D acceleration. Defaults to +`false`. * `sound_type` - [Set the virtual sound card](https://libvirt.org/formatdomain.html#elementsSound) Defaults to "ich6". * `machine_type` - Sets machine type. Equivalent to qemu `-machine`. Use diff --git a/lib/vagrant-libvirt/action/create_domain.rb b/lib/vagrant-libvirt/action/create_domain.rb index 9bb0b83..28115e4 100644 --- a/lib/vagrant-libvirt/action/create_domain.rb +++ b/lib/vagrant-libvirt/action/create_domain.rb @@ -78,9 +78,11 @@ module VagrantPlugins else "passwd='#{config.graphics_passwd}'" end + @graphics_gl = config.graphics_gl @video_type = config.video_type @sound_type = config.sound_type @video_vram = config.video_vram + @video_accel3d = config.video_accel3d @keymap = config.keymap @kvm_hidden = config.kvm_hidden @@ -299,6 +301,7 @@ module VagrantPlugins env[:ui].info(" -- Graphics Password: #{@graphics_passwd.empty? ? 'Not defined' : 'Defined'}") env[:ui].info(" -- Video Type: #{@video_type}") env[:ui].info(" -- Video VRAM: #{@video_vram}") + env[:ui].info(" -- Video 3D accel: #{@video_accel3d}") env[:ui].info(" -- Sound Type: #{@sound_type}") env[:ui].info(" -- Keymap: #{@keymap}") env[:ui].info(" -- TPM Backend: #{@tpm_type}") diff --git a/lib/vagrant-libvirt/action/start_domain.rb b/lib/vagrant-libvirt/action/start_domain.rb index 38d8022..ad129eb 100644 --- a/lib/vagrant-libvirt/action/start_domain.rb +++ b/lib/vagrant-libvirt/action/start_domain.rb @@ -222,6 +222,24 @@ module VagrantPlugins graphics.attributes['passwd'] = config.graphics_passwd end end + graphics_gl = REXML::XPath.first(xml_descr, '/domain/devices/graphics/gl') + if graphics_gl.nil? + if config.graphics_gl + graphics_gl = REXML::Element.new('gl', REXML::XPath.first(xml_descr, '/domain/devices/graphics')) + graphics_gl.attributes['enable'] = 'yes' + descr_changed = true + end + else + if config.graphics_gl + if graphics_gl.attributes['enable'] != 'yes' + graphics_gl.attributes['enable'] = 'yes' + descr_changed = true + end + else + graphics_gl.parent.delete_element(graphics_gl) + descr_changed = true + end + end else # graphics_type = none, remove entire element graphics.parent.delete_element(graphics) unless graphics.nil? @@ -280,6 +298,24 @@ module VagrantPlugins video_model.attributes['vram'] = config.video_vram end end + video_accel = REXML::XPath.first(xml_descr, '/domain/devices/video/model/acceleration') + if video_accel.nil? + if config.video_accel3d + video_accel = REXML::Element.new('acceleration', REXML::XPath.first(xml_descr, '/domain/devices/video/model')) + video_accel.attributes['accel3d'] = 'yes' + descr_changed = true + end + else + if config.video_accel3d + if video_accel.attributes['accel3d'] != 'yes' + video_accel.attributes['accel3d'] = 'yes' + descr_changed = true + end + else + video_accel.parent.delete_element(video_accel) + descr_changed = true + end + end end # Sound device diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index 6c12725..a81bbfe 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -119,8 +119,10 @@ module VagrantPlugins attr_accessor :graphics_port attr_accessor :graphics_passwd attr_accessor :graphics_ip + attr_accessor :graphics_gl attr_accessor :video_type attr_accessor :video_vram + attr_accessor :video_accel3d attr_accessor :keymap attr_accessor :kvm_hidden attr_accessor :sound_type @@ -270,8 +272,10 @@ module VagrantPlugins @graphics_port = UNSET_VALUE @graphics_ip = UNSET_VALUE @graphics_passwd = UNSET_VALUE + @graphics_gl = UNSET_VALUE @video_type = UNSET_VALUE @video_vram = UNSET_VALUE + @video_accel3d = UNSET_VALUE @sound_type = UNSET_VALUE @keymap = UNSET_VALUE @kvm_hidden = UNSET_VALUE @@ -890,6 +894,8 @@ module VagrantPlugins @graphics_ip = '127.0.0.1' if @graphics_ip == UNSET_VALUE @video_type = 'cirrus' if @video_type == UNSET_VALUE @video_vram = 9216 if @video_vram == UNSET_VALUE + @video_accel3d = false if @video_accel3d == UNSET_VALUE + @graphics_gl = @video_accel3d if @graphics_gl == UNSET_VALUE @sound_type = nil if @sound_type == UNSET_VALUE @keymap = 'en-us' if @keymap == UNSET_VALUE @kvm_hidden = false if @kvm_hidden == UNSET_VALUE diff --git a/lib/vagrant-libvirt/templates/domain.xml.erb b/lib/vagrant-libvirt/templates/domain.xml.erb index 634c444..c0d17a1 100644 --- a/lib/vagrant-libvirt/templates/domain.xml.erb +++ b/lib/vagrant-libvirt/templates/domain.xml.erb @@ -216,9 +216,13 @@ <% end %> <% if @graphics_type != 'none' %> <%# Video device -%> - /> + <% if not @graphics_gl %>/><% else %>> + + <% end %> <%#End Video -%> <% end %> diff --git a/spec/unit/templates/domain_all_settings.xml b/spec/unit/templates/domain_all_settings.xml index 4b8dcfa..1b57211 100644 --- a/spec/unit/templates/domain_all_settings.xml +++ b/spec/unit/templates/domain_all_settings.xml @@ -112,9 +112,13 @@ - + + + /dev/random diff --git a/spec/unit/templates/domain_spec.rb b/spec/unit/templates/domain_spec.rb index 958c569..30fb5d2 100644 --- a/spec/unit/templates/domain_spec.rb +++ b/spec/unit/templates/domain_spec.rb @@ -112,6 +112,8 @@ describe 'templates/domain' do domain.shares = '1024' domain.cpuset = '1-4,^3,6' domain.nodeset = '1-4,^3,6' + + domain.video_accel3d = true end let(:test_file) { 'domain_all_settings.xml' } it 'renders template' do