File: preflight_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 (234 lines) | stat: -rw-r--r-- 7,713 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package preflight

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/crc-org/crc/v2/pkg/crc/constants"
	"github.com/crc-org/crc/v2/pkg/crc/network"
	"github.com/crc-org/crc/v2/pkg/crc/preset"
	crcpreset "github.com/crc-org/crc/v2/pkg/crc/preset"
	"github.com/crc-org/crc/v2/pkg/os/windows/powershell"
	"github.com/crc-org/crc/v2/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 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-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, NetworkMode: System},
	},
	{
		cleanupDescription: "Removing dns server from interface",
		cleanup:            removeDNSServerAddress,
		flags:              CleanUpOnly,

		labels: labels{Os: Windows, NetworkMode: System},
	},
}

var cleanupCheckRemoveCrcVM = Check{
	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{
	{
		cleanupDescription: "Stop daemon process if it is running",
		cleanup:            killDaemonProcessIfRunning,
		flags:              CleanUpOnly,

		labels: labels{Os: Windows},
	},
	{
		configKeySuffix:    "check-background-launcher-install",
		checkDescription:   "Checking if the win32 background launcher is installed",
		check:              checkWin32BackgroundLauncherInstalled,
		fixDescription:     "Installing the win32 background launcher",
		fix:                fixWin32BackgroundLauncherInstalled,
		cleanupDescription: "Removing the win32 background launcher",
		cleanup:            removeWin32BackgroundLauncher,

		labels: labels{Os: Windows},
	},
	{
		configKeySuffix:    "check-daemon-task-install",
		checkDescription:   "Checking if the daemon task is installed",
		check:              checkIfDaemonTaskInstalled,
		fixDescription:     "Installing the daemon task",
		fix:                fixDaemonTaskInstalled,
		cleanupDescription: "Removing the daemon task",
		cleanup:            removeDaemonTask,

		labels: labels{Os: Windows},
	},
	{
		configKeySuffix:  "check-daemon-task-running",
		checkDescription: "Checking if the daemon task is running",
		check:            checkIfDaemonTaskRunning,
		fixDescription:   "Running the daemon task",
		fix:              fixDaemonTaskRunning,

		labels: labels{Os: Windows},
	},
}

var adminHelperServiceCheks = []Check{
	{
		configKeySuffix:  "check-admin-helper-service-running",
		checkDescription: "Checking admin helper service is running",
		check:            checkIfAdminHelperServiceRunning,
		fixDescription:   "Make sure you installed crc using the Windows installer and performed required reboot",
		flags:            NoFix,

		labels: labels{Os: Windows},
	},
}

// 'crc-user' group is supposed to be created by the msi or chocolatey
// this check makes sure that was done before stating crc, it does not
// have a fix function since this'll be handled by the msi or choco
var crcUsersGroupExistsCheck = Check{
	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},
}

var userPartOfCrcUsersAndHypervAdminsGroupCheck = Check{
	configKeySuffix:  "check-user-in-crc-users-and-hyperv-admins-group",
	checkDescription: "Checking if current user is in crc-users and Hyper-V admins group",
	check:            checkUserPartOfCrcUsersAndHypervAdminsGroup,
	fixDescription:   "Adding logon user to crc-users and Hyper-V admins group",
	fix:              fixUserPartOfCrcUsersAndHypervAdminsGroup,

	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
// - 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, false)
}

func getChecks(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) []Check {
	checks := []Check{}
	checks = append(checks, memoryCheck(preset))
	checks = append(checks, removePodmanFromOcBinDirCheck())
	checks = append(checks, hypervPreflightChecks...)
	checks = append(checks, crcUsersGroupExistsCheck)
	checks = append(checks, userPartOfCrcUsersAndHypervAdminsGroupCheck)
	checks = append(checks, vsockChecks...)
	checks = append(checks, bundleCheck(bundlePath, preset, enableBundleQuayFallback))
	checks = append(checks, genericCleanupChecks...)
	checks = append(checks, cleanupCheckRemoveCrcVM)
	checks = append(checks, daemonTaskChecks...)
	checks = append(checks, adminHelperServiceCheks...)
	checks = append(checks, sshPortCheck())
	return checks
}

func getPreflightChecks(_ bool, networkMode network.Mode, bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) []Check {
	filter := newFilter()
	filter.SetNetworkMode(networkMode)

	return filter.Apply(getChecks(bundlePath, preset, enableBundleQuayFallback))
}