hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.18k stars 4.43k forks source link

hyperv : Vagrant change secondary network adapter Vswitch #11073

Open mysiki opened 5 years ago

mysiki commented 5 years ago

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

References

Maybe : https://github.com/hashicorp/vagrant/issues/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 :

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 commented 5 years ago

I think the issue is locate here : https://github.com/hashicorp/vagrant/blob/2272a0bfa7b37e26aaf86466ce29153bf8e09de3/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1#L690

$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 commented 4 years ago

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 commented 4 years ago

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