/
preflight_linux.go
228 lines (211 loc) · 6.47 KB
/
preflight_linux.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
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
package preflight
import (
"errors"
"fmt"
"os"
"os/user"
"strings"
"syscall"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/crc/network"
crcos "github.com/code-ready/crc/pkg/os"
"github.com/code-ready/crc/pkg/os/linux"
)
var libvirtPreflightChecks = [...]Check{
{
configKeySuffix: "check-virt-enabled",
checkDescription: "Checking if Virtualization is enabled",
check: checkVirtualizationEnabled,
fixDescription: "Setting up virtualization",
fix: fixVirtualizationEnabled,
},
{
configKeySuffix: "check-kvm-enabled",
checkDescription: "Checking if KVM is enabled",
check: checkKvmEnabled,
fixDescription: "Setting up KVM",
fix: fixKvmEnabled,
},
{
configKeySuffix: "check-libvirt-installed",
checkDescription: "Checking if libvirt is installed",
check: checkLibvirtInstalled,
fixDescription: "Installing libvirt service and dependencies",
fix: fixLibvirtInstalled,
},
{
configKeySuffix: "check-user-in-libvirt-group",
checkDescription: "Checking if user is part of libvirt group",
check: checkUserPartOfLibvirtGroup,
fixDescription: "Adding user to libvirt group",
fix: fixUserPartOfLibvirtGroup,
},
{
configKeySuffix: "check-libvirt-running",
checkDescription: "Checking if libvirt daemon is running",
check: checkLibvirtServiceRunning,
fixDescription: "Starting libvirt service",
fix: fixLibvirtServiceRunning,
},
{
configKeySuffix: "check-libvirt-version",
checkDescription: "Checking if a supported libvirt version is installed",
check: checkLibvirtVersion,
fixDescription: "Installing a supported libvirt version",
fix: fixLibvirtVersion,
},
{
configKeySuffix: "check-libvirt-driver",
checkDescription: "Checking if crc-driver-libvirt is installed",
check: checkMachineDriverLibvirtInstalled,
fixDescription: "Installing crc-driver-libvirt",
fix: fixMachineDriverLibvirtInstalled,
},
{
cleanupDescription: "Removing the crc VM if exists",
cleanup: removeCrcVM,
flags: CleanUpOnly,
},
}
var libvirtNetworkPreflightChecks = [...]Check{
{
configKeySuffix: "check-crc-network",
checkDescription: "Checking if libvirt 'crc' network is available",
check: checkLibvirtCrcNetworkAvailable,
fixDescription: "Setting up libvirt 'crc' network",
fix: fixLibvirtCrcNetworkAvailable,
cleanupDescription: "Removing 'crc' network from libvirt",
cleanup: removeLibvirtCrcNetwork,
},
{
configKeySuffix: "check-crc-network-active",
checkDescription: "Checking if libvirt 'crc' network is active",
check: checkLibvirtCrcNetworkActive,
fixDescription: "Starting libvirt 'crc' network",
fix: fixLibvirtCrcNetworkActive,
},
}
var vsockPreflightChecks = Check{
configKeySuffix: "check-vsock",
checkDescription: "Checking if vsock is correctly configured",
check: checkVsock,
fixDescription: "Checking if vsock is correctly configured",
fix: fixVsock,
}
func checkVsock() error {
executable, err := os.Executable()
if err != nil {
return err
}
getcap, _, err := crcos.RunWithDefaultLocale("getcap", executable)
if err != nil {
return err
}
if !strings.Contains(string(getcap), "cap_net_bind_service+eip") {
return fmt.Errorf("capabilities are not correct for %s", executable)
}
info, err := os.Stat("/dev/vsock")
if err != nil {
return err
}
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
group, err := user.LookupGroupId(fmt.Sprint(stat.Gid))
if err != nil {
return err
}
if group.Name != "libvirt" {
return errors.New("/dev/vsock is not is the right group")
}
} else {
return errors.New("cannot cast info")
}
if info.Mode()&0060 == 0 {
return errors.New("/dev/vsock doesn't have the right permissions")
}
return nil
}
func fixVsock() error {
executable, err := os.Executable()
if err != nil {
return err
}
_, _, err = crcos.RunWithPrivilege("setcap cap_net_bind_service=+eip", "setcap", "cap_net_bind_service=+eip", executable)
if err != nil {
return err
}
_, _, err = crcos.RunWithPrivilege("modprobe vhost_vsock", "modprobe", "vhost_vsock")
if err != nil {
return err
}
_, _, err = crcos.RunWithPrivilege("chown /dev/vsock", "chown", "root:libvirt", "/dev/vsock")
if err != nil {
return err
}
_, _, err = crcos.RunWithPrivilege("chmod /dev/vsock", "chmod", "g+rw", "/dev/vsock")
if err != nil {
return err
}
return nil
}
func getAllPreflightChecks() []Check {
checks := getPreflightChecksForDistro(distro(), network.DefaultMode)
checks = append(checks, vsockPreflightChecks)
return checks
}
func getPreflightChecks(_ bool, networkMode network.Mode) []Check {
return getPreflightChecksForDistro(distro(), networkMode)
}
func getNetworkChecksForDistro(distro *linux.OsRelease, networkMode network.Mode) []Check {
var checks []Check
if networkMode == network.VSockMode {
return append(checks, vsockPreflightChecks)
}
switch distroID(distro) {
default:
logging.Warnf("distribution-specific preflight checks are not implemented for '%s'", distroID(distro))
fallthrough
case linux.RHEL, linux.CentOS, linux.Fedora:
checks = append(checks, nmPreflightChecks[:]...)
if usesSystemdResolved(distro) {
checks = append(checks, systemdResolvedPreflightChecks[:]...)
} else {
checks = append(checks, dnsmasqPreflightChecks[:]...)
}
case linux.Ubuntu:
break
}
return checks
}
func getPreflightChecksForDistro(distro *linux.OsRelease, networkMode network.Mode) []Check {
var checks []Check
checks = append(checks, genericPreflightChecks[:]...)
checks = append(checks, nonWinPreflightChecks[:]...)
checks = append(checks, libvirtPreflightChecks[:]...)
networkChecks := getNetworkChecksForDistro(distro, networkMode)
checks = append(checks, networkChecks...)
checks = append(checks, libvirtNetworkPreflightChecks[:]...)
return checks
}
func usesSystemdResolved(osRelease *linux.OsRelease) bool {
switch distroID(osRelease) {
case linux.Fedora:
return osRelease.VersionID >= "33"
default:
return false
}
}
func distroID(osRelease *linux.OsRelease) linux.OsType {
if osRelease == nil {
return "unknown"
}
// FIXME: should also use IDLike
return osRelease.ID
}
func distro() *linux.OsRelease {
distro, err := linux.GetOsRelease()
if err != nil {
logging.Warnf("cannot get distribution name: %v", err)
return nil
}
return distro
}