diff --git a/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb b/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb index be52bb0ac..a0fb94c8b 100644 --- a/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +++ b/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb @@ -9,17 +9,7 @@ module Vagrant end def call(env) - # Destroy all the host only network adapters which are empty. - VirtualBox::Global.global(true).host.network_interfaces.each do |iface| - # We only care about host only interfaces - next if iface.interface_type != :host_only - - # Destroy it if there is nothing attached - if iface.attached_vms.empty? - env[:ui].info I18n.t("vagrant.actions.vm.destroy_network.destroying") - iface.destroy - end - end + env[:vm].driver.delete_unused_host_only_networks # Continue along @app.call(env) diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index 99d86011a..59e5eaeb5 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -52,6 +52,28 @@ module Vagrant execute("unregistervm", @uuid, "--delete") end + # Deletes any host only networks that aren't being used for anything. + def delete_unused_host_only_networks + networks = [] + execute("list", "hostonlyifs").split("\n").each do |line| + networks << $1.to_s if line =~ /^Name:\s+(.+?)$/ + end + + execute("list", "vms").split("\n").each do |line| + if line =~ /^".+?"\s+{(.+?)}$/ + execute("showvminfo", $1.to_s, "--machinereadable").split("\n").each do |info| + if info =~ /^hostonlyadapter\d+="(.+?)"$/ + networks.delete($1.to_s) + end + end + end + end + + networks.each do |name| + execute("hostonlyif", "remove", name) + end + end + # Forwards a set of ports for a VM. # # This will not affect any previously set forwarded ports,