Skip to content

Commit

Permalink
Add optional Virtual Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumerose committed Oct 5, 2020
1 parent d2fe955 commit b84bb76
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyperv

import (
"errors"
"fmt"
"net"
"time"
Expand Down Expand Up @@ -158,6 +159,10 @@ func (d *Driver) PreCreateCheck() error {
return ErrNotAdministrator
}

if d.VirtualSwitch == "" {
return nil
}

// Check that there is a virtual switch already configured
if _, err := d.chooseVirtualSwitch(); err != nil {
return err
Expand All @@ -172,21 +177,32 @@ func (d *Driver) Create() error {
return err
}

log.Infof("Creating VM...")
virtualSwitch, err := d.chooseVirtualSwitch()
if err != nil {
return err
}

log.Infof("Using switch %q", virtualSwitch)

if err := cmd("Hyper-V\\New-VM",
args := []string{
"Hyper-V\\New-VM",
d.MachineName,
"-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")),
"-SwitchName", quote(virtualSwitch),
"-MemoryStartupBytes", toMb(d.Memory)); err != nil {
"-MemoryStartupBytes", toMb(d.Memory),
}
if d.VirtualSwitch != "" {
log.Infof("Creating VM...")
virtualSwitch, err := d.chooseVirtualSwitch()
if err != nil {
return err
}
log.Infof("Using switch %q", virtualSwitch)
args = append(args, "-SwitchName", quote(virtualSwitch))
}

if err := cmd(args...); err != nil {
return err
}

if d.VirtualSwitch == "" {
if err := cmd("Hyper-V\\Remove-VMNetworkAdapter", "-VMName", d.MachineName); err != nil {
return err
}
}

if d.DisableDynamicMemory {
if err := cmd("Hyper-V\\Set-VMMemory",
"-VMName", d.MachineName,
Expand All @@ -203,7 +219,7 @@ func (d *Driver) Create() error {
}
}

if d.MacAddress != "" {
if d.VirtualSwitch != "" && d.MacAddress != "" {
if err := cmd("Hyper-V\\Set-VMNetworkAdapter",
"-VMName", d.MachineName,
"-StaticMacAddress", fmt.Sprintf("\"%s\"", d.MacAddress)); err != nil {
Expand All @@ -223,19 +239,7 @@ func (d *Driver) Create() error {

func (d *Driver) chooseVirtualSwitch() (string, error) {
if d.VirtualSwitch == "" {
// Default to the first external switch and in the process avoid DockerNAT
stdout, err := cmdOut("[Console]::OutputEncoding = [Text.Encoding]::UTF8; (Hyper-V\\Get-VMSwitch -SwitchType External).Name")
if err != nil {
return "", err
}

switches := parseLines(stdout)

if len(switches) < 1 {
return "", fmt.Errorf("no external virtual switch found. A valid virtual switch must be available for this command to run")
}

return switches[0], nil
return "", errors.New("no virtual switch given")
}

stdout, err := cmdOut("[Console]::OutputEncoding = [Text.Encoding]::UTF8; (Hyper-V\\Get-VMSwitch).Name")
Expand All @@ -262,6 +266,10 @@ func (d *Driver) chooseVirtualSwitch() (string, error) {

// waitForIP waits until the host has a valid IP
func (d *Driver) waitForIP() (string, error) {
if d.VirtualSwitch == "" {
return "", errors.New("no virtual switch given")
}

log.Infof("Waiting for host to start...")

for {
Expand Down Expand Up @@ -298,6 +306,10 @@ func (d *Driver) Start() error {
return err
}

if d.VirtualSwitch == "" {
return nil
}

ip, err := d.waitForIP()
if err != nil {
return err
Expand Down Expand Up @@ -365,6 +377,10 @@ func (d *Driver) Kill() error {
}

func (d *Driver) GetIP() (string, error) {
if d.VirtualSwitch == "" {
return "", errors.New("no virtual switch given")
}

s, err := d.GetState()
if err != nil {
return "", err
Expand Down

0 comments on commit b84bb76

Please sign in to comment.