| 1 |
package hyperv |
| 2 |
|
| 3 |
import ( |
| 4 |
"path/filepath" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/crc-org/crc/v2/pkg/crc/constants" |
| 8 |
"github.com/crc-org/crc/v2/pkg/crc/machine/config" |
| 9 |
"github.com/crc-org/crc/v2/pkg/crc/network" |
| 10 |
"github.com/crc-org/crc/v2/pkg/drivers/hyperv" |
| 11 |
winnet "github.com/crc-org/crc/v2/pkg/os/windows/network" |
| 12 |
"github.com/crc-org/machine/libmachine/drivers" |
| 13 |
) |
| 14 |
|
| 15 |
func CreateHost(machineConfig config.MachineConfig) *hyperv.Driver { |
| 16 |
hypervDriver := hyperv.NewDriver(machineConfig.Name, constants.MachineBaseDir) |
| 17 |
|
| 18 |
config.InitVMDriverFromMachineConfig(machineConfig, hypervDriver.VMDriver) |
| 19 |
|
| 20 |
hypervDriver.DisableDynamicMemory = true |
| 21 |
|
| 22 |
if machineConfig.NetworkMode == network.UserNetworkingMode { |
| 23 |
hypervDriver.VirtualSwitch = "" |
| 24 |
} else { |
| 25 |
// Determine the Virtual Switch to be used |
| 26 |
_, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork) |
| 27 |
hypervDriver.VirtualSwitch = switchName |
| 28 |
} |
| 29 |
|
| 30 |
hypervDriver.SharedDirs = configureShareDirs(machineConfig) |
| 31 |
return hypervDriver |
| 32 |
} |
| 33 |
|
| 34 |
// converts a path like c:\users\crc to /mnt/c/users/crc |
| 35 |
func convertToUnixPath(path string) string { |
| 36 |
/* podman internally converts windows style paths like C:\Users\crc to |
| 37 |
* /mnt/c/Users/crc so it expects the shared folder to be mounted under |
| 38 |
* '/mnt' instead of '/' like in the case of macOS and linux |
| 39 |
* see: https://github.com/containers/podman/blob/468aa6478c73e4acd8708ce8bb0bb5a056f329c2/pkg/specgen/winpath.go#L24-L59 |
| 40 |
*/ |
| 41 |
path = filepath.ToSlash(path) |
| 42 |
if len(path) > 1 && path[1] == ':' { |
| 43 |
return ("/mnt/" + strings.ToLower(path[0:1]) + path[2:]) |
| 44 |
} |
| 45 |
return path |
| 46 |
} |
| 47 |
|
| 48 |
func configureShareDirs(machineConfig config.MachineConfig) []drivers.SharedDir { |
| 49 |
var sharedDirs []drivers.SharedDir |
| 50 |
for _, dir := range machineConfig.SharedDirs { |
| 51 |
sharedDir := drivers.SharedDir{ |
| 52 |
Source: dir, |
| 53 |
Target: convertToUnixPath(dir), |
| 54 |
Tag: "crc-dir0", // smb share 'crc-dir0' is created in the msi |
| 55 |
Type: "cifs", |
| 56 |
Username: machineConfig.SharedDirUsername, |
| 57 |
} |
| 58 |
sharedDirs = append(sharedDirs, sharedDir) |
| 59 |
} |
| 60 |
return sharedDirs |
| 61 |
} |