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.
This commit is contained in:
Seth Vargo 2016-06-24 19:16:44 -04:00
parent 627091de35
commit ccc40eb19d
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
3 changed files with 16 additions and 7 deletions

View File

@ -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,
)

View File

@ -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 -%>

View File

@ -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 -%>