Destroying unused network interfaces via VBoxManage

This commit is contained in:
Mitchell Hashimoto 2011-12-21 15:17:10 -08:00
parent d2e33768f3
commit 7a70755362
2 changed files with 23 additions and 11 deletions

View File

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

View File

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