File: driver_windows.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (61 lines) | stat: -rw-r--r-- 2,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package hyperv

import (
	"path/filepath"
	"strings"

	"github.com/crc-org/crc/v2/pkg/crc/constants"
	"github.com/crc-org/crc/v2/pkg/crc/machine/config"
	"github.com/crc-org/crc/v2/pkg/crc/network"
	"github.com/crc-org/crc/v2/pkg/drivers/hyperv"
	winnet "github.com/crc-org/crc/v2/pkg/os/windows/network"
	"github.com/crc-org/machine/libmachine/drivers"
)

func CreateHost(machineConfig config.MachineConfig) *hyperv.Driver {
	hypervDriver := hyperv.NewDriver(machineConfig.Name, constants.MachineBaseDir)

	config.InitVMDriverFromMachineConfig(machineConfig, hypervDriver.VMDriver)

	hypervDriver.DisableDynamicMemory = true

	if machineConfig.NetworkMode == network.UserNetworkingMode {
		hypervDriver.VirtualSwitch = ""
	} else {
		// Determine the Virtual Switch to be used
		_, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork)
		hypervDriver.VirtualSwitch = switchName
	}

	hypervDriver.SharedDirs = configureShareDirs(machineConfig)
	return hypervDriver
}

// converts a path like c:\users\crc to /mnt/c/users/crc
func convertToUnixPath(path string) string {
	/* podman internally converts windows style paths like C:\Users\crc  to
	 * /mnt/c/Users/crc so it expects the shared folder to be mounted under
	 * '/mnt' instead of '/' like in the case of macOS and linux
	 * see: https://github.com/containers/podman/blob/468aa6478c73e4acd8708ce8bb0bb5a056f329c2/pkg/specgen/winpath.go#L24-L59
	 */
	path = filepath.ToSlash(path)
	if len(path) > 1 && path[1] == ':' {
		return ("/mnt/" + strings.ToLower(path[0:1]) + path[2:])
	}
	return path
}

func configureShareDirs(machineConfig config.MachineConfig) []drivers.SharedDir {
	var sharedDirs []drivers.SharedDir
	for _, dir := range machineConfig.SharedDirs {
		sharedDir := drivers.SharedDir{
			Source:   dir,
			Target:   convertToUnixPath(dir),
			Tag:      "crc-dir0", // smb share 'crc-dir0' is created in the msi
			Type:     "cifs",
			Username: machineConfig.SharedDirUsername,
		}
		sharedDirs = append(sharedDirs, sharedDir)
	}
	return sharedDirs
}