Add qemu commandline environment variable support. (#961)

Make it easier to tweak some qemu options by allowing passthru of
command line environment variables.

- Also cleans up weird variable indirection used for qemu commandline args
  variable through `qargs` in various constructors.
- Addresses some functionality discussed in #776.
This commit is contained in:
Julio Lajara
2020-05-10 09:37:31 -04:00
committed by GitHub
parent f5f9e34da0
commit f00bc0eaae
6 changed files with 62 additions and 10 deletions

View File

@@ -57,7 +57,7 @@ can help a lot :-)
- [Customized Graphics](#customized-graphics) - [Customized Graphics](#customized-graphics)
- [TPM Devices](#tpm-devices) - [TPM Devices](#tpm-devices)
- [Libvirt communication channels](#libvirt-communication-channels) - [Libvirt communication channels](#libvirt-communication-channels)
- [Custom command line arguments](#custom-command-line-arguments) - [Custom command line arguments and environment variables](#custom-command-line-arguments-and-environment-variables)
- [Box Format](#box-format) - [Box Format](#box-format)
- [Create Box](#create-box) - [Create Box](#create-box)
- [Package Box from VM](#package-box-from-vm) - [Package Box from VM](#package-box-from-vm)
@@ -1413,8 +1413,8 @@ Vagrant.configure(2) do |config|
end end
``` ```
## Custom command line arguments ## Custom command line arguments and environment variables
You can also specify multiple qemuargs arguments for qemu-system You can also specify multiple qemuargs arguments or qemuenv environment variables for qemu-system
* `value` - Value * `value` - Value
@@ -1423,6 +1423,9 @@ Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt| config.vm.provider :libvirt do |libvirt|
libvirt.qemuargs :value => "-device" libvirt.qemuargs :value => "-device"
libvirt.qemuargs :value => "intel-iommu" libvirt.qemuargs :value => "intel-iommu"
libvirt.qemuenv QEMU_AUDIO_DRV: 'pa'
libvirt.qemuenv QEMU_AUDIO_TIMER_PERIOD: '150'
libvirt.qemuenv QEMU_PA_SAMPLES: '1024', QEMU_PA_SERVER: '/run/user/1000/pulse/native'
end end
end end
``` ```

View File

@@ -110,6 +110,12 @@ module VagrantPlugins
@redirdevs = config.redirdevs @redirdevs = config.redirdevs
@redirfilters = config.redirfilters @redirfilters = config.redirfilters
# Additional QEMU commandline arguments
@qemu_args = config.qemu_args
# Additional QEMU commandline environment variables
@qemu_env = config.qemu_env
# smartcard device # smartcard device
@smartcard_dev = config.smartcard_dev @smartcard_dev = config.smartcard_dev
@@ -322,15 +328,22 @@ module VagrantPlugins
env[:ui].info(" -- smartcard device: mode=#{@smartcard_dev[:mode]}, type=#{@smartcard_dev[:type]}") env[:ui].info(" -- smartcard device: mode=#{@smartcard_dev[:mode]}, type=#{@smartcard_dev[:type]}")
end end
@qargs = config.qemu_args unless @qemu_args.empty?
if not @qargs.empty?
env[:ui].info(' -- Command line args: ') env[:ui].info(' -- Command line args: ')
@qargs.each do |arg| @qemu_args.each do |arg|
msg = " -> value=#{arg[:value]}, " msg = " -> value=#{arg[:value]}, "
env[:ui].info(msg) env[:ui].info(msg)
end end
end end
unless @qemu_env.empty?
env[:ui].info(' -- Command line environment variables: ')
@qemu_env.each do |env_var, env_value|
msg = " -> #{env_var}=#{env_value}, "
env[:ui].info(msg)
end
end
env[:ui].info(" -- Command line : #{@cmd_line}") unless @cmd_line.empty? env[:ui].info(" -- Command line : #{@cmd_line}") unless @cmd_line.empty?
# Create Libvirt domain. # Create Libvirt domain.

View File

@@ -165,6 +165,9 @@ module VagrantPlugins
# Additional qemuargs arguments # Additional qemuargs arguments
attr_accessor :qemu_args attr_accessor :qemu_args
# Additional qemuenv arguments
attr_accessor :qemu_env
# Use QEMU session instead of system # Use QEMU session instead of system
attr_accessor :qemu_use_session attr_accessor :qemu_use_session
@@ -284,7 +287,12 @@ module VagrantPlugins
# Attach mgmt network # Attach mgmt network
@mgmt_attach = UNSET_VALUE @mgmt_attach = UNSET_VALUE
@qemu_args = [] # Additional QEMU commandline arguments
@qemu_args = UNSET_VALUE
# Additional QEMU commandline environment variables
@qemu_env = UNSET_VALUE
@qemu_use_session = UNSET_VALUE @qemu_use_session = UNSET_VALUE
end end
@@ -599,9 +607,17 @@ module VagrantPlugins
end end
def qemuargs(options = {}) def qemuargs(options = {})
@qemu_args = [] if @qemu_args == UNSET_VALUE
@qemu_args << options if options[:value] @qemu_args << options if options[:value]
end end
def qemuenv(options = {})
@qemu_env = {} if @qemu_env == UNSET_VALUE
@qemu_env.merge!(options)
end
# code to generate URI from a config moved out of the connect action # code to generate URI from a config moved out of the connect action
def _generate_uri def _generate_uri
# builds the Libvirt connection URI from the given driver config # builds the Libvirt connection URI from the given driver config
@@ -788,7 +804,11 @@ module VagrantPlugins
# Attach mgmt network # Attach mgmt network
@mgmt_attach = true if @mgmt_attach == UNSET_VALUE @mgmt_attach = true if @mgmt_attach == UNSET_VALUE
# Additional QEMU commandline arguments
@qemu_args = [] if @qemu_args == UNSET_VALUE @qemu_args = [] if @qemu_args == UNSET_VALUE
# Additional QEMU commandline environment variables
@qemu_env = {} if @qemu_env == UNSET_VALUE
end end
def validate(machine) def validate(machine)
@@ -824,6 +844,10 @@ module VagrantPlugins
c = cdroms.dup c = cdroms.dup
c += other.cdroms c += other.cdroms
result.cdroms = c result.cdroms = c
c = qemu_env != UNSET_VALUE ? qemu_env.dup : {}
c.merge!(other.qemu_env) if other.qemu_env != UNSET_VALUE
result.qemu_env = c
end end
end end
end end

View File

@@ -266,11 +266,14 @@
<% end %> <% end %>
</devices> </devices>
<% unless @qargs.empty? %> <% if not @qemu_args.empty? or not @qemu_env.empty? %>
<qemu:commandline> <qemu:commandline>
<% @qargs.each do |arg| %> <% @qemu_args.each do |arg| %>
<qemu:arg value='<%= arg[:value] %>'/> <qemu:arg value='<%= arg[:value] %>'/>
<% end %> <% end %>
<% @qemu_env.each do |env_var, env_value| %>
<qemu:env name='<%= env_var.to_s %>' value='<%= env_value %>'/>
<% end %>
</qemu:commandline> </qemu:commandline>
<% end %> <% end %>
</domain> </domain>

View File

@@ -143,5 +143,9 @@
<qemu:commandline> <qemu:commandline>
<qemu:arg value='-device'/> <qemu:arg value='-device'/>
<qemu:arg value='dummy-device'/> <qemu:arg value='dummy-device'/>
<qemu:env name='QEMU_AUDIO_DRV' value='pa'/>
<qemu:env name='QEMU_AUDIO_TIMER_PERIOD' value='150'/>
<qemu:env name='QEMU_PA_SAMPLES' value='1024'/>
<qemu:env name='QEMU_PA_SERVER' value='/run/user/1000/pulse/native'/>
</qemu:commandline> </qemu:commandline>
</domain> </domain>

View File

@@ -11,7 +11,6 @@ describe 'templates/domain' do
def finalize! def finalize!
super super
@qargs = @qemu_args
end end
end end
@@ -76,6 +75,12 @@ describe 'templates/domain' do
domain.qemuargs(value: '-device') domain.qemuargs(value: '-device')
domain.qemuargs(value: 'dummy-device') domain.qemuargs(value: 'dummy-device')
domain.qemuenv(QEMU_AUDIO_DRV: 'pa')
domain.qemuenv(QEMU_AUDIO_TIMER_PERIOD: '150')
domain.qemuenv(QEMU_PA_SAMPLES: '1024')
domain.qemuenv(QEMU_PA_SERVER: '/run/user/1000/pulse/native')
domain.shares = '1024' domain.shares = '1024'
domain.cpuset = '1-4,^3,6' domain.cpuset = '1-4,^3,6'
domain.nodeset = '1-4,^3,6' domain.nodeset = '1-4,^3,6'