Module: VagrantPlugins::ProviderLibvirt::Util::NetworkUtil
- Includes:
- Vagrant::Util::NetworkIP, Vagrant::Util::ScopedHashOverride
- Defined in:
- lib/vagrant-libvirt/util/network_util.rb
Instance Method Summary collapse
- #configured_networks(machine, logger) ⇒ Object
- #find_empty(array, start, stop) ⇒ Object
-
#interface_network(driver, options) ⇒ Object
Return network name according to interface options.
-
#libvirt_networks(driver) ⇒ Object
Return a list of all (active and inactive) Libvirt networks as a list of hashes with their name, network address and status (active or not).
- #network_interfaces(machine, logger) ⇒ Object
Instance Method Details
#configured_networks(machine, logger) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/vagrant-libvirt/util/network_util.rb', line 55 def configured_networks(machine, logger) qemu_use_session = machine.provider_config.qemu_use_session qemu_use_agent = machine.provider_config.qemu_use_agent management_network_device = machine.provider_config.management_network_device management_network_name = machine.provider_config.management_network_name management_network_address = machine.provider_config.management_network_address management_network_mode = machine.provider_config.management_network_mode management_network_mac = machine.provider_config.management_network_mac management_network_guest_ipv6 = machine.provider_config.management_network_guest_ipv6 management_network_autostart = machine.provider_config.management_network_autostart management_network_pci_bus = machine.provider_config.management_network_pci_bus management_network_pci_slot = machine.provider_config.management_network_pci_slot management_network_domain = machine.provider_config.management_network_domain management_network_mtu = machine.provider_config.management_network_mtu management_network_keep = machine.provider_config.management_network_keep management_network_driver_iommu = machine.provider_config.management_network_driver_iommu management_network_iface_name = machine.provider_config.management_network_iface_name management_network_model_type = machine.provider_config.management_network_model_type logger.info "Using #{management_network_name} at #{management_network_address} as the management network #{management_network_mode} is the mode" begin management_network_ip = IPAddr.new(management_network_address) rescue ArgumentError raise Errors::ManagementNetworkError, error_message: "#{management_network_address} is not a valid IP address" end # capture address into $1 and mask into $2 management_network_ip.inspect =~ /IPv4:(.*)\/(.*)>/ if Regexp.last_match(2) == '255.255.255.255' raise Errors::ManagementNetworkError, error_message: "#{management_network_address} does not include both an address and subnet mask" end if qemu_use_session = { iface_type: :public_network, model_type: management_network_model_type, dev: management_network_device, mode: 'bridge', type: 'bridge', bus: management_network_pci_bus, slot: management_network_pci_slot } else = { iface_type: :private_network, model_type: management_network_model_type, network_name: management_network_name, ip: Regexp.last_match(1), netmask: Regexp.last_match(2), dhcp_enabled: true, forward_mode: management_network_mode, guest_ipv6: management_network_guest_ipv6, autostart: management_network_autostart, bus: management_network_pci_bus, slot: management_network_pci_slot } end [:driver_iommu] = management_network_driver_iommu [:iface_name] = management_network_iface_name unless management_network_mac.nil? [:mac] = management_network_mac end unless management_network_domain.nil? [:domain_name] = management_network_domain end unless management_network_mtu.nil? [:mtu] = management_network_mtu end unless management_network_pci_bus.nil? and management_network_pci_slot.nil? [:bus] = management_network_pci_bus [:slot] = management_network_pci_slot end if management_network_keep [:always_destroy] = false end # if there is a box and management network is disabled # need qemu agent enabled and at least one network that can be accessed if ( machine.config.vm.box && !machine.provider_config.mgmt_attach && !machine.provider_config.qemu_use_agent && !machine.config.vm.networks.any? { |type, _| ["private_network", "public_network"].include?(type.to_s) } ) raise Errors::ManagementNetworkRequired end # add management network to list of networks to check # unless mgmt_attach set to false networks = if machine.provider_config.mgmt_attach [] else [] end machine.config.vm.networks.each do |type, | logger.debug "In config found network type #{type} options #{}" # Options can be specified in Vagrantfile in short format (:ip => ...), # or provider format # (:libvirt__network_name => ...). # https://github.com/mitchellh/vagrant/blob/main/lib/vagrant/util/scoped_hash_override.rb = scoped_hash_override(, :libvirt) # store type in options # use default values if not already set = { iface_type: type, netmask: [:network_address] ? IPAddr.new([:network_address]).get_mask : '255.255.255.0', dhcp_enabled: true, forward_mode: 'nat', always_destroy: true }.merge() if [:type].to_s == 'dhcp' && [:ip].nil? [:network_name] = [:network_name] ? [:network_name] : 'vagrant-private-dhcp' end # add to list of networks to check networks.push() end networks end |
#find_empty(array, start, stop) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/vagrant-libvirt/util/network_util.rb', line 236 def find_empty(array, start, stop) (start..stop).each do |i| return i unless array[i] end nil end |
#interface_network(driver, options) ⇒ Object
Return network name according to interface options.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/vagrant-libvirt/util/network_util.rb', line 244 def interface_network(driver, ) # no need to get interface network for tcp tunnel config return 'tunnel_interface' if .fetch(:tunnel_type, nil) if [:network_name] @logger.debug 'Found network by name' return [:network_name] end # Get list of all (active and inactive) Libvirt networks. available_networks = libvirt_networks(driver) return 'public' if [:iface_type] == :public_network if [:ip] address = network_address([:ip], [:netmask]) available_networks.each do |network| if address == network[:network_address] @logger.debug 'Found network by ip' return network[:name] end end end raise Errors::NetworkNotAvailableError, network_name: [:ip] end |
#libvirt_networks(driver) ⇒ Object
Return a list of all (active and inactive) Libvirt networks as a list of hashes with their name, network address and status (active or not)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/vagrant-libvirt/util/network_util.rb', line 192 def libvirt_networks(driver) libvirt_networks = [] # Iterate over all (active and inactive) networks. driver.list_all_networks.each do |libvirt_network| # Parse ip address and netmask from the network xml description. xml = Nokogiri::XML(libvirt_network.xml_desc) ip = xml.xpath('/network/ip/@address').first ip = ip.value if ip netmask = xml.xpath('/network/ip/@netmask').first netmask = netmask.value if netmask dhcp_enabled = if xml.at_xpath('//network/ip/dhcp') true else false end domain_name = xml.at_xpath('/network/domain/@name') domain_name = domain_name.value if domain_name # Calculate network address of network from ip address and # netmask. network_address = (network_address(ip, netmask) if ip && netmask) libvirt_networks << { name: libvirt_network.name, ip_address: ip, netmask: netmask, network_address: network_address, dhcp_enabled: dhcp_enabled, bridge_name: libvirt_network.bridge_name, domain_name: domain_name, created: true, active: libvirt_network.active?, autostart: libvirt_network.autostart?, libvirt_network: libvirt_network } end libvirt_networks end |
#network_interfaces(machine, logger) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vagrant-libvirt/util/network_util.rb', line 23 def network_interfaces(machine, logger) # Setup list of interfaces before creating them. adapters = [] # Vagrant gives you adapter 0 by default # Assign interfaces to slots. configured_networks(machine, logger).each do || # don't need to create interface for this type next if [:iface_type] == :forwarded_port # TODO: fill first ifaces with adapter option specified. if [:adapter] if adapters[[:adapter]] raise Errors::InterfaceSlotNotAvailable end free_slot = [:adapter].to_i @logger.debug "Using specified adapter slot #{free_slot}" else free_slot = find_empty(adapters, 0, machine.provider_config.nic_adapter_count) @logger.debug "Adapter not specified so found slot #{free_slot}" raise Errors::InterfaceSlotExhausted if free_slot.nil? end # We have slot for interface, fill it with interface configuration. adapters[free_slot] = adapters[free_slot][:network_name] = interface_network(machine.provider.driver, adapters[free_slot]) end adapters end |