Debug output
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
There was an error while executing VBoxManage
, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["modifyvm", "de8f03f8-3b2e-4de9-acb7-b55669b5faf7", "--natpf1", "delete", "ssh", "--natpf1", "delete", "ssh"]
Stderr: VBoxManage.exe: error: Code E_INVALIDARG (0x80070057) (extended info not available)
VBoxManage.exe: error: Context: "RemoveRedirect(Bstr(ValueUnion.psz).raw())" at line 2108 of file VBoxManageModifyVM.cpp
Expected behavior
Vagrant up should not stop with errors when clearing forwarded ports.
Actual behavior
When vagrant up tries to clear any forwarded ports, it lists the forwarded ports and the same port gets duplicated which causes the issue.
read_forwarded_port() output:
DEBUG virtualbox_7_0: - [1, “ssh”, 2222, 22, “127.0.0.1”]
DEBUG virtualbox_7_0: - [1, “ssh”, 2222, 22, “127.0.0.1”]
See more troubleshooting details here:
https://discuss.hashicorp.com/t/vbox-vm-with-vagrant-stopped-working/45826/4
The issue has to do with the routine read_forwarded_port(). It needs to be revisited to account for VB VM that makes use of Snapshots not to have the same forwarded port reported multiple times.
hashicorp/vagrant/blob/8549450cff40a55ec193540a5387a09caee309b4/plugins/providers/virtualbox/driver/version_7_0.rb#L224
Reproduction information
Vagrant version
vagrant 2.3.4
Host operating system
Windows 10 Enterprise
VirtualBox Version 7.0.0 r153978 (Qt5.15.2)
Steps to reproduce
- Create a VM with vagrant up
- Vagrant halt
- Take a snapshot of the VM with VB gui (--> that would add a new section in the .vbox config file that duplicates the forwarded port)
- vagrant up
Vagrantfile
Here is the Vagrantfile but
require_relative 'devenv'
if not is_valid_environment()
exit
end
settings = load_settings('vagrant.yml')
box_disk_size_gb = settings['vm']['disksize']
fw_home = settings['framework']['home']
ansible_template = "#{fw_home}/template"
vb_name = File.basename(Dir.getwd)
Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
config.vm.hostname = vb_name
config.vm.box = settings['framework']['box_name']
if settings['framework']['box_url'] != ""
config.vm.box_url = settings['framework']['box_url']
end
config.vm.box_check_update = false
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = false
end
config.vm.synced_folder ".", "/vagrant"
config.vm.synced_folder ansible_template, "/template"
(settings['ssh']['dir'] != nil) && (config.vm.synced_folder settings['ssh']['dir'], "/ssh_config")
settings['vm']['mounts'].each do |mount|
config.vm.synced_folder mount[0], mount[1], mount_options: [ "dmode=777" ]
end
settings['vm']['ports'].each do |port|
config.vm.network "forwarded_port", guest: port[0], host: port[1]
end
config.disksize.size = "#{box_disk_size_gb}GB"
config.vm.provider "virtualbox" do |vb|
vb.gui = settings['vm']['gui']
vb.name = vb_name
vb.memory = settings['vm']['memory']
vb.cpus = settings['vm']['cpus']
vb.customize ["modifyvm", :id, "--usbxhci", "off"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ["modifyvm", :id, "--vram", "64"]
vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
vb.customize ["modifyvm", :id, "--cpuexecutioncap", settings['vm']['cpuexecutioncap']]
end
config.vm.provision "ansible_local" do |ansible|
ansible.compatibility_mode = "2.0"
ansible.config_file = "/template/ansible.cfg"
ansible.install_mode = 'pip_args_only'
ansible.pip_install_cmd = "sudo apt-get install -y python3-pip python-is-python3 haveged && sudo ln -s -f /usr/bin/pip3 /usr/bin/pip"
ansible.pip_args = "ansible==2.10.7"
ansible.playbook = "playbook.yml"
ansible.raw_arguments = ['--diff']
end
end
Workaround (temporary)
Change definition of read_forwarded_ports() in C:\HashiCorp\Vagrant\embedded\gems\2.3.4\gems\vagrant-2.3.4\plugins\providers\virtualbox\driver\version_7_0.rb
def read_forwarded_ports(uuid=nil, active_only=false)
return super
end
Note: This workaround might not work with initial release of VB 7.0
provider/virtualbox networking