Skip to content

Instantly share code, notes, and snippets.

@savishy
Last active January 22, 2024 07:29
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save savishy/8ed40cd8692e295d64f45e299c2b83c9 to your computer and use it in GitHub Desktop.
Save savishy/8ed40cd8692e295d64f45e299c2b83c9 to your computer and use it in GitHub Desktop.
Notes on Vagrant and Hyper-V

Vagrant and Hyper-V: Working with Virtual Network Interfaces

References

Introduction

These notes will help to:

  • configure an internal virtual network interface (vEthernet) in Hyper-V
  • set it up so it can access the internet from your laptop's WiFi connection
  • use it in Vagrant so that your Vagrant box will be provisioned successfully with hyperv provider.

1. Create Virtual Network Interface

  1. Open Hyper-V Manager. Look at the References section if you cannot find Hyper-V Manager.
  2. Click your machine name on left panel. On the right-most panel you will find the option Virtual Switch Manager. Click that.
  3. Create an Internal interface. Name it "InternalSwitch".

image

2a. Setup Network Sharing between WiFi and InternalSwitch

  1. Now you need to configure your WiFi's network adapter.
  2. In the Run Dialog, type control netconnections. This opens the Network Adapter window.
  3. Right-click your WiFi network adapter, open Properties > Sharing tab.
  4. Configure sharing between WiFi adapter and the InternalSwitch virtual interface created above.

image

3. Configure IPV4 Settings for InternalSwitch

❗ *If your VM is running while you configure network, please restart the VM (vagrant reload) !

Next, configure the IPV4 Settings for the InternalSwitch adapter.

  1. Open properties for the InternalSwitch adapter.
  2. Click "Internet Protocol Version 4" and click "Properties".

And then configure its properties as follows (only the DNS address may be missing usually):

image

4. Make sure Docker for Windows is turned off

At this point, if you have Docker for Windows running, your virtual interface will show "No Internet Access". See the references section; there is documentation that says only one such internal interface can be used at a time.

image

Quit Docker.

image

The network interface will now show an internet connection.

image

5. Execute vagrant up

Its assumed you have a Vagrantfile with a hyperv box and configuration - example this Vagrantfile.

Run vagrant up --provider=hyperv.

❗️ Make sure to from an elevated terminal. ❗️ Vagrant does not allow auto-configuring the interface. It will ask you to choose the interface, select the appropriate number:

    devops-host: Please choose a switch to attach to your Hyper-V instance.
    devops-host: If none of these are appropriate, please open the Hyper-V manager
    devops-host: to create a new virtual switch.
    devops-host:
    devops-host: 1) DockerNAT
    devops-host: 2) Layered_Wi-Fi
    devops-host: 3) InternalSwitch
    devops-host:
    devops-host: What switch would you like to use? 3

Troubleshooting: Error during vagrant up: failed to get IP address

==> devops-host: Waiting for the machine to report its IP address...
    devops-host: Timeout: 120 seconds
Hyper-V failed to determine your machine's IP address within the
configured timeout. Please verify the machine properly booted and
the network works. To do this, open the Hyper-V manager, find your
virtual machine, and connect to it.

The most common cause for this error is that the running virtual
machine doesn't have the latest Hyper-V integration drivers. Please
research for your operating system how to install these in order
for the VM to properly communicate its IP address to Hyper-V.

The root cause is that it runs a script C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.9.7\plugins\providers\hyperv\scripts\get_network_config.ps1 which ultimately calls Get-VMNetworkAdapter.

And this cmdlet returns empty IP addresses, which trips up vagrant.

Get-VMNetworkAdapter -VMName devops-host

Name            IsManagementOs VMName      SwitchName     MacAddress   Status                      IPAddresses
----            -------------- ------      ----------     ----------   ------                      -----------
Network Adapter False          devops-host InternalSwitch 00155D570114 {Degraded, ProtocolVersion} {}

Cause #1: the Vagrant Box itself

There is very little documentation on this online. Some references:

  1. hashicorp/vagrant#3247
  2. technet forum post that says this could be related to the way the Linux VM is created.

I tried switching different Vagrant hyperv boxes:

  • generic/ubuntu1604: 👎
  • kmm/ubuntu-xenial64: 👍 This worked and went past the timeout!

image

Cause #2: IPV6

On one occasion, I had to make sure that IPV6 was disabled on the network properties for the InternalSwitch virtual interface.

Cause #3: hv_kvp_daemon service on Ubuntu

On another occasion, I had to install a set of packages inside my Ubuntu Hyper-V VM to ensure it connects properly. The package was called linux-cloud-tools or something ( link )

Troubleshooting: IPV6 Address shows up during vagrant up and also

Symptom #1: IPV6 address shows up during vagrant up --provider=hyperv

==> chefserver: Waiting for the machine to report its IP address...
    chefserver: Timeout: 240 seconds
    chefserver: IP: fe80::215:5dff:fe00:1102
==> chefserver: Waiting for machine to boot. This may take a few minutes...
    chefserver: SSH address: fe80::215:5dff:fe00:1102:22
    chefserver: SSH username: vagrant
    chefserver: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

Symptom #2: InternalSwitch Virtual Adapter shows "No Internet Access" when you right-click it (from control netconnections window) and select "Status".

Possible Cause and Solution

  • I had created the InternalSwitch switch (as described above) many months ago. Since then my Windows had updated to Windows 10 build 1607.
  • There are known issues with Internet Connection Sharing not working after that update. link.
  • I deleted Hyper-V vagrant boxes that were using the switch.
  • I deleted the InternalSwitch Virtual Adapter from Hyper-V Manager.
  • I disabled all existing Hyper-V Virtual Ethernet Adapters from control netconnections Window.
  • I re-created the Adapter again using above documentation. This time it showed "Internet Access" in the Properties. 👍
  • I did a vagrant up --provider=hyperv again. This time the box got an IPV4 address! 👍
@mloskot
Copy link

mloskot commented Mar 18, 2018

Good notes, thanks!

Regarding the Vagrant Boxes for Hyper-V, I've tested successfully the following:

  • bento/ubuntu-16.04
  • synax/ubuntu-17-10-server

Also, instead of creating new switch, the 'new' Default Switch worked for me.

@h3ct0rjs
Copy link

Hi @savishy this thing really sucks. We should be able to programmatically modified the virtual network adapters, Why is this not implemented yet ?

@savishy
Copy link
Author

savishy commented Oct 10, 2020

If I had to guess it would be the problem that plagues all Open Source projects- too many backlog items and too few maintainers!
BTW I moved out of Vagrant a long time ago. I don't miss it.

@SashaMkrt
Copy link

you can set virtualswitch name in Vagrantfile:
config.vm.network "public_network", bridge: "InternalSwitch"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment