Skip to content

Commit

Permalink
Issue #722 Allow to use 'crc' named Virtual Switch as alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
gbraad authored and praveenkumar committed Nov 25, 2019
1 parent 5ab8ea8 commit 8fa57f1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pkg/crc/machine/hyperv/constants_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hyperv

const (
// Alternative
AlternativeNetwork = "crc"
)
3 changes: 2 additions & 1 deletion pkg/crc/machine/hyperv/driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func CreateHost(machineConfig config.MachineConfig) *hyperv.Driver {
hypervDriver.BundleName = machineConfig.BundleName
hypervDriver.Memory = machineConfig.Memory

_, switchName := winnet.GetDefaultSwitchName()
// Determine the Virtual Switch to be used
_, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork)
hypervDriver.VirtualSwitch = switchName

// DiskPath should come from the bundle's metadata (unflattened)
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 10 additions & 21 deletions pkg/crc/preflight/preflight_checks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

"github.com/code-ready/crc/pkg/crc/errors"
"github.com/code-ready/crc/pkg/crc/logging"

winnet "github.com/code-ready/crc/pkg/os/windows/network"
"github.com/code-ready/crc/pkg/os/windows/powershell"

"github.com/code-ready/crc/pkg/crc/machine/hyperv"
)

const (
// Fall Creators update comes with the "Default Switch"
minimumWindowsReleaseId = 1709

hypervDefaultVirtualSwitchName = "Default Switch"
hypervDefaultVirtualSwitchId = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"
)

func checkVersionOfWindowsUpdate() (bool, error) {
Expand Down Expand Up @@ -126,25 +127,13 @@ func fixUserPartOfHyperVAdmins() (bool, error) {
}

func checkIfHyperVVirtualSwitchExists() (bool, error) {
// TODO: vswitch configurable (use MachineConfig)
switchName := hypervDefaultVirtualSwitchName

// check for default switch by using the Id
if switchName == hypervDefaultVirtualSwitchName {
checkIfDefaultSwitchExists := fmt.Sprintf("Get-VMSwitch -Id %s | ForEach-Object { $_.Name }", hypervDefaultVirtualSwitchId)
_, stdErr, err := powershell.Execute(checkIfDefaultSwitchExists)
if err != nil {
logging.Debug(err.Error())
return false, errors.New("Failed checking if Hyper-V Default Switch exists")
}

if !strings.Contains(stdErr, "Get-VMSwitch") {
// found the default
return true, nil
} else {
return false, errors.New("Incorrect permissions")
}
switchName := hyperv.AlternativeNetwork

// use winnet instead
exists, foundName := winnet.SelectSwitchByNameOrDefault(switchName)
if exists {
logging.Info("Found Virtual Switch to use: ", foundName)
return true, nil
}

return false, errors.New("Virtual Switch not found")
Expand Down
7 changes: 6 additions & 1 deletion pkg/crc/services/dns/dns_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"github.com/code-ready/crc/pkg/os/windows/win32"
)

const (
// Alternative
AlternativeNetwork = "crc"
)

func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *services.ServicePostStartResult) (services.ServicePostStartResult, error) {
// bailout for Virtualbox
if serviceConfig.DriverName == "virtualbox" {
Expand All @@ -23,7 +28,7 @@ func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *se
return *result, nil
}

_, switchName := winnet.GetDefaultSwitchName()
_, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork)
networkInterface := fmt.Sprintf("vEthernet (%s)", switchName)

setInterfaceNameserverValue(networkInterface, serviceConfig.IP)
Expand Down
25 changes: 25 additions & 0 deletions pkg/os/windows/network/vswitch_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (

const hypervDefaultVirtualSwitchId = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"

func SelectSwitchByNameOrDefault(name string) (bool, string) {
// if named exists
if ExistsSwitchByName(name) {
return true, name
}

// else use Default
return GetDefaultSwitchName()
}

func ExistsSwitchByName(name string) bool {
getSwitchByNameCmd := fmt.Sprintf("Get-VMSwitch %s | ForEach-Object { $_.Name }", name)
stdOut, stdErr, _ := powershell.Execute(getSwitchByNameCmd)

// If stdErr contains the command then execution failed
if strings.Contains(stdErr, "Get-VMSwitch") {
return false
}

if strings.Contains(stdOut, name) {
return true
}
return false
}

func GetDefaultSwitchName() (bool, string) {
getDefaultSwitchNameCmd := fmt.Sprintf("[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-VMSwitch -Id %s | ForEach-Object { $_.Name }", hypervDefaultVirtualSwitchId)
stdOut, stdErr, _ := powershell.Execute(getDefaultSwitchNameCmd)
Expand Down

0 comments on commit 8fa57f1

Please sign in to comment.