From ccc40eb19d8bc7a425c34daec160f5b782498b5c Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 24 Jun 2016 19:16:44 -0400 Subject: [PATCH] guests/arch: compute netmask Arch expects /24 for netmask, but users usually specify as a netmask block like 255.255.255.0. This automatically converts it. --- plugins/guests/arch/cap/configure_networks.rb | 9 +++++++++ templates/guests/arch/network_static.erb | 6 +++--- templates/guests/arch/network_static6.erb | 8 ++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index 8f75548f0..2fa9c8017 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -1,3 +1,5 @@ +require "ipaddr" +require "socket" require "tempfile" require_relative "../../../../lib/vagrant/util/template_renderer" @@ -17,6 +19,13 @@ module VagrantPlugins networks.each.with_index do |network, i| network[:device] = interfaces[network[:interface]] + # Arch expects netmasks to be in the "24" or "64", but users may + # specify IPV4 netmasks like "255.255.255.0". This magic converts + # the netmask to the proper value. + if network[:netmask] && network[:netmask].to_s.include?(".") + network[:netmask] = (32-Math.log2((IPAddr.new(network[:netmask], Socket::AF_INET).to_i^0xffffffff)+1)).to_i + end + entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}", options: network, ) diff --git a/templates/guests/arch/network_static.erb b/templates/guests/arch/network_static.erb index f8cb06f1b..52b2f2d77 100644 --- a/templates/guests/arch/network_static.erb +++ b/templates/guests/arch/network_static.erb @@ -2,7 +2,7 @@ Connection=ethernet Description='A basic static ethernet connection' Interface=<%= options[:device] %> IP=static -Address=('<%= options[:ip]%>/24') -<% if options[:gateway] %> +Address=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> Gateway='<%= options[:gateway] %>' -<% end %> +<% end -%> diff --git a/templates/guests/arch/network_static6.erb b/templates/guests/arch/network_static6.erb index 40e61c596..e64ffd5ca 100644 --- a/templates/guests/arch/network_static6.erb +++ b/templates/guests/arch/network_static6.erb @@ -2,7 +2,7 @@ Connection=ethernet Description='A basic IPv6 ethernet connection' Interface=<%= options[:device] %> IP6=static -Address=('<%= options[:ip]%>/<%= options[:netmask] %>') -<% if options[:gateway] %> -Gateway='<%= options[:gateway] %>' -<% end %> +Address6=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> +Gateway6='<%= options[:gateway] %>' +<% end -%>