From 81688f0fd9c3ddc57279e6674e331ce0a222e6d6 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Mon, 13 Jul 2015 15:52:26 -0700 Subject: [PATCH 1/5] Allow veryisolated networks With veryisolated networks, there is no ip address assigned thus matching the network name and ip address fails. This allows the null ip address to match a named network when that forward_mode is set. --- lib/vagrant-libvirt/action/create_networks.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index 883c83f..6c86afe 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -116,13 +116,16 @@ module VagrantPlugins # @available_networks should be filled before calling this function. def handle_ip_option(env) return if !@options[:ip] + net_address = nil + if @options[:forward_mode] != 'veryisolated' + net_address = network_address(@options[:ip], @options[:netmask]) + # Set IP address of network (actually bridge). It will be used as + # gateway address for machines connected to this network. + net = IPAddr.new(net_address) + end - net_address = network_address(@options[:ip], @options[:netmask]) @interface_network[:network_address] = net_address - # Set IP address of network (actually bridge). It will be used as - # gateway address for machines connected to this network. - net = IPAddr.new(net_address) # Default to first address (after network name) @interface_network[:ip_address] = @options[:host_ip].nil? ? net.to_range.begin.succ : IPAddr.new(@options[:host_ip]) From 6d8c45542e35a94ae316a715c6d733213c26fed4 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Fri, 17 Jul 2015 15:31:17 -0700 Subject: [PATCH 2/5] Select the network by name --- lib/vagrant-libvirt/action/create_networks.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index 6c86afe..da3b3c9 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -140,6 +140,13 @@ module VagrantPlugins break end end + # if network is veryisolated, search by name instead + if @options[:libvirt__forward_mode] == "veryisolated" + if lookup_network_by_name(@options[:network_name]) + @interface_network = lookup_network_by_name(@options[:network_name]) + @logger.debug @interface_network + end + end if @interface_network[:created] verify_dhcp From 546c98c0a2a97c5cbcd4d7ea58e23343b8472029 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Fri, 17 Jul 2015 17:07:18 -0700 Subject: [PATCH 3/5] Allow veryisolated networks without IP * Moved network lookup by IP to `lookup_network_by_ip` * Lookup by name only if lookup by ip fails *and* forward_mode is `veryisolated`. This will preserve existing functionality. * Added network creation ability to `handle_network_name_option` ** Network **must** be veryisolated to be created by name ** Network **must not** have an ip address to be used in the `handle_network_name_option` path. Minor changes: * corrected inverted autostart logic * screen formatting Fixes #402 --- lib/vagrant-libvirt/action/create_networks.rb | 90 +++++++++++++------ 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index da3b3c9..2dd333a 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -67,10 +67,10 @@ module VagrantPlugins # because cannot have name without ip # https://github.com/mitchellh/vagrant/commit/cf2f6da4dbcb4f57c9cdb3b94dcd0bba62c5f5fd elsif @options[:network_name] - handle_network_name_option + handle_network_name_option(env) end - autostart_network if !@interface_network[:autostart] + autostart_network if @interface_network[:autostart] activate_network if !@interface_network[:active] end end @@ -80,11 +80,25 @@ module VagrantPlugins private + def lookup_network_by_ip(ip) + @logger.debug "looking up network with ip == #{ip}" + @available_networks.each do |network| + if network[:network_address] == ip + @logger.debug "found existing network by ip: #{network}" + return network + end + end + nil + end + # Return hash of network for specified name, or nil if not found. def lookup_network_by_name(network_name) @logger.debug "looking up network named #{network_name}" @available_networks.each do |network| - return network if network[:name] == network_name + if network[:name] == network_name + @logger.debug "found existing network by name: #{network}" + return network + end end nil end @@ -127,31 +141,27 @@ module VagrantPlugins @interface_network[:network_address] = net_address # Default to first address (after network name) - @interface_network[:ip_address] = @options[:host_ip].nil? ? net.to_range.begin.succ : IPAddr.new(@options[:host_ip]) - + @interface_network[:ip_address] = @options[:host_ip].nil? ? \ + net.to_range.begin.succ : \ + IPAddr.new(@options[:host_ip]) + # Is there an available network matching to configured ip # address? - @available_networks.each do |available_network| - if available_network[:network_address] == \ - @interface_network[:network_address] - @interface_network = available_network - @logger.debug "found existing network by ip, values are" - @logger.debug @interface_network - break - end + if net_address + network = lookup_network_by_ip(net_address) + @interface_network = network if network end + # if network is veryisolated, search by name instead - if @options[:libvirt__forward_mode] == "veryisolated" - if lookup_network_by_name(@options[:network_name]) - @interface_network = lookup_network_by_name(@options[:network_name]) - @logger.debug @interface_network - end + if !@interface_network and @options[:libvirt__forward_mode] == "veryisolated" + network = lookup_network_by_name(@options[:network_name]) + @interface_network = network if network end if @interface_network[:created] verify_dhcp end - + if @options[:network_name] @logger.debug "Checking that network name does not clash with ip" if @interface_network[:created] @@ -170,13 +180,13 @@ module VagrantPlugins ip_address: @options[:ip], network_name: @options[:network_name] end - + # Network with 'name' doesn't exist. Set it as name for new # network. @interface_network[:name] = @options[:network_name] end end - + # Do we need to create new network? if !@interface_network[:created] @@ -219,15 +229,39 @@ module VagrantPlugins # Handle network_name option, if ip was not specified. Variables # @options and @available_networks should be filled before calling this # function. - def handle_network_name_option - return if @options[:ip] || !@options[:network_name] + def handle_network_name_option(env) + return if @options[:ip] || \ + !@options[:network_name] || \ + !@options[:libvirt__forward_mode] == "veryisolated" - @interface_network = lookup_network_by_name(@options[:network_name]) - if !@interface_network + network = lookup_network_by_name(@options[:network_name]) + @interface_network = network if network + + # if this interface has a network address, something's wrong. + if @interface_network[:network_address] raise Errors::NetworkNotAvailableError, network_name: @options[:network_name] - else - verify_dhcp + end + + # Do we need to create new network? + if !@interface_network[:created] + @interface_network[:name] = @options[:network_name] + + # Generate a unique name for network bridge. + count = 0 + while @interface_network[:bridge_name].nil? + @logger.debug "generating name for bridge" + bridge_name = 'virbr' + bridge_name << count.to_s + count += 1 + + next if lookup_bridge_by_name(bridge_name) + + @interface_network[:bridge_name] = bridge_name + end + + # Create a private network. + create_private_network(env) end end @@ -246,7 +280,7 @@ module VagrantPlugins # Find out DHCP addresses pool range. network_address = "#{@interface_network[:network_address]}/" network_address << "#{@interface_network[:netmask]}" - net = IPAddr.new(network_address) + net = @interface_network[:network_address] ? IPAddr.new(network_address) : nil # First is address of network, second is gateway (by default). # So start the range two addresses after network address by default. From 9b7c9def3c54b75653346532ded9c5aece96ad33 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Fri, 17 Jul 2015 17:17:37 -0700 Subject: [PATCH 4/5] Removed comment Vagrant only requires an ip **if** `type` is set **and** is not `dhcp`. The only other type, currently, is `nfs`. Obviously nfs requires an IP so under that circumstance or if `type` is set to anything but dhcp, vagrant will error if an ip is not assigned. If `type` is `nil`, it is deleted from the hash. --- lib/vagrant-libvirt/action/create_networks.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index 2dd333a..45bf77e 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -63,9 +63,6 @@ module VagrantPlugins if @options[:ip] handle_ip_option(env) - # in vagrant 1.2.3 and later it is not possible to take this branch - # because cannot have name without ip - # https://github.com/mitchellh/vagrant/commit/cf2f6da4dbcb4f57c9cdb3b94dcd0bba62c5f5fd elsif @options[:network_name] handle_network_name_option(env) end From fc24db479f1c4797faed4f86a6672bfa7237f9ef Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Mon, 20 Jul 2015 13:01:34 -0700 Subject: [PATCH 5/5] Make lookup_network_by_name the default for veryisolated networks --- lib/vagrant-libvirt/action/create_networks.rb | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/vagrant-libvirt/action/create_networks.rb b/lib/vagrant-libvirt/action/create_networks.rb index 45bf77e..b8cb08d 100644 --- a/lib/vagrant-libvirt/action/create_networks.rb +++ b/lib/vagrant-libvirt/action/create_networks.rb @@ -133,27 +133,27 @@ module VagrantPlugins # Set IP address of network (actually bridge). It will be used as # gateway address for machines connected to this network. net = IPAddr.new(net_address) + + # Default to first address (after network name) + @interface_network[:ip_address] = @options[:host_ip].nil? ? \ + net.to_range.begin.succ : \ + IPAddr.new(@options[:host_ip]) end @interface_network[:network_address] = net_address - # Default to first address (after network name) - @interface_network[:ip_address] = @options[:host_ip].nil? ? \ - net.to_range.begin.succ : \ - IPAddr.new(@options[:host_ip]) - - # Is there an available network matching to configured ip - # address? - if net_address - network = lookup_network_by_ip(net_address) - @interface_network = network if network - end - - # if network is veryisolated, search by name instead - if !@interface_network and @options[:libvirt__forward_mode] == "veryisolated" + # if network is veryisolated, search by name + if @options[:libvirt__forward_mode] == "veryisolated" + network = lookup_network_by_name(@options[:network_name]) + elsif net_address + # otherwise, search by ip (if set) + network = lookup_network_by_ip(net_address) + else + # leaving this here to mimic prior behavior. If we get + # here, something's probably broken. network = lookup_network_by_name(@options[:network_name]) - @interface_network = network if network end + @interface_network = network if network if @interface_network[:created] verify_dhcp