@@ -16,16 +16,23 @@ import (
1616 "github.com/docker/machine/libmachine/state"
1717)
1818
19+ const (
20+ DefaultProtocol = iota
21+ PreferIPv4
22+ PreferIPv6
23+ )
24+
1925type Driver struct {
2026 * drivers.BaseDriver
21- Boot2DockerURL string
22- VSwitch string
23- DiskSize int
24- MemSize int
25- CPU int
26- MacAddr string
27- VLanID int
28- DisableDynamicMemory bool
27+ Boot2DockerURL string
28+ VSwitch string
29+ DiskSize int
30+ MemSize int
31+ CPU int
32+ MacAddr string
33+ VLanID int
34+ DisableDynamicMemory bool
35+ PreferableNetworkProtocol int
2936}
3037
3138const (
@@ -99,6 +106,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
99106 Usage : "Disable dynamic memory management setting" ,
100107 EnvVar : "HYPERV_DISABLE_DYNAMIC_MEMORY" ,
101108 },
109+ mcnflag.IntFlag {
110+ Name : "hyperv-preferable-network-protocol" ,
111+ Usage : "Preferable network protocol (IPv4/v6)" ,
112+ EnvVar : "HYPERV_PREFERABLE_NETWORK_PROTOCOL" ,
113+ },
102114 }
103115}
104116
@@ -428,6 +440,10 @@ func (d *Driver) Kill() error {
428440 return nil
429441}
430442
443+ func isIPv4 (address string ) bool {
444+ return strings .Count (address , ":" ) < 2
445+ }
446+
431447func (d * Driver ) GetIP () (string , error ) {
432448 s , err := d .GetState ()
433449 if err != nil {
@@ -437,7 +453,7 @@ func (d *Driver) GetIP() (string, error) {
437453 return "" , drivers .ErrHostIsNotRunning
438454 }
439455
440- stdout , err := cmdOut ("((" , "Hyper-V\\ Get-VM" , d .MachineName , ").networkadapters[0]).ipaddresses[0] " )
456+ stdout , err := cmdOut ("((" , "Hyper-V\\ Get-VM" , d .MachineName , ").networkadapters[0]).ipaddresses" )
441457 if err != nil {
442458 return "" , err
443459 }
@@ -447,7 +463,33 @@ func (d *Driver) GetIP() (string, error) {
447463 return "" , fmt .Errorf ("IP not found" )
448464 }
449465
450- return resp [0 ], nil
466+ switch d .PreferableNetworkProtocol {
467+ case PreferIPv4 :
468+ for _ , ipStr := range resp {
469+ ip := net .ParseIP (ipStr )
470+ if isIPv4 (ipStr ) && ip .To4 () != nil && ip .IsGlobalUnicast () {
471+ return ipStr , nil
472+ }
473+ }
474+
475+ case PreferIPv6 :
476+ for _ , ipStr := range resp {
477+ ip := net .ParseIP (ipStr )
478+ if ! isIPv4 (ipStr ) && ip .IsGlobalUnicast () {
479+ return ipStr , nil
480+ }
481+ }
482+
483+ default :
484+ for _ , ipStr := range resp {
485+ ip := net .ParseIP (ipStr )
486+ if ip .IsGlobalUnicast () {
487+ return ipStr , nil
488+ }
489+ }
490+ }
491+
492+ return "" , nil
451493}
452494
453495func (d * Driver ) publicSSHKeyPath () string {
0 commit comments