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

Add optional Virtual Switch #46

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions drivers/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hyperv

import (
"encoding/json"
"errors"
"fmt"
"net"
"time"
Expand Down Expand Up @@ -191,6 +192,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 @@ -205,21 +210,32 @@ func (d *Driver) Create() error {
return err
}

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

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

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

if err := cmd("Hyper-V\\New-VM",
d.MachineName,
"-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")),
"-SwitchName", quote(virtualSwitch),
"-MemoryStartupBytes", toMb(d.Memory)); err != nil {
return err
if d.VirtualSwitch == "" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this move up and be part of the else clause of:

if d.VirtualSwitch != "" {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VM is created line 229 here in cmd(args...). else is before and too early in the process.

if err := cmd("Hyper-V\\Remove-VMNetworkAdapter", "-VMName", d.MachineName); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guillaumerose this logic is here just to remove the crc named network adapter in case o there is no virtualswitch provided? Is this virtualswitch present by default?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hyper-V\New-VM always attach a network adapter. I didn't found an argument to disable it. I had to remove it by hand just after.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attach a network adapter as name of the machine? @gbraad you have any idea around it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New-vm might enforce an interface, just not aware this was the case. Will check

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no way around this

return err
}
}

if d.DisableDynamicMemory {
if err := cmd("Hyper-V\\Set-VMMemory",
"-VMName", d.MachineName,
Expand All @@ -236,7 +252,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 @@ -256,19 +272,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 @@ -295,6 +299,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 @@ -331,6 +339,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 @@ -398,6 +410,10 @@ func (d *Driver) Kill() error {
}

func (d *Driver) GetIP() (string, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

over time we could get rid of these functions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

if d.VirtualSwitch == "" {
return "", errors.New("no virtual switch given")
}

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