Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hyperv : Vagrant change secondary network adapter Vswitch #11073

Open
mysiki opened this issue Sep 16, 2019 · 3 comments
Open

hyperv : Vagrant change secondary network adapter Vswitch #11073

mysiki opened this issue Sep 16, 2019 · 3 comments

Comments

@mysiki
Copy link

mysiki commented Sep 16, 2019

Vagrant version

Vagrant 2.2.5

Host operating system

Win10 V1903 Build 18975

Guest operating system

Ubuntu : generic/ubuntu1604

Vagrantfile

config.vm.network "public_network", ip: "192.168.10.78", bridge: "AFSS"

# Typed trigger for calling Powershell script which create other network adapter and set IP from Windows 
## Here for information, NOT WORK IF YOU COPIE IT 
config.trigger.before :"VagrantPlugins::HyperV::Action::StartInstance", type: :action do |hypervtriggerforip|
  hypervtriggerforip.info = "------------------ Set IP with powershell script : #{vm_name} -------------------"
  hypervtriggerforip.only_on  = "#{vm_name}"
  hypervtriggerforip.run = {
    inline: "./scripts/hyperv-set_network_adapter.ps1 -VMName '#{vm_name}' -VSwitchName 'WSL' -VMNetworkAdapter 'WSLAdapter' -GatewayInfo '192.168.50.1' -NetworkInfo '192.168.50.21/24'"
  }
end

Debug output

Expected behavior

 Get-VMNetworkAdapter -VMName "afsvagrantb01t"
Name         IsManagementOs VMName         SwitchName MacAddress   Status IPAddresses
----         -------------- ------         ---------- ----------   ------ -----------
Carte réseau False          afsvagrantb01t AFSS       00155D380186 {Ok}   {192.168.10.78}
WSLAdapter   False          afsvagrantb01t WSL       00155D380187 {Ok}   {192.168.50.21}

#WSLAdapter is create by my powershell script calling in typed trigger

Actual behavior

When first vagrant up, Actual = Expected
After other vagrant up

 Get-VMNetworkAdapter -VMName "afsvagrantb01t"
Name         IsManagementOs VMName         SwitchName MacAddress   Status IPAddresses
----         -------------- ------         ---------- ----------   ------ -----------
Carte réseau False          afsvagrantb01t AFSS       00155D380186 {Ok}   {192.168.10.78}
WSLAdapter   False          afsvagrantb01t AFSS       00155D380187 {Ok}   {192.168.50.21}

Steps to reproduce

  • Hyper-v AFSS vswitch need to be create manually
  1. Vagrant up with simple vagrantfile than add network adapter (see line in Vagrantfile section)
  2. Add manually (or via powershell) secondary adapter bind on other vswitch (WSL in my case)
  3. Stop VM
  4. Run vagrant up

References

Maybe : #10455

Details

In case of Vagrant have hard limitation on Hyper-v network, I used typed trigger and powershell script for :

In first UP, all is find, I get 2 adpater, each on the right VSwitch
In next UP, vagrant change my secondary adapter to Vswitch pass in vagrant file, instead of it already have one...

Trying to :

  • Remove network line from Vagrant file : Error, vagrant force to create network adapter even in case of network are handled separatly
  • Add two line for each network in vagrant file : Error, other network never be take by vagrant
config.vm.network "public_network", ip: "192.168.10.78", bridge: "AFSS"
config.vm.network "public_network", ip: "192.168.50.21", bridge: "WSL"

Until vagrant don't manager hyper-v network, we need a variable to disabled "auto network creation".
Other think, why Vagrant put all the network adapter to the Vswitch give in vagrant file ? It need to control only here adapter, than can be easy doing using the name of the network (currently vagrant do not set the name, hyper-v create a default name, but it can be a parameter of network configuration in vagrant file).

@mysiki
Copy link
Author

mysiki commented Sep 17, 2019

I think the issue is locate here :

Hyper-V\Connect-VMNetworkAdapter -VMNetworkAdapter $Adapter -SwitchName $SwitchName

$Adapter = Hyper-V\Get-VMNetworkAdapter -VM $VM Return all Adapter.
Next line : Hyper-V\Connect-VMNetworkAdapter -VMNetworkAdapter $Adapter -SwitchName $SwitchName will probably change all $Adapter to the Vswitch given in parameter.

@chrisroberts
Copy link
Member

Hi there,

Would you please provide a gist with the debug output from the first successful vagrant up and then the debug output from the second failed vagrant up.

Thanks!

@mysiki
Copy link
Author

mysiki commented Sep 24, 2019

Done :

My vagrant file : Typed trigger and hyperv-set_network_adapter.ps1 are use for set IP from Windows, you can remove it vagrant cannot get IP but the problem keep the same

VAGRANTFILE_API_VERSION = "2"
ENV['VAGRANT_EXPERIMENTAL'] = "typed_triggers"

########################################################################
  # Create and configure VM
########################################################################
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.trigger.before :"VagrantPlugins::HyperV::Action::StartInstance", type: :action do |hypervtriggerforip|
    hypervtriggerforip.info = "------------------ Set IP with powershell script -------------------"
    hypervtriggerforip.run = {
      inline: "./scripts/hyperv-set_network_adapter.ps1 -VMName 'ForDebugIssue11073' -VSwitchName 'AFSS' -VMNetworkAdapter 'Vagrant_adapter' -AutoStartVM $false -GatewayInfo '192.168.10.1' -NetworkInfo '192.168.10.5/24' -DNS '8.8.8.8' -DeriveIpFromNetworkInfo $false"
    }
  end
          
  config.vm.box = "generic/ubuntu1604"
   
  config.vm.network "public_network", bridge: "AFSS"
  config.vm.hostname = "ForDebugIssue11073"
  
  config.vm.provider :hyperv do |h|
  h.vmname = "ForDebugIssue11073"
    h.linked_clone = true
  end
end

First Run : https://gist.github.com/mysiki/7368b9d7a33ab65068d1cf83cf14c272#file-vagrant-log-11073-run01-log

vagrant up --debug 2>&1 | Tee-Object -file vagrant-log-11073-run01.log 

Halt : https://gist.github.com/mysiki/7368b9d7a33ab65068d1cf83cf14c272#file-vagrant-log-11073-run02-log

vagrant halt --debug 2>&1 | Tee-Object -file vagrant-log-11073-run02.log

Powershell, show adapter and add one : Vswitch AFSS and WSL already exist Name Vagrant_adapter are set by my default, by default, Vagrant do not provide name, so adapter get default hyperV name "default connection"

Get-VMNetworkAdapter -VMName 'ForDebugIssue11073'
Name            IsManagementOs VMName             SwitchName MacAddress   Status IPAddresses
----            -------------- ------             ---------- ----------   ------ -----------
Vagrant_adapter False          ForDebugIssue11073 AFSS       00155D3801A4        {}

Add-VMNetworkAdapter -VMName 'ForDebugIssue11073' -Name 'Secondary' -SwitchName 'WSL'

Get-VMNetworkAdapter -VMName 'ForDebugIssue11073'
Name            IsManagementOs VMName             SwitchName MacAddress   Status IPAddresses
----            -------------- ------             ---------- ----------   ------ -----------
Vagrant_adapter False          ForDebugIssue11073 AFSS       00155D3801A6        {}
Secondary       False          ForDebugIssue11073 WSL        000000000000        {}

Secondary UP : https://gist.github.com/mysiki/7368b9d7a33ab65068d1cf83cf14c272#file-vagrant-log-11073-run03-log

vagrant up --debug 2>&1 | Tee-Object -file vagrant-log-11073-run03.log

Result :

Get-VMNetworkAdapter -VMName 'ForDebugIssue11073'
Name            IsManagementOs VMName             SwitchName MacAddress   Status IPAddresses
----            -------------- ------             ---------- ----------   ------ -----------
Vagrant_adapter False          ForDebugIssue11073 AFSS       00155D3801A4 {Ok}   {192.168.10.5}
Secondary       False          ForDebugIssue11073 AFSS       00155D3801A5 {Ok}   {}

Both adapter are set to AFSS Vswitch

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

No branches or pull requests

2 participants