Method: Vagrant::Action::VM::Network#hostonly_config

Defined in:
lib/vagrant/action/vm/network.rb

#hostonly_config(args) ⇒ Object



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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/vagrant/action/vm/network.rb', line 157

def hostonly_config(args)
  ip      = args[0]
  options = args[1] || {}

  # Determine if we're dealing with a static IP or a DHCP-served IP.
  type    = ip == :dhcp ? :dhcp : :static

  # Default IP is in the 20-bit private network block for DHCP based networks
  ip = "172.28.128.1" if type == :dhcp

  options = {
    :type    => type,
    :ip      => ip,
    :netmask => "255.255.255.0",
    :adapter => nil,
    :mac     => nil,
    :name    => nil,
    :auto_config => true
  }.merge(options)

  # Verify that this hostonly network wouldn't conflict with any
  # bridged interfaces
  verify_no_bridge_collision(options)

  # Get the network address and IP parts which are used for many
  # default calculations
  netaddr  = network_address(options[:ip], options[:netmask])
  ip_parts = netaddr.split(".").map { |i| i.to_i }

  # Calculate the adapter IP, which we assume is the IP ".1" at the
  # end usually.
  adapter_ip    = ip_parts.dup
  adapter_ip[3] += 1
  options[:adapter_ip] ||= adapter_ip.join(".")

  if type == :dhcp
    # Calculate the DHCP server IP, which is the network address
    # with the final octet + 2. So "172.28.0.0" turns into "172.28.0.2"
    dhcp_ip    = ip_parts.dup
    dhcp_ip[3] += 2
    options[:dhcp_ip] ||= dhcp_ip.join(".")

    # Calculate the lower and upper bound for the DHCP server
    dhcp_lower    = ip_parts.dup
    dhcp_lower[3] += 3
    options[:dhcp_lower] ||= dhcp_lower.join(".")

    dhcp_upper    = ip_parts.dup
    dhcp_upper[3] = 254
    options[:dhcp_upper] ||= dhcp_upper.join(".")
  end

  # Return the hostonly network configuration
  return options
end