Last active
June 8, 2016 15:52
-
-
Save hnakamur/f23e35083bf60d5c573392bfdad94466 to your computer and use it in GitHub Desktop.
A patch to support predictable network interface names like enp0s25 on Ubuntu 16.04 with Vagrant 1.8.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -ruN vagrant-1.8.1.orig/plugins/guests/debian/cap/configure_networks.rb vagrant-1.8.1/plugins/guests/debian/cap/configure_networks.rb | |
--- vagrant-1.8.1.orig/plugins/guests/debian/cap/configure_networks.rb 2015-12-25 06:30:19.000000000 +0900 | |
+++ vagrant-1.8.1/plugins/guests/debian/cap/configure_networks.rb 2016-04-24 03:22:08.000000000 +0900 | |
@@ -1,3 +1,4 @@ | |
+require 'log4r' | |
require 'set' | |
require 'tempfile' | |
@@ -9,8 +10,22 @@ | |
class ConfigureNetworks | |
include Vagrant::Util | |
+ @logger = Log4r::Logger.new("vagrant::plugins::guest") | |
+ | |
def self.configure_networks(machine, networks) | |
machine.communicate.tap do |comm| | |
+ main_interface = '' | |
+ comm.execute(%q!ip route | awk '$1=="default"{print $NF;exit}'!) do |type, data| | |
+ main_interface = data.chomp if type == :stdout | |
+ end | |
+ @logger.debug("debian configure_networks. main_interface=#{main_interface.inspect}") | |
+ available_interfaces = [] | |
+ comm.execute("ls /sys/class/net | grep -v -E '^(lo$|docker|lx[cd])|veth'") do |type, data| | |
+ available_interfaces = data.chomp.split("\n") if type == :stdout | |
+ end | |
+ available_interfaces.delete(main_interface) | |
+ @logger.debug("debian configure_networks. available_interfaces=#{available_interfaces.inspect}") | |
+ | |
# First, remove any previous network modifications | |
# from the interface file. | |
comm.sudo("sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre") | |
@@ -21,11 +36,15 @@ | |
# later. | |
interfaces = Set.new | |
entries = [] | |
- networks.each do |network| | |
- interfaces.add(network[:interface]) | |
+ networks.each_with_index do |network, i| | |
+ interface = available_interfaces[i] | |
+ interfaces.add(interface) | |
+ network[:interface] = interface | |
entry = TemplateRenderer.render("guests/debian/network_#{network[:type]}", | |
- options: network) | |
+ options: network, | |
+ main_interface: main_interface) | |
+ @logger.debug("debian configure_networks. updated entry \##{i}:\n#{entry}") | |
entries << entry | |
end | |
@@ -37,22 +56,25 @@ | |
temp.close | |
comm.upload(temp.path, "/tmp/vagrant-network-entry") | |
+ @logger.debug("debian configure_networks. uploaded /tmp/vagrant-network-entry") | |
# Bring down all the interfaces we're reconfiguring. By bringing down | |
- # each specifically, we avoid reconfiguring eth0 (the NAT interface) so | |
+ # each specifically, we avoid reconfiguring main_interface (ex. eth0) (the NAT interface) so | |
# SSH never dies. | |
interfaces.each do |interface| | |
- comm.sudo("/sbin/ifdown eth#{interface} 2> /dev/null") | |
- comm.sudo("/sbin/ip addr flush dev eth#{interface} 2> /dev/null") | |
+ comm.sudo("if [ `/bin/cat /sys/class/net/#{interface}/operstate` = up ]; then /sbin/ifdown #{interface} 2> /dev/null; fi") | |
+ comm.sudo("/sbin/ip addr flush dev #{interface} 2> /dev/null") | |
end | |
+ @logger.debug("debian configure_networks. brought down all the network interfaces.") | |
comm.sudo('cat /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post > /etc/network/interfaces') | |
comm.sudo('rm -f /tmp/vagrant-network-interfaces.pre /tmp/vagrant-network-entry /tmp/vagrant-network-interfaces.post') | |
# Bring back up each network interface, reconfigured | |
interfaces.each do |interface| | |
- comm.sudo("/sbin/ifup eth#{interface}") | |
+ comm.sudo("/sbin/ifup #{interface}") | |
end | |
+ @logger.debug("debian configure_networks. brought back up all the network interfaces.") | |
end | |
end | |
end | |
diff -ruN vagrant-1.8.1.orig/plugins/guests/ubuntu/cap/change_host_name.rb vagrant-1.8.1/plugins/guests/ubuntu/cap/change_host_name.rb | |
--- vagrant-1.8.1.orig/plugins/guests/ubuntu/cap/change_host_name.rb 2015-12-25 06:30:19.000000000 +0900 | |
+++ vagrant-1.8.1/plugins/guests/ubuntu/cap/change_host_name.rb 2016-04-24 03:21:13.000000000 +0900 | |
@@ -6,6 +6,23 @@ | |
super | |
end | |
+ def initialize(machine, new_hostname) | |
+ super | |
+ @logger = Log4r::Logger.new("vagrant::plugins::guest") | |
+ end | |
+ | |
+ def get_current_hostname | |
+ return super unless systemd? | |
+ | |
+ hostname = "" | |
+ sudo "hostnamectl --static status" do |type, data| | |
+ hostname = data.chomp if type == :stdout && hostname.empty? | |
+ end | |
+ @logger.debug("ubuntu get_current_hostname got hostname=#{hostname} by hostnamectl.") | |
+ | |
+ hostname | |
+ end | |
+ | |
def update_etc_hostname | |
return super unless systemd? | |
sudo("hostnamectl set-hostname '#{short_hostname}'") | |
diff -ruN vagrant-1.8.1.orig/templates/guests/debian/network_dhcp.erb vagrant-1.8.1/templates/guests/debian/network_dhcp.erb | |
--- vagrant-1.8.1.orig/templates/guests/debian/network_dhcp.erb 2015-12-25 06:30:19.000000000 +0900 | |
+++ vagrant-1.8.1/templates/guests/debian/network_dhcp.erb 2016-04-23 22:46:12.000000000 +0900 | |
@@ -1,13 +1,13 @@ | |
#VAGRANT-BEGIN | |
# The contents below are automatically generated by Vagrant. Do not modify. | |
-auto eth<%= options[:interface] %> | |
-iface eth<%= options[:interface] %> inet dhcp | |
+auto <%= options[:interface] %> | |
+iface <%= options[:interface] %> inet dhcp | |
<% if !options[:use_dhcp_assigned_default_route] %> | |
post-up route del default dev $IFACE || true | |
<% else %> | |
# We need to disable eth0, see GH-2648 | |
- post-up route del default dev eth0 || true | |
+ post-up route del default dev <%= main_interface %> || true | |
post-up dhclient $IFACE | |
- pre-down route add default dev eth0 | |
+ pre-down route add default dev <%= main_interface %> | |
<% end %> | |
#VAGRANT-END | |
diff -ruN vagrant-1.8.1.orig/templates/guests/debian/network_static.erb vagrant-1.8.1/templates/guests/debian/network_static.erb | |
--- vagrant-1.8.1.orig/templates/guests/debian/network_static.erb 2015-12-25 06:30:19.000000000 +0900 | |
+++ vagrant-1.8.1/templates/guests/debian/network_static.erb 2016-04-23 22:45:46.000000000 +0900 | |
@@ -1,7 +1,7 @@ | |
#VAGRANT-BEGIN | |
# The contents below are automatically generated by Vagrant. Do not modify. | |
-auto eth<%= options[:interface] %> | |
-iface eth<%= options[:interface] %> inet static | |
+auto <%= options[:interface] %> | |
+iface <%= options[:interface] %> inet static | |
address <%= options[:ip] %> | |
netmask <%= options[:netmask] %> | |
<% if options[:gateway] %> | |
diff -ruN vagrant-1.8.1.orig/templates/guests/debian/network_static6.erb vagrant-1.8.1/templates/guests/debian/network_static6.erb | |
--- vagrant-1.8.1.orig/templates/guests/debian/network_static6.erb 2015-12-25 06:30:19.000000000 +0900 | |
+++ vagrant-1.8.1/templates/guests/debian/network_static6.erb 2016-04-23 22:45:37.000000000 +0900 | |
@@ -1,7 +1,7 @@ | |
#VAGRANT-BEGIN | |
# The contents below are automatically generated by Vagrant. Do not modify. | |
-auto eth<%= options[:interface] %> | |
-iface eth<%= options[:interface] %> inet6 static | |
+auto <%= options[:interface] %> | |
+iface <%= options[:interface] %> inet6 static | |
address <%= options[:ip] %> | |
netmask <%= options[:netmask] %> | |
<% if options[:gateway] %> |
How to apply this patch:
cd /opt/vagrant/embedded/gems/gems/vagrant-1.8.1
sudo patch -p1 < ${path_to_this_patch_file}
I updated this patch to ignore network interfaces whose name matches the regex ^lxd and ^veth too.
See hashicorp/vagrant#7253 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a patch to vagrant master, please refer to
hashicorp/vagrant#7253