Skip to content

Commit

Permalink
Check for conflicting DHCP servers
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jan 9, 2012
1 parent d79f5d8 commit 8aa4e58
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
21 changes: 15 additions & 6 deletions lib/vagrant/action/vm/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,21 @@ def hostonly_adapter(config)
end

if config[:type] == :dhcp
# TODO: Check that there isn't another DHCP server on this network
# that is different.

# Configure the DHCP server for the network.
@logger.debug("Creating a DHCP server...")
@env[:vm].driver.create_dhcp_server(interface[:name], config)
# Check that if there is a DHCP server attached on our interface,
# then it is identical. Otherwise, we can't set it.
if interface[:dhcp]
valid = interface[:dhcp][:ip] == config[:dhcp_ip] &&
interface[:dhcp][:lower] == config[:dhcp_lower] &&
interface[:dhcp][:upper] == config[:dhcp_upper]

raise Errors::NetworkDHCPAlreadyAttached if !valid

@logger.debug("DHCP server already properly configured")
else
# Configure the DHCP server for the network.
@logger.debug("Creating a DHCP server...")
@env[:vm].driver.create_dhcp_server(interface[:name], config)
end
end

return {
Expand Down
26 changes: 25 additions & 1 deletion lib/vagrant/driver/virtualbox_4_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def create_host_only_network(options)
return {
:name => name,
:ip => options[:adapter_ip],
:netmask => options[:netmask]
:netmask => options[:netmask],
:dhcp => nil
}
end

Expand Down Expand Up @@ -235,6 +236,26 @@ def read_guest_additions_version
end

def read_host_only_interfaces
dhcp = {}
execute("list", "dhcpservers").split("\n\n").each do |block|
info = {}

block.split("\n").each do |line|
if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/
info[:network] = $1.to_s
elsif line =~ /^IP:\s+(.+?)$/
info[:ip] = $1.to_s
elsif line =~ /^lowerIPAddress:\s+(.+?)$/
info[:lower] = $1.to_s
elsif line =~ /^upperIPAddress:\s+(.+?)$/
info[:upper] = $1.to_s
end
end

# Set the DHCP info
dhcp[info[:network]] = info
end

execute("list", "hostonlyifs").split("\n\n").collect do |block|
info = {}

Expand All @@ -248,6 +269,9 @@ def read_host_only_interfaces
end
end

# Set the DHCP info if it exists
info[:dhcp] = dhcp[info[:name]] if dhcp[info[:name]]

info
end
end
Expand Down
26 changes: 25 additions & 1 deletion lib/vagrant/driver/virtualbox_4_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def create_host_only_network(options)
return {
:name => name,
:ip => options[:adapter_ip],
:netmask => options[:netmask]
:netmask => options[:netmask],
:dhcp => nil
}
end

Expand Down Expand Up @@ -235,6 +236,26 @@ def read_guest_additions_version
end

def read_host_only_interfaces
dhcp = {}
execute("list", "dhcpservers").split("\n\n").each do |block|
info = {}

block.split("\n").each do |line|
if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/
info[:network] = $1.to_s
elsif line =~ /^IP:\s+(.+?)$/
info[:ip] = $1.to_s
elsif line =~ /^lowerIPAddress:\s+(.+?)$/
info[:lower] = $1.to_s
elsif line =~ /^upperIPAddress:\s+(.+?)$/
info[:upper] = $1.to_s
end
end

# Set the DHCP info
dhcp[info[:network]] = info
end

execute("list", "hostonlyifs").split("\n\n").collect do |block|
info = {}

Expand All @@ -248,6 +269,9 @@ def read_host_only_interfaces
end
end

# Set the DHCP info if it exists
info[:dhcp] = dhcp[info[:name]] if dhcp[info[:name]]

info
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/vagrant/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ class NetworkNoAdapters < VagrantError
error_key(:no_adapters, "vagrant.actions.vm.network")
end

class NetworkDHCPAlreadyAttached < VagrantError
status_code(68)
error_key(:dhcp_already_attached, "vagrant.actions.vm.network")
end

class NetworkNotFound < VagrantError
status_code(30)
error_key(:not_found, "vagrant.actions.vm.host_only_network")
Expand Down
8 changes: 8 additions & 0 deletions templates/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ en:
overlap.
configuring: |-
Configuring and enabling network interfaces...
dhcp_already_attached: |-
A host only network interface you're attempting to configure via DHCP
already has a conflicting host only adapter with DHCP enabled. The
DHCP on this adapter is incompatible with the DHCP settings. Two
host only network interfaces are not allowed to overlap, and each
host only network interface can have only one DHCP server. Please
reconfigure your host only network or remove the virtual machine
using the other host only network.
no_adapters: |-
No available adapters on the virtual machine were found to accomodate
for all configured networks. VirtualBox virtual machines have 8
Expand Down

0 comments on commit 8aa4e58

Please sign in to comment.