Add a :libvirt__always_destroy flag to network definitions (#1381)

This ensures that a network is always appended to the created_networks
file, to avoid the case where a domain that created the network gets
destroyed first during cleanup, therefore skipping the network destroy
because some other domain is still using it. Since the second domain did
not create the network, the existing behaviour is that it gets left
behind.

Seting :libvirt__always_destroy to true will ensure that the domain will
destroy the network even if it did not create it, if there are no other
users of the network remaining.
This commit is contained in:
Jamie Barber 2021-10-19 14:52:54 +01:00 committed by GitHub
parent 23a23029a7
commit d5971f894f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -906,6 +906,9 @@ starts with `libvirt__` string. Here is a list of those options:
If not specified the default is 'false'.
* `:bus` - The bus of the PCI device. Both :bus and :slot have to be defined.
* `:slot` - The slot of the PCI device. Both :bus and :slot have to be defined.
* `:libvirt__always_destroy` - Allow domains that use but did not create a
network to destroy it when the domain is destroyed (default: `true`). Set to
`false` to only allow the domain that created the network to destroy it.
When the option `:libvirt__dhcp_enabled` is to to 'false' it shouldn't matter
whether the virtual network contains a DHCP server or not and vagrant-libvirt

View File

@ -208,6 +208,9 @@ module VagrantPlugins
# Create a private network.
create_private_network(env)
write_created_network(env)
else
write_created_network(env) unless @options[:always_destroy] == false
end
end
@ -247,6 +250,9 @@ module VagrantPlugins
# Create a private network.
create_private_network(env)
write_created_network(env)
else
write_created_network(env) unless @options[:always_destroy] == false
end
end
@ -273,6 +279,9 @@ module VagrantPlugins
# Create a private network.
create_private_network(env)
write_created_network(env)
else
write_created_network(env) unless @options[:always_destroy] == false
end
end
@ -347,7 +356,9 @@ module VagrantPlugins
rescue => e
raise Errors::CreateNetworkError, error_message: e.message
end
end
def write_created_network(env)
created_networks_file = env[:machine].data_dir + 'created_networks'
message = 'Saving information about created network ' \

View File

@ -119,7 +119,8 @@ module VagrantPlugins
IPAddr.new(options[:network_address]).get_mask :
'255.255.255.0',
dhcp_enabled: true,
forward_mode: 'nat'
forward_mode: 'nat',
always_destroy: true
}.merge(options)
if options[:type].to_s == 'dhcp' && options[:ip].nil?