1
1
package preflight
2
2
3
3
import (
4
- "errors"
4
+ // "errors"
5
5
"fmt"
6
+ "strconv"
7
+ "strings"
6
8
7
9
"github.com/code-ready/crc/pkg/crc/logging"
8
10
"github.com/code-ready/crc/pkg/crc/oc"
11
+
12
+ "github.com/code-ready/crc/pkg/crc/errors"
13
+ //"github.com/code-ready/crc/pkg/os/windows/win32"
14
+ "github.com/code-ready/crc/pkg/os/windows/powershell"
15
+ )
16
+
17
+ const (
18
+ // Fall Creators update comes with the "Default Switch"
19
+ minimumWindowsReleaseId = 1709
20
+
21
+ hypervDefaultVirtualSwitchName = "Default Switch"
22
+ hypervDefaultVirtualSwitchId = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444"
9
23
)
10
24
11
25
// Check if oc binary is cached or not
@@ -26,3 +40,115 @@ func fixOcBinaryCached() (bool, error) {
26
40
logging .Debug ("oc binary cached" )
27
41
return true , nil
28
42
}
43
+
44
+ func checkVersionOfWindowsUpdate () (bool , error ) {
45
+ windowsReleaseId := `(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId`
46
+
47
+ stdOut , _ , _ := powershell .Execute (windowsReleaseId )
48
+ yourWindowsReleaseId , err := strconv .Atoi (strings .TrimSpace (stdOut ))
49
+
50
+ if err != nil {
51
+ return false , errors .New ("Failed to get Windows release id" )
52
+ }
53
+
54
+ if yourWindowsReleaseId < minimumWindowsReleaseId {
55
+ return false , errors .Newf ("Please update Windows. Currently %d is the minimum release needed to run. You are running %d" , minimumWindowsReleaseId , yourWindowsReleaseId )
56
+ }
57
+ return true , nil
58
+ }
59
+
60
+ // Unable to update automatically
61
+ func fixVersionOfWindowsUpdate () (bool , error ) {
62
+ return false , errors .New ("Please manually update your Windows 10 installation" )
63
+ }
64
+
65
+ func checkHyperVInstalled () (bool , error ) {
66
+ // check to see if a hypervisor is present. if hyper-v is installed and enabled,
67
+ checkHypervisorPresent := `@(Get-Wmiobject Win32_ComputerSystem).HypervisorPresent`
68
+ stdOut , _ , _ := powershell .Execute (checkHypervisorPresent )
69
+ if ! strings .Contains (stdOut , "True" ) {
70
+ return false , errors .New ("Hyper-V not installed" )
71
+ }
72
+
73
+ // Check if Hyper-V's Virtual Machine Management Service is running
74
+ checkVmmsRunning := `@(Get-Service vmms).Status`
75
+ stdOut , _ , _ = powershell .Execute (checkVmmsRunning )
76
+ if strings .TrimSpace (stdOut ) != "Running" {
77
+ return false , errors .New ("Hyper-V Virtual Machine Management service not running" )
78
+ }
79
+
80
+ return true , nil
81
+ }
82
+
83
+ //
84
+ func fixHyperVInstalled () (bool , error ) {
85
+ enableHyperVCommand := `Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All`
86
+ _ , _ , err := powershell .ExecuteAsAdmin (enableHyperVCommand )
87
+
88
+ if err != nil {
89
+ return false , errors .New ("Error occured installing Hyper-V" )
90
+ }
91
+
92
+ // We do need to error out as a restart might be needed (unfortunately no output redirect possible)
93
+ return true , errors .New ("Please reboot your system" )
94
+ }
95
+
96
+ func checkIfUserPartOfHyperVAdmins () (bool , error ) {
97
+ // https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
98
+ // BUILTIN\Hyper-V Administrators => S-1-5-32-578
99
+
100
+ checkIfMemberOfHyperVAdmins :=
101
+ `$sid = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-578")
102
+ @([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole($sid)`
103
+ stdOut , _ , _ := powershell .Execute (checkIfMemberOfHyperVAdmins )
104
+ if ! strings .Contains (stdOut , "True" ) {
105
+ return false , errors .New ("User is not a member of the Hyper-V administrators group" )
106
+ }
107
+
108
+ return true , nil
109
+ }
110
+
111
+ func fixUserPartOfHyperVAdmins () (bool , error ) {
112
+ outGroupName , _ , err := powershell .Execute (`(New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-578")).Translate([System.Security.Principal.NTAccount]).Value` )
113
+ if err != nil {
114
+ return false , errors .New ("Unable to get group name" )
115
+ }
116
+ groupName := strings .TrimSpace (strings .Replace (strings .TrimSpace (outGroupName ), "BUILTIN\\ " , "" , - 1 ))
117
+
118
+ outUsername , _ , err := powershell .Execute (`Write-Host $env:USERNAME` )
119
+ if err != nil {
120
+ return false , errors .New ("Unable to get user name" )
121
+ }
122
+ username := strings .TrimSpace (outUsername )
123
+
124
+ netCmdArgs := fmt .Sprintf (`([adsi]"WinNT://./%s,group").Add("WinNT://%s,user")` , groupName , username )
125
+ _ , _ , err = powershell .ExecuteAsAdmin (netCmdArgs )
126
+ if err != nil {
127
+ return false , errors .New ("Error adding user to group" )
128
+ }
129
+
130
+ return true , nil
131
+ }
132
+
133
+ func checkIfHyperVVirtualSwitchExists () (bool , error ) {
134
+ // TODO: vswitch configurable (use MachineConfig)
135
+ switchName := hypervDefaultVirtualSwitchName
136
+
137
+ // check for default switch by using the Id
138
+ if switchName == hypervDefaultVirtualSwitchName {
139
+ checkIfDefaultSwitchExists := fmt .Sprintf ("Get-VMSwitch -Id %s | ForEach-Object { $_.Name }" , hypervDefaultVirtualSwitchId )
140
+ _ , stdErr , _ := powershell .Execute (checkIfDefaultSwitchExists )
141
+
142
+ if ! strings .Contains (stdErr , "Get-VMSwitch" ) {
143
+ // found the default
144
+ return true , nil
145
+ }
146
+ }
147
+
148
+ return false , errors .New ("Virtual Switch not found" )
149
+ }
150
+
151
+ // Unable to do for now
152
+ func fixHyperVVirtualSwitch () (bool , error ) {
153
+ return false , errors .New ("Please override the default by adding an external virtual switch and set configuration" )
154
+ }
0 commit comments