Skip to content

Instantly share code, notes, and snippets.

@ares
Last active January 11, 2020 22:54
Show Gist options
  • Save ares/9fc36e66039274cb165a to your computer and use it in GitHub Desktop.
Save ares/9fc36e66039274cb165a to your computer and use it in GitHub Desktop.
Preseed networking setup snippet
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>
real=`ip -o link | grep <%= @host.mac -%> | awk '{print $2;}' | sed s/://`
<% if @host.respond_to?(:has_primary_interface) && @host.has_primary_interface? %>
cat << EOF > /etc/network/interfaces
auto lo
iface lo inet loopback
auto $real
allow-hotplug $real
iface $real inet <%= dhcp ? 'dhcp' : 'static' %>
<% unless dhcp -%>
address <%= @host.ip %>
netmask <%= subnet.mask %>
<% end %>
EOF
<% end -%>
<% @host.interfaces.each do |interface| %>
<% next if !interface.managed? || interface.subnet.nil? -%>
<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% vlan = virtual && subnet.has_vlanid? -%>
<% alias_type = virtual && !subnet.has_vlanid? && interface.identifier.include?(':') -%>
<% dhcp = subnet.nil? ? false : subnet.dhcp_boot_mode? -%>
real=`ip -o link | grep <%= interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% if virtual -%>
real=`echo <%= interface.identifier -%> | sed s/<%= interface.physical_device -%>/$real/`
<% end -%>
cat << EOF >> /etc/network/interfaces
auto $real
iface $real inet <%= dhcp ? 'dhcp' : 'static' %>
<% unless dhcp -%>
address <%= interface.ip %>
netmask <%= subnet.mask %>
<% end %>
EOF
<% end -%>
service networking restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment