/
preflight_windows.go
192 lines (166 loc) · 5.67 KB
/
preflight_windows.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package preflight
import (
"errors"
"fmt"
"os"
"strings"
"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/network"
"github.com/code-ready/crc/pkg/crc/preset"
crcpreset "github.com/code-ready/crc/pkg/crc/preset"
"github.com/code-ready/crc/pkg/os/windows/powershell"
"github.com/code-ready/crc/pkg/os/windows/win32"
)
var hypervPreflightChecks = []Check{
{
configKeySuffix: "check-administrator-user",
checkDescription: "Checking if running in a shell with administrator rights",
check: checkIfRunningAsNormalUser,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-windows-version",
checkDescription: "Checking Windows 10 release",
check: checkVersionOfWindowsUpdate,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-windows-edition",
checkDescription: "Checking Windows edition",
check: checkWindowsEdition,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-hyperv-installed",
checkDescription: "Checking if Hyper-V is installed and operational",
check: checkHyperVInstalled,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-crc-users-group-exists",
checkDescription: "Checking if crc-users group exists",
check: func() error {
if _, _, err := powershell.Execute("Get-LocalGroup -Name crc-users"); err != nil {
return fmt.Errorf("'crc-users' group does not exist: %v", err)
}
return nil
},
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-user-in-hyperv-group",
checkDescription: "Checking if current user is in Hyper-V Admins group",
check: checkIfUserPartOfHyperVAdmins,
fixDescription: "Adding current user to Hyper-V Admins group",
fix: fixUserPartOfHyperVAdmins,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-hyperv-service-running",
checkDescription: "Checking if Hyper-V service is enabled",
check: checkHyperVServiceRunning,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-hyperv-switch",
checkDescription: "Checking if the Hyper-V virtual switch exists",
check: checkIfHyperVVirtualSwitchExists,
flags: StartUpOnly,
labels: labels{Os: Windows},
},
{
cleanupDescription: "Removing dns server from interface",
cleanup: removeDNSServerAddress,
flags: CleanUpOnly,
labels: labels{Os: Windows},
},
{
cleanupDescription: "Removing crc's virtual machine",
cleanup: removeCrcVM,
flags: CleanUpOnly,
labels: labels{Os: Windows},
},
}
var vsockChecks = []Check{
{
configKeySuffix: "check-vsock",
checkDescription: "Checking if vsock is correctly configured",
check: checkVsock,
flags: StartUpOnly,
labels: labels{Os: Windows, NetworkMode: User},
},
}
var daemonTaskChecks = []Check{
{
configKeySuffix: "check-daemon-task-install",
checkDescription: "Checking if daemon task is installed",
check: checkIfDaemonTaskInstalled,
fixDescription: "Installing the daemon task",
fix: fixDaemonTaskInstalled,
cleanupDescription: "Removing daemon task",
cleanup: removeDaemonTask,
labels: labels{Os: Windows},
},
{
configKeySuffix: "check-daemon-task-running",
checkDescription: "Checking if daemon task is running",
check: checkIfDaemonTaskRunning,
fixDescription: "Running the daemon task",
fix: fixDaemonTaskRunning,
labels: labels{Os: Windows},
},
}
var errReboot = errors.New("Please reboot your system and run 'crc setup' to complete the setup process")
func username() string {
if ok, _ := win32.DomainJoined(); ok {
return fmt.Sprintf(`%s\%s`, os.Getenv("USERDOMAIN"), os.Getenv("USERNAME"))
}
return os.Getenv("USERNAME")
}
const (
// This key is required to activate the vsock communication
registryDirectory = `HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices`
// First part of the key is the vsock port. The rest is not used and just a placeholder.
registryKey = "00000400-FACB-11E6-BD58-64006A7986D3"
registryValue = "gvisor-tap-vsock"
)
func checkVsock() error {
stdout, _, err := powershell.Execute(fmt.Sprintf(`Get-Item -Path "%s\%s"`, registryDirectory, registryKey))
if err != nil {
return err
}
if !strings.Contains(stdout, registryValue) {
return errors.New("VSock registry key not correctly configured")
}
return nil
}
// We want all preflight checks including
// - experimental checks
// - tray checks when using an installer, regardless of tray enabled or not
// - both user and system networking checks
//
// Passing 'UserNetworkingMode' to getPreflightChecks currently achieves this
// as there are no system networking specific checks
func getAllPreflightChecks() []Check {
return getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift)
}
func getChecks(bundlePath string, preset crcpreset.Preset) []Check {
checks := []Check{}
checks = append(checks, hypervPreflightChecks...)
checks = append(checks, vsockChecks...)
checks = append(checks, bundleCheck(bundlePath, preset))
checks = append(checks, genericCleanupChecks...)
checks = append(checks, daemonTaskChecks...)
return checks
}
func getPreflightChecks(_ bool, networkMode network.Mode, bundlePath string, preset crcpreset.Preset) []Check {
filter := newFilter()
filter.SetNetworkMode(networkMode)
return filter.Apply(getChecks(bundlePath, preset))
}