Inject default qemu_agent allowing for explicit disable (#1408)

Facilitate the injection of a default channel when qemu_agent is set to
true to make enabling it's use a simple `qemu_agent = true` for most
cases.

Where custom values are needed, they can be provided and the code will
skip adding a corresponding entry, or if necessary the user can add an
entry and mark it disabled similar to how synced folders work.

Closes: #1341
This commit is contained in:
Darragh Bailey 2021-11-26 18:12:21 +00:00 committed by GitHub
parent 2908888934
commit cf3b81bfc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 3 deletions

View File

@ -999,6 +999,19 @@ if it detects an attached channel during boot.
* `qemu_use_agent` - false by default, if set to true, attempt to extract configured
ip address via qemu agent.
By default if `qemu_use_agent` is set to `true` the code will automatically
inject a suitable channel unless there already exists an entry with a
`:target_name` matching `'org.qemu.guest_agent.'`.
Alternatively if setting `qemu_use_agent` but, needing to disable the addition
of the channel, simply use a disabled flag as follows:
```ruby
Vagrant.configure(2) do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :disabled => true
end
end
```
To use the management network interface with an external dhcp service you need
to setup a bridged host network manually and define it via
`management_network_name` in your Vagrantfile.

View File

@ -498,7 +498,9 @@ module VagrantPlugins
target_address: options[:target_address],
target_name: options[:target_name],
target_port: options[:target_port],
target_type: options[:target_type])
target_type: options[:target_type],
disabled: options[:disabled],
)
end
def random(options = {})
@ -906,7 +908,18 @@ module VagrantPlugins
@inputs = [{ type: 'mouse', bus: 'ps2' }] if @inputs == UNSET_VALUE
# Channels
@channels = [] if @channels == UNSET_VALUE
if @channels == UNSET_VALUE
@channels = []
if @qemu_use_agent == true
if @channels.all? { |channel| !channel.fetch(:target_name, '').start_with?('org.qemu.guest_agent.') }
channel(:type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio')
end
end
end
# filter channels of anything explicitly disabled so it's possible to inject an entry to
# avoid the automatic addition of the guest_agent above, and disable it from subsequent use.
@channels = @channels.reject { |channel| channel[:disabled] }.tap {|channel| channel.delete(:disabled) }
# PCI device passthrough
@pcis = [] if @pcis == UNSET_VALUE

View File

@ -531,6 +531,47 @@ describe VagrantPlugins::ProviderLibvirt::Config do
end
end
end
context '@channels' do
it 'should be empty by default' do
subject.finalize!
expect(subject.channels).to be_empty
end
context 'when qemu_use_agent is set' do
before do
subject.qemu_use_agent = true
end
it 'should inject a qemu agent channel' do
subject.finalize!
expect(subject.channels).to_not be_empty
expect(subject.channels).to match([a_hash_including({:target_name => 'org.qemu.guest_agent.0'})])
end
context 'when agent channel already added' do
it 'should not modify the channels' do
subject.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
subject.finalize!
expect(subject.channels.length).to eq(1)
end
context 'when agent channel explicitly disbaled' do
it 'should not include an agent channel' do
subject.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :disabled => true
subject.finalize!
expect(subject.channels).to be_empty
end
end
end
end
end
end
def assert_invalid

View File

@ -7,7 +7,6 @@ Vagrant.configure("2") do |config|
config.vm.box = "generic/debian10"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider :libvirt do |libvirt|
libvirt.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
libvirt.qemu_use_agent = true
end
end