diff --git a/drivers/virtualbox/network.go b/drivers/virtualbox/network.go index e1a91022fb..df1543340e 100644 --- a/drivers/virtualbox/network.go +++ b/drivers/virtualbox/network.go @@ -154,45 +154,36 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, vbox VBoxMana return nil, err } + // Search for an existing host-only adapter. hostOnlyAdapter := getHostOnlyAdapter(nets, hostIP, netmask) if hostOnlyAdapter != nil { return hostOnlyAdapter, nil } // No existing host-only adapter found. Create a new one. - hostOnlyAdapter, err = createHostonlyAdapter(vbox) + _, err = createHostonlyAdapter(vbox) if err != nil { // Sometimes the host-only adapter fails to create. See https://www.virtualbox.org/ticket/14040 // BUT, it is created in fact! So let's wait until it appears last in the list log.Warnf("Creating a new host-only adapter produced an error: %s", err) log.Warn("This is a known VirtualBox bug. Let's try to recover anyway...") - - hostOnlyAdapter, err = waitForNewHostOnlyNetwork(nets, vbox) - if err != nil { - return nil, err - } - - log.Warnf("Found a new host-only adapter: %q", hostOnlyAdapter.Name) } + // It can take some time for an adapter to appear. Let's poll. + hostOnlyAdapter, err = waitForNewHostOnlyNetwork(nets, vbox) + if err != nil { + // Sometimes, Vbox says it created it but then it cannot be found... + return nil, errNewHostOnlyAdapterNotVisible + } + + log.Warnf("Found a new host-only adapter: %q", hostOnlyAdapter.Name) + hostOnlyAdapter.IPv4.IP = hostIP hostOnlyAdapter.IPv4.Mask = netmask if err := hostOnlyAdapter.Save(vbox); err != nil { return nil, err } - // Check that the adapter still exists. - // Sometimes, Vbox says it created it but then it cannot be found... - nets, err = listHostOnlyAdapters(vbox) - if err != nil { - return nil, err - } - - found := getHostOnlyAdapter(nets, hostIP, netmask) - if found == nil { - return nil, errNewHostOnlyAdapterNotVisible - } - return hostOnlyAdapter, nil } diff --git a/drivers/virtualbox/virtualbox_test.go b/drivers/virtualbox/virtualbox_test.go index ff5999740c..7e4044a066 100644 --- a/drivers/virtualbox/virtualbox_test.go +++ b/drivers/virtualbox/virtualbox_test.go @@ -345,7 +345,6 @@ func TestStart(t *testing.T) { {"vbm showvminfo default --machinereadable", `VMState="poweroff"`, nil}, {"vbm list hostonlyifs", "", nil}, {"vbm hostonlyif create", "Interface 'VirtualBox Host-Only Ethernet Adapter' was successfully created", nil}, - {"vbm hostonlyif ipconfig VirtualBox Host-Only Ethernet Adapter --ip 192.168.99.1 --netmask 255.255.255.0", "", nil}, {"vbm list hostonlyifs", ` Name: VirtualBox Host-Only Ethernet Adapter GUID: 786f6276-656e-4074-8000-0a0027000000 @@ -358,6 +357,7 @@ HardwareAddress: 0a:00:27:00:00:00 MediumType: Ethernet Status: Up VBoxNetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter`, nil}, + {"vbm hostonlyif ipconfig VirtualBox Host-Only Ethernet Adapter --ip 192.168.99.1 --netmask 255.255.255.0", "", nil}, {"vbm list dhcpservers", "", nil}, {"vbm list dhcpservers", "", nil}, {"vbm dhcpserver add --netname HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter --ip 192.168.99.6 --netmask 255.255.255.0 --lowerip 192.168.99.100 --upperip 192.168.99.254 --enable", "", nil}, @@ -419,18 +419,6 @@ MediumType: Ethernet Status: Up VBoxNetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter`, nil}, {"vbm hostonlyif ipconfig VirtualBox Host-Only Ethernet Adapter --ip 192.168.99.1 --netmask 255.255.255.0", "", nil}, - {"vbm list hostonlyifs", ` -Name: VirtualBox Host-Only Ethernet Adapter -GUID: 786f6276-656e-4074-8000-0a0027000000 -DHCP: Disabled -IPAddress: 192.168.99.1 -NetworkMask: 255.255.255.0 -IPV6Address: -IPV6NetworkMaskPrefixLength: 0 -HardwareAddress: 0a:00:27:00:00:00 -MediumType: Ethernet -Status: Up -VBoxNetworkName: HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter`, nil}, {"vbm list dhcpservers", "", nil}, {"vbm list dhcpservers", "", nil}, {"vbm dhcpserver add --netname HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter --ip 192.168.99.6 --netmask 255.255.255.0 --lowerip 192.168.99.100 --upperip 192.168.99.254 --enable", "", nil},