Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIST natpf rules in Virtualbox/Vagrant

I often get errors like this when running Vagrant:

VBoxManage: error: A NAT rule of this name already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component     NATEngine, interface INATEngine, callee nsISupports
VBoxManage: error: Context: "AddRedirect(Bstr(strName).raw(), proto,    Bstr(strHostIp).raw(), RTStrToUInt16(strHostPort), Bstr(strGuestIp).raw(),       RTStrToUInt16(strGuestPort))" at line 1524 of file VBoxManageModifyVM.cpp

I'd like to remove all port forwarding rules before doing vagrant up, but I have trouble LISTING natpf rules. Is there any way to do it using vboxmanage or via some facilities in Vagrant?

Update: Vagrant version 1.3.4. I can replicate the problem as follows: start vm installation normal way (vagrant up) and force power off the vm during installation (this simulates e.g. failed install). Then the natpf1 port forwarding rule is left in the system. The only way to clean it up is like vboxmanage modifyvm #{vmid} --natpf1 delete rule_name, but you have to know the rule name beforehand... Furthermore, the rule stays there after vagrant destroy and it seems that my Ruby natpf1 clearing function present in Vagrant.configure is not ran, which means that the stale rule still clashes with "fresh" one that Vagrant attempts to create.

like image 501
LetMeSOThat4U Avatar asked Feb 05 '14 13:02

LetMeSOThat4U


1 Answers

This got the job done for me:

VBoxManage showvminfo $VM_NAME --machinereadable | awk -F '[",]' '/^Forwarding/ { printf ("Rule %s host port %d forwards to guest port %d\n", $2, $5, $7); }'
like image 180
Andrew Wolfe Avatar answered Oct 10 '22 05:10

Andrew Wolfe