From 474ac3a63d6cfab778a755ec0ee2f4f63960f98c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 31 Dec 2011 11:53:04 -0800 Subject: [PATCH] Enabling bridged networks on Debian --- lib/vagrant/action/vm/bridged_network.rb | 2 ++ lib/vagrant/guest/base.rb | 16 ++++++++- lib/vagrant/guest/debian.rb | 36 ++++++++++++++++--- templates/guests/debian/network_bridged.erb | 7 ++++ .../debian/network_hostonly.erb} | 4 +-- templates/locales/en.yml | 8 +++++ 6 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 templates/guests/debian/network_bridged.erb rename templates/{network_entry_debian.erb => guests/debian/network_hostonly.erb} (85%) diff --git a/lib/vagrant/action/vm/bridged_network.rb b/lib/vagrant/action/vm/bridged_network.rb index 1a1a7b9c5..cdc2867df 100644 --- a/lib/vagrant/action/vm/bridged_network.rb +++ b/lib/vagrant/action/vm/bridged_network.rb @@ -42,8 +42,10 @@ module Vagrant @env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.enabling") # Prepare for new networks + @env[:vm].guest.prepare_bridged_networks(networks) # Enable the networks + @env[:vm].guest.enable_bridged_networks(networks) end end diff --git a/lib/vagrant/guest/base.rb b/lib/vagrant/guest/base.rb index e1704b3d0..974ce064e 100644 --- a/lib/vagrant/guest/base.rb +++ b/lib/vagrant/guest/base.rb @@ -79,8 +79,22 @@ module Vagrant # @param [Hash] net_options The options for the network. def enable_host_only_network(net_options); end + # Prepares the guest for bridged networks. + # + # @param [Array] networks Array of networks to prepare for. + def prepare_bridged_networks(networks) + raise BaseError, :_key => :unsupported_host_only + end + + # Enable bridged networks on a guest. + # + # @param [Array] networks Array of networks to prepare for. + def enable_bridged_networks(networks) + raise BaseError, :_key => :unsupported_bridged + end + def change_host_name(name) - raise BaseError, :_key => :unsupported_host_name + raise BaseError, :_key => :unsupported_bridged end end end diff --git a/lib/vagrant/guest/debian.rb b/lib/vagrant/guest/debian.rb index b29c97ffc..da6bb7988 100644 --- a/lib/vagrant/guest/debian.rb +++ b/lib/vagrant/guest/debian.rb @@ -2,17 +2,16 @@ module Vagrant module Guest class Debian < Linux def prepare_host_only_network(net_options=nil) - # Remove any previous host only network additions to the - # interface file. + # Remove any previous host only network additions to the interface file. vm.ssh.execute do |ssh| - # Clear out any previous entries - ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces") + ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN-HOSTONLY/,/^#VAGRANT-END-HOSTONLY/ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces") ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-interfaces > /etc/network/interfaces'") end end def enable_host_only_network(net_options) - entry = TemplateRenderer.render('network_entry_debian', :net_options => net_options) + entry = TemplateRenderer.render('guests/debian/network_hostonly', + :net_options => net_options) vm.ssh.upload!(StringIO.new(entry), "/tmp/vagrant-network-entry") vm.ssh.execute do |ssh| @@ -22,6 +21,33 @@ module Vagrant end end + def prepare_bridged_networks(networks) + # Remove any previous bridged network additions to the interface file. + vm.ssh.execute do |ssh| + ssh.exec!("sudo sed -e '/^#VAGRANT-BEGIN-BRIDGED/,/^#VAGRANT-END-BRIDGED/ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces") + ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-interfaces > /etc/network/interfaces'") + end + end + + def enable_bridged_networks(networks) + entry = TemplateRenderer.render('guests/debian/network_bridged', + :networks => networks) + + vm.ssh.upload!(StringIO.new(entry), "/tmp/vagrant-network-entry") + + vm.ssh.execute do |ssh| + networks.each do |network| + ssh.exec!("sudo /sbin/ifdown eth#{network[:adapter]} 2> /dev/null") + end + + ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-entry >> /etc/network/interfaces'") + + networks.each do |network| + ssh.exec!("sudo /sbin/ifup eth#{network[:adapter]}") + end + end + end + def change_host_name(name) vm.ssh.execute do |ssh| if !ssh.test?("sudo hostname | grep '#{name}'") diff --git a/templates/guests/debian/network_bridged.erb b/templates/guests/debian/network_bridged.erb new file mode 100644 index 000000000..c9342b9b6 --- /dev/null +++ b/templates/guests/debian/network_bridged.erb @@ -0,0 +1,7 @@ +<% networks.each do |network| -%> +#VAGRANT-BEGIN-BRIDGED +# The contents below are automatically generated by Vagrant. Do not modify. +auto eth<%= network[:adapter] %> +iface eth<%= network[:adapter] %> inet dhcp +#VAGRANT-END-BRIDGED +<% end -%> \ No newline at end of file diff --git a/templates/network_entry_debian.erb b/templates/guests/debian/network_hostonly.erb similarity index 85% rename from templates/network_entry_debian.erb rename to templates/guests/debian/network_hostonly.erb index 04def830c..61d2653f3 100644 --- a/templates/network_entry_debian.erb +++ b/templates/guests/debian/network_hostonly.erb @@ -1,8 +1,8 @@ -#VAGRANT-BEGIN +#VAGRANT-BEGIN-HOSTONLY # The contents below are automatically generated by Vagrant. # Please do not modify any of these contents. auto eth<%= net_options[:adapter] %> iface eth<%= net_options[:adapter] %> inet static address <%= net_options[:ip] %> netmask <%= net_options[:netmask] %> -#VAGRANT-END +#VAGRANT-END-HOSTONLY diff --git a/templates/locales/en.yml b/templates/locales/en.yml index de714feca..d24cc859a 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -564,6 +564,14 @@ en: guest: base: + unsupported_bridged: |- + Bridged networking is very distro-specific. Vagrant has support for many + distros built-in: Debian, Ubuntu, Gentoo, and RedHat. The distro of your VM + couldn't be detected, or isn't supported for host only networking. + + Most of the time this is simply due to the fact that no one has contributed + back the SSH commands necessary to set this up. Please report a bug and this + will be fixed for your distro. unsupported_host_only: |- Host only networking is very distro-specific. Vagrant has support for many distros built-in: Debian, Ubuntu, Gentoo, and RedHat. The distro of your VM